我在Protege中创建了一个本体,并使用Jena在Eclipse中导入了该本体。我已在规则文件中编写规则,并希望规则的输出显示在Jena中,但不显示输出。程序正在执行而没有错误。本体的细节是
事情
的儿童
玩具
添加了一个数据类型属性playWith域名子项和范围玩具反向播放的玩具添加了四个生命值
@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')]
代码是;
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代码
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>
我希望输出规则执行,并且要打印的子类的所有语句都没有发生请帮助我找到解决方案,因为我很多天都在努力。
非常感谢你花时间在我的问题上 很抱歉没有以正确的格式提出问题。我已经更新了请让我知道错误我想使用儿童本体并做个性化预测儿童喜欢的玩具基于他们的年龄组通过使用推理器设计规则请帮助我找到解决方案。