我在 Protege 中创建了一个本体,并使用 Jena 在 Eclipse 中导入了该本体。我在规则文件中编写了规则,并希望规则的输出显示在 Jena 中,但它不显示输出。程序正在执行而没有错误。本体的细节是
事情
孩子
- 0-12个月
- 1-3年
- 4-7年
- 7-12年
玩具
- 自行车
- 建筑模块
- 娃娃
- 冰鞋
- 毛绒玩具
添加了一种数据类型属性 playWith 域子项和范围玩具,其反向播放通过添加了四个个人
- 0-12月的Jhon实例
- 1-3年的玛丽实例
- 4-7 年的 sam 实例
- 史密斯实例7-12年
- Stuffed_toy 的 teddy 实例编写了一条规则,用于使用属性 playWith 作为 stuffed_toys teddy 的实例来分配 0-12Months 类的实例
@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>
我想要规则执行的输出,并且子类的所有语句都不会打印,请帮助我找到我多年来一直在苦苦挣扎的解决方案。
非常感谢您在我的问题上花费时间很抱歉没有以正确的格式提出问题。我已更新,请让我知道我想使用儿童本体的错误,并通过使用推理器设计一组规则来进行个性化以根据儿童的年龄组预测他们喜欢的玩具,请帮助我找到解决方案。