2013-04-28 80 views
0

我試圖顯示我的本體的實例的數據屬性值。我正在爲此使用耶拿。我的情況如下:從本體的實例中提取數據屬性值

<!-- 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> 

而且我在Java代碼中使用耶拿如下:

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

現在印刷的第一次迭代只打印零,而第二隻293(7次0,隨後到293倍)。我想我正在做一些錯誤的事情,因爲它爲所有事物檢索相同的值。我看到它的方式只是打印每個實例的最後一個數據屬性值。

回答

2

正如你需要在

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

指定整個IRI你需要

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

指定整個IRI因爲有可能不與IRI HasProtocolp[0]的OntProperty(和其餘)被設置爲null,並且在很多地方,Jena API將null解釋爲通配符。所以

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

只是查詢三合爲一的模型instance爲主體,任何斷言和任何對象。 Jena每次都會爲每個instance返回相同的三倍。我猜想這不是文件中提到的最後一個,而是[實例,hasDestBytes,...]三元組,基於存儲或索引三元組的一些工件。無論哪種方式,因爲它只是通配符匹配的結果,所以行爲可能沒有被精確定義。做這樣的事情,而不是你應該全部設置:

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