課題の答え、3種類

(案1)

// 写真検索プログラムPhoto.java

 

import java.io.PrintWriter;

import java.util.Iterator;

 

import com.hp.hpl.jena.ontology.OntModel;

import com.hp.hpl.jena.ontology.OntModelSpec;

import com.hp.hpl.jena.rdf.model.InfModel;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.rdf.model.ModelFactory;

import com.hp.hpl.jena.rdf.model.Statement;

//import com.hp.hpl.jena.reasoner.Derivation;

import com.hp.hpl.jena.reasoner.Reasoner;

import com.hp.hpl.jena.reasoner.ReasonerRegistry;

import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;

import com.hp.hpl.jena.reasoner.rulesys.Rule;

import com.hp.hpl.jena.util.FileManager;

 

public class Photo

{

              static String ns = "http://www.baseball.com.tw/baseball#";

 

    public static void main(String[] args)

    {

 

        OntModel schema = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF);

        schema.read("file:./schema.owl");

 

        Model data = FileManager.get().loadModel("file:./data.rdf");

 

        Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner();

        InfModel owlInfModel = ModelFactory.createInfModel(owlReasoner, schema, data);

 

        GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL("file:./myrule.rule"));

        reasoner.setDerivationLogging(true);

        InfModel infModel = ModelFactory.createInfModel(reasoner, owlInfModel);

/*

        // Test 1 : Print all data about Place "Hawaii"

        System.out.println("Test 1 : Print all data about Place \"Hawaii\"");

        printModelWithSubjectPredicate("Hawaii", null, infModel);

        System.out.println("\n");

 

        // Test 2 : Find all Places who has Sight(s) in Photo2008

        System.out.println("Test 2 : Find all Places who has Sight(s) in Game2008");

        printModelWithSubjectPredicate("Photo2008", "SightCreatedBy", infModel);        System.out.println("\n");

 

        // Test3 : Print all data about Place "Shooting0002"

        System.out.println("Test 3 : Print all data about \"Shooting0002\"");

        printModelWithSubjectPredicate("Shooting0002", "type", infModel);

        System.out.println("\n");

*/

        // Kadai1 : Print all data about Place "BeMemberOf" "Japan"

        System.out.println("Kadai1 : Print all data about \"BeMemberOf\" \"Japan\"");

        printModelWithPredicateObject("BeMemberOf", "Japan", infModel);

        System.out.println("\n");

 

        // Kadai1 : Print all data about Place "Shooting0003" "Locky"

        System.out.println("Kadai1 : Print all data about \"Shooting0003\" \"Locky\"");

        printModelWithSubjectObject("Shooting0003", "Locky", infModel);

        System.out.println("\n");

 

        // Kadai2 : Print all data about Place "Shooting0002"

        System.out.println("Kadai2 : Print all data about \"Japan\"\"Animal\"");

//        printModelWithPredicateObject("HasLifeTarget", "Animal", infModel);

//                          printModelWithSubjectPredicate("Japan", "HasMember", infModel);

                            printModelWithCountryTarget("Japan", "Animal", infModel);

        System.out.println("\n");

 

        // Kadai3 : Print all data about Place "Shooting0002"

        System.out.println("Kadai3 : Print all data about \"2008\"\"Spring\"");

        printModelWithYearSeason("2008", "Spring", infModel);

        System.out.println("\n");

 

    }

 

public static void printModelWithSubjectPredicate(String name1, String name2, InfModel model)

    {

 

        StringBuffer sb = new StringBuffer();

        PrintWriter out = new PrintWriter(System.out);

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

        {

            Statement stmt = (Statement) itr.next();

            if ((name1==null || name1.equals(stmt.getSubject().getLocalName()))

                          && (name2==null || name2.equals(stmt.getPredicate().getLocalName())))

                          {

                sb.append("( ");

                sb.append(stmt.getSubject().getLocalName());

                sb.append(", ");

                sb.append(stmt.getPredicate().getLocalName());

                sb.append(", ");

                sb.append(stmt.getObject());

 

                sb.append(" )");

                System.out.println(sb.toString());

                sb = null;

                sb = new StringBuffer();

                out.flush();

            }

 

            // out.flush();

        }

    }

 

public static void printModelWithPredicateObject(String name1, String name2, InfModel model)

    {

 

        StringBuffer sb = new StringBuffer();

        PrintWriter out = new PrintWriter(System.out);

 

                            String fix_name2 =  new String();

                            if(name2 != null) fix_name2 = ns + name2;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

        {

            Statement stmt = (Statement) itr.next();

            if ((name1==null || name1.equals(stmt.getPredicate().getLocalName()))

                          && (name2==null || fix_name2.equals(stmt.getObject().toString())))

                          {

                sb.append("( ");

                sb.append(stmt.getPredicate().getLocalName());

                sb.append(", ");

                sb.append(name2);

                sb.append(", ");

                sb.append(stmt.getSubject());

                sb.append(" )");

                System.out.println(sb.toString());

                sb = null;

                sb = new StringBuffer();

                out.flush();

            }

 

            // out.flush();

        }

    }

 

public static void printModelWithSubjectObject(String name1, String name2, InfModel model)

    {

 

        StringBuffer sb = new StringBuffer();

        PrintWriter out = new PrintWriter(System.out);

 

                            String fix_name2 =  new String();

                            if(name2 != null) fix_name2 = ns + name2;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

        {

            Statement stmt = (Statement) itr.next();

            if ((name1==null || name1.equals(stmt.getSubject().getLocalName()))

                          && (name2==null || fix_name2.equals(stmt.getObject().toString())))

                          {

                sb.append("( ");

                sb.append(stmt.getSubject().getLocalName());

                sb.append(", ");

                sb.append(name2);

                sb.append(", ");

                sb.append(stmt.getPredicate());

                sb.append(" )");

                System.out.println(sb.toString());

                sb = null;

                sb = new StringBuffer();

                out.flush();

            }

 

            // out.flush();

        }

    }

 

 

public static void printModelWithCountryTarget(String name1, String name2, InfModel model)

    {

 

        StringBuffer sb = new StringBuffer();

        PrintWriter out = new PrintWriter(System.out);

 

                            String[] str1 =  new String[10];

                            String[] str2 =  new String[10];

 

                            String fix_name2 =  new String();

                            if(name2 != null) fix_name2 = ns + name2;

 

                            int i=0;

                            int max_i=0;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

        {

            Statement stmt = (Statement) itr.next();

            if (((stmt.getPredicate().getLocalName().equals("HasLifeTarget"))

                                                                                    ||(stmt.getPredicate().getLocalName().equals("HasSightTarget")))

                        && (name2==null || fix_name2.equals(stmt.getObject().toString())))

                          {

                sb.append(stmt.getSubject());

                str1[i] = sb.toString();

                sb = null;

                                                        sb = new StringBuffer();

                                                        i++;

                                          }

        }

                            max_i=i;

 

                            int j=0;

                            int max_j=0;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

        {

                                          Statement stmt = (Statement) itr.next();

            if ((name1==null || name1.equals(stmt.getSubject().getLocalName()))

                          && (stmt.getPredicate().getLocalName().equals("HasMember")))

                          {

                sb.append(stmt.getObject());

                str2[j] = sb.toString();

                sb = null;

                                                        sb = new StringBuffer();

                                                        j++;

            }

                            }

                            max_j=j;

 

                            for (i=0;i<max_i;i++){

                                          for(j=0;j<max_j;j++){

                                                        if (str1[i].equals(str2[j])){

                                    sb.append("( ");

                sb.append(name1);

                sb.append(", ");

                sb.append(name2);

                sb.append(", ");

                sb.append(str1[i]);

                sb.append(" )");

                System.out.println(sb.toString());

                                                        }

                                          }

                            }

              }

 

public static void printModelWithYearSeason(String name1, String name2, InfModel model)

    {

 

        StringBuffer sb = new StringBuffer();

        PrintWriter out = new PrintWriter(System.out);

 

                            String fix_name =  new String();

                            if((name2 != null)&&(name1 != null)) fix_name = name2+"_Photo"+name1;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

        {

            Statement stmt = (Statement) itr.next();

            if ((fix_name.equals(stmt.getSubject().getLocalName()))

                          && (stmt.getPredicate().getLocalName().equals("Happens")))

                          {

                sb.append("( ");

                sb.append(name1);

                sb.append(", ");

                sb.append(name2);

                sb.append(", ");

                sb.append(stmt.getObject());

                sb.append(" )");

                System.out.println(sb.toString());

                sb = null;

                sb = new StringBuffer();

                out.flush();

            }

 

            // out.flush();

        }

    }

}

 

==

//OWLファイル(schema.owl

 

<!DOCTYPE rdf:RDF [

    <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >

    <!ENTITY owl  "http://www.w3.org/2002/07/owl#" >

    <!ENTITY h "http://www.baseball.com.tw/baseball#" > ]>

 

<rdf:RDF

               xmlns:daml="http://www.daml.org/2001/03/daml+oil#"

               xmlns:owl="http://www.w3.org/2002/07/owl#"

               xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

               xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"

               xmlns:xsd ="http://www.w3.org/2001/XMLSchema#">

 

              <owl:Class rdf:about="&h;Photo"/>

 

              <owl:Class rdf:about="&h;Season"/>

 

              <owl:Class rdf:about="&h;Shooting"/>

 

              <owl:Class rdf:about="&h;Sight">

                            <rdfs:subClassOf rdf:resource="&h;Shooting" />   

              </owl:Class>

 

              <owl:Class rdf:about="&h;Life">

                            <rdfs:subClassOf rdf:resource="&h;Shooting" />   

              </owl:Class>

 

              <owl:Class rdf:about="&h;Mountain">

                      <rdfs:subClassOf rdf:resource="&h;Sight" />

                  </owl:Class>

 

              <owl:Class rdf:about="&h;Sea">

                      <rdfs:subClassOf rdf:resource="&h;Sight" />

              </owl:Class>

 

              <owl:Class rdf:about="&h;Animal">

                      <rdfs:subClassOf rdf:resource="&h;Life" />

              </owl:Class>

 

              <owl:Class rdf:about="&h;Plant">

                        <rdfs:subClassOf rdf:resource="&h;Life" />

              </owl:Class>

 

               <owl:Class rdf:about="&h;Country"/>

 

              <owl:Class rdf:about="&h;Place"/>              

 

              <owl:ObjectProperty rdf:about="&h;Happens" >

                            <rdf:type rdf:resource="&owl;TransitiveProperty" />

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;HappenIn">

                            <owl:inverseOf rdf:resource="&h;Happens"/>

                            <rdf:type rdf:resource="&owl;TransitiveProperty" />

              </owl:ObjectProperty>

 

              <owl:DatatypeProperty rdf:about="&h;Year">

                            <rdfs:domain rdf:resource="&h;Photo" />   

                            <rdfs:range  rdf:resource="&xsd;positiveInteger"/>

              </owl:DatatypeProperty>  

 

              <owl:DatatypeProperty rdf:about="&h;Number">

                            <rdfs:domain rdf:resource="&h;Place" />   

                            <rdfs:range  rdf:resource="&xsd;positiveInteger"/>

              </owl:DatatypeProperty>

 

              <owl:DatatypeProperty rdf:about="&h;Outs">

                            <rdfs:domain rdf:resource="&h;Shooting" />   

                            <rdfs:range  rdf:resource="&xsd;nonNegativeInteger"/>

              </owl:DatatypeProperty>

 

              <owl:ObjectProperty rdf:about="&h;HasCountry">

                            <rdfs:domain rdf:resource="&h;Photo" />

                            <rdfs:range rdf:resource="&h;Country" />

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;BeCountryIn">

                            <rdfs:domain rdf:resource="&h;Country" />

                            <rdfs:range rdf:resource="&h;Photo" />

                            <owl:inverseOf rdf:resource="&h;HasCountry"/>

              </owl:ObjectProperty>  

 

              <owl:ObjectProperty rdf:about="&h;HasPlace">

                            <rdfs:domain rdf:resource="&h;Photo" />

                            <rdfs:range rdf:resource="&h;Place" />

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;BePlaceIn">

                            <rdfs:domain rdf:resource="&h;Place" />

                            <rdfs:range rdf:resource="&h;Photo" />

                            <owl:inverseOf rdf:resource="&h;HasPlace"/>

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;HasMember">

                            <rdfs:domain rdf:resource="&h;Country" />

                            <rdfs:range rdf:resource="&h;Place" />

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;BeMemberOf">

                            <rdfs:domain rdf:resource="&h;Place" />

                            <rdfs:range rdf:resource="&h;Country" />

                            <owl:inverseOf rdf:resource="&h;HasMember"/>

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;HasSight">

                            <rdfs:domain rdf:resource="&h;Place" />

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;SightCreatedBy">

                            <rdfs:range rdf:resource="&h;Place" />

                            <owl:inverseOf rdf:resource="&h;HasSight"/>

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;Shoots">

                            <rdfs:range rdf:resource="&h;Shooting" />

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;ShotBy">

                            <rdfs:domain rdf:resource="&h;Shooting" />

                            <owl:inverseOf rdf:resource="&h;Shoots"/>

                            <rdf:type rdf:resource="&owl;FunctionalProperty"/>

              </owl:ObjectProperty>

 

</rdf:RDF>

 

==

//データファイル (data.rdf

 

<!DOCTYPE rdf:RDF [

    <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >

    <!ENTITY h "http://www.baseball.com.tw/baseball#" > ]>

 

<rdf:RDF

    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"

    xmlns:h="http://www.baseball.com.tw/baseball#">

 

    <!-- Define the photo -->

              <rdf:Description rdf:about="&h;Photo2008">

                            <rdf:type rdf:resource="&h;Photo"/>

                            <h:Year rdf:datatype="&xsd;positiveInteger">1</h:Year> 

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Spring_Photo2008">

                  <rdf:type rdf:resource="&h;Season"/>

                  <h:HappenIn rdf:resource="&h;Photo2008"/>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Autumn_Photo2008">

                  <rdf:type rdf:resource="&h;Season"/>

                  <h:HappenIn rdf:resource="&h;Photo2008"/>

              </rdf:Description>

 

              <!-- Define the Countrys -->

 

              <rdf:Description rdf:about="&h;Japan">

                  <rdf:type rdf:resource="&h;Country"/>

                  <h:BeCountryIn rdf:resource="&h;Photo2008"/>

              </rdf:Description>

 

                <rdf:Description rdf:about="&h;USA">

                  <rdf:type rdf:resource="&h;Country"/>

                  <h:BeCountryIn rdf:resource="&h;Photo2008"/>

              </rdf:Description>

 

              <!-- Define the Places -->

 

              <rdf:Description rdf:about="&h;Ueno">

                            <rdf:type rdf:resource="&h;Place"/>

                            <h:BeMemberOf rdf:resource="&h;Japan"/>

                            <h:Number rdf:datatype="&xsd;positiveInteger">11</h:Number>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Hawaii">

                            <rdf:type rdf:resource="&h;Place"/>

                            <h:BeMemberOf rdf:resource="&h;USA"/>

                            <h:Number rdf:datatype="&xsd;positiveInteger">12</h:Number>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Locky">

                            <rdf:type rdf:resource="&h;Place"/>

                            <h:BeMemberOf rdf:resource="&h;USA"/>

                           <h:Number rdf:datatype="&xsd;positiveInteger">21</h:Number>

                           <h:PitchesDuring rdf:resource="&h;Photo2008"/>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Koishikawa">

                            <rdf:type rdf:resource="&h;Place"/>

                            <h:BeMemberOf rdf:resource="&h;Japan"/>

                            <h:Number rdf:datatype="&xsd;positiveInteger">22</h:Number>

              </rdf:Description>

 

              <!-- Define the Shootings --> 

 

              <rdf:Description rdf:about="&h;Shooting0001">

                            <rdf:type rdf:resource="&h;Animal"/>

                            <h:HappenIn rdf:resource="&h;Spring_Photo2008"/>

                            <h:ShotBy rdf:resource="&h;Ueno"/>

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">0</h:Outs>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Shooting0002">

                            <rdf:type rdf:resource="&h;Sea"/>

                            <h:HappenIn rdf:resource="&h;Spring_Photo2008"/>

                            <h:ShotBy rdf:resource="&h;Hawaii"/>

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">0</h:Outs>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Shooting0003">

                            <rdf:type rdf:resource="&h;Mountain"/>

                            <h:HappenIn rdf:resource="&h;Autumn_Photo2008"/>

                            <h:ShotBy rdf:resource="&h;Locky"/>

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">1</h:Outs>

              </rdf:Description>

 

              <rdf:Description rdf:about="&h;Shooting0004">

                            <rdf:type rdf:resource="&h;Plant"/>

                            <h:HappenIn rdf:resource="&h;Autumn_Photo2008"/>

                            <h:ShotBy rdf:resource="&h;Koishikawa"/>

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">1</h:Outs>

              </rdf:Description>

 

</rdf:RDF>

 

==

//自作ルールファイル(myrule.rule

@prefix h: <http://www.baseball.com.tw/baseball#> .

@include <RDFS>.

@include <OWL>.        

 

[HasSight_Infer: (?place h:Shoots ?shooting) (?shooting rdf:type h:Sight)

              -> (?place h:HasSight ?shooting)]

 

[HasSight_Infer2: (?place h:HasSight ?event1) (?event1 h:HappenIn ?event2)

              -> (?place h:HasSight ?event2)]

 

[HasSightTarget: (?place h:HasSight ?event1) (?event1 rdf:type ?event2)

              -> (?place h:HasSightTarget ?event2)]

 

[HasLife_Infer: (?place h:Shoots ?shooting) (?shooting rdf:type h:Life)

              -> (?place h:HasLife ?shooting)]

 

[HasLifeTarget: (?place h:HasLife ?event1) (?event1 rdf:type ?event2)

è     (?place h:HasLifeTarget ?event2)]

 

==案1終わり

 

 

(案2)

// Photo.java

 

import java.io.PrintWriter;

 

import java.util.Iterator;

 

import com.hp.hpl.jena.ontology.OntModel;

 

import com.hp.hpl.jena.ontology.OntModelSpec;

 

import com.hp.hpl.jena.rdf.model.InfModel;

 

import com.hp.hpl.jena.rdf.model.Model;

 

import com.hp.hpl.jena.rdf.model.ModelFactory;

 

import com.hp.hpl.jena.rdf.model.Statement;

 

//import com.hp.hpl.jena.reasoner.Derivation;

 

import com.hp.hpl.jena.reasoner.Reasoner;

 

import com.hp.hpl.jena.reasoner.ReasonerRegistry;

 

import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;

 

import com.hp.hpl.jena.reasoner.rulesys.Rule;

 

import com.hp.hpl.jena.util.FileManager;

 

 

 

public class Photo

 

{

    static String ns = "http://www.baseball.com.tw/baseball#";

 

    public static void main(String[] args)

 

    {

 

 

 

          

 

        OntModel schema = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF);

 

        schema.read("file:./schema.owl");

 

 

 

        Model data = FileManager.get().loadModel("file:./data.rdf");

 

 

 

        Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner();

 

        InfModel owlInfModel = ModelFactory.createInfModel(owlReasoner, schema, data);

 

 

 

        GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL("file:./myrule.rule"));

 

        reasoner.setDerivationLogging(true);

 

        InfModel infModel = ModelFactory.createInfModel(reasoner, owlInfModel);

 

 

/*

        // Test 1 : Print all data about Place "Hawaii"

 

        System.out.println("Test 1 : Print all data about Place \"Hawaii\"");

 

        printModelWithSubjectPredicate("Hawaii", null, infModel);

 

        System.out.println("\n");

 

       

 

        // Test 2 : Find all Places who has Sight(s) in Photo2008

 

        System.out.println("Test 2 : Find all Places who has Sight(s) in Game2008");

 

        printModelWithSubjectPredicate("Photo2008", "SightCreatedBy", infModel);        System.out.println("\n");

 

 

 

        // Test3 : Print all data about Place "Shooting0002"

 

        System.out.println("Test 3 : Print all data about \"Shooting0002\"");

 

        printModelWithSubjectPredicate("Shooting0002", "type", infModel);

 

        System.out.println("\n");

*/

 

              System.out.println("Kadai1 printModelWithPredicateObject : Print all data about \"BeMemberOf Japan\"");

 

              printModelWithPredicateObject("BeMemberOf","Japan",infModel);

 

              System.out.println("\n");

 

              System.out.println("Kadai1 printModelWithSubjectObject : Print all data about \"Shooting0003 Locky\"");

 

              printModelWithSubjectObject("Shooting0003","Locky",infModel);

 

              System.out.println("\n");

 

              System.out.println("Kadai2 printPlaceWithCountryAndTarget : Print all data about \"USA Sea\"");

 

              printPlaceWithCountryAndTarget("USA","Sea",infModel);

 

              System.out.println("\n");

 

              System.out.println("Kadai2 printPlaceWithCountryAndTarget : Print all data about \"Japan Animal\"");

 

              printPlaceWithCountryAndTarget("Japan","Animal",infModel);

 

              System.out.println("\n");

 

              System.out.println("kadai3 printPhotoFileWithSeasonAndYear : Print all data about \"Spring 2008\"");

 

              printPhotoFileWithSeasonAndYear("Spring","2008",infModel);

 

              System.out.println("\n");

 

              System.out.println("kadai3 printPhotoFileWithSeasonAndYear : Print all data about \"Autumn 2008\"");

 

              printPhotoFileWithSeasonAndYear("Autumn","2008",infModel);

 

              System.out.println("\n");

 

    }

 

 

 

public static void printModelWithSubjectPredicate(String name1, String name2, InfModel model)

 

    {

 

        StringBuffer sb = new StringBuffer();

 

        PrintWriter out = new PrintWriter(System.out);

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

 

        {

 

            Statement stmt = (Statement) itr.next();

 

            if ((name1==null || name1.equals(stmt.getSubject().getLocalName())) &&

                            (name2==null || name2.equals(stmt.getPredicate().getLocalName())))

 

                          {

 

                sb.append("( ");

 

                sb.append(stmt.getSubject().getLocalName());

 

                sb.append(", ");

 

                sb.append(stmt.getPredicate().getLocalName());

 

                sb.append(", ");

 

                sb.append(stmt.getObject());

 

                sb.append(" )");

 

                System.out.println(sb.toString());

 

                sb = null;

 

                sb = new StringBuffer();

 

                out.flush();

 

            }

 

            // out.flush();

 

        }

 

    }

 

 

public static void printModelWithPredicateObject(String name1, String name2, InfModel model) //kadai1

{

        StringBuffer sb = new StringBuffer();

 

        PrintWriter out = new PrintWriter(System.out);

 

              String fix_name2 = new String();

              if(name2 != null) fix_name2 = ns + name2;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

 

        {

 

            Statement stmt = (Statement) itr.next();

            if ((name1==null || name1.equals(stmt.getPredicate().getLocalName())) &&

                            (name2==null || fix_name2.equals(stmt.getObject().toString())))

 

             {

                sb.append("( ");

 

                sb.append(stmt.getPredicate().getLocalName());

 

                sb.append(", ");

 

                sb.append(name2);

 

                sb.append(", ");

 

                sb.append(stmt.getSubject().getLocalName());

 

                sb.append(" )");

 

                System.out.println(sb.toString());

 

                sb = null;

 

                sb = new StringBuffer();

 

                out.flush();

 

            }

 

            // out.flush();

 

        }

}

 

public static void printModelWithSubjectObject(String name1, String name2, InfModel model) //kadai1

{

        StringBuffer sb = new StringBuffer();

 

        PrintWriter out = new PrintWriter(System.out);

 

              String fix_name2 = new String();

              if(name2 != null) fix_name2 = ns + name2;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

 

        {

 

            Statement stmt = (Statement) itr.next();

 

            if ((name1==null || name1.equals(stmt.getSubject().getLocalName())) &&

                (name2==null || fix_name2.equals(stmt.getObject().toString())))

               {

                sb.append("( ");

 

                sb.append(stmt.getSubject().getLocalName());

 

                sb.append(", ");

 

                sb.append(name2);

 

                sb.append(", ");

 

                sb.append(stmt.getPredicate().getLocalName());

 

                sb.append(" )");

 

                System.out.println(sb.toString());

 

                sb = null;

 

                sb = new StringBuffer();

 

                out.flush();

 

            }

 

            // out.flush();

 

        }

}

 

 

public static void printPlaceWithCountryAndTarget(String name1, String name2, InfModel model) //ex. "Japan","Plant"->Koishikawa

{

        StringBuffer sb = new StringBuffer();

 

        PrintWriter out = new PrintWriter(System.out);

 

              String fix_name2 = ns + name2;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

 

        {

 

            Statement stmt = (Statement) itr.next();

 

                  if( name1.equals(stmt.getSubject().getLocalName()) &&

                            stmt.getPredicate().getLocalName().equals("HasTarget") &&

                            fix_name2.equals(stmt.getObject().toString())){

                                          String sbj_name = stmt.getSubject().getLocalName();

                                          String obj_name = stmt.getObject().toString();

 

                                          for(Iterator itr2 = model.listStatements(); itr2.hasNext();)

                                          {

 

                                                        Statement stmt2 = (Statement)itr2.next();

                                                        if(stmt2.getPredicate().getLocalName().equals("HasMember")

                                                           && sbj_name.equals(stmt2.getSubject().getLocalName())){

                                                                      String obj_name2 = stmt2.getObject().toString();

 

                                                                      for(Iterator itr3 = model.listStatements(); itr3.hasNext();)

                                                                      {

                                                                                    Statement stmt3 = (Statement)itr3.next();

 

                                                                                    if(stmt3.getPredicate().getLocalName().equals("ShotTargetByPlace") &&

                                                                                       obj_name2.equals(ns + stmt3.getSubject().getLocalName()) &&

                                                                                       fix_name2.equals(stmt3.getObject().toString())){

                                                                                                  System.out.println("("+name1+", "+name2+", "+stmt3.getSubject().getLocalName()+" )");

                                                                                    }

                                                                      }

                                                        }

                                          }

                  }

      }

 

}

 

 

public static void printPhotoFileWithSeasonAndYear(String name1, String name2, InfModel model) //ex. "Autumn","2008"->Shooting004

{

        StringBuffer sb = new StringBuffer();

 

        PrintWriter out = new PrintWriter(System.out);

 

              boolean flagseason=false,flagyear=false,flag=false;

              String photo_name=new String();

 

              String fix_name1 = ns + name1;

              String fix_name2 = ns + name2;

 

        for (Iterator itr = model.listStatements(); itr.hasNext();)

 

        {

 

            Statement stmt = (Statement) itr.next();

 

                  if(stmt.getPredicate().getLocalName().equals("HappenIn")){

                                          String sbj_name = stmt.getSubject().getLocalName();

                                          String obj_name = stmt.getObject().toString();

                                          flagseason=false;flagyear=false;flag=false;

 

                                          for(Iterator itr2 = model.listStatements(); itr2.hasNext();)

                                          {

                                                        Statement stmt2 = (Statement)itr2.next();

                                                        if(stmt2.getPredicate().getLocalName().equals("HasSeason") &&

                                                           obj_name.equals(ns+stmt2.getSubject().getLocalName()) &&

                                                           fix_name1.equals(stmt2.getObject().toString()))

                                                                    flagseason = true;

                                                                     

                                                        if(stmt2.getPredicate().getLocalName().equals("HasYear") &&

                                                           obj_name.equals(ns+stmt2.getSubject().getLocalName()) &&

                                                           fix_name2.equals(stmt2.getObject().toString()))

                                                                    flagyear = true;

                                                                     

                                                        if(flagseason == true && flagyear == true) {photo_name = sbj_name;flag=true;flagseason=false;flagyear=false;}

                                          }

                                          if(flag==true) System.out.println("("+name1+", "+name2+", "+photo_name+" )");

                  }

      }

 

 

}

 

}

 

==

//OWLファイル(schema.owl

 

<!DOCTYPE rdf:RDF [

 

    <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >

 

    <!ENTITY owl  "http://www.w3.org/2002/07/owl#" >

 

    <!ENTITY h "http://www.baseball.com.tw/baseball#" > ]>

 

   

 

<rdf:RDF

 

               xmlns:daml="http://www.daml.org/2001/03/daml+oil#"

 

               xmlns:owl="http://www.w3.org/2002/07/owl#"

 

               xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

 

               xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"

 

               xmlns:xsd ="http://www.w3.org/2001/XMLSchema#">

 

            

 

              <owl:Class rdf:about="&h;Photo"/>

 

            

 

              <owl:Class rdf:about="&h;Season"/>

 

            

 

              <owl:Class rdf:about="&h;Shooting"/>

 

   

 

              <owl:Class rdf:about="&h;Sight">

 

                            <rdfs:subClassOf rdf:resource="&h;Shooting" />

 

              </owl:Class>

 

 

 

              <owl:Class rdf:about="&h;Life">

 

                            <rdfs:subClassOf rdf:resource="&h;Shooting" />   

 

              </owl:Class>

 

   

 

              <owl:Class rdf:about="&h;Mountain">

 

                      <rdfs:subClassOf rdf:resource="&h;Sight" />

 

                  </owl:Class>

 

 

 

                  <owl:Class rdf:about="&h;Sea">

 

                      <rdfs:subClassOf rdf:resource="&h;Sight" />

 

                  </owl:Class>

 

   

 

                  <owl:Class rdf:about="&h;Animal">

 

                      <rdfs:subClassOf rdf:resource="&h;Life" />

 

                  </owl:Class>

 

 

 

              <owl:Class rdf:about="&h;Plant">

 

                            <rdfs:subClassOf rdf:resource="&h;Life" />

 

              </owl:Class>

 

   

 

               <owl:Class rdf:about="&h;Country"/>

 

            

 

              <owl:Class rdf:about="&h;Place"/>

 

   

 

              <owl:ObjectProperty rdf:about="&h;Happens" >

 

                            <rdf:type rdf:resource="&owl;TransitiveProperty" />

 

              </owl:ObjectProperty>

 

            

 

              <owl:ObjectProperty rdf:about="&h;HappenIn">

 

                            <owl:inverseOf rdf:resource="&h;Happens"/>

 

                            <rdf:type rdf:resource="&owl;TransitiveProperty" />

 

              </owl:ObjectProperty>

 

   

 

              <owl:DatatypeProperty rdf:about="&h;Year">

 

                            <rdfs:domain rdf:resource="&h;Photo" />

 

                            <rdfs:range  rdf:resource="&xsd;positiveInteger"/>

 

              </owl:DatatypeProperty>

 

 

 

              <owl:DatatypeProperty rdf:about="&h;Number">

 

                            <rdfs:domain rdf:resource="&h;Place" />   

 

                            <rdfs:range  rdf:resource="&xsd;positiveInteger"/>

 

              </owl:DatatypeProperty>

 

 

 

              <owl:DatatypeProperty rdf:about="&h;Outs">

 

                            <rdfs:domain rdf:resource="&h;Shooting" />

 

                            <rdfs:range  rdf:resource="&xsd;nonNegativeInteger"/>

 

              </owl:DatatypeProperty>

 

 

 

              <owl:ObjectProperty rdf:about="&h;HasCountry">

 

                            <rdfs:domain rdf:resource="&h;Photo" />

 

                            <rdfs:range rdf:resource="&h;Country" />

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;BeCountryIn">

 

                            <rdfs:domain rdf:resource="&h;Country" />

 

                            <rdfs:range rdf:resource="&h;Photo" />

 

                            <owl:inverseOf rdf:resource="&h;HasCountry"/>

 

              </owl:ObjectProperty>  

 

   

 

              <owl:ObjectProperty rdf:about="&h;HasPlace">

 

                            <rdfs:domain rdf:resource="&h;Photo" />

 

                            <rdfs:range rdf:resource="&h;Place" />

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;BePlaceIn">

 

                            <rdfs:domain rdf:resource="&h;Place" />

 

                            <rdfs:range rdf:resource="&h;Photo" />

 

                            <owl:inverseOf rdf:resource="&h;HasPlace"/>

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;HasMember">

 

                            <rdfs:domain rdf:resource="&h;Country" />

 

                            <rdfs:range rdf:resource="&h;Place" />

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;BeMemberOf">

 

                            <rdfs:domain rdf:resource="&h;Place" />

 

                            <rdfs:range rdf:resource="&h;Country" />

 

                            <owl:inverseOf rdf:resource="&h;HasMember"/>

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;HasSight">

 

                            <rdfs:domain rdf:resource="&h;Place" />

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;SightCreatedBy">

 

                            <rdfs:range rdf:resource="&h;Place" />

 

                            <owl:inverseOf rdf:resource="&h;HasSight"/>

 

              </owl:ObjectProperty>

 

 

 

              <owl:ObjectProperty rdf:about="&h;Shoots">

 

                            <rdfs:range rdf:resource="&h;Shooting" />

 

              </owl:ObjectProperty>

 

   

 

              <owl:ObjectProperty rdf:about="&h;ShotBy">

 

                            <rdfs:domain rdf:resource="&h;Shooting" />

 

                            <owl:inverseOf rdf:resource="&h;Shoots"/>

 

                            <rdf:type rdf:resource="&owl;FunctionalProperty"/>

 

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;ShotTarget">

 

                            <rdfs:domain rdf:resource="&h;Plant" />

                            <rdfs:domain rdf:resource="&h;Animal" />

                            <rdfs:domain rdf:resource="&h;Sea" />

                            <rdfs:domain rdf:resource="&h;Mountain" />

 

                            <rdfs:range rdf:resource="&h;Shooting" />

 

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;ShotTargetByPlace">

 

                            <rdfs:domain rdf:resource="&h;Plant" />

                            <rdfs:domain rdf:resource="&h;Animal" />

                            <rdfs:domain rdf:resource="&h;Sea" />

                            <rdfs:domain rdf:resource="&h;Mountain" />

 

                            <rdfs:range rdf:resource="&h;Place" />

 

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;IsOneOf">

 

                            <rdfs:domain rdf:resource="&h;Place" />

 

                            <rdfs:range rdf:resource="&h;Plant" />

                            <rdfs:range rdf:resource="&h;Animal" />

                            <rdfs:range rdf:resource="&h;Mountain" />

                            <rdfs:range rdf:resource="&h;Sea" />

 

                            <owl:inverseOf rdf:resource="&h;IsTarget"/>

 

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;HasYear">

 

                            <rdfs:domain rdf:resource="&h;Year" />

 

                            <rdfs:range rdf:resource="&h;Photo" />

 

              </owl:ObjectProperty>

 

              <owl:ObjectProperty rdf:about="&h;HasSeason">

 

                            <rdfs:domain rdf:resource="&h;Season" />

 

                            <rdfs:range rdf:resource="&h;Photo" />

 

              </owl:ObjectProperty>

</rdf:RDF>

 

==

//データファイル(data.rdf

<!DOCTYPE rdf:RDF [

 

    <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >

 

    <!ENTITY h "http://www.baseball.com.tw/baseball#" > ]>

 

 

 

<rdf:RDF

 

    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

 

    xmlns:xsd="http://www.w3.org/2001/XMLSchema#"

 

    xmlns:h="http://www.baseball.com.tw/baseball#">

 

  

 

    <!-- Define the photo -->

 

              <rdf:Description rdf:about="&h;Photo2008">

 

                            <rdf:type rdf:resource="&h;Photo"/>

 

                            <h:Year rdf:datatype="&xsd;positiveInteger">1</h:Year>

 

              </rdf:Description>

 

            

 

              <rdf:Description rdf:about="&h;Spring_Photo2008">

 

                  <rdf:type rdf:resource="&h;Season"/>

 

                  <h:HappenIn rdf:resource="&h;Photo2008"/>

 

                              <h:HasSeason rdf:resource="&h;Spring"/>

 

                              <h:HasYear rdf:resource="&h;2008"/>

 

              </rdf:Description>

 

 

 

              <rdf:Description rdf:about="&h;Autumn_Photo2008">

 

                  <rdf:type rdf:resource="&h;Season"/>

 

                  <h:HappenIn rdf:resource="&h;Photo2008"/>

 

                              <h:HasSeason rdf:resource="&h;Autumn"/>

 

                              <h:HasYear rdf:resource="&h;2008"/>

 

              </rdf:Description>

 

 

              <!-- Define the Countrys -->

 

            

 

              <rdf:Description rdf:about="&h;Japan">

 

                  <rdf:type rdf:resource="&h;Country"/>

 

                  <h:BeCountryIn rdf:resource="&h;Photo2008"/>

 

              </rdf:Description>

 

 

 

                <rdf:Description rdf:about="&h;USA">

 

                  <rdf:type rdf:resource="&h;Country"/>

 

                  <h:BeCountryIn rdf:resource="&h;Photo2008"/>

 

              </rdf:Description>

 

            

 

              <!-- Define the Places -->

 

            

 

              <rdf:Description rdf:about="&h;Ueno">

 

                            <rdf:type rdf:resource="&h;Place"/>

 

                                              <h:IsTarget rdf:resource="&h;Animal"/>

 

                                              <h:ShotTargetByPlace rdf:resource="&h;Animal"/>

 

                            <h:BeMemberOf rdf:resource="&h;Japan"/>

 

                            <h:Number rdf:datatype="&xsd;positiveInteger">11</h:Number>

 

              </rdf:Description>

 

            

 

              <rdf:Description rdf:about="&h;Hawaii">

 

                            <rdf:type rdf:resource="&h;Place"/>

 

                                              <h:IsTarget rdf:resource="&h;Sea"/>

 

                                              <h:ShotTargetByPlace rdf:resource="&h;Sea"/>

 

                            <h:BeMemberOf rdf:resource="&h;USA"/>

 

                            <h:Number rdf:datatype="&xsd;positiveInteger">12</h:Number>

 

              </rdf:Description>

 

 

 

              <rdf:Description rdf:about="&h;Locky">

 

                            <rdf:type rdf:resource="&h;Place"/>

 

                                              <h:IsTarget rdf:resource="&h;Mountain"/>

 

                                              <h:ShotTargetByPlace rdf:resource="&h;Mountain"/>

 

                            <h:BeMemberOf rdf:resource="&h;USA"/>

 

                           <h:Number rdf:datatype="&xsd;positiveInteger">21</h:Number>

 

                           <h:PitchesDuring rdf:resource="&h;Photo2008"/>

 

              </rdf:Description>

 

 

 

              <rdf:Description rdf:about="&h;Koishikawa">

 

                            <rdf:type rdf:resource="&h;Place"/>

 

                                              <h:IsTarget rdf:resource="&h;Plant"/>

 

                                              <h:ShotTargetByPlace rdf:resource="&h;Plant"/>

 

                            <h:BeMemberOf rdf:resource="&h;Japan"/>

 

                            <h:Number rdf:datatype="&xsd;positiveInteger">22</h:Number>

 

              </rdf:Description>

 

 

 

              <!-- Define the Shootings -->

 

 

 

              <rdf:Description rdf:about="&h;Shooting0001">

 

                            <h:ShotTarget rdf:resource="&h;Animal"/>

 

                            <h:HappenIn rdf:resource="&h;Spring_Photo2008"/>

 

                            <h:ShotBy rdf:resource="&h;Ueno"/>

 

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">0</h:Outs>

 

              </rdf:Description>

 

 

 

              <rdf:Description rdf:about="&h;Shooting0002">

 

                            <rdf:type rdf:resource="&h;Sea"/>

 

                            <h:HappenIn rdf:resource="&h;Spring_Photo2008"/>

 

                            <h:ShotBy rdf:resource="&h;Hawaii"/>

 

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">0</h:Outs>

 

              </rdf:Description>

 

 

 

              <rdf:Description rdf:about="&h;Shooting0003">

 

                            <rdf:type rdf:resource="&h;Mountain"/>

 

                            <h:HappenIn rdf:resource="&h;Autumn_Photo2008"/>

 

                            <h:ShotBy rdf:resource="&h;Locky"/>

 

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">1</h:Outs>

 

              </rdf:Description>

 

 

              <rdf:Description rdf:about="&h;Shooting0004">

 

                            <h:ShotTarget rdf:resource="&h;Plant"/>

 

                            <h:HappenIn rdf:resource="&h;Autumn_Photo2008"/>

 

                            <h:ShotBy rdf:resource="&h;Koishikawa"/>

 

                            <h:Outs rdf:datatype="&xsd;nonNegativeInteger">1</h:Outs>

 

              </rdf:Description>

 

 

 

</rdf:RDF>

 

==

//自作ルール(myrule.rule

@include <RDFS>.

 

@include <OWL>.

 

 

 

[HasSight_Infer: (?place h:Shoots ?shooting) (?shooting rdf:type h:Sight)

 

              -> (?place h:HasSight ?shooting)]

 

[HasSight_Infer2: (?place h:HasSight ?event1) (?event1 h:HappenIn ?event2)

 

              -> (?place h:HasSight ?event2)]

 

[ShotByCountry: (?shooting h:ShotBy ?place) (?place h:BeMemberOf ?country)

              -> (?shooting h:ShotByCountry ?country)]

 

[HasTarget: (?shooting h:ShotByCountry ?country) (?shooting h:ShotTarget ?tplace)

              -> (?country h:HasTarget ?tplace)]

 

[HasTarget2: (?country h:HasMember ?place) (?place h:IsTarget ?tplace)

è     (?country h:HasTarget ?tplace)]

 

==案2終わり

 

==案3

//Photo.java

 

import java.io.PrintWriter;

import java.util.Iterator;

import com.hp.hpl.jena.ontology.OntModel;

import com.hp.hpl.jena.ontology.OntModelSpec;

import com.hp.hpl.jena.rdf.model.InfModel;

import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.rdf.model.ModelFactory;

import com.hp.hpl.jena.rdf.model.Statement;

//import com.hp.hpl.jena.reasoner.Derivation;

import com.hp.hpl.jena.reasoner.Reasoner;

import com.hp.hpl.jena.reasoner.ReasonerRegistry;

import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;

import com.hp.hpl.jena.reasoner.rulesys.Rule;

import com.hp.hpl.jena.util.FileManager;

 

public class Photo{

    static String ns = "http://www.baseball.com.tw/baseball#";

   

    public static void main(String[] args){

              OntModel schema = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_TRANS_INF);

              schema.read("file:./schema.owl");

              Model data = FileManager.get().loadModel("file:./data.rdf");

              Reasoner owlReasoner = ReasonerRegistry.getOWLReasoner();

              InfModel owlInfModel = ModelFactory.createInfModel(owlReasoner, schema, data);

              GenericRuleReasoner reasoner = new GenericRuleReasoner(Rule.rulesFromURL("file:./myrule.rule"));

              reasoner.setDerivationLogging(true);

              InfModel infModel = ModelFactory.createInfModel(reasoner, owlInfModel);

             

              //kadai1_1

              ///*

              System.out.println("Kadai 1_1 : test for \"printModelWithPredicateObject()\"");

              System.out.println("Predicate=\" BeMemberOf \",Object=\" Japan \"");

              printModelWithPredicateObject("BeMemberOf","Japan",infModel);

             

              System.out.println("\n-------------------------\n");

              //*/

             

              //kadai1_2

              ///*

              System.out.println("Kadai 1_2 : test for \"printModelWithSubjectObject()\"");

              System.out.println("Subject=\" Shooting0003 \",Object=\" Locky \"");

              printModelWithSubjectObject("Shooting0003","Locky",infModel);

             

              System.out.println("\n-------------------------\n");

              //*/

             

              //kadai2

        ///*

              Kadai2("Japan","Animal",infModel);

              System.out.println("\n-------------------------\n");

              //*/

             

              //kadai3

              ///*

              Kadai3("2008","Spring",infModel);

              System.out.println("\n\nend");

              //*/

 

    }  

   

    public static void printModelWithPredicateObject(String name1, String name2, InfModel model){

              StringBuffer sb = new StringBuffer();

              PrintWriter out = new PrintWriter(System.out);

              //

              String str2=new String();

              if(name2!=null)str2=ns+name2;

              //

              for (Iterator itr = model.listStatements(); itr.hasNext();){

                  Statement stmt = (Statement) itr.next();

                  if ((name1==null || name1.equals(stmt.getPredicate().getLocalName())) && (name2==null || str2.equals(stmt.getObject().toString()))){

                            sb.append("( ");

                            sb.append(stmt.getPredicate().getLocalName());

                            sb.append(", ");

                            sb.append(name2);

                            sb.append(", ");

                            sb.append(stmt.getSubject().getLocalName());

                            sb.append(" )");

                            System.out.println(sb.toString());

                            sb = null;

                            sb = new StringBuffer();

                            out.flush();

                  }

                  // out.flush();

              }

    }

 

   

    public static void printModelWithSubjectObject(String name1, String name2, InfModel model){

              StringBuffer sb = new StringBuffer();

              PrintWriter out = new PrintWriter(System.out);

              //

              String str2=new String();

              if(name2!=null)str2=ns+name2;

              for (Iterator itr = model.listStatements(); itr.hasNext();){

                  Statement stmt = (Statement) itr.next();

                  if ((name1==null || name1.equals(stmt.getSubject().getLocalName())) && (name2==null || str2.equals(stmt.getObject().toString()))){

                            sb.append("( ");

                            sb.append(stmt.getSubject().getLocalName());

                            sb.append(", ");

                            sb.append(name2);

                            sb.append(", ");

                            sb.append(stmt.getPredicate().getLocalName());

                            sb.append(" )");

                            System.out.println(sb.toString());

                            sb = null;

                            sb = new StringBuffer();

                            out.flush();

                  }

                  // out.flush();

              }

    }

   

    public static void Kadai2(String name1,String name2,InfModel model){

              StringBuffer sb = new StringBuffer();

              PrintWriter out = new PrintWriter(System.out);

             

              System.out.println("Kadai2,Country,taishou\n");

             

              for (Iterator itr = model.listStatements(); itr.hasNext();){

                  Statement stmt1 = (Statement) itr.next();

                  if (name1.equals(stmt1.getSubject().getLocalName())){

                            if(stmt1.getPredicate().getLocalName().equals("HasMember")){

                                for (Iterator itr2 = model.listStatements(); itr2.hasNext();){

                                          Statement stmt2 = (Statement) itr2.next();

                                          if(name2.equals(stmt2.getSubject().getLocalName())){  

                                              if(stmt2.getPredicate().getLocalName().equals("SightCreatedBy")){

                                                        System.out.println(stmt2.getObject().toString());

                                              }

                                          }

                                }

                            }

                  }

              }

    }

             

 

    public static void Kadai3(String name1,String name2,InfModel model){

              StringBuffer sb = new StringBuffer();

              PrintWriter out = new PrintWriter(System.out);

             

              System.out.println("Kadai3,Year,Season -> Photofile\n");

             

              String str=name2+"_Photo"+name1;

              for (Iterator itr = model.listStatements(); itr.hasNext();){

                  Statement stmt = (Statement) itr.next();

                  if (str.equals(stmt.getSubject().getLocalName()) && stmt.getPredicate().getLocalName().equals("Happens")){

                            System.out.println(stmt.getObject().toString());

                  }

              }

    }   

}

==

//OWLファイル(schema.owl

 

<!DOCTYPE rdf:RDF [

  <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >

  <!ENTITY owl  "http://www.w3.org/2002/07/owl#" >

  <!ENTITY h "http://www.baseball.com.tw/baseball#" > ]>

<rdf:RDF

  xmlns:daml="http://www.daml.org/2001/03/daml+oil#"

  xmlns:owl="http://www.w3.org/2002/07/owl#"

  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"

  xmlns:xsd ="http://www.w3.org/2001/XMLSchema#">

  <owl:Class rdf:about="&h;Photo"/>

  <owl:Class rdf:about="&h;Season"/>

  <owl:Class rdf:about="&h;Shooting"/>

  <owl:Class rdf:about="&h;Sight">

    <rdfs:subClassOf rdf:resource="&h;Shooting" />

  </owl:Class>

  <owl:Class rdf:about="&h;Life">

    <rdfs:subClassOf rdf:resource="&h;Shooting" />

  </owl:Class>

  <owl:Class rdf:about="&h;Mountain">

    <rdfs:subClassOf rdf:resource="&h;Sight" />

  </owl:Class>

  <owl:Class rdf:about="&h;Sea">

    <rdfs:subClassOf rdf:resource="&h;Sight" />

  </owl:Class>

  <owl:Class rdf:about="&h;Animal">

    <rdfs:subClassOf rdf:resource="&h;Life" />

  </owl:Class>

  <owl:Class rdf:about="&h;Plant">

    <rdfs:subClassOf rdf:resource="&h;Life" />

  </owl:Class>

  <owl:Class rdf:about="&h;Country"/>

  <owl:Class rdf:about="&h;Place"/>

 

  <owl:ObjectProperty rdf:about="&h;Happens" >

    <rdf:type rdf:resource="&owl;TransitiveProperty" />

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;HappenIn">

    <owl:inverseOf rdf:resource="&h;Happens"/>

    <rdf:type rdf:resource="&owl;TransitiveProperty" />

  </owl:ObjectProperty>

  <owl:DatatypeProperty rdf:about="&h;Year">

    <rdfs:domain rdf:resource="&h;Photo" />   

    <rdfs:range  rdf:resource="&xsd;positiveInteger"/>

  </owl:DatatypeProperty>  

  <owl:DatatypeProperty rdf:about="&h;Number">

    <rdfs:domain rdf:resource="&h;Place" />   

    <rdfs:range  rdf:resource="&xsd;positiveInteger"/>

  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:about="&h;Outs">

    <rdfs:domain rdf:resource="&h;Shooting" />   

    <rdfs:range  rdf:resource="&xsd;nonNegativeInteger"/>

  </owl:DatatypeProperty>

  <owl:ObjectProperty rdf:about="&h;HasCountry">

    <rdfs:domain rdf:resource="&h;Photo" />

    <rdfs:range rdf:resource="&h;Country" />

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;BeCountryIn">

    <rdfs:domain rdf:resource="&h;Country" />

    <rdfs:range rdf:resource="&h;Photo" />

    <owl:inverseOf rdf:resource="&h;HasCountry"/>

  </owl:ObjectProperty>  

  <owl:ObjectProperty rdf:about="&h;HasPlace">

    <rdfs:domain rdf:resource="&h;Photo" />

    <rdfs:range rdf:resource="&h;Place" />

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;BePlaceIn">

    <rdfs:domain rdf:resource="&h;Place" />

    <rdfs:range rdf:resource="&h;Photo" />

    <owl:inverseOf rdf:resource="&h;HasPlace"/>

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;HasMember">

    <rdfs:domain rdf:resource="&h;Country" />

    <rdfs:range rdf:resource="&h;Place" />

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;BeMemberOf">

    <rdfs:domain rdf:resource="&h;Place" />

    <rdfs:range rdf:resource="&h;Country" />

    <owl:inverseOf rdf:resource="&h;HasMember"/>

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;HasSight">

    <rdfs:domain rdf:resource="&h;Place" />

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;SightCreatedBy">

    <rdfs:range rdf:resource="&h;Place" />

    <owl:inverseOf rdf:resource="&h;HasSight"/>

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;Shoots">

    <rdfs:range rdf:resource="&h;Shooting" />

  </owl:ObjectProperty>

  <owl:ObjectProperty rdf:about="&h;ShotBy">

    <rdfs:domain rdf:resource="&h;Shooting" />

    <owl:inverseOf rdf:resource="&h;Shoots"/>

    <rdf:type rdf:resource="&owl;FunctionalProperty"/>

  </owl:ObjectProperty>

</rdf:RDF>

 

==

//データファイル(data.rdf

<!DOCTYPE rdf:RDF [

  <!ENTITY xsd  "http://www.w3.org/2001/XMLSchema#" >

  <!ENTITY h "http://www.baseball.com.tw/baseball#" > ]>

<rdf:RDF

  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

  xmlns:xsd="http://www.w3.org/2001/XMLSchema#"

  xmlns:h="http://www.baseball.com.tw/baseball#">

<!-- Define the photo -->

  <rdf:Description rdf:about="&h;Photo2008">

    <rdf:type rdf:resource="&h;Photo"/>

    <h:Year rdf:datatype="&xsd;positiveInteger">1</h:Year>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Spring_Photo2008">

    <rdf:type rdf:resource="&h;Season"/>

    <h:HappenIn rdf:resource="&h;Photo2008"/>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Autumn_Photo2008">

    <rdf:type rdf:resource="&h;Season"/>

    <h:HappenIn rdf:resource="&h;Photo2008"/>

  </rdf:Description>

<!-- Define the Countrys -->

  <rdf:Description rdf:about="&h;Japan">

    <rdf:type rdf:resource="&h;Country"/>

    <h:BeCountryIn rdf:resource="&h;Photo2008"/>

  </rdf:Description>

  <rdf:Description rdf:about="&h;USA">

    <rdf:type rdf:resource="&h;Country"/>

    <h:BeCountryIn rdf:resource="&h;Photo2008"/>

  </rdf:Description>

<!-- Define the Places -->

  <rdf:Description rdf:about="&h;Ueno">

    <rdf:type rdf:resource="&h;Place"/>

    <h:BeMemberOf rdf:resource="&h;Japan"/>

    <h:Number rdf:datatype="&xsd;positiveInteger">11</h:Number>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Hawaii">

    <rdf:type rdf:resource="&h;Place"/>

    <h:BeMemberOf rdf:resource="&h;USA"/>

    <h:Number rdf:datatype="&xsd;positiveInteger">12</h:Number>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Locky">

    <rdf:type rdf:resource="&h;Place"/>

    <h:BeMemberOf rdf:resource="&h;USA"/>

    <h:Number rdf:datatype="&xsd;positiveInteger">21</h:Number>

    <h:PitchesDuring rdf:resource="&h;Photo2008"/>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Koishikawa">

    <rdf:type rdf:resource="&h;Place"/>

    <h:BeMemberOf rdf:resource="&h;Japan"/>

    <h:Number rdf:datatype="&xsd;positiveInteger">22</h:Number>

  </rdf:Description>

<!-- Define the Shootings --> 

  <rdf:Description rdf:about="&h;Shooting0001">

    <rdf:type rdf:resource="&h;Animal"/>

    <h:HappenIn rdf:resource="&h;Spring_Photo2008"/>

    <h:ShotBy rdf:resource="&h;Ueno"/>

    <h:Outs rdf:datatype="&xsd;nonNegativeInteger">0</h:Outs>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Shooting0002">

    <rdf:type rdf:resource="&h;Sea"/>

    <h:HappenIn rdf:resource="&h;Spring_Photo2008"/>

    <h:ShotBy rdf:resource="&h;Hawaii"/>

    <h:Outs rdf:datatype="&xsd;nonNegativeInteger">0</h:Outs>

  </rdf:Description>

  <rdf:Description rdf:about="&h;Shooting0003">

    <rdf:type rdf:resource="&h;Mountain"/>

      <h:HappenIn rdf:resource="&h;Autumn_Photo2008"/>

      <h:ShotBy rdf:resource="&h;Locky"/>

      <h:Outs rdf:datatype="&xsd;nonNegativeInteger">1</h:Outs>

    </rdf:Description>

    <rdf:Description rdf:about="&h;Shooting0004">

      <rdf:type rdf:resource="&h;Plant"/>

      <h:HappenIn rdf:resource="&h;Autumn_Photo2008"/>

      <h:ShotBy rdf:resource="&h;Koishikawa"/>

      <h:Outs rdf:datatype="&xsd;nonNegativeInteger">1</h:Outs>

    </rdf:Description>

</rdf:RDF>

 

==

//自作ルール(myrule.rule

@prefix h: <http://www.baseball.com.tw/baseball#> .

@include <RDFS>.

@include <OWL>.

[HasSight_Infer: (?place h:Shoots ?shooting) (?shooting rdf:type h:Sight)

              -> (?place h:HasSight ?shooting)]

[HasSight_Infer2: (?place h:HasSight ?event1) (?event1 h:HappenIn ?event2)

              -> (?place h:HasSight ?event2)]

[HasSight_Infer3: (?place h:HasSight ?Sight1) (?Sight1 rdf:type h:Mountain)

                    ->  (?place h:HasSight h:Mountain)]

[HasSight_Infer4: (?place h:HasSight ?Sight2) (?Sight2 rdf:type h:Sea)

                    ->  (?place hHasSight h:Sea)]

[HasSight_Infer5: (?place h:Shoots ?shooting) (?shooting rdf:type h:Life)

              -> (?place h:HasSight ?shooting)]

[HasSight_Infer6: (?place h:HasSight ?Life1) (?Life1 rdf:type h:Animal)

                    ->  (?place h:HasSight h:Animal)]

[HasSight_Infer7: (?place h:HasSight ?Life2) (?Life2 rdf:type h:Plant)

                    ->  (?place hHasSight ?Life2)]

 

==案3終わり