如何在dotnetrdf中比较简单文字和类型文字? [英] How to compare simple and typed literals in dotnetrdf?
问题描述
我正在比较两个图,一个来自带有简单文字对象的Turtle文件,另一个来自具有显式数据类型IRI的文件.否则图是相等的.
I'm comparing two graphs, one from a Turtle file with simple literal objects, the other from a file with explicit datatype IRIs. The graphs are otherwise equal.
图A:
<s> <p> "o"
图B:
<s> <p> "o"^^xsd:string
根据 RDF 1.1(3.3个文字),"[s]简单文字是具有数据类型IRI http://www的抽象语法文字的语法糖.w3.org/2001/XMLSchema#string ".这也反映在具体的语法规范中( N-Triples ,乌龟,
According to RDF 1.1 (3.3 Literals), "[s]imple literals are syntactic sugar for abstract syntax literals with the datatype IRI http://www.w3.org/2001/XMLSchema#string". This is reflected in the concrete syntax specifications as well (N-Triples, Turtle, RDF XML).
所以我希望我的两个图都由一个三元组和一个URI节点 s 主题,一个URI节点 p 谓词以及一个文字节点 o 对象,其类型为 xsd:string 对象.基于此,我希望两者之间没有区别.
So I'd expect both my graphs to consists of a single triple with a URI node s subject, a URI node p predicate, and a literal node o with type xsd:string object. Based on this I'd expect there to be no difference between the two.
但是实际上并非如此:
var graphStringA = "<http://example.com/subject> <http://example.com/predicate> \"object\".";
var graphStringB = "<http://example.com/subject> <http://example.com/predicate> \"object\"^^<http://www.w3.org/2001/XMLSchema#string>.";
var graphA = new Graph();
var graphB = new Graph();
StringParser.Parse(graphA, graphStringA);
StringParser.Parse(graphB, graphStringB);
var diff = graphA.Difference(graphB);
差异报告中添加了一个,删除了三个.图形不同,因为对象节点的数据类型不同: graphA.Triples.First().Object.Datatype
什么都不是,而 graphB.Triples.First().Object.数据类型
是正确的URI.
There's one added and one removed triple in the difference report. The graphs are different, because the datatypes for the object nodes are different: graphA.Triples.First().Object.Datatype
is nothing, while graphB.Triples.First().Object.Datatype
is the correct URI.
在我看来,要修改此行为,我要么要么
It appears to me that to modify this behaviour I'd have to either
- 一直下降到 LiteralNode (并更改其对文字节点的假设),或
- 创建一个新的 GraphDiff (将字符串文字的默认数据类型考虑在内).
- go all the way down to LiteralNode (and change its assumptions about literal nodes), or
- create a new GraphDiff (that takes the default datatype of string literals into account).
一种解决方法是删除默认"数据类型:
A workaround is to remove the "default" datatypes:
private static void RemoveDefaultDatatype(IGraph g)
{
var triplesWithDefaultDatatype =
from triple in g.Triples
where triple.Object is ILiteralNode
let literal = triple.Object as ILiteralNode
where literal.DataType != null
where literal.DataType.AbsoluteUri == "http://www.w3.org/2001/XMLSchema#string" || literal.DataType.AbsoluteUri == "http://www.w3.org/2001/XMLSchema#langString"
select triple;
var triplesWithNoDatatype =
from triple in triplesWithDefaultDatatype
let literal = triple.Object as ILiteralNode
select new Triple(
triple.Subject,
triple.Predicate,
g.CreateLiteralNode(
literal.Value,
literal.Language));
g.Assert(triplesWithNoDatatype.ToArray());
g.Retract(triplesWithDefaultDatatype);
}
dotnetrdf中是否存在一种方法,可以以与RDF 1.1一致的方式将简单文字与带类型文字进行比较,而无需采用上述的大型重写或变通方法?
Is there a way in dotnetrdf to compare simple literals to typed literals in a way that's consistent with RDF 1.1, without resorting to major rewrite or workaround as above?
推荐答案
dotNetRDF不符合RDF 1.1,我们也声称不符合.有一个分支被重写为符合标准,但是还不能远程生产.
dotNetRDF is not RDF 1.1 compliant nor do we claim to be. There is a branch which is rewritten to be compliant but it is not remotely production ready.
假设您控制解析过程,则可以使用 RDF处理程序API .然后,您可以根据需要重写 HandleTriple(Triple t)
方法来删除隐式的 xsd:string
输入到系统中的文字.
Assuming that you control the parsing process you can customise the handling of incoming data using the RDF Handlers API. You can then strip the implicit xsd:string
type off literals as they come into the system by overriding the HandleTriple(Triple t)
method as desired.
这篇关于如何在dotnetrdf中比较简单文字和类型文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!