sparql-用三联体替换三联体

时间:2018-12-03 08:43:46

标签: java sparql jena

以下是来自lubm的简单sparql查询。

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX ub: <http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#>
SELECT ?X  ?Y1  ?Y2  ?Y3
WHERE
{?X rdf:type ub:Professor .}

还有一个Map<String,List<String>>,其中包含从SCHEMA推断出的一些子类数据,例如

{
    "<http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#Professor>":[
        "<http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#Chair>",
        "<http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#Dean>",
        "<http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#AssistantProfessor>",
        ....
    ]
}

我想用其子类的并集代替sparql查询中的这个三元组?X rdf:type ub:Professor .

{{{{?X a <http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#Professor>}
    UNION
   {?X a <http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#Chair>}
  }
   UNION
  {?X a <http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#Dean>}
 }
  UNION
 {?X a <http://swat.cse.lehigh.edu/onto/univ-bench-dl.owl#AssistantProfessor>}
}

但是我不知道该怎么做。

能否请您提供一些代码在jena(java)中执行此操作??

感谢:)

1 个答案:

答案 0 :(得分:0)

我以这种方式解决了这个问题:

使用子类数据创建ElementUnion并将其添加到Element的{​​{1}}。

query

弄清楚如何创建Query query = QueryFactory.read(queryFile); ElementGroup element = (ElementGroup) query.getQueryPattern(); Triple pattern1 = Triple.create(Var.alloc("s1"), Var.alloc("p1"), Var.alloc("o1")); Triple pattern2 = Triple.create(Var.alloc("s2"), Var.alloc("p2"), Var.alloc("o2")); ElementTriplesBlock block1 = new ElementTriplesBlock(); ElementTriplesBlock block2 = new ElementTriplesBlock(); block1.addTriple(pattern1); block2.addTriple(pattern2); ElementUnion eu = new ElementUnion(); eu.addElement(block1); eu.addElement(block2); element.addElement(eu); 之后,此问题已解决。