簡體   English   中英

使用推理規則創建一個新對象

[英]Create a new object using the inference rules

我有一個語義網絡。 是否可以使用jena框架基於某些規則在語義網中創建新對象。 例如,有一個具有特定屬性的對象,那么您需要創建一個新對象並在它們之間建立連接。 可能嗎?

是的,這在耶拿的規則系統中是可能的。 通常,我們使用makeSkolem Reasoner Builtin Primitive創建此類節點:

[example:
  (?a urn:ex:owns ?b) 
  makeSkolem(?ownership,?a,?b)
  ->
  (?a urn:ex:hasOwnership ?ownership)
  (?ownership urn:ex:of ?b)
]

這將在圖中創建一個新的空白節點,該節點將用於驗證<urn:ex:owns>三元組。 例如,當給定一個包含三元組<urn:ex:a> <urn:ex:owns> <urn:ex:b>作為輸入的圖形時,上述規則將生成以下圖形結構:

<urn:ex:a> <urn:ex:hasOwnership> [
  <urn:ex:of> <urn:ex:b>
].

如果您有一些生成URI的方案,那么您也可以在規則中構造URI。

Java示例

假設so.rules存在於您的類路徑中並包含上述規則,則以下Java代碼將演示此任務的自定義規則。

// Obtains a list of rules to pass to a rule-based reasoner
// These rules are read from a file.
// This is the most common case.
final List<Rule> rules;
try (final InputStream src = Resources.getResource("so.rules").openStream()) {
  rules = Rule
      .parseRules(Rule.rulesParserFromReader(new BufferedReader(new InputStreamReader(src))));
}

// Create a rule-based reasoner.
// There are multiple types of reasoners available.
// You may prefer some over others (e.g., when performing OWL inference in tandem with custom rules)
final GenericRuleReasoner reasoner =
    (GenericRuleReasoner) GenericRuleReasonerFactory.theInstance().create(null);
reasoner.setRules(rules);

// Create a RDF Model to store data in.
// Create an inference model to interact with.
// The inference model will store any added data in the base model.
// The inference model will store inferred triples internally.
final Model baseModel = ModelFactory.createDefaultModel();
final InfModel model = ModelFactory.createInfModel(reasoner, baseModel);
model.prepare();

// Stimulate the rule by introducing the desired triples to the graph
// :a :owns :b
final Property owns = model.createProperty("urn:ex:", "owns");
final Property hasOwnership = model.createProperty("urn:ex:","hasOwnership");
final Property of = model.createProperty("urn:ex:","of");

final Resource a = model.createResource("urn:ex:a");
final Resource b = model.createResource("urn:ex:b");
model.add(a,owns,b);

// Verify that the rule has fired. That is, that we have created some node
// and that the node relates our other two resources
// -> :a :hasOwnership [ :of :b ]
assertTrue(a.hasProperty(hasOwnership));
final Resource createdObject = a.getPropertyResourceValue(hasOwnership);
assertTrue(createdObject.hasProperty(of,b));

如果您的需求相當簡單,則可以使用SPARQL CONSTRUCT查詢,即

CONSTRUCT { ?p :hasGrandfather ?g . }

WHERE {
   ?p      :hasParent ?parent .
   ?parent :hasParent ?g .
   ?g      :gender    :male .
}

將導致生成三元組以聲明祖父關系。

如果您的需求更復雜,則可以使用SHACL來實現,而SHACL可以在Jena之上實現 我將舉一個簡短的例子。 假定您具有以下RDF數據:

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

ex:InvalidRectangle
    a ex:Rectangle .

ex:NonSquareRectangle
    a ex:Rectangle ;
    ex:height 2 ;
    ex:width 3 .

ex:SquareRectangle
    a ex:Rectangle ;
    ex:height 4 ;
    ex:width 4 . 

為此,您定義了以下形狀文件:

@prefix ex: <http://example.com/ns#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix dash: <http://datashapes.org/dash#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

ex:Rectangle
    a rdfs:Class, sh:NodeShape ;
    rdfs:label "Rectangle" ;
    sh:property [
        sh:path ex:height ;
        sh:datatype xsd:integer ;
        sh:maxCount 1 ;
        sh:minCount 1 ;
        sh:name "height" ;
    ] ;
    sh:property [
        sh:path ex:width ;
        sh:datatype xsd:integer ;
        sh:maxCount 1 ;
        sh:minCount 1 ;
        sh:name "width" ;
    ] ;
    sh:rule [
        a sh:TripleRule ;
        sh:subject sh:this ;
        sh:predicate rdf:type ;
        sh:object ex:Square ;
        sh:condition ex:Rectangle ;
        sh:condition [
            sh:property [
                sh:path ex:width ;
                sh:equals ex:height ;
            ] ;
        ] ;
    ] .

它將生成以下RDF數據:

@prefix owl:   <http://www.w3.org/2002/07/owl#> .
@prefix rdf:   <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd:   <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs:  <http://www.w3.org/2000/01/rdf-schema#> .

<http://example.com/ns#SquareRectangle>
        a       <http://example.com/ns#Square> .

您可以將其添加到RDF存儲中。

這里可以找到帶有代碼的示例以及更高級的示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM