根据本体模型中的时间对插入的数据进行排序

时间:2017-04-27 10:39:29

标签: sorting sparql ontology protege

我有一个本体模型。我通过Sparql更新在一个类实例中插入整数数据。该模型随机存储数据,无需任何订单。现在,当我想通过Sparql Query提取这些数据时,我希望它按插入时间顺序排列。我怎么能实现这个目标?有什么想法吗?

P.S:我的本体模型是在Protege软件中制作的。 我的插入数据查询低于一个。

PREFIX test:<http://www.semanticweb.org/muhammad/ontologies/2017/2/untitled-ontology-14#> 
INSERT { 
  ?KPI_Variables test:hasValue_ROB1 10
} WHERE {
  ?KPI_Variables test:hasValue_ROB1 ?Newvalue 
  FILTER(?KPI_Variables= test:Actual_Production_Time)
}

为获取数据,我使用以下查询:

PREFIX test:<http://www.semanticweb.org/muhammad/ontologies/2017/2/untitled-ontology-14#> 
SELECT ?KPI_Variables ?Newvalue WHERE {
  ?KPI_Variables test:hasValue_ROB1 ?Newvalue 
  FILTER(?KPI_Variables = test:Actual_Production_Time)
} LIMIT 25

1 个答案:

答案 0 :(得分:4)

RDF中的数据简直是三元组。当将三元组添加到图形时,没有的概念。如果您需要这种信息,则需要在数据模型中明确说明。 SPARQL包含一个 now 函数,可以让您获取运行查询的时间戳。这意味着你可以做这样的事情:

prefix  : <urn:ex:>

insert {
  [] :hasSubject ?s ;
     :hasPredicate ?p ;
     :hasObject ?o ;
     :hasTime ?now .
}
where {
    #-- Fake a couple of triples
    values (?s ?p ?o) {
        (:a :p :b)
        (:c :q :d)
    }
    #-- Get the current time
    bind (now() as ?now)
}

现在,您的图表包含以下数据:

@prefix :      <urn:ex:> .

[ :hasObject     :d ;
  :hasPredicate  :q ;
  :hasSubject    :c ;
  :hasTime       "2017-04-28T13:32:11.482+00:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>
] .

[ :hasObject     :b ;
  :hasPredicate  :p ;
  :hasSubject    :a ;
  :hasTime       "2017-04-28T13:32:11.482+00:00"^^<http://www.w3.org/2001/XMLSchema#dateTime>
] .

您可以查询:

prefix  : <urn:ex:>

select ?s ?p ?o ?time {
  [] :hasSubject ?s ;
     :hasPredicate ?p ;
     :hasObject ?o ;
     :hasTime ?time
}
order by ?time
s,p,o,time
urn:ex:c,urn:ex:q,urn:ex:d,2017-04-28T13:32:11.482+00:00
urn:ex:a,urn:ex:p,urn:ex:b,2017-04-28T13:32:11.482+00:00

一旦你在不同的时间插入了一些东西,你就会有不同的时间值,所以排序是有意义的。我建议你不要像我一样重新实现三元组(如果你打算采用直接的实现方式,你应该使用标准词汇表),而是要有一些实际上有时间戳的有意义的结构。作为其中的一部分。