1 6 package com.hp.hpl.jena.rdf.model.test; 7 8 import java.util.*; 9 10 import com.hp.hpl.jena.graph.*; 11 import com.hp.hpl.jena.mem.GraphMem; 12 import com.hp.hpl.jena.rdf.model.*; 13 import com.hp.hpl.jena.rdf.model.impl.*; 14 import com.hp.hpl.jena.reasoner.*; 15 import com.hp.hpl.jena.reasoner.rulesys.*; 16 import com.hp.hpl.jena.shared.*; 17 import com.hp.hpl.jena.util.FileManager; 18 import com.hp.hpl.jena.vocabulary.*; 19 20 import junit.framework.TestSuite; 21 22 26 public class TestModelSpecRevised extends ModelTestBase 27 { 28 public TestModelSpecRevised( String name ) 29 { super( name ); } 30 31 public static TestSuite suite() 32 { 33 return new TestSuite( TestModelSpecRevised.class ); 37 } 38 39 public static final Resource A = resource( "_a" ); 40 41 public void testTriples() 42 { 43 check( "a R b", "a R b", new Object [] {} ); 44 check( "a R b", "a R ?0", new Object [] {"b"} ); 45 check( "a P b", "a ?0 ?1", new Object [] {"P", "b"} ); 46 check( "_a P 17; Q P _a", "?0 ?1 ?2; ?3 ?1 ?0", new Object [] {"_a", "P", "17", "Q"} ); 47 } 48 49 public void check( String wanted, String template, Object [] args ) 50 { 51 Model m = modelWithStatements( template, args ); 52 assertIsoModels( modelWithStatements( wanted ), m ); 53 } 54 55 60 private static Model modelWithStatements( String facts, Object [] objects ) 61 { 62 Graph g = new GraphMem(); 63 StringTokenizer semis = new StringTokenizer( facts, ";" ); 64 while (semis.hasMoreTokens()) 65 g.add( replace( triple( PrefixMapping.Extended, semis.nextToken() ), objects ) ); 66 return ModelFactory.createModelForGraph( g ); 67 } 68 69 74 private static Triple replace( Triple t, Object [] objects ) 75 { 76 return Triple.create 77 ( replace( t.getSubject(), objects ), replace( t.getPredicate(), objects ), replace( t.getObject(), objects ) ); 78 } 79 80 84 private static Node replace( Node n, Object [] objects ) 85 { 86 if (n.isVariable()) 87 { 88 String name = n.getName(); 89 if (Character.isDigit( name.charAt(0))) 90 return Node.create( (String ) objects[Integer.parseInt( name )] ); 91 } 92 return n; 93 } 94 95 protected static void assertSameRules( List wanted, List got ) 96 { 97 assertEquals( new HashSet( wanted ), new HashSet( got ) ); 98 } 99 100 public void testNoReasonerSuppliedException() 101 { 102 Model rs = modelWithStatements( "_a rdf:type jms:ReasonerSpec" ); 103 Resource A = resource( "_a" ); 104 try { InfModelSpec.getReasonerFactory( A, rs ); fail( "should catch missing reasoner" ); } 105 catch (NoReasonerSuppliedException e) { pass(); } 106 } 107 108 public void testNoSuchReasonerException() 109 { 110 Model rs = modelWithStatements( "_a rdf:type jms:ReasonerSpec; _a jms:reasoner nosuch:reasoner" ); 111 Resource A = resource( "_a" ); 112 try 113 { InfModelSpec.getReasonerFactory( A, rs ); 114 fail( "should catch unknown reasoner" ); } 115 catch (NoSuchReasonerException e) 116 { assertEquals( "nosuch:reasoner", e.getURI() ); 117 assertContains( "nosuch:reasoner", e.toString() ); } 118 } 119 120 public void testGetOWLFBReasoner() 121 { 122 testGetReasoner( OWLFBRuleReasonerFactory.URI, OWLFBRuleReasoner.class ); 123 } 124 125 public void testGetRDFSRuleReasoner() 126 { 127 testGetReasoner( RDFSRuleReasonerFactory.URI, RDFSRuleReasoner.class ); 128 } 129 130 public void testRulesetURLFails() 131 { 132 String uri = GenericRuleReasonerFactory.URI; 133 Model rs = modelWithStatements( "_a jms:reasoner ?0; _a jms:ruleSetURL nowhere:man", new Object [] { uri } ); 134 try { InfModelSpec.getReasonerFactory( A, rs ).create( null ); fail( "should report ruleset failure" ); } 135 catch (RulesetNotFoundException e) { assertEquals( "nowhere:man", e.getURI() ); } 136 } 137 138 public void testEmptyRulesetURLWorks() 139 { 140 testRuleSetURL( GenericRuleReasonerFactory.URI, file( "empty.rules" ) ); 141 } 142 143 public void testNonEmptyRulesetURLWorks() 144 { 145 testRuleSetURL( GenericRuleReasonerFactory.URI, file( "example.rules" ) ); 146 } 147 148 public void testMultipleRulesetURLsWork() 149 { 150 String factoryURI = GenericRuleReasonerFactory.URI; 151 String rulesA = file( "example.rules" ), rulesB = file( "extra.rules" ); 152 List rules = append( Rule.rulesFromURL( rulesA ), Rule.rulesFromURL( rulesB ) ); 153 Model rs = modelWithStatements( "_a jms:reasoner ?0; _a jms:ruleSetURL ?1; _a jms:ruleSetURL ?2", new Object [] {factoryURI, rulesA, rulesB}); 154 ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs ); 155 RuleReasoner gr = (RuleReasoner) rf.create( null ); 156 assertSameRules( rules, gr.getRules() ); 157 } 158 159 public void testInlineRulesets() 160 { 161 String factoryURI = GenericRuleReasonerFactory.URI; 162 String ruleStringA = "[rdfs2: (?x ?p ?y), (?p rdfs:domain ?c) -> (?x rdf:type ?c)]"; 163 String ruleStringB = "[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x) -> (?a rdf:type ?y)]"; 164 List rules = append( Rule.parseRules( ruleStringA ), Rule.parseRules( ruleStringB ) ); 165 Model rs = rSpec( factoryURI ) 166 .add( A, JenaModelSpec.ruleSet, resource( "onward:rules" ) ) 167 .add( resource( "onward:rules" ), JenaModelSpec.hasRule, ruleStringA ) 168 .add( resource( "onward:rules" ), JenaModelSpec.hasRule, ruleStringB ); 169 ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs ); 170 RuleReasoner gr = (RuleReasoner) rf.create( null ); 171 assertSameRules( rules, gr.getRules() ); 172 } 173 174 public void testURLRulesets() 175 { 176 String factoryURI = GenericRuleReasonerFactory.URI; 177 String ruleFileA = file( "example.rules" ); 178 String ruleFileB = file( "extra.rules" ); 179 List rules = append( Rule.rulesFromURL( ruleFileA ), Rule.rulesFromURL( ruleFileB ) ); 180 Model rs = rSpec( factoryURI ) 181 .add( A, JenaModelSpec.ruleSet, resource( "onward:rules" ) ) 182 .add( resource( "onward:rules" ), JenaModelSpec.ruleSetURL, resource( ruleFileA ) ) 183 .add( resource( "onward:rules" ), JenaModelSpec.ruleSetURL, resource( ruleFileB ) ); 184 ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs ); 185 RuleReasoner gr = (RuleReasoner) rf.create( null ); 186 assertSameRules( rules, gr.getRules() ); 187 } 188 189 public void testSchema() 190 { 191 ReasonerRegistry.theRegistry().register( "fake:factory", new FakeFactory() ); 192 String d = 193 "_root jms:reasonsWith _reasoner" 194 + "; _reasoner jms:reasoner fake:factory" 195 + "; _root jms:maker _maker" 196 + "; _maker rdf:type jms:MemMakerSpec" 197 + "; _maker jms:reificationMode jms:rsMinimal" 198 + "; _reasoner jms:schemaURL ?0" 199 + "; _reasoner jms:schemaURL ?1" 200 ; 201 Model desc = modelWithStatements( d, new Object [] {file("schema.n3"), file("schema2.n3")} ); 202 ModelSpec spec = ModelFactory.createSpec( desc ); 203 validateHasSchema( loadBoth( "schema.n3", "schema2.n3" ), spec.createModel() ); 204 } 205 206 209 private Graph loadBoth( String x, String y ) 210 { 211 Model schema = FileManager.get().loadModel( file( x ) ); 212 Model schema2 = FileManager.get().loadModel( file( y ) ); 213 schema.add( schema2 ); 214 return schema.getGraph(); 215 } 216 217 private void validateHasSchema( Graph schema, Model m ) 218 { 219 ((FakeReasoner) ((InfGraph) m.getGraph()).getReasoner()).validate( schema ); 220 } 221 222 protected static class FakeReasoner implements Reasoner 223 { 224 public Graph bound = new GraphMem(); 225 226 public void validate( Graph desired ) 227 { assertTrue( "bound graph is not correct", desired.isIsomorphicWith( bound ) ); } 228 229 public Reasoner bindSchema( Graph tbox ) throws ReasonerException 230 { bound.getBulkUpdateHandler().add( tbox ); return this; } 231 232 public Reasoner bindSchema( Model tbox ) throws ReasonerException 233 { return bindSchema( tbox.getGraph() ); } 234 235 public InfGraph bind( Graph data ) throws ReasonerException 236 { return new BasicForwardRuleInfGraph( this, new ArrayList(), new GraphMem(), new GraphMem() ); } 237 238 public void setDerivationLogging( boolean logOn ) 239 { throw new JenaException( "fakes don't do this" ); } 240 241 public void setParameter( Property parameterUri, Object value ) 242 { throw new JenaException( "fakes don't do this" ); } 243 244 public Model getReasonerCapabilities() 245 { throw new JenaException( "fakes don't do this" ); } 246 247 public void addDescription( Model configSpec, Resource base ) 248 { throw new JenaException( "fakes don't do this" ); } 249 250 public boolean supportsProperty( Property property ) 251 { throw new JenaException( "fakes don't do this" ); } 252 253 public Capabilities getGraphCapabilities() { 254 return new BaseInfGraph.InfCapabilities(); 255 } 256 257 } 258 259 protected static class FakeFactory implements ReasonerFactory 260 { 261 public Reasoner create( Resource configuration ) 262 { return new FakeReasoner(); 263 } 264 265 public Model getCapabilities() 266 { 267 return null; 269 } 270 271 public String getURI() 272 { 273 return null; 275 } 276 } 277 278 public void testCreateReasoningModel() 279 { 280 String ruleString = "[rdfs3a: (?x rdfs:range ?y), (?y rdfs:subClassOf ?z) -> (?x rdfs:range ?z)]"; 281 List wanted = Rule.parseRules( ruleString ); 282 ModelSpec spec = createInfModelSpec( ruleString ); 283 Model m = spec.createModel(); 284 Graph g = m.getGraph(); 285 assertTrue( g instanceof InfGraph ); 286 Reasoner r = ((InfGraph) g).getReasoner(); 287 assertTrue( r instanceof RuleReasoner ); 288 RuleReasoner rr = (RuleReasoner) r; 289 List rules = rr.getRules(); 290 assertSameRules( wanted, rules ); 291 } 292 293 public void testDescription() 294 { 295 String ruleString = "[rdfs3a: (?x rdfs:range ?y), (?y rdfs:subClassOf ?z) -> (?x rdfs:range ?z)]"; 296 Model desc = createInfModelDesc( A, GenericRuleReasonerFactory.URI, ruleString ); 297 ModelSpec s = ModelSpecFactory.createSpec( desc ); 298 assertIsoModels( desc, s.getDescription() ); 299 } 300 301 private ModelSpec createInfModelSpec( String ruleString ) 302 { 303 Model desc = createInfModelDesc( A, GenericRuleReasonerFactory.URI, ruleString ); 304 return ModelSpecFactory.createSpec( desc ); 305 } 306 307 public static Model createInfModelDesc( Resource root, String URI, String ruleString ) 308 { 309 String d = 310 "_root jms:reasonsWith _reasoner" 311 + "; _reasoner jms:reasoner ?0" 312 + "; _root jms:maker _maker" 313 + "; _maker rdf:type jms:MemMakerSpec" 314 + "; _maker jms:reificationMode jms:rsMinimal" 315 + "; _reasoner jms:ruleSet _rules" 316 + "; _rules jms:hasRule ?1" 317 ; 318 return modelWithStatements( d, new Object [] {URI, "'" + ruleString + "'"} ); 319 } 320 321 325 private void testRuleSetURL( String factoryURI, String rulesURL ) 326 { 327 List rules = Rule.rulesFromURL( rulesURL ); 328 Model rs = modelWithStatements( "_a jms:reasoner ?0; _a jms:ruleSetURL ?1", new Object [] {factoryURI, rulesURL} ); 329 ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rs ); 330 GenericRuleReasoner gr = (GenericRuleReasoner) rf.create( null ); 331 assertSameRules( rules, gr.getRules() ); 332 } 333 334 protected void testGetReasoner( String uri, Class wantClass ) 335 { 336 ReasonerFactory rf = InfModelSpec.getReasonerFactory( A, rSpec( uri ) ); 337 assertEquals( wantClass, rf.create( null ).getClass() ); 338 } 339 340 protected Model rSpec( String factoryURI ) 341 { return modelWithStatements( "_a jms:reasoner " + factoryURI ); } 342 343 protected static String file( String name ) 344 { return "file:testing/modelspecs/" + name; } 345 346 protected void assertContains( String x, String y ) 347 { 348 if (y == null) fail( "<null> does not contain anything, especially '" + x + "'" ); 349 if (y.indexOf( x ) < 0) fail( "'" + y + "' does not contain '" + x + "'" ); 350 } 351 352 } 353 354 355 384 | Popular Tags |