简体   繁体   English

JSON-LD 框架指定 rdfs 的顺序

[英]JSON-LD framing specify ordering of rdfs

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

emp:hasName rdf:type rdfs:Property .

emp:dateOfBirth  rdf:type rdfs:Property ;
    rdf:after emp:passportNumber .

emp:passportNumber rdf:type rdfs:Property .

Given the above Turtle RDF model, I want to generate the following JSON-LD:鉴于上述 Turtle RDF model,我想生成以下 JSON-LD:

"@graph": [
    {
        "@id": "emp:hasName",
        "@type": "rdfs:Property"
    },
    {
        "@id": "emp:passportNumber",
        "@type": "rdfs:Property"
    },
    {
        "@id": "emp:dateOfBirth",
        "@type": "rdfs:Property",
    }
]

Notice that emp:dateOfBirth is ordered after emp:passportNumber in the list as I specified rdf:after "emp:passportNumber" in the model.请注意, emp:dateOfBirth在列表中的emp:passportNumber之后排序,因为我在 model 中指定了rdf:after "emp:passportNumber"

How could I accomplish this via JSON-LD Framing?我如何通过 JSON-LD 框架来实现这一点?

Is it safe to rely on the order in which these rdfs are declared in, as most RDF->JSON-LD libs seem to preserve the ordering (despite the specs defining them as unordered)?依赖这些 rdf 的声明顺序是否安全,因为大多数 RDF->JSON-LD 库似乎都保留了顺序(尽管规范将它们定义为无序)?

I do not want to explicitly define an exhaustive ordered list in the RDF model as the list would be huge and only a few RDFs need to be ordered (eg. I do not care where emp:hasName is).我不想在 RDF model 中明确定义详尽的有序列表,因为该列表会很大,并且只需要订购几个 RDF(例如,我不关心emp:hasName在哪里)。

The fact that you see an "order" of the triples in JSON-LD is just an artifact of the serialization, something you would also see in RDF/XML (as XML elements are ordered) and Turtle (as text files are ordered), but it is not something to rely on to be stable.您在 JSON-LD 中看到三元组的“顺序”这一事实只是序列化的产物,您还会在 RDF/XML(因为 XML 元素是有序的)和 Turtle(因为文本文件是有序的)中看到的,但它不是依靠稳定的东西。 An implementation can very easily use a hash set to store the triples before converting to another format, and it could rearrange the triples as it sees fit.实现可以很容易地使用 hash 集来存储三元组,然后再转换为另一种格式,并且它可以根据需要重新排列三元组。

Now JSON-LD Framing seems to be used to produce compacted JSON documents, not flattened (ie with @graph ), but if it can be used both ways, I suppose this should work:现在 JSON-LD 框架似乎用于生成压缩的 JSON 文档,而不是扁平化(即使用@graph ),但如果它可以同时使用,我想这应该可以工作:

{
  "@context": ...,
  "@graph": [
    {
      "@id": "emp:passportNumber"
    },
    {
      "@id": "emp:dateOfBirth"
    }
  ]
}

Assuming you are fine with having a single global order.假设您可以拥有一个单一的全球订单。 There is however nothing in that specification to give you the ability to indicate the order in RDF itself.然而,该规范中没有任何内容让您能够在 RDF 本身中指示顺序。

If you need this for the sake of presentation, I suggest you preserve the ordering relation in the RDF itself for as long as possible.如果您出于演示的目的需要它,我建议您尽可能长时间地保留 RDF 本身中的排序关系。 As there doesn't seem to be an rdf:after , you could use something like xhv:prev for that general purpose, or use something more specific, depending on the context.由于似乎没有rdf:after ,您可以根据上下文使用xhv:prev类的东西,或者使用更具体的东西。 You might also use SPARQL to order the data based on that property, as SPARQL results are ordered.您还可以使用 SPARQL 根据该属性对数据进行排序,因为 SPARQL 结果是有序的。

The JSON-LD algorithms will optionally sort keys in an object, not not sort objects themselves. JSON-LD 算法将选择性地对 object 中的键进行排序,而不是不对对象本身进行排序。 One way you might be able to do this is using an id map , so that the subject IRIs of the different RDFS terms were then used as the keys of this map.您可能能够做到这一点的一种方法是使用id map ,以便不同 RDFS 术语的主题 IRI 然后用作此 map 的键。 For that, they would all need to be a value of some other entity.为此,它们都需要是某个其他实体的值。 One trick for doing this is to use rdfs:isDefinedBy as a reverse property, so that the ontology itself appears as the top-level object with reverse relationships to the terms it defines.这样做的一个技巧是使用rdfs:isDefinedBy作为反向属性,以便本体本身显示为顶级 object,与其定义的术语具有反向关系。 Combining a reverse property with an id map would make the object value have subject IRIs as keys, which could then be ordered.将反向属性与 id map 组合将使 object 值具有主题 IRI 作为键,然后可以对其进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM