|
@@ -24,6 +24,9 @@ def string_to_instance(value):
|
|
|
class ModelverseState(object):
|
|
|
def __init__(self, bootfile = None):
|
|
|
self.graph = rdflib.Graph()
|
|
|
+ self.mv = rdflib.Namespace("http://modelverse.mv/#")
|
|
|
+ self.graph.bind("MV", self.mv)
|
|
|
+
|
|
|
self.parse(bootfile)
|
|
|
self.root = 0
|
|
|
|
|
@@ -97,8 +100,8 @@ class ModelverseState(object):
|
|
|
if not isinstance(target, rdflib.BNode):
|
|
|
return (None, status.FAIL_CE_TARGET)
|
|
|
edge = rdflib.BNode()
|
|
|
- self.graph.add((edge, "hasSource", source))
|
|
|
- self.graph.add((edge, "hasTarget", target))
|
|
|
+ self.graph.add((edge, self.mv.hasSource, source))
|
|
|
+ self.graph.add((edge, self.mv.hasTarget, target))
|
|
|
return (edge, status.SUCCESS)
|
|
|
|
|
|
def is_valid_datavalue(self, value):
|
|
@@ -117,7 +120,7 @@ class ModelverseState(object):
|
|
|
if not self.is_valid_datavalue(value):
|
|
|
return (None, status.FAIL_CNV_OOB)
|
|
|
node = rdflib.BNode()
|
|
|
- self.graph.add((node, "hasValue", rdflib.Literal(value)))
|
|
|
+ self.graph.add((node, self.mv.hasValue, rdflib.Literal(value)))
|
|
|
return (node, status.SUCCESS)
|
|
|
|
|
|
def create_dict(self, source, data, destination):
|
|
@@ -140,9 +143,9 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?value
|
|
|
WHERE {
|
|
|
- %s "hasValue" ?value.
|
|
|
+ _:%s MV:hasValue ?value .
|
|
|
}
|
|
|
- """ % node
|
|
|
+ """ % node)
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RV_NO_VALUE)
|
|
|
return (result[0], status.SUCCESS)
|
|
@@ -154,9 +157,9 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?link
|
|
|
WHERE {
|
|
|
- %s "hasTarget" ?link.
|
|
|
+ _:%s "hasTarget" ?link .
|
|
|
}
|
|
|
- """ % elem
|
|
|
+ """ % elem)
|
|
|
return (list(result), status.SUCCESS)
|
|
|
|
|
|
def read_incoming(self, elem):
|
|
@@ -166,9 +169,9 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?link
|
|
|
WHERE {
|
|
|
- %s "hasSource" ?link.
|
|
|
+ _:%s MV:hasSource ?link .
|
|
|
}
|
|
|
- """ % elem
|
|
|
+ """ % elem)
|
|
|
return (list(result), status.SUCCESS)
|
|
|
|
|
|
def read_edge(self, edge):
|
|
@@ -176,10 +179,10 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?source, ?target
|
|
|
WHERE {
|
|
|
- %s "hasSource" ?source;
|
|
|
- "hasTarget" ?target.
|
|
|
+ _:%s MV:hasSource ?source ;
|
|
|
+ MV:hasTarget ?target .
|
|
|
}
|
|
|
- """ % edge
|
|
|
+ """ % edge)
|
|
|
if len(result) == 0:
|
|
|
return ([None, None], status.FAIL_RE_UNKNOWN)
|
|
|
else:
|
|
@@ -190,18 +193,20 @@ class ModelverseState(object):
|
|
|
return (None, status.FAIL_RDICT_UNKNWON)
|
|
|
if not self.is_valid_datavalue(value):
|
|
|
return (None, status.FAIL_RDICT_OOB)
|
|
|
- result = self.graph.query(
|
|
|
- """
|
|
|
+
|
|
|
+ q = """
|
|
|
SELECT ?value
|
|
|
WHERE {
|
|
|
- ?main_edge "hasSource" %s;
|
|
|
- "hasTarget" ?value_node.
|
|
|
- ?attr_edge "hasSource" ?main_edge;
|
|
|
- "hasTarget" ?attr_node.
|
|
|
- ?attr_node "hasValue" %s.
|
|
|
- ?value_node "hasValue" ?value.
|
|
|
+ ?main_edge MV:hasSource _:%s .
|
|
|
+ ?main_edge MV:hasTarget ?value_node .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget ?attr_node .
|
|
|
+ ?attr_node MV:hasValue %s .
|
|
|
+ ?value_node MV:hasValue ?value .
|
|
|
}
|
|
|
""" % (node, value)
|
|
|
+ print(q)
|
|
|
+ result = self.graph.query(q)
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RDICT_NOT_FOUND)
|
|
|
return (result[0], status.SUCCESS)
|
|
@@ -214,11 +219,11 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?key
|
|
|
WHERE {
|
|
|
- ?main_edge "hasSource" %s.
|
|
|
- ?attr_edge "hasSource" ?main_edge;
|
|
|
- "hasTarget" ?key.
|
|
|
+ ?main_edge MV:hasSource _:%s .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget ?key .
|
|
|
}
|
|
|
- """
|
|
|
+ """)
|
|
|
return (list(result), status.SUCCESS)
|
|
|
|
|
|
def read_dict_edge(self, node, value):
|
|
@@ -230,13 +235,13 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?main_edge
|
|
|
WHERE {
|
|
|
- ?main_edge "hasSource" %s;
|
|
|
- "hasTarget" ?value_node.
|
|
|
- ?attr_edge "hasSource" ?main_edge;
|
|
|
- "hasTarget" ?attr_node.
|
|
|
- ?attr_node "hasValue" %s.
|
|
|
+ ?main_edge MV:hasSource _:%s ;
|
|
|
+ MV:hasTarget ?value_node .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget ?attr_node .
|
|
|
+ ?attr_node MV:hasValue %s .
|
|
|
}
|
|
|
- """ % (node, value)
|
|
|
+ """ % (node, value))
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RDICTE_NOT_FOUND)
|
|
|
return (result[0], status.SUCCESS)
|
|
@@ -248,13 +253,13 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?value
|
|
|
WHERE {
|
|
|
- ?main_edge "hasSource" %s;
|
|
|
- "hasTarget" ?value_node.
|
|
|
- ?attr_edge "hasSource" ?main_edge;
|
|
|
- "hasTarget" %s.
|
|
|
- ?value_node "hasValue" ?value.
|
|
|
+ ?main_edge MV:hasSource _:%s ;
|
|
|
+ MV:hasTarget ?value_node .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget _:%s .
|
|
|
+ ?value_node MV:hasValue ?value .
|
|
|
}
|
|
|
- """ % (node, value)
|
|
|
+ """ % (node, value))
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RDICTN_NOT_FOUND)
|
|
|
return (result[0], status.SUCCESS)
|
|
@@ -266,11 +271,11 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?main_edge
|
|
|
WHERE {
|
|
|
- ?main_edge "hasSource" %s.
|
|
|
- ?attr_edge "hasSource" ?main_edge;
|
|
|
- "hasTarget" %s.
|
|
|
+ ?main_edge MV:hasSource _:%s .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget _:%s .
|
|
|
}
|
|
|
- """ % (node, value)
|
|
|
+ """ % (node, value))
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RDICTNE_NOT_FOUND)
|
|
|
return (result[0], status.SUCCESS)
|
|
@@ -284,12 +289,12 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?main_edge
|
|
|
WHERE {
|
|
|
- ?main_edge "hasTarget" %s.
|
|
|
- ?attr_edge "hasSource" ?main_edge;
|
|
|
- "hasTarget" ?value_node.
|
|
|
- ?value_node "hasValue" %s.
|
|
|
+ ?main_edge MV:hasTarget _:%s .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget ?value_node .
|
|
|
+ ?value_node MV:hasValue %s .
|
|
|
}
|
|
|
- """ % (node, value)
|
|
|
+ """ % (node, value))
|
|
|
|
|
|
return (list(result), status.SUCCESS)
|
|
|
|
|
@@ -306,11 +311,11 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?edge
|
|
|
WHERE {
|
|
|
- { ?edge "hasTarget" %s. }
|
|
|
+ { ?edge MV:hasTarget _:%s . }
|
|
|
UNION
|
|
|
- { ?edge "hasSource" %s. }
|
|
|
+ { ?edge MV:hasSource _:%s . }
|
|
|
}
|
|
|
- """
|
|
|
+ """)
|
|
|
# ... and remove them
|
|
|
for e in result:
|
|
|
self.delete_edge(e)
|
|
@@ -328,11 +333,11 @@ class ModelverseState(object):
|
|
|
"""
|
|
|
SELECT ?edge
|
|
|
WHERE {
|
|
|
- { ?edge "hasTarget" %s. }
|
|
|
+ { ?edge MV:hasTarget _:%s . }
|
|
|
UNION
|
|
|
- { ?edge "hasSource" %s. }
|
|
|
+ { ?edge MV:hasSource _:%s . }
|
|
|
}
|
|
|
- """
|
|
|
+ """)
|
|
|
# ... and remove them
|
|
|
for e in result:
|
|
|
self.delete_edge(e)
|