使用多个程序更新图表会自动将龟文件的位置预先设置为以前的URI

时间:2017-12-19 18:18:53

标签: python ontology rdflib

sosa_sensor.py应该创建turtle文件,update_sosa_sensor.py应该更新同一个文件,龟文件存储在与两个程序相同的位置。我的代码和海龟文件如下:

sosa_sensor.py

from rdflib import Graph, Literal, BNode, RDF,URIRef
from rdflib import Namespace

data={'bn': 'Bhubaneswar/IIIT/iotlab/temperature','rt': 'oic.temperature.rt','id': 'xme2345','e':{'n':'temperature','u':'degC','v':'22'}}

SOSA = Namespace("http://www.w3.org/ns/sosa/")
g = Graph()

g.bind('sosa',SOSA)
sensor=URIRef('sensor/'+data['id'])
obs=URIRef('observation/'+data['id'])

g.add((sensor,RDF.type,SOSA['Sensor']))
g.add((sensor,SOSA["observes"],obs))
g.add((obs,RDF.type,SOSA['Observation']))
g.add((obs,SOSA['madeBySensor'],sensor))
g.add((obs,SOSA['observedProperty'],URIRef(data['bn'])))
g.add((obs,SOSA['hasSimpleResult'],Literal(data['e']['v']+" "+data['e']['u'])))
g.serialize("temp_sensor.ttl", format="turtle")  

执行上述后创建的龟文件如下:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sosa: <http://www.w3.org/ns/sosa/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<observation/xme2345> a sosa:Observation ;
    sosa:hasSimpleResult "22 degC" ;
    sosa:madeBySensor <sensor/xme2345> ;
    sosa:observedProperty <Bhubaneswar/IIIT/iotlab/temperature> .

<sensor/xme2345> a sosa:Sensor ;
    sosa:observes <observation/xme2345> .

update_sosa_sensor.py

from rdflib import Graph,URIRef,Namespace,RDF,Literal
g=Graph()
g.parse("temp_sensor.ttl",format="turtle")

data={'bn': 'Bhubaneswar/IIIT/iotlab/humidity','rt': 'oic.humidity.rt','id': 'bxe345','e':{'n':'temperature','u':'%','v':'50'}}

sen=URIRef(data['bn'].split('/')[-1]+'sensor'+'/'+data['id'])
obs=URIRef('observation'+'/'+data['id'])

SOSA = Namespace("http://www.w3.org/ns/sosa/")
g.bind('sosa',SOSA)


g.add((sen,RDF.type,SOSA['Sensor']))
g.add((sen,SOSA["observes"],obs))
g.add((sen,SOSA["observes"],obs))
g.add((obs,RDF.type,SOSA['Observation']))
g.add((obs,SOSA['madeBySensor'],sen))
g.add((obs,SOSA['observedProperty'],URIRef(data['bn'])))
g.add((obs,SOSA['hasSimpleResult'],Literal(data['e']['v']+" "+data['e']['u'])))
g.serialize("temp_sensor.ttl", format="turtle")

执行上述后,龟文件更新如下:

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sosa: <http://www.w3.org/ns/sosa/> .
@prefix xml: <http://www.w3.org/XML/1998/namespace> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

<file:///home/pratik/iotprojects/random_programs/project_implementation/observation/xme2345> a sosa:Observation ;
    sosa:hasSimpleResult "22 degC" ;
    sosa:madeBySensor <file:///home/pratik/iotprojects/random_programs/project_implementation/sensor/xme2345> ;
    sosa:observedProperty <file:///home/pratik/iotprojects/random_programs/project_implementation/Bhubaneswar/IIIT/iotlab/temperature> .

<file:///home/pratik/iotprojects/random_programs/project_implementation/sensor/xme2345> a sosa:Sensor ;
    sosa:observes <file:///home/pratik/iotprojects/random_programs/project_implementation/observation/xme2345> .

<humiditysensor/bxe345> a sosa:Sensor ;
    sosa:observes <observation/bxe345> .

<observation/bxe345> a sosa:Observation ;
    sosa:hasSimpleResult "50 %" ;
    sosa:madeBySensor <humiditysensor/bxe345> ;
    sosa:observedProperty <Bhubaneswar/IIIT/iotlab/humidity> .

运行update_sosa_sensor.py时,它会将turtle文件位置的路径预先添加到图中已存在的旧URI中,如何防止这种情况发生?

0 个答案:

没有答案