On Tue, Mar 19, 2013 at 11:07 AM, Ed Swing <ed.sw...@sas.com> wrote:
> On to my next unexplainable thing in Jena – Property domains. The code below
> is trying to identify what properties apply to a class based on the property
> domain. Here’s the code:
>
> package ontology;
>
>
>
> import java.io.FileInputStream;
>
> import java.io.IOException;
>
> import java.util.Iterator;
>
> import com.hp.hpl.jena.ontology.*;
>
> import com.hp.hpl.jena.rdf.model.ModelFactory;
>
>
>
> /**
>
> * @author <a href="http://www.ganae.com/edswing";>Edward Swing</a>
>
> */
>
> public class PropTest {
>
>     /**
>
>      * TODO: DOCUMENT ME
>
>      *
>
>      * @param args
>
>      * @throws IOException
>
>      */
>
>     public static void main(String[] args) throws IOException {
>
>         OntModel model =
>
>
> ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF);
>
>         FileInputStream inStream =
>
>             new FileInputStream("ontology/terrorism.xml");
>
>         model.read(inStream, null);
>
>         inStream.close();
>
>         String uriBase = model.getNsPrefixURI("");
>
>         OntClass cls = model.getOntClass(uriBase + "TerrorAttack");
>
>         System.out.println(cls);
>
>         for (Iterator<OntProperty> iter = model.listAllOntProperties(); iter
>
>            .hasNext();) {
>
>             OntProperty prop = iter.next();
>
>             OntResource res = prop.getDomain();
>
>             if (res != null && res.isURIResource() && res.isClass()) {
>
>                 try {
>
>                     OntClass dom = res.asClass();
>
>                     if (cls.hasSuperClass(dom)) {
>
>                         System.out.println("Prop: " + prop + "  (domain: "
>
>                             + dom + ")");
>
>                     }
>
>                 } catch (ConversionException exc) {
>
>                 }
>
>             }
>
>         }
>
>     }
>
> }
>
>
>
> Here’s what is printed:
>
> http://blsdev1.vsticorp.com/terrorism#TerrorAttack
>
> Prop: http://blsdev1.vsticorp.com/terrorism#numberKilled  (domain:
> http://blsdev1.vsticorp.com/terrorism#Killing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#isSuicide  (domain:
> http://blsdev1.vsticorp.com/terrorism#TerrorAttack)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#eventType  (domain:
> http://blsdev1.vsticorp.com/terrorism#Event)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#numberInjured  (domain:
> http://blsdev1.vsticorp.com/terrorism#Killing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#weaponType  (domain:
> http://blsdev1.vsticorp.com/terrorism#Killing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#destination  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#relatedEvent  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#victim  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#inCountry  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#forCountry  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#regionOfInfluence  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#actor  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#organizationsInBody  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#eventDate  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#isNearTo  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#personsInBody  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#responsibleFor  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#hasLocation  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#locatedIn  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#reportedBy  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#hasTopic  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#isPartOf  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#locationsInBody  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
> Prop: http://blsdev1.vsticorp.com/terrorism#hasMember  (domain:
> http://www.w3.org/2002/07/owl#Thing)
>
>
>
> Several of these are incorrect. For instance, organizationsInBody explicitly
> has a domain of NewsArticle:
>
>
>
>        <owl:ObjectProperty rdf:ID="organizationsInBody">
>
>               <rdfs:domain rdf:resource="#NewsArticle" />
>
>               <rdfs:range rdf:resource="#Organization" />
>
>        </owl:ObjectProperty>
>
>
>
> The Javadoc description for getDomain() is “Answer a resource that
> represents the domain class of this property. If there is more than one such
> resource, an arbitrary selection is made.”
>
>
>
> If the domain of a property can be generalized into Thing (trivially true)
> then this method is useless – all it has to do is return Thing every time to
> be technically correct.

There is a rule in owl-fb.rules that says:

[objectProperty: (?P rdf:type owl:ObjectProperty) ->
                                                (?P rdfs:domain owl:Thing) (?P 
rdfs:range owl:Thing) ]

so there would be rdfs:domain owl:Thing for every property.  So, since
OntProperty#getDomain can return an arbitrary domain of the property,
returning owl:Thing is acceptable.  Dave Reynolds wrote a reply
linking to more discussion, so I'll just respond as before that you
might get what you want by querying the base model via RDF, or by
having a non inferencing OntModel to work with (perhaps as a submodel
of the inferencing OntModel).  For instance:

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.ModelFactory;

public class PropTest {
        public static void main(String[] args) throws IOException {
        OntModel base =
ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM );
        InputStream inStream = PropTest.class.getResourceAsStream(
"terrorism.xml" );
        base.read(inStream, null);
        inStream.close();
        OntModel model = ModelFactory.createOntologyModel(
OntModelSpec.OWL_DL_MEM_RULE_INF );
        model.addSubModel( base );

        String uriBase = base.getNsPrefixURI("");
        OntClass cls = model.getOntClass(uriBase + "TerrorAttack");
        System.out.println(cls);

        for ( OntModel m : new OntModel[] { base, model } ) {
                System.out.println( "*****" );
                for (Iterator<OntProperty> iter = m.listAllOntProperties();
iter.hasNext();) {
                        OntProperty prop = iter.next();
                        System.out.println( "prop: "+prop+"; domain: 
"+prop.getDomain() );
                }
        }
    }
}

Output includes:

...
prop: http://blsdev1.vsticorp.com/terrorism#organizationsInBody;
domain: http://blsdev1.vsticorp.com/terrorism#NewsArticle
...
prop: http://blsdev1.vsticorp.com/terrorism#organizationsInBody;
domain: http://www.w3.org/2002/07/owl#Thing
...
-- 
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/

Reply via email to