【问题标题】:Data property value extraction from an instance of an ontology从本体实例中提取数据属性值
【发布时间】:2013-04-28 17:42:05
【问题描述】:

我正在尝试显示我的本体实例的数据属性值。我为此目的使用耶拿。我的实例如下:

<!-- 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 中使用 Jena 的代码如下:

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,然后是 7 乘以 293)。我想我做错了什么,因为它正在为所有内容检索相同的值。我看到它的方式只是打印每个实例的最后一个数据属性值。

【问题讨论】:

    标签: java eclipse rdf jena ontology


    【解决方案1】:

    正如您需要在

    中指定整个 IRI
    inf.getOntClass("http://www.semanticweb.org/ontologies/2013/3/Ontology1365003423152.owl#Attack");
    

    你需要在

    中指定整个IRI
    p[0] = inf.getOntProperty("HasProtocol");
    

    由于可能没有带有 IRI 的 OntProperty HasProtocolp[0](以及其他)被设置为 null,并且在许多地方 Jena API 将 null 解释为通配符。所以

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

    只是在模型中查询以instance 作为主语和任何谓词和任何宾语的三元组。 Jena 恰好每次都为每个 instance 返回相同的三元组。我猜这不是文件中提到的最后一个,而是 [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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2017-11-14
      • 1970-01-01
      • 2016-04-17
      • 2016-12-30
      • 2020-12-27
      相关资源
      最近更新 更多