程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

在文本 SPARQL 语法和 SPIN RDF 词汇表之间转换:如何添加 rdfs:comment 和 sp:text

发布于2023-03-09 14:42     阅读(1203)     评论(0)     点赞(1)     收藏(0)


使用 SPIN API ( http://topbraid.org/spin/api/ ) 并使用https://github.com/spinrdf/spinrdf/blob/master/src-examples/org/topbraid/spin中的示例代码/examples/SPINParsingExample.java我正在尝试将 rdfs:comment 和 spin:text 的处理添加到示例中。Topbraid Composer 免费版 (TBC FE) 确实允许每个 SPIN 规则一条评论,该规则包含在 RDF 中。TBC FE 还可以选择通过 sp:text 属性将 SPIN SPARQL 源代码包含为 xsd:string 值。我想在示例的扩展版本中做同样的事情,然后将其转移到我想嵌入 SPIN 规则编辑的工作代码中。

这是我当前的示例代码的扩展版本。请忽略日志记录警告。请注意,我在示例查询(第 54 行)顶部插入的注释会静静地放入输出中(输出也包含在下面)。

/*
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  See the NOTICE file distributed with this work for additional
 *  information regarding copyright ownership.
 */

package mil.disa.dso.spo.a2i.nsc.sharing2025.raaDemo;

import org.topbraid.spin.arq.ARQ2SPIN;
import org.topbraid.spin.arq.ARQFactory;
import org.topbraid.spin.model.Select;
import org.topbraid.spin.system.SPINModuleRegistry;

import org.apache.jena.query.Query;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.util.FileUtils;
import org.apache.jena.vocabulary.RDF;


/**
 * Converts between textual SPARQL representation and SPIN RDF model.
 * 
 * @author Holger Knublauch
 */
public class SPINParsingExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // Register system functions (such as sp:gt (>))
        SPINModuleRegistry.get().init();

        // Create an empty OntModel importing SP
        Model model = ModelFactory.createDefaultModel();
        model.setNsPrefix("rdf", RDF.getURI());
        model.setNsPrefix("ex", "http://example.org/demo#");

        String query =
            "# This is an example SPARQL comment\n" +
            "SELECT ?person\n" +
            "WHERE {\n" +
            "    ?person a ex:Person .\n" +
            "    ?person ex:age ?age .\n" +
            "    FILTER (?age > 18) .\n" +
            "}";

        System.out.println("Original SPARQL query string:\n\n" + query);

        Query arqQuery = ARQFactory.get().createQuery(model, query);
        ARQ2SPIN arq2SPIN = new ARQ2SPIN(model);
        Select spinQuery = (Select) arq2SPIN.createQuery(arqQuery, null);

        // TODO what about the sp:text?  It's not in the artifacts printed below...
        // TODO figure out how to add a comment to the tokenized query... does not propagate from source string above
        //  perhaps this is through and addProperty call to add an rdfs:comment??  many calls, not clear how to use...
        //  get javadoc?

        System.out.println("\n-----");
        System.out.println("SPIN query in Turtle:\n");
        model.write(System.out, FileUtils.langTurtle);

        System.out.println("\n-----");
        System.out.println("SPIN query in XML:\n");
        model.write(System.out, FileUtils.langXML);

        System.out.println("\n-----");
        String str = spinQuery.toString();
        System.out.println("SPIN query:\n\n" + str);

        // Now turn it back into a Jena Query
        Query parsedBack = ARQFactory.get().createQuery(spinQuery);
        System.out.println("Jena query:\n" + parsedBack);
    }
}

上面的输出...

log4j:WARN No appenders could be found for logger (Jena).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Original SPARQL query string:

# This is an example SPARQL comment
SELECT ?person
WHERE {
    ?person a ex:Person .
    ?person ex:age ?age .
    FILTER (?age > 18) .
}

-----
SPIN query in Turtle:

@prefix ex:    <http://example.org/demo#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix sp:    <http://spinrdf.org/sp#> .

[ a                   sp:Select ;
  sp:resultVariables  ( [ sp:varName  "person" ]
                      ) ;
  sp:where            ( [ sp:object     ex:Person ;
                          sp:predicate  rdf:type ;
                          sp:subject    [ sp:varName  "person" ]
                        ]
                        [ sp:object     [ sp:varName  "age" ] ;
                          sp:predicate  ex:age ;
                          sp:subject    [ sp:varName  "person" ]
                        ]
                        [ a              sp:Filter ;
                          sp:expression  [ a        sp:gt ;
                                           sp:arg1  [ sp:varName  "age" ] ;
                                           sp:arg2  18
                                         ]
                        ]
                      )
] .

-----
SPIN query in XML:

<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns:ex="http://example.org/demo#"
    xmlns:sp="http://spinrdf.org/sp#">
  <sp:Select>
    <sp:resultVariables rdf:parseType="Collection">
      <rdf:Description>
        <sp:varName>person</sp:varName>
      </rdf:Description>
    </sp:resultVariables>
    <sp:where rdf:parseType="Collection">
      <rdf:Description>
        <sp:object rdf:resource="http://example.org/demo#Person"/>
        <sp:predicate rdf:resource="http://www.w3.org/1999/02/22-rdf-syntax-ns#type"/>
        <sp:subject rdf:parseType="Resource">
          <sp:varName>person</sp:varName>
        </sp:subject>
      </rdf:Description>
      <rdf:Description>
        <sp:object rdf:parseType="Resource">
          <sp:varName>age</sp:varName>
        </sp:object>
        <sp:predicate rdf:resource="http://example.org/demo#age"/>
        <sp:subject rdf:parseType="Resource">
          <sp:varName>person</sp:varName>
        </sp:subject>
      </rdf:Description>
      <sp:Filter>
        <sp:expression>
          <sp:gt>
            <sp:arg2 rdf:datatype="http://www.w3.org/2001/XMLSchema#integer"
            >18</sp:arg2>
            <sp:arg1 rdf:parseType="Resource">
              <sp:varName>age</sp:varName>
            </sp:arg1>
          </sp:gt>
        </sp:expression>
      </sp:Filter>
    </sp:where>
  </sp:Select>
</rdf:RDF>

-----
SPIN query:

SELECT ?person
WHERE {
    ?person a ex:Person .
    ?person ex:age ?age .
    FILTER (?age > 18) .
}
Jena query:
SELECT  ?person
WHERE
  { ?person  a                     <http://example.org/demo#Person> ;
             <http://example.org/demo#age>  ?age
    FILTER ( ?age > 18 )
  }

我确信有一种方法可以将 rdfs:comment 和源代码(通过 sp:text)添加到 RDF,但我还没有找到它。我怀疑可以通过示例(第 66 行)中的 spinQuery 实例调用 Select 类的方法来完成这两项操作,但我不确定如何操作。任何指针将不胜感激。

这个问题与我之前回答的问题有关How to turn a SPARQL/SPIN query/rule into an RDF structure from Java? 但专门研究 RDF 中的注释和源代码。


解决方案


看来您rdfs:comment只能添加到详细的 rdf 形式的查询中。不可能传递rdfs:commentsp:text,因为最后一个是字符串文字的谓词,你只能作为字符串的一部分来传递。似乎 Topbraid 仅允许通过 RDF 图进行评论,而文本查询仅存在于 GUI 中(作为分离模型)。一个建议:保持你的查询为 RDF 格式,在这种情况下你也不会有前缀问题。

例如如何添加rdfs:commentsp:text

public static void main(String ... args) {
    Model model = ModelFactory.createDefaultModel();
    model.setNsPrefix("rdf", RDF.getURI());
    model.setNsPrefix("ex", "http://example.org/demo#");
    model.setNsPrefix("sp", SP.getURI());
    model.setNsPrefix("rdfs", RDFS.getURI());

    String query = "SELECT ?person\n" +
                    "WHERE {\n" +
                    "    ?person a ex:Person .\n" +
                    "    ?person ex:age ?age .\n" +
                    "    FILTER (?age > 18) .\n" +
                    "}";
    Query arqQuery = ARQFactory.get().createQuery(model, query);
    ARQ2SPIN arq2SPIN = new ARQ2SPIN(model);
    Select select1 = (Select) arq2SPIN.createQuery(arqQuery, null);
    select1.addProperty(RDFS.comment, "Comment1"); // <-- as part of rdf

    Resource anon = model.createResource();
    anon.addProperty(RDF.type, SP.Select);
    anon.addProperty(SP.text, model.createTypedLiteral(
            "# Comment2\n" + // <-- as part of string
            "SELECT ?person\n" +
            "WHERE {\n" +
            "    ?person a ex:Person .\n" +
            "    ?person ex:age ?age .\n" +
            "    FILTER (?age < 22) .\n" +
            "}"));
    Select select2 = anon.as(Select.class);
    System.out.println("========================");
    model.write(System.out, "ttl");
    System.out.println("========================");
    System.out.println("Select1:\n" + select1);
    System.out.println("Select2:\n" + select2);
}

并输出:

========================
@prefix ex:    <http://example.org/demo#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sp:    <http://spinrdf.org/sp#> .

_:b0    a              sp:Filter ;
        sp:expression  [ a        sp:gt ;
                         sp:arg1  [ sp:varName  "age" ] ;
                         sp:arg2  18
                       ] .

_:b1    sp:object     [ sp:varName  "age" ] ;
        sp:predicate  ex:age ;
        sp:subject    [ sp:varName  "person" ] .

[ a                   sp:Select ;
  rdfs:comment        "Comment1" ;
  sp:resultVariables  ( _:b2 ) ;
  sp:where            ( _:b3 _:b1 _:b0 )
] .

_:b3    sp:object     ex:Person ;
        sp:predicate  rdf:type ;
        sp:subject    [ sp:varName  "person" ] .

_:b2    sp:varName  "person" .

[ a        sp:Select ;
  sp:text  "# Comment2\nSELECT ?person\nWHERE {\n    ?person a ex:Person .\n    ?person ex:age ?age .\n    FILTER (?age < 22) .\n}"
] .
========================
Select1:
# Comment1
SELECT ?person
WHERE {
    ?person a ex:Person .
    ?person ex:age ?age .
    FILTER sp:gt(?age, 18) .
}
Select2:
# Comment2
SELECT ?person
WHERE {
    ?person a ex:Person .
    ?person ex:age ?age .
    FILTER (?age < 22) .
}


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/649273/4f5aa616481a308e2227/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

1 0
收藏该文
已收藏

评论内容:(最多支持255个字符)