简体   繁体   中英

How to write the result of a rule, into a model (in Jena)?

I am working on a project where I need to create some rules using the Jena freamwork.

I have created the rules and they work as they should. I can see the output on the screen but what I would like to do is storing the result into the model of ontology I have.

I have this code :

@prefix ex: <http://www.semanticweb.org/prova_rules_M#> . 
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . 
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@include <OWL>
[pippo:
(?p http://www.semanticweb.org/prova_rules_M#anni_persona ?x)
         <- 
         (?p rdf:type ex:Persona)
         (?p http://www.semanticweb.org/prova_rules_M/persona#data_nascita ?c)
         myDiffDateYear(?c,"2014-06-18T00:00:00.0"^^xsd:dateTime,?x)
]

In this example I am using a custom built-in which I have created previously. It returns the number of years of difference between two dates. I can see the output in my screen thanks to the following code :

String percorsoFile ="./prova_rules_M_rdf.owl";
String ruleFile= "./prova_rules_M_rdf_7_diffDate.txt";

Model rawModel = ModelFactory.createDefaultModel();

//create a resource (empty model)
Resource configuration = rawModel.createResource();

// set engine mode
configuration.addProperty(ReasonerVocabulary.PROPruleMode, "hybrid");
// set the rules file
configuration.addProperty(ReasonerVocabulary.PROPruleSet, ruleFile);

List<Rule> rules = Rule.rulesFromURL(ruleFile);

GenericRuleReasoner reasonerRULE = (GenericRuleReasoner) GenericRuleReasonerFactory.theInstance().create(configuration);
reasonerRULE.setRules(rules);

Model modelRULE= FileManager.get().loadModel(percorsoFile);
//create the inference model
InfModel infModelRULE = ModelFactory.createInfModel(reasonerRULE, modelRULE);
//force starting the rule execution
infModelRULE.prepare();

//write down the result in RDFXML form
infModelRULE.write(System.out);

How can I write the result of the data property "anni_persona" inside my model (and not only as output) ?

Thank you.

The way that a backwards chaining inference rule works when it is associated with a model (in this case, your infModelRULE ), is that any inferred tripled would be available when something attempts to read that model.

For example, if you asked if infModelRule.contains(...) a triple that you know was inferred and that you see in your output, then you'll get true as a result. Your model doesn't need a separate step to 'write' the result.

If you write your model to disk instead of just to standard out (psudocode):

try( final FileOutputStream out = new FileOutputStream(new File("blah.rdf")) ){
   infModelRule.write(out);
} 

... then you'll see the triples that were inferred there as well. If you were to later read that file into a model without a reasoner attached, the triples would remain.

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