在耶拿遍历匿名/空白节点

时间:2017-07-28 04:09:25

标签: java sparql jena semantic-web blank-nodes

我正在使用Apache Jena的API,其中图表包含一些匿名/空白节点,因为unionOf和intersectionOf。其中一个例子是:

<owl:Class>
   <owl:unionOf rdf:parseType="Collection">
        <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity1"/>
        <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity2"/>
   </owl:unionOf>
</owl:Class>

这是一个匿名节点/资源。当我尝试获取其URI时,它类似于:

  

&#34; -50a5734d:15d839467d9:-1b8b&#34;

我无法使用此类URI进行SPARQL查询(由于解析此类URI时出现异常),也无法找到合适的Jena方法来处理它。

我正在寻找一种方法来爆炸这些节点并获取它的所有嵌套资源。

例如,在下面的情况下,它应该返回<http:/.../Entity1><http:/.../Entity2><http:/.../Entity3>

<owl:Class>
   <owl:unionOf rdf:parseType="Collection">
        <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity1"/>
        <owl:unionOf rdf:parseType="Collection">
            <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity2"/>
            <rdf:Description rdf:about="http://www.summyUrl.com/something#Entity3"/>
        </owl:unionOf>
   </owl:unionOf>
</owl:Class>
  1. 是否有任何内置的Jena方法可以处理它?<​​/ p>

  2. 如果没有,我该如何有效地做到这一点?

2 个答案:

答案 0 :(得分:3)

我试过这样做,效果很好:

/**
 * Explodes <b>Anonymous resource</b> (Collection resource) in recursive way and provides
 * nested resources. Mainly considers <code>owl:unionOf</code>, <code>owl:intersactionOf</code>, <code>rdf:first</code> and <code>rdf:rest</code>
 * while traversing.
 * 
 * @param resource
 * @return LinkedList<Resource>
 */
private List<Resource> explodeAnonymousResource(Resource resource)
{
    private static List<Property> collectionProperties = new LinkedList<Property>(Arrays.asList(OWL.unionOf,OWL.intersectionOf,RDF.first,RDF.rest));

    List<Resource> resources=new LinkedList<Resource>();
    Boolean needToTraverseNext=false;

    if(resource.isAnon())
    {
        for(Property cp:collectionProperties)
        {
            if(resource.hasProperty(cp) && !resource.getPropertyResourceValue(cp).equals(RDF.nil))
            {
                Resource nextResource=resource.getPropertyResourceValue(cp);
                resources.addAll(explodeAnonymousResource(nextResource));

                needToTraverseNext=true;
            }
        }

        if(!needToTraverseNext)
        {
            resources.add(resource);
        }
    }
    else
    {
        resources.add(resource);
    }

    return resources;
}

答案 1 :(得分:1)

使用jena-model-api:

        String s = "<rdf:RDF\n" +
            "    xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
            "    xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" +
            "    xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n" +
            "    xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n" +
            "    xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\">\n" +
            "  <owl:Ontology/>\n" +
            "  <owl:Class>\n" +
            "    <owl:unionOf rdf:parseType=\"Collection\">\n" +
            "      <owl:Class rdf:about=\"http://www.summyUrl.com/something#Entity1\"/>\n" +
            "      <owl:Class>\n" +
            "        <owl:unionOf rdf:parseType=\"Collection\">\n" +
            "          <owl:Class rdf:about=\"http://www.summyUrl.com/something#Entity1\"/>\n" +
            "          <owl:Class rdf:about=\"http://www.summyUrl.com/something#Entity2\"/>\n" +
            "        </owl:unionOf>\n" +
            "      </owl:Class>\n" +
            "    </owl:unionOf>\n" +
            "  </owl:Class>\n" +
            "</rdf:RDF>";
    Model m = ModelFactory.createDefaultModel();
    try (InputStream in = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8))) {
        m.read(in, Lang.RDFXML.getLabel());
    }
    //m.write(System.out, "ttl");
    m.listStatements()
            .mapWith(Statement::getObject)
            .filterKeep(RDFNode::isURIResource)
            .mapWith(RDFNode::asResource)
            .filterDrop(OWL.Class::equals)
            .filterDrop(OWL.Ontology::equals)
            .filterDrop(RDF.nil::equals)
            .mapWith(Resource::getURI)
            .forEachRemaining(System.out::println);

输出:

http://www.summyUrl.com/something#Entity1
http://www.summyUrl.com/something#Entity2
http://www.summyUrl.com/something#Entity1

这只是一个例子。有很多方法可以处理任何事情