使用 rdf4j 将元组转换为 json

Convert tuples to json using rdf4j

问题如下:在解析sparql查询时,我得到了简化的ast,我需要将其传递给第三方库进行进一步处理,但开箱即用的功能(rdf4j)不允许以机器可读格式传输 ast,只有 tex,我想以 json 格式接收。

        StringBuilder qb = new StringBuilder();
        qb.append("    PREFIX has_vaccine_allergen: <http://purl.obolibrary.org/obo/VO_0000531>\n" +
                "    PREFIX chicken_egg_protein_allergen: <http://purl.obolibrary.org/obo/VO_0000912>   \n" +
                "    SELECT distinct ?vaccine_label ?vaccine \n" +
                "    FROM <http://purl.obolibrary.org/obo/merged/VO>\n" +
                "    WHERE {\n" +
                "        ?vaccine rdfs:label ?vaccine_label .\n" +
                "        ?vaccine rdfs:subClassOf ?vaccine_restriction .\n" +
                "        ?vaccine_restriction owl:onProperty has_vaccine_allergen:; owl:someValuesFrom chicken_egg_protein_allergen: .\n" +
                "\t}");
        SPARQLParser parser = new SPARQLParser();

        ParsedTupleQuery q = (ParsedTupleQuery)parser.parseQuery(qb.toString(), null);
        TupleExpr te = q.getTupleExpr();
        System.out.println(te);

代码执行结果:

SelectQuery
[PrefixDecl (prefix=has_vaccine_allergen), PrefixDecl (prefix=chicken_egg_protein_allergen), SelectQuery]
Distinct
   Projection
      ProjectionElemList
         ProjectionElem "vaccine_label"
         ProjectionElem "vaccine"
      Join
         Join
            Join
               StatementPattern
                  Var (name=vaccine)
                  Var (name=_const_9285ccfc_uri, value=http://www.w3.org/2000/01/rdf-schema#label, anonymous)
                  Var (name=vaccine_label)
               StatementPattern
                  Var (name=vaccine)
                  Var (name=_const_4592be07_uri, value=http://www.w3.org/2000/01/rdf-schema#subClassOf, anonymous)
                  Var (name=vaccine_restriction)
            StatementPattern
               Var (name=vaccine_restriction)
               Var (name=_const_a509c4e0_uri, value=http://www.w3.org/2002/07/owl#onProperty, anonymous)
               Var (name=_const_3319983d_uri, value=http://purl.obolibrary.org/obo/VO_0000531, anonymous)
         StatementPattern
            Var (name=vaccine_restriction)
            Var (name=_const_6539d60c_uri, value=http://www.w3.org/2002/07/owl#someValuesFrom, anonymous)
            Var (name=_const_3319a704_uri, value=http://purl.obolibrary.org/obo/VO_0000912, anonymous)

我需要将其转换为 json 格式

首先:TupleExpr 对象(以及它下面的树)不是抽象语法树 (AST),而是 RDF4J 的查询代数模型。如果您希望直接处理 SPARQL 查询的 AST,可以使用 SyntaxTreeBuilder.parseQuery(queryString) 来接收实际的 AST。也就是说,对 AST 或代数模型进行查询重新处理都可以,这在一定程度上取决于您的输出应该做什么,哪个是最佳选择。

对于 AST 或查询代数模型,RDF4J 具有抽象 Visitor implementations that you can freely extend for your own needs. For AST trees, AbstractASTVisitor is a good starting point (make sure you pick the right one, RDF4J has two: one for SeRQL, one for SPARQL). For query algebra models, the place to start is AbstractQueryModelVisitor。在 RDF4J 代码库本身中有大量的实现示例。典型的模式是在您想要定义自己的行为的地方重写 meet 方法,当您处理不是叶子的节点时,请注意在最后调用 super.meet()