当导入的RDF文件中存在sp:text时,如何指示RDF4J使用SPIN RDF三元组?

时间:2017-09-07 15:01:07

标签: sparql rdf4j topbraid-composer spin-rdf

我使用的是RDF4J服务器和工作台版本2.2.2。我在RDF4J中使用了支持SPIN的存储库,并且我正在读取我在包含许多spin:construct规则的TobBraid Composer免费版(TBC FE)中创建的RDF文件。 TBD FE设置为通过sp:text属性包含我的构造函数的SPARQL源代码。我观察到,当sp:text三元组存在使得SPARQL源代码可用于RDF4J时,RDF4J似乎使用SPARQL源而不是使用相同查询的RDF标记化表示。我的主要和次要问题是:

  • 当导入的RDF文件中存在sp:text和RDF表示时,如何指示RDF4J使用SPIN构造函数的RDF表示?
  • 如果RDF4J使用SPIN构造函数的sp:text表示,如何指示使用导入的RDF文件头部的前缀定义?我有69个构造函数远远不够。虽然我可以在每个构造函数中嵌入前缀定义,但这样做似乎很浪费。

当我剥夺RDF4J的sp:text SPARQL源代码表示时,构造函数使用导入的RDF文件头部定义的前缀按预期运行。我可以通过在我实例化已定义构造函数的类之前执行查询来删除源代码来剥夺RDF4J的源代码。我已使用此SPARQL更新查询来完成此任务:

PREFIX sp: <http://spinrdf.org/sp#>
DELETE {?s sp:text ?o .} WHERE {?s sp:text ?o .}

我希望将SPARQL源代码保留在GUI I建筑物中,以便通过SPARQL查询与RDF4J服务器通信。我尴尬的临时修复是使用下面步骤5中使用的SPARQL更新查询替换sp:text属性的另一个自定义数据属性。此属性替换成功阻止RDF4J查找SPIN构造函数的SPARQL源代码。然后RDF4J解释规则的标记化RDF表示。有没有更简洁的方法来强制RDF4J从RDF而不是从SPARQL源代码执行SPIN构造函数?我认为必须有一些方法来表示RDF4J解释RDF表示而不是SPARQL源代码,但我不知道如何。

现在可以通过血腥细节使这种情况再现......

  1. 使用RDF4J工作台在RDF4J中创建一个支持SPIN的存储库。我的名字叫TakeRDF4J4SPIN(Repositories→New Repository)
  2. 使用RDF4J工作台(修改→清除)
  3. 清除存储库
  4. 使用RDF4J工作台加载pub7.rdf ...(修改→添加w /使用基URI作为上下文标识符未选中)
  5. 请注意,此RDF文件定义文件头部的所有相关前缀:

    <?xml version="1.0"?>
    <rdf:RDF
        xmlns:fn="http://www.w3.org/2005/xpath-functions#"
        xmlns:soo="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#"
        xmlns:spolicy="http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/spolicy#"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:spin="http://spinrdf.org/spin#"
        xmlns:sp="http://spinrdf.org/sp#"
        xmlns:smf="http://topbraid.org/sparqlmotionfunctions#"
        xmlns:owl="http://www.w3.org/2002/07/owl#"
        xmlns:arg="http://spinrdf.org/arg#"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
        xmlns:pub7="http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#"
        xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
        xmlns:spl="http://spinrdf.org/spl#"
      xml:base="http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7">
    

    另请注意,附加到类pub7:Pub7TestClass的SPIN构造函数是:

    CONSTRUCT {
        ?this spin:hasConstraintViolation _:b0 . 
        _:b0 a spin:ConstraintViolation ;
            rdfs:label "Test message." ;
            spin:violationRoot ?this ;
            spin:violationLevel spin:Error ;
           spolicy:sPActionRecordHasTimestamp ?timestamp .
    }
    WHERE {
        BIND(now() AS ?timestamp) .
    }
    
    1. 尝试创建pub7:Pub7TestClass ...
    2. 的实例

      我在RDF4J工作台的修改→SPARQL更新中使用此SPARQL更新查询来创建实例...

      PREFIX inst: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/Instantiations#>
      PREFIX pub7: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#>
      
      INSERT DATA {
        inst:aPub7TestClassInstance_test1 a pub7:Pub7TestClass .
      }
      

      由于尝试基于sp:text SPARQL源代码表示执行上述构造函数,导致RDF4J工作台中报告的前缀未定义错误:

      org.eclipse.rdf4j.query.UpdateExecutionException: org.eclipse.rdf4j.repository.RepositoryException: org.eclipse.rdf4j.sail.SailException: org.eclipse.rdf4j.query.MalformedQueryException: org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException: QName 'spin:hasConstraintViolation' uses an undefined prefix
      
      1. 现在隐藏SPIN构造函数&#39;来自RDF4J的sp:text使用以下SPARQL更新查询:
      2. PREFIX sp: <http://spinrdf.org/sp#>
        PREFIX soo: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#>
        DELETE { 
          ?originalPolicyURI sp:systemProperty ?policySourceCodeString . 
          ?originalPolicyURI sp:text ?policySourceCodeString .
        } 
        INSERT { 
          ?originalPolicyURI soo:policySourceCode ?policySourceCodeString . 
        }
        WHERE {
          ?originalPolicyURI sp:text ?policySourceCodeString . 
        }
        
        1. 现在重新运行步骤4中显示的SPARQL更新查询以实例化测试类。此次更新查询应无错误地运行。

        2. 现在使用RDF4J Workbench的Explore→Explore功能来查看我们刚刚创建的实例/个人inst:aPub7TestClassInstance_test1。应该看到实例化通过spin:hasConstratintViolation属性有一个约束违规通知,提供证据表明测试构造函数确实在没有sp:text表示的情况下运行,强制使用SPIN的RDF标记化表示构造

1 个答案:

答案 0 :(得分:0)

我有一个部分的,不优雅的解决方案来解决关于在旋转规则中包含前缀的第二个问题。作为初始化步骤,我运行以下SPARQL更新查询,为每个sp:text字符串添加所需的前缀。当然,这种解决方法是有限的,因为查询嵌入了我正在使用的前缀映射。如果需要其他前缀,则需要编辑下面的查询以嵌入新的查询定义。

这是我在将类/规则/属性定义本体加载到RDF4J之后但在加载实例化本体之前加载的SPARQL更新查询。 (由于实例化本体开始调用SPIN构造函数,因此时序至关重要。)

PREFIX sp: <http://spinrdf.org/sp#>

DELETE { 
    ?rule sp:text ?ruleText . 
} 
INSERT { 
    ?rule sp:text ?newRuleText . 
} 
WHERE { 
    ?rule sp:text ?ruleText . 
    BIND (IF (CONTAINS(?ruleText, "fn:"),      "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>\n","") 
        AS ?prefixComponent00) . 
    BIND (IF (CONTAINS(?ruleText, "owl:"),     "PREFIX owl: <http://www.w3.org/2002/07/owl#>\n","")
        AS ?prefixComponent01) . 
    BIND (IF (CONTAINS(?ruleText, "spin:"),    "PREFIX spin: <http://spinrdf.org/spin#>\n","")
        AS ?prefixComponent02) . 
    BIND (IF (CONTAINS(?ruleText, "spl:"),     "PREFIX spl: <http://spinrdf.org/spl#>\n","")
        AS ?prefixComponent03) . 
    BIND (IF (CONTAINS(?ruleText, "sp:"),      "PREFIX sp: <http://spinrdf.org/sp#>\n","")
        AS ?prefixComponent04) . 
    BIND (IF (CONTAINS(?ruleText, "par:"),     "PREFIX par: <http://parliament.semwebcentral.org/parliament#>\n","")
        AS ?prefixComponent05) . 
    BIND (IF (CONTAINS(?ruleText, "rdf:"),     "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n","")
        AS ?prefixComponent06) . 
    BIND (IF (CONTAINS(?ruleText, "rdfs:"),    "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n","")
        AS ?prefixComponent07) . 
    BIND (IF (CONTAINS(?ruleText, "time:"),    "PREFIX time: <http://www.w3.org/2006/time#>\n","")
        AS ?prefixComponent08) . 
    BIND (IF (CONTAINS(?ruleText, "xsd:"),     "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n","")
        AS ?prefixComponent09) . 
    BIND (IF (CONTAINS(?ruleText, "geo:"),     "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n","")
        AS ?prefixComponent10) . 
    BIND (IF (CONTAINS(?ruleText, "geof:"),    "PREFIX geof: <http://www.opengis.net/def/function/geosparql/>\n:","")
        AS ?prefixComponent11) . 
    BIND (IF (CONTAINS(?ruleText, "inst:"),    "PREFIX inst: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/Instantiations#>\n","")
        AS ?prefixComponent12) . 
    BIND (IF (CONTAINS(?ruleText, "pub7:"),    "PREFIX pub7: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub7#>\n","")
        AS ?prefixComponent13) . 
    BIND (IF (CONTAINS(?ruleText, "pub8:"),    "PREFIX pub8: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Interface/Pub8#>\n","")
        AS ?prefixComponent14) . 
    BIND (IF (CONTAINS(?ruleText, "soo:"),     "PREFIX soo: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/SpectrumOperationsOntology#>\n","")
        AS ?prefixComponent15) . 
    BIND (IF (CONTAINS(?ruleText, "spolicy:"), "PREFIX spolicy: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/spolicy#>\n","")
        AS ?prefixComponent16) . 
    BIND (IF (CONTAINS(?ruleText, "sharing:"), "PREFIX sharing: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/sharing#>\n","")
        AS ?prefixComponent17) . 
    BIND (IF (CONTAINS(?ruleText, "dd1494:"),  "PREFIX dd1494: <http://www.disa.mil/dso/a2i/ontologies/PBSM/Sharing/dd1494#>\n","")
        AS ?prefixComponent18) . 
    BIND (IF (CONTAINS(?ruleText, "Nuvio:"),   "PREFIX Nuvio: <http://cogradio.org/ont/Nuvio.owl#>\n","")
        AS ?prefixComponent19) . 
    BIND (IF (CONTAINS(?ruleText, "CRO2:"),    "PREFIX CRO2: <http://cogradio.org/ont/CRO2.owl#>\n","")
        AS ?prefixComponent20) . 
    BIND (IF (CONTAINS(?ruleText, "olo:"),     "PREFIX olo: <http://purl.org/ontology/olo/core#>\n","")
        AS ?prefixComponent21) . 
    BIND (CONCAT( 
      ?prefixComponent00, 
      ?prefixComponent01, 
      ?prefixComponent02, 
      ?prefixComponent03, 
      ?prefixComponent04, 
      ?prefixComponent05, 
      ?prefixComponent06, 
      ?prefixComponent07, 
      ?prefixComponent08, 
      ?prefixComponent09, 
      ?prefixComponent10, 
      ?prefixComponent11, 
      ?prefixComponent12, 
      ?prefixComponent13, 
      ?prefixComponent14, 
      ?prefixComponent15, 
      ?prefixComponent16, 
      ?prefixComponent17, 
      ?prefixComponent18, 
      ?prefixComponent19, 
      ?prefixComponent20, 
      ?prefixComponent21 
      ) AS ?prefixes ) . 
    BIND (CONCAT(?prefixes, "\n", ?ruleText) AS ?newRuleText) . 
}

如果前缀定义恰好包含在嵌入字符串或注释中,则上述查询可能包含基于误报的前缀定义。例如,文字"Greg’s solo: five measures"^^xsd:string会导致上述查询包含olo:前缀的定义,可能是不必要的。但是,包含不需要的前缀对性能没有重大影响,也不会影响正确性。通过使用正则表达式代替上面查询中的简单CONTAINS匹配,可以做得更好。