I was finally able to get my rule to work. I’m not sure this is the best way to
do it though as it seems very clunky.
It appears that the reasoner is applied only at the time of the ontModel
creation. Since i was updating the reasoner rules after the ontModel was
created the model and its inferred axioms were not regenerated. I tried just a
plain rebind on the ontModel but that did not work either. What I finally did
was:
1) rebind the reasoner that is associated to the current ontModel (which
already had its rules updated) with the base model of the ontModel
2) create an InfModel using the updated reasoner and the base model
3) rebind the current ontModel to reset it (not sure why this did not use the
update reasoner rules)
4) add the deduced triples from the infModel into the ontModel
I’m haven’t looked into why the ontModel.rebind() doesn’t seem to redo the
reasoning but at least I have something to demo now.
Curiously, the rule update works with the RDFSReasoner and the
GenericRuleReasoner but not the OWLFBRULE reasoner. I’m not sure why that is
the case either although I expect that my rule is not valid for some option set
with the OWLFBRule reasoner.
Here is the relevant bit of code. If someone has a better way of doing this
please let me know.
ruleReasoner.setRules(existingRules);
ruleReasoner.bind(getRepository().getOntModel().getBaseModel().getGraph());
InfModel inf =
ModelFactory.createInfModel(ruleReasoner,
getRepository().getOntModel().getBaseModel());
getRepository().getOntModel().rebind();
getRepository().getOntModel().add(inf.getDeductionsModel());
Thanks,
Chris
> On Dec 9, 2015, at 12:31 PM, Chris Snyder <[email protected]> wrote:
>
> Hello,
>
> I could use some input on getting my first Jena rule working.
>
> I have a simple test ontology here: http://mind-tap.net/kbswt/ruletest.ttl
>
> I apply this rule:
>
> @prefix : <http://mind-tap.net/kbswt/ruletest#>.
> [ chris1: (?s :isOutput ?p) -> (?s :meOutput ?p) ]
>
> And I run this SPARQL query
>
> PREFIX : <http://mind-tap.net/kbswt/ruletest#>
> select * where {?s :isOutput ?o.
> OPTIONAL {?s :meOutput ?x} }
>
> I was expecting to see a :meOutput value for ?x but I’m not.
>
> I have tried the different built in reasoners and in my test application I
> can see that the rules are applied to the reasoner that my SPARQL query is
> executing against.
>
> The application is basically following these steps
>
> #create my ontModel
> OntModelSpec ontModelSpec = new OntModelSpec(
> ModelFactory.createMemModelMaker(), null,
> RDFSRuleReasonerFactory.theInstance(),
> ProfileRegistry.OWL_DL_LANG);
>
> OntModel m = ModelFactory.createOntologyModel(ontModelSpec);
> m.read("http://mind-tap.net/kbswt/ruletest.ttl");
>
> Then I use my ruleFromString class (a copy of the rulesFromURL updated to
> handle strings and show down below) to parse the rules and
> ontModel.getReasoner().setRules( [parsed rules list])
>
> I next verify that the rules are applied to the reasoner by reading them back
> using a call to:
> reasoner.getRules();
>
> and then I run my SPARQL on the ontModel.
>
> I have my code in the GUI and common folders in a GIT repository here:
> https://bitbucket.org/blackburnphd/ontometa/src.
> To use:
> 1) GUI has the main method. Run it
> 2) Setup the options on the Main tab and click init repository
> 3) Click the rule tab enter the rule and click “Replace” to clear the rules
> and add the rules listed in the rule text area
> 4) Click the SPARQL tab to enter and execute the SPARQL query.
>
> Hopefully it is simply a mis-understanding of how to create the rules.
>
> Any guidance would be appreciated.
>
> Thank you,
> Chris
>
>
> BTW, I think this class should be added to the Rule class. It took me a while
> to figure out that the Rule.parse(string) call did not handle prefixes.
>
> /**
> * Answer the list of rules parsed from the given string.
> *
> * @throws RulesetNotFoundException
> */
> public static List<Rule> rulesFromString(String ruleInput) {
> BufferedReader br = null;
> try {
> StringReader sr = new StringReader(ruleInput);
> br = new BufferedReader(sr);
> return Rule.parseRules(Rule.rulesParserFromReader(br));
> } finally {
> if (br != null)
> try {
> br.close();
> } catch (IOException e2) {
> }
> }
> }
>
>
> Mindtap Inc
> Chris Snyder
> [email protected]
> 561-374-8772
> 4769 Pepper Bush Ln
> Boynton Beach, FL 33436
>
>
>