forked from pragnadas23/PUEC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddNewTT.py
144 lines (124 loc) · 5.77 KB
/
addNewTT.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 22 12:26:22 2017
@author: pragna
"""
import sys
sys.path.insert(0,'/home/pragna/rdflib')
import os
from rdflib import RDF, RDFS, Graph, Literal, Namespace#, URIRef, ConjunctiveGraph
#from rdflib import Literal#, URIRef
#from rdflib.namespace import XSD
from rdflib.plugins.sparql import prepareQuery
from collections import defaultdict
#import time
class newTT():
def __init__(self, AGVno, rootDic):
self.qresOther = defaultdict(dict)
self.dirName = rootDic
self.baseN = 'AGVont'
self.suffix = '.owl'
self.g = Graph()
self.xsdNS = Namespace("http://www.w3.org/2001/XMLSchema#")
self.xsdFloat = self.xsdNS["float"]
self.owlNS = Namespace("http://www.w3.org/2002/07/owl#")
# < http: // www.semanticweb.org / hilaire / ontologies / 2016 / 4 / untitled - ontology - 87 # >
self.costNS = Namespace("http://www.semanticweb.org/hilaire/ontologies/2016/4/untitled-ontology-87#")
self.objPropNS = self.owlNS["ObjectProperty"]
self.dataPropNS = self.owlNS["DatatypeProperty"]
self.nodeClass = self.costNS["Node"]
# self.timeStampNS = self.costNS["timeStamp"]
# self.
self.ontFile = os.path.join(self.dirName, self.baseN + str(AGVno) + self.suffix)
def parseOnt(self, AGVno):
self.ontfile = os.path.join(self.dirName, self.baseN + str(AGVno) + self.suffix)
print('Parsing ont of AGV', AGVno)
self.g.parse(self.ontfile, format="nt")
self.g.commit()
def serializeOnt(self, AGVno, format='nt'):
print('Serializing ont of AGV', AGVno)
self.g.serialize(destination=self.ontFile, format=format)
self.g.commit()
def addNewTT(self, origin, destination,k,
itr, X):
edgeClass = self.costNS["Edge"]
strEdg = "edge-" + str(itr)
invEdgNS = self.costNS[strEdg]
self.g.add((invEdgNS, RDF.type, edgeClass))
# create "org" objprop
orgClass = self.costNS['origin']
self.g.add((orgClass, RDF.type, self.objPropNS))
self.g.add((orgClass, RDFS.domain, edgeClass))
self.g.add((orgClass, RDFS.range, self.nodeClass))
# give edge indv 'origin' property
self.g.add((edgeClass, self.objPropNS, orgClass))
strOrg = "node-" + str(origin)
orgIndNS = self.costNS[strOrg]
self.g.add((invEdgNS, orgClass, orgIndNS))
# create "dest" objprop
destClass = self.costNS['destination']
self.g.add((destClass, RDF.type, self.objPropNS))
self.g.add((destClass, RDFS.domain, edgeClass))
self.g.add((destClass, RDFS.range, self.nodeClass))
# give edge indv 'destination' property
self.g.add((edgeClass, self.objPropNS, destClass))
strDest = "node-" + str(destination)
desIndNS = self.costNS[strDest]
self.g.add((invEdgNS, destClass, desIndNS))
# create "timestamp" dataprop
timeClass = self.costNS["timeStamp"]
self.g.add((timeClass, RDF.type, self.dataPropNS))
self.g.add((timeClass, RDFS.domain, edgeClass))
self.g.add((timeClass, RDFS.range, self.xsdFloat))
# give edge indv 'timestamp' property
self.g.add((edgeClass, self.dataPropNS, timeClass))
k = Literal(k, lang="foo")
#timeStampIndNS = self.costNS[k]
self.g.add((invEdgNS, timeClass, k))
# create "traveltime" dataprop
ttClass = self.costNS['travelTime']
self.g.add((ttClass, RDF.type, self.dataPropNS))
self.g.add((ttClass, RDFS.domain, edgeClass))
self.g.add((ttClass, RDFS.range, self.xsdFloat))
# give edge indv 'tt' property
self.g.add((edgeClass, self.dataPropNS, ttClass))
tt = Literal(X, lang="foo")
#ttIndNS = self.costNS[tt]
self.g.add((invEdgNS, ttClass, tt))
def fetchOtherObs(self,AGVno,orig,dest,rootDic,ontObjList,k):
self.parseOnt(AGVno)
strOrg = "node-" + str(orig)
strDest = "node-" + str(dest)
origin = self.costNS[
strOrg] # URIRef("http://www.semanticweb.org/hilaire/ontologies/2016/4/untitled-ontology-87#node-"+str_curr_u)
destination = self.costNS[strDest]
k = Literal(k, lang="foo")
str2 = ("""
PREFIX ns: <http://www.semanticweb.org/hilaire/ontologies/2016/4/untitled-ontology-87#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?mCost
WHERE {
?edge rdf:type ns:Edge;
ns:origin ?org .
?edge rdf:type ns:Edge;
ns:destination ?dest .
?edge rdf:type ns:Edge;
ns:timeStamp ?k .
?edge rdf:type ns:Edge;
ns:travelTime ?mCost .
}""")
finalquery = prepareQuery(str2)
count = 1
for i in range(0, len(ontObjList)):
if (i !=(AGVno-1)):
qres = ontObjList[i].g.query(finalquery, initBindings={'org': origin, 'dest':destination})#, initBindings={'org': origin, 'dest':destination}) t2=time.clock()
if (len(qres)!=0):
print("Observation found in other AGV(s) for the set of nodes")
for row in qres:
print("%s %s" % row)
count = count +1
dictQres = { AGVno : qres}
return dictQres
#end if
#end if
#end for