Joshua, Thanks for this example. From your output it seems that rule 1is derives the statement (:iron, :conducts, :electricity) and rule 2 derives (:iron, :conductorof, :electricity)
I am more interested in finding out the possible ways in which the query statement: (:iron, :conductorOf, :electricity) can be derived. Given the following rules: @prefix : http://myexample.org/ [rule1: (?a :conductorOf :electricity) <- (?a rdf:type :Metal) print(rule1 ?a )] [rule2: (?x :conductorOf ?z) <- (?x :conducts ?z) print( rule2 ?x ?z )] (:iron rdf:type :Metal) <- . (:iron :conducts :electricity) <- . I'd expect to see both rule 1 and rule 2 as two possible derivations. As Dave Reynolds pointed out in his reply this seems not doable with Jena as it stops inference as soon as the answer is found. ~ Niranjan. On Aug 12, 2013, at 1:28 PM, Joshua TAYLOR wrote: > On Mon, Aug 12, 2013 at 4:19 PM, Joshua TAYLOR <[email protected]> wrote: >> > > Sorry for so much replying to my own noise, but I'm twice mistaken > now. The problem with the original ruleset is that Jena rules don't > support the abbreviation 'a' for `rdf:type` and I'd naïvely copied the > data into the rules. If you have a ruleset like > > @prefix : http://myexample.org/ > [rule1: (?a :conductorOf :electricity) <- (?a rdf:type :Metal) print( > rule1 ?a )] > [rule2: (?x :conductorOf ?z) <- (?x :conducts ?z) print( rule2 ?x ?z )] > (:iron rdf:type :Metal) <- . > (:iron :conducts :electricity) <- . > > and you create an infModel based on it, and iterate through the > statements in the model, both rules will get triggered, as seen in the > output: > > [http://myexample.org/iron, > http://www.w3.org/1999/02/22-rdf-syntax-ns#type, > http://myexample.org/Metal] > [http://myexample.org/iron, http://myexample.org/conducts, > http://myexample.org/electricity] > rule1 http://myexample.org/iron > [http://myexample.org/iron, http://myexample.org/conductorOf, > http://myexample.org/electricity] > rule2 http://myexample.org/iron http://myexample.org/electricity > > which has three (as expected) triples, and two lines generated by the > print statements. The output from print, since it's the last term in > the rule, means that the rule has fired successfully and derived the > appropriate triple. Here's code to reproduce: > > import java.io.BufferedReader; > import java.io.ByteArrayInputStream; > import java.io.IOException; > import java.io.InputStream; > import java.io.InputStreamReader; > > import com.hp.hpl.jena.rdf.model.InfModel; > import com.hp.hpl.jena.rdf.model.Model; > import com.hp.hpl.jena.rdf.model.ModelFactory; > import com.hp.hpl.jena.rdf.model.StmtIterator; > import com.hp.hpl.jena.reasoner.Reasoner; > import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner; > import com.hp.hpl.jena.reasoner.rulesys.Rule; > > > public class DerivationsFinder { > > final static String conductorRules = ""+ > "@prefix : http://myexample.org/\n" + > "[rule1: (?a :conductorOf :electricity) <- (?a rdf:type > :Metal) > print( rule1 ?a )]\n" + > "[rule2: (?x :conductorOf ?z) <- (?x :conducts ?z) > print( rule2 ?x ?z )]\n" + > "(:iron rdf:type :Metal) <- .\n" + > "(:iron :conducts :electricity) <- .\n" + > ""; > > public static void main(String[] args) throws IOException { > try ( final InputStream in = new ByteArrayInputStream( > conductorRules.getBytes() ); > final InputStreamReader reader = new > InputStreamReader( in ); > final BufferedReader buff = new BufferedReader( > reader ) ) { > > final Reasoner reasoner = new GenericRuleReasoner( > Rule.parseRules( > Rule.rulesParserFromReader( buff ))); > final Model model = ModelFactory.createDefaultModel(); > final InfModel infModel = ModelFactory.createInfModel( > reasoner, model ); > > for ( final StmtIterator it = > infModel.listStatements(); it.hasNext(); ) { > System.out.println( it.next() ); > } > } > } > } > > > -- > Joshua Taylor, http://www.cs.rpi.edu/~tayloj/
