Protege でオントロジーを作成し、そのオントロジーを Jena を使用して Eclipse にインポートしました。ルール ファイルにルールを記述し、ルールの出力を Jena に表示したいのですが、出力が表示されません。プログラムはエラーなしで実行されています。オントロジーの詳細は、
シング
チルドレン
- 0-12ヶ月
- 1-3年
- 4-7年
- 7-12年
おもちゃ
- バイク
- ビルディングブロック
- 人形
- スケート
- ぬいぐるみ
1 つのデータ型プロパティ playWith ドメインの子と範囲を追加しました逆が playBy であるおもちゃ
- 0-12Months の Jhon インスタンス
- 1-3Years の mary インスタンス
- 4-7Years の sam インスタンス
- 7-12Years の smith インスタンス
- 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>
ルール実行の出力が欲しいのですが、children クラスのすべてのステートメントが印刷されないので、何日も苦労している解決策を見つけてください。
私の質問に時間を割いてくれてありがとう 質問を適切な形式で入れていなくてごめんなさい。私は更新しましたエラーを教えてください私は子供のオントロジーを使用し、推論を使用してルールのセットを考案することにより、年齢層に基づいて子供が好きなおもちゃを予測するためのパーソナライズを行いたいです解決策を見つけるのを手伝ってください.