Sam Thomas
Sam Thomas

Reputation: 676

Data property value extraction from an instance of an ontology

I am trying to display the data property values of the instances of my ontology. I am using Jena for this purpose. My instances are as follows:

<!-- http://www.semanticweb.org/ontologies/2013/3/Ontology1365003423152.owl#Mailbomb -->

<NamedIndividual rdf:about="&Ontology1365003423152;Mailbomb">
    <rdf:type rdf:resource="&Ontology1365003423152;Attack"/>
    <Ontology1365003423152:HasService>smtp</Ontology1365003423152:HasService>
    <Ontology1365003423152:HasFlag>SF</Ontology1365003423152:HasFlag>
    <Ontology1365003423152:HasDuration>1</Ontology1365003423152:HasDuration>
    <Ontology1365003423152:hasSrcBytes>2599</Ontology1365003423152:hasSrcBytes>
    <Ontology1365003423152:HasType>Mailbomb</Ontology1365003423152:HasType>
    <Ontology1365003423152:HasProtocol>tcp</Ontology1365003423152:HasProtocol>
    <Ontology1365003423152:hasDestBytes>293</Ontology1365003423152:hasDestBytes>
</NamedIndividual>

<!-- http://www.semanticweb.org/ontologies/2013/3/Ontology1365003423152.owl#Smurf -->

<NamedIndividual rdf:about="&Ontology1365003423152;Smurf">
    <rdf:type rdf:resource="&Ontology1365003423152;Attack"/>
    <Ontology1365003423152:HasService>ecr_i</Ontology1365003423152:HasService>
    <Ontology1365003423152:HasProtocol>icmp</Ontology1365003423152:HasProtocol>
    <Ontology1365003423152:hasSrcBytes>1032</Ontology1365003423152:hasSrcBytes>
    <Ontology1365003423152:HasFlag>SF</Ontology1365003423152:HasFlag>
    <Ontology1365003423152:HasType>Smurf</Ontology1365003423152:HasType>
    <Ontology1365003423152:hasDestBytes>0</Ontology1365003423152:hasDestBytes>
    <Ontology1365003423152:HasDuration>0</Ontology1365003423152:HasDuration>
</NamedIndividual>

And my code in Java using Jena is as follows:

public static void main(String[] args) {
            String a[] =  new String [7];
            OntProperty p[] = new OntProperty [7];  
     OntModel inf = ModelFactory.createOntologyModel();
     InputStream in = FileManager.get().open(inputFileName);
     if (in == null) {
       throw new IllegalArgumentException("File: " + inputFileName + " not found");
     }
     inf.read(in, "");
     OntClass clas =inf.getOntClass("http://www.semanticweb.org/ontologies/2013/3/Ontology1365003423152.owl#Attack");
     p[0] = inf.getOntProperty("HasProtocol");
     p[1] = inf.getOntProperty("HasService");
     p[2] = inf.getOntProperty("HasDuration");
     p[3] = inf.getOntProperty("HasFlag");
     p[4] = inf.getOntProperty("hasSrcBytes");
     p[5] = inf.getOntProperty("hasDestBytes");
     p[6] = inf.getOntProperty("HasType");
     ExtendedIterator instances = clas.listInstances();
     Individual instance = null;
     while (instances.hasNext()) {
       instance = (Individual) instances.next();
       System.out.println(a[0] = instance.getPropertyValue(p[0]).toString());
       System.out.println(a[1] = instance.getPropertyValue(p[1]).toString());
       System.out.println(a[2] = instance.getPropertyValue(p[2]).toString());
       System.out.println(a[3] = instance.getPropertyValue(p[3]).toString());
       System.out.println(a[4] = instance.getPropertyValue(p[4]).toString());
       System.out.println(a[5] = instance.getPropertyValue(p[5]).toString());
       System.out.println(a[6] = instance.getPropertyValue(p[6]).toString());
     }
}

Now the first iteration of printing only prints zero whereas the second only 293 (7 times 0 followed by 7 times 293). I guess I am doing something wrong as it's retrieving the same value for everything. The way I see it is only printing the last data property values of each instance.

Upvotes: 0

Views: 1235

Answers (1)

Joshua Taylor
Joshua Taylor

Reputation: 85813

Just as you need to specify the whole IRI in

inf.getOntClass("http://www.semanticweb.org/ontologies/2013/3/Ontology1365003423152.owl#Attack");

you need to specify the whole IRI in

p[0] = inf.getOntProperty("HasProtocol");

Since there's probably not an OntProperty with the IRI HasProtocol, p[0] (and the rest) are being set to null, and in many places the Jena API interprets null as a wildcard. So

instance.getPropertyValue(p[0]).toString());

is just querying the model for a triple with instance as the subject and any predicate and any object. Jena just happens to return the same triple for each instance every time. I'd guess that it's not the last one mentioned in the file, but rather the [instance, hasDestBytes, ...] triple, based on some artifact of how triples are stored or indexed. Either way, since it's just the result of a wildcard match, the behavior probably isn't precisely defined. Do something like this instead and you should be all set:

String ns = "http://www.semanticweb.org/ontologies/2013/3/Ontology1365003423152.owl#";
OntClass clas = inf.getOntClass(ns + "Attack");
p[0] = inf.getOntProperty(ns + "HasProtocol");
p[1] = inf.getOntProperty(ns + "HasService");

Upvotes: 2

Related Questions