|
@@ -3,10 +3,9 @@ import sys
|
|
|
from collections import defaultdict
|
|
|
import os
|
|
|
import rdflib
|
|
|
+from rdflib.plugins.sparql import prepareQuery
|
|
|
import json
|
|
|
|
|
|
-import cPickle as pickle
|
|
|
-
|
|
|
# Work around Python 2 where a 'big integer' automatically becomes a long
|
|
|
if sys.version > '3': # pragma: no cover
|
|
|
integer_types = (int,)
|
|
@@ -27,7 +26,54 @@ class ModelverseState(object):
|
|
|
self.graph = rdflib.Graph()
|
|
|
self.mv = rdflib.Namespace("http://modelverse.mv/#")
|
|
|
self.graph.bind("MV", self.mv)
|
|
|
- self.free_id = 0
|
|
|
+ self.free_id = -1
|
|
|
+ self.prepared_queries = {
|
|
|
+ "read_value": """
|
|
|
+ SELECT ?value
|
|
|
+ WHERE {
|
|
|
+ ?var1 MV:hasValue ?value .
|
|
|
+ }
|
|
|
+ """,
|
|
|
+ "read_outgoing": """
|
|
|
+ SELECT ?link
|
|
|
+ WHERE {
|
|
|
+ ?link MV:hasSource ?var1 .
|
|
|
+ }
|
|
|
+ """,
|
|
|
+ "read_incoming": """
|
|
|
+ SELECT ?link
|
|
|
+ WHERE {
|
|
|
+ ?link MV:hasTarget ?var1 .
|
|
|
+ }
|
|
|
+ """,
|
|
|
+ "read_edge": """
|
|
|
+ SELECT ?source ?target
|
|
|
+ WHERE {
|
|
|
+ ?var1 MV:hasSource ?source ;
|
|
|
+ MV:hasTarget ?target .
|
|
|
+ }
|
|
|
+ """,
|
|
|
+ "read_dict_keys": """
|
|
|
+ SELECT ?key
|
|
|
+ WHERE {
|
|
|
+ ?main_edge MV:hasSource ?var1 .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget ?key .
|
|
|
+ }
|
|
|
+ """,
|
|
|
+ "read_dict_node": """
|
|
|
+ SELECT ?value_node
|
|
|
+ WHERE {
|
|
|
+ ?main_edge MV:hasSource ?var1 ;
|
|
|
+ MV:hasTarget ?value_node .
|
|
|
+ ?attr_edge MV:hasSource ?main_edge ;
|
|
|
+ MV:hasTarget ?var2 .
|
|
|
+ }
|
|
|
+ """,
|
|
|
+ }
|
|
|
+
|
|
|
+ for k, v in self.prepared_queries.iteritems():
|
|
|
+ self.prepared_queries[k] = prepareQuery(self.prepared_queries[k], initNs={"MV": self.mv})
|
|
|
|
|
|
self.root = self.parse(bootfile)
|
|
|
|
|
@@ -129,14 +175,7 @@ class ModelverseState(object):
|
|
|
def read_value(self, node):
|
|
|
if not isinstance(node, rdflib.URIRef):
|
|
|
return (None, status.FAIL_RV_UNKNOWN)
|
|
|
- q = \
|
|
|
- """
|
|
|
- SELECT ?value
|
|
|
- WHERE {
|
|
|
- <%s> MV:hasValue ?value .
|
|
|
- }
|
|
|
- """ % node
|
|
|
- result = self.graph.query(q)
|
|
|
+ result = self.graph.query(self.prepared_queries["read_value"], initBindings={"var1": node})
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RV_NO_VALUE)
|
|
|
return (json.loads(list(result)[0][0]), status.SUCCESS)
|
|
@@ -144,36 +183,17 @@ class ModelverseState(object):
|
|
|
def read_outgoing(self, elem):
|
|
|
if not isinstance(elem, rdflib.URIRef):
|
|
|
return (None, status.FAIL_RO_UNKNOWN)
|
|
|
- result = self.graph.query(
|
|
|
- """
|
|
|
- SELECT ?link
|
|
|
- WHERE {
|
|
|
- ?link MV:hasSource <%s> .
|
|
|
- }
|
|
|
- """ % elem)
|
|
|
+ result = self.graph.query(self.prepared_queries["read_outgoing"], initBindings={"var1": elem})
|
|
|
return ([i[0] for i in result], status.SUCCESS)
|
|
|
|
|
|
def read_incoming(self, elem):
|
|
|
if not isinstance(elem, rdflib.URIRef):
|
|
|
return (None, status.FAIL_RI_UNKNOWN)
|
|
|
- result = self.graph.query(
|
|
|
- """
|
|
|
- SELECT ?link
|
|
|
- WHERE {
|
|
|
- ?link MV:hasTarget <%s> .
|
|
|
- }
|
|
|
- """ % elem)
|
|
|
+ result = self.graph.query(self.prepared_queries["read_incoming"], initBindings={"var1": elem})
|
|
|
return ([i[0] for i in result], status.SUCCESS)
|
|
|
|
|
|
def read_edge(self, edge):
|
|
|
- result = self.graph.query(
|
|
|
- """
|
|
|
- SELECT ?source ?target
|
|
|
- WHERE {
|
|
|
- <%s> MV:hasSource ?source ;
|
|
|
- MV:hasTarget ?target .
|
|
|
- }
|
|
|
- """ % edge)
|
|
|
+ result = self.graph.query(self.prepared_queries["read_edge"], initBindings={"var1": edge})
|
|
|
if len(result) == 0:
|
|
|
return ([None, None], status.FAIL_RE_UNKNOWN)
|
|
|
else:
|
|
@@ -207,15 +227,7 @@ class ModelverseState(object):
|
|
|
if not isinstance(node, rdflib.URIRef):
|
|
|
return (None, status.FAIL_RDICT_UNKNOWN)
|
|
|
|
|
|
- result = self.graph.query(
|
|
|
- """
|
|
|
- SELECT ?key
|
|
|
- WHERE {
|
|
|
- ?main_edge MV:hasSource <%s> .
|
|
|
- ?attr_edge MV:hasSource ?main_edge ;
|
|
|
- MV:hasTarget ?key .
|
|
|
- }
|
|
|
- """ % node)
|
|
|
+ result = self.graph.query(self.prepared_queries["read_dict_keys"], initBindings={"var1": node})
|
|
|
return ([i[0] for i in result], status.SUCCESS)
|
|
|
|
|
|
def read_dict_edge(self, node, value):
|
|
@@ -241,16 +253,7 @@ class ModelverseState(object):
|
|
|
def read_dict_node(self, node, value_node):
|
|
|
if not isinstance(node, rdflib.URIRef):
|
|
|
return (None, status.FAIL_RDICTN_UNKNOWN)
|
|
|
- result = self.graph.query(
|
|
|
- """
|
|
|
- SELECT ?value_node
|
|
|
- WHERE {
|
|
|
- ?main_edge MV:hasSource <%s> ;
|
|
|
- MV:hasTarget ?value_node .
|
|
|
- ?attr_edge MV:hasSource ?main_edge ;
|
|
|
- MV:hasTarget <%s> .
|
|
|
- }
|
|
|
- """ % (node, value_node))
|
|
|
+ result = self.graph.query(self.prepared_queries["read_dict_node"], initBindings={"var1": node, "var2": value_node})
|
|
|
if len(result) == 0:
|
|
|
return (None, status.FAIL_RDICTN_NOT_FOUND)
|
|
|
return (list(result)[0][0], status.SUCCESS)
|
|
@@ -278,9 +281,10 @@ class ModelverseState(object):
|
|
|
return (None, status.FAIL_RRDICT_OOB)
|
|
|
result = self.graph.query(
|
|
|
"""
|
|
|
- SELECT ?main_edge
|
|
|
+ SELECT ?source_node
|
|
|
WHERE {
|
|
|
- ?main_edge MV:hasTarget <%s> .
|
|
|
+ ?main_edge MV:hasTarget <%s> ;
|
|
|
+ MV:hasSource ?source_node .
|
|
|
?attr_edge MV:hasSource ?main_edge ;
|
|
|
MV:hasTarget ?value_node .
|
|
|
?value_node MV:hasValue '%s' .
|