问题
I created an ontology in Protege and imported that ontology in Eclipse using Jena. I have written rules in rule file and want the output of the rules to be displayed in Jena but its not showing output. Program is getting executed without errors. The details of ontology are
Thing
Children
- 0-12Months
- 1-3Years
- 4-7Years
- 7-12Years
Toys
- Bikes
- Building_Blocks
- Dolls
- Skates
- Stuffed_toys
Added one datatype property playWith domain children and range Toys whose inverse is playedBy Added four inviduals
- Jhon instance of 0-12Months
- mary instance of 1-3Years
- sam instance of 4-7Years
- smith instance of 7-12Years
- teddy instance of Stuffed_toy written a rule as for assigning an instance of 0-12Months class using property playWith as an instance of stuffed_toys teddy
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
@prefix df: <http://www.semanticweb.org/ontologies/2014/9/9/child#>
[rule1: (?x rdf:type df:0-12Months) -> (?x df:playWith ?Stuffed_toys)]
[rule21: (?x df:playWith ?Stuffed_toys) -> print(?x ,'Plays with teddy')]
The code is;
public class GetStartedSemWeb
{
static String defaultNameSpace="http://semanticweb.org/ontologies#";
static OntModel schema = null;
OntModel infmodel =null;
public static void main(String[] args) throws IOException
{
GetStartedSemWeb getsemweb = new GetStartedSemWeb();
System.out.println(" Adding toy ontology ");
getsemweb.loadontology();
}
private void loadontology() throws IOException
{
schema = ModelFactory.createOntologyModel();
java.io.InputStream inschema =FileManager.get().open("C:/Users/Desktop/Documents/PERSONALIZATION_CHILDREN/child.owl");
schema.read(inschema,defaultNameSpace);
schema.write(System.out, "RDF/XML");
String rulefile="C:/Users/Desktop/Documents/PERSONALIZATION_CHILDREN/rule1.txt";
java.util.List rules1 = Rule.rulesFromURL(rulefile);
System.out.println(rules1);
Reasoner reasoner = new GenericRuleReasoner(rules1);
reasoner = reasoner.bindSchema(schema);
InfModel infmodel = ModelFactory.createInfModel(reasoner,schema);
Resource children = infmodel.getResource("http://semanticweb.org/ontologies/2014/9/9/child.owl/Children#");
System.out.println(children);
printStatements(infmodel,children,null ,null);
}
private static void printStatements(InfModelinfmodel,Resource s, Property p1, Resource o1)
{
System.out.println("Children");
for(StmtIterator i = infmodel.listStatements(s,p1,o1);i.hasNext();)
{
System.out.println("Children");
Statement stmt = i.nextStatement();
System.out.println(" - "+PrintUtil.print(stmt));
}
}
}
RDF/xml CODE IS
df:RDF xml:base="http://www.semanticweb.org/ontologies/2014/9/9/child.owl"><owl:Ontology
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl"/><!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Object Properties
//
///////////////////////////////////////////////////////////////////////////////////////
--><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#PlayedBy -->
<owl:ObjectProperty
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#PlayedBy">
<rdfs:range
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children"/>
<rdfs:domain rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/>
</owl:ObjectProperty><!--
http://www.semanticweb.org/ontologies/2014/9/9/child.owl#playWith -->
<owl:ObjectProperty
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#playWith">
<rdfs:domain
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children"/>
<owl:inverseOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#PlayedBy"/><rdfs:range
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/>
</owl:ObjectProperty><!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
--><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#0-12Months --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#0-12Months">
<owl:Class rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#1-3Years">
<rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children"/></owl:Class>
<!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#4-7Years --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#4-7Years"><rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children"/>
</owl:Class><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#7-12Years -->
<owl:Class rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#7-12Years">
<rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children"/></owl:Class>
<!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Bikes --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Bikes">
<rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/></owl:Class>
<!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Building_Blocks --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Building_Blocks">
<rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/></owl:Class>
<!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Children">
<rdfs:subClassOf rdf:resource="http://www.w3.org/2002/07/owl#Thing"/><owl:disjointWith
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/>
</owl:Class><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Doll --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Doll"><rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/></owl:Class>
<!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Skates --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Skates">
<rdfs:subClassOf
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/></owl:Class><!--
http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Stuffed_toys --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Stuffed_toys">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"/>
</owl:Class><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy --><owl:Class
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Toy"><rdfs:subClassOf
rdf:resource="http://www.w3.org/2002/07/owl#Thing"/></owl:Class><!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Individuals
//
///////////////////////////////////////////////////////////////////////////////////////
--><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#John --><owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#John"><rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#0-12Months"/>
</owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#mary
--><owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#mary"><rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#1-3Years"/>
</owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#sam -->
<owl:NamedIndividual rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#sam">
<rdf:type rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#4-7Years"/>
</owl:NamedIndividual><!-- http://www.semanticweb.org/ontologies/2014/9/9/child.owl#smith -->
<owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#smith"><rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#7-12Years"/>
</owl:NamedIndividual><!--
--><owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#teddy"><rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Stuffed_toys"/>
</owl:NamedIndividual>
///////////////////////////////////////////////////////////////////////////////////////
//
// General axioms
//
///////////////////////////////////////////////////////////////////////////////////////
--><rdf:Description><rdf:type rdf:resource="http://www.w3.org/2002/07/owl#AllDifferent"/>
<owl:distinctMembers rdf:parseType="Collection"><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#John"/><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#mary"/><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#sam"/><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#smith"/>
</owl:distinctMembers></rdf:Description><rdf:Description><rdf:type
rdf:resource="http://www.w3.org/2002/07/owl#AllDisjointClasses"/><owl:members
rdf:parseType="Collection"><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Bikes"/>
<rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Building_Blocks"/>
<rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Doll"/><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Skates"/>
<rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#Stuffed_toys"/>
</owl:members></rdf:Description><rdf:Description><rdf:type
rdf:resource="http://www.w3.org/2002/07/owl#AllDisjointClasses"/><owl:members
rdf:parseType="Collection"><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#0-12Months"/>
<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#1-
3Years"/><rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#4-7Years"/>
<rdf:Description
rdf:about="http://www.semanticweb.org/ontologies/2014/9/9/child.owl#7-12Years"/>
</owl:members> </rdf:Description></rdf:RDF>
I want the output for the rule execution, and the all statements of children class to be printed its not happening please help me to find the solution i am struggling since many days.
thanks a lot for spending time on my question Sorry for not putting the question in proper format. i have updated please let me know the error i want to use the child ontology and do personalization for predicting the toys liked by children based on their age group by devising set of rule using a reasoner please help me to find the solution .
来源:https://stackoverflow.com/questions/26292160/no-output-for-rule-in-jena-using-generic-rule-reasoner