// ŽΚ^ŒŸυƒvƒƒOƒ‰ƒ€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

{

    public static void main(String[] args)

    {

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

          

        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");

    }

 

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();

        }

    }

}

 

//ƒIƒ“ƒgƒƒW[@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>

 

//ŽB‰e‹L˜^@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>

 

//Ž©•ͺ‚ΕŒˆ‚ί‚½„˜_‹K‘₯@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)]