简体   繁体   中英

How to add Jena rule to OntModel

I am trying to add a Jena rule submitted as string to the below method. However the rule is not being added as I can verify no new change on the ontology written to E://1_1_1 Could someone help me how to do this. Other questions on SO that may be related are 26292160 , 349652 ; both of which are similar to my case.

public String ValidateAndConfigureRule(String string) {     

    try{
        GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.parseRules(string));
        Model oModel = m.getOntology();

        reasoner.setDerivationLogging(true);            
        reasoner.setOWLTranslation(true);
        reasoner.setTraceOn(true);
        reasoner.setTransitiveClosureCaching(true);

        InfModel inf = ModelFactory.createInfModel(reasoner, oModel);

        inf.write(new FileWriter("E://1_1_1"));

        Model baseModel = ModelFactory.createDefaultModel();            
        baseModel.add(inf);

        final OntModel model  = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF, baseModel);          

        ValidityReport validity = model.validate();
    }
}

The rule itself is in the format [ r1: (?x :objProp1 :ind_x) -> (?x :objProp2 :ind_y) ] where objProp 's are object properties and ind_x , ind_y are individuals along with the necessary prefix for ":" in the rule string.

Andy's answer captures the most important parts here: when you define a rule-based inference model, the contents of the model are the asserted and inferred triples; the rules aren't written into the model. The rules are part of the reasoner , not the data .

This is actually in contrast to SWRL rules, which have defined serialization formats so that SWRL rules can be written alongside OWL data. You might consider using SWRL rules and a reasoner that supports them (eg, Pellet, HermiT) instead of Jena rules, since the rules could be written along with the data.

As a workaround, you could also store the text of your Jena rules as the value of an annotation property on your OWL ontology and read them in again when you load the ontology. That is, you might end up with something like:

@prefix : <http://example.org/my-ontology/>

<http://example.org/my-ontology>
        a                owl:Ontology ;
        rdfs:comment     "My ontology with some Jena rules"@en ;
        :hasJenaRules    "...rule content here..." .

Then, when you load the ontology, you can check whether there are triples with the property :hasJenaRules , and if there is, you can take their objects, parse them as a Jena rules, and create a reasoner with the rules. I think that would be the easiest way to store your rules alongside your data with Jena.

When you write an InfModel it writes the base data - which has not changed. Rules don't get written.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM