|
|
@@ -1,13 +1,13 @@
|
|
|
import urllib
|
|
|
-import urllib2
|
|
|
import json
|
|
|
import random
|
|
|
-from urllib2 import URLError
|
|
|
import sys
|
|
|
import time
|
|
|
import threading
|
|
|
+import uuid
|
|
|
|
|
|
-COMPILER_PATH = "../../interface/HUTN"
|
|
|
+import sccd.runtime.socket2event as socket2event
|
|
|
+from sccd.runtime.statecharts_core import Event
|
|
|
|
|
|
MODE_UNCONNECTED = 0
|
|
|
MODE_UNAUTHORIZED = 1
|
|
|
@@ -17,10 +17,6 @@ MODE_DIALOG = 4
|
|
|
MODE_MANUAL = 5
|
|
|
MODE_SERVICE = 6
|
|
|
|
|
|
-# Bind to the compiler (might have to update path manually!)
|
|
|
-sys.path.append(COMPILER_PATH)
|
|
|
-from hutn_compiler.compiler import main as do_compile
|
|
|
-
|
|
|
# Exceptions
|
|
|
class ModelverseException(Exception):
|
|
|
pass
|
|
|
@@ -60,12 +56,59 @@ class UnknownMetamodellingHierarchy(ModelverseException):
|
|
|
|
|
|
# Helper functions and configuration: do not use yourself!
|
|
|
taskname = None
|
|
|
-address = None
|
|
|
-last_output = None
|
|
|
mode = MODE_UNCONNECTED
|
|
|
prev_mode = None
|
|
|
current_model = None
|
|
|
registered_metamodels = {}
|
|
|
+outputs = {}
|
|
|
+ctrl_input = None
|
|
|
+ctrl_output = None
|
|
|
+
|
|
|
+def _output_thread(controller, outp, task):
|
|
|
+ req_out = controller.addOutputListener("request_out")
|
|
|
+ my_id = str(uuid.uuid4())
|
|
|
+
|
|
|
+ try:
|
|
|
+ while 1:
|
|
|
+ controller.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "get_output", "taskname": task}), my_id]))
|
|
|
+ event = req_out.fetch(-1)
|
|
|
+
|
|
|
+ if event.parameters[1] == my_id:
|
|
|
+ outp[task].append(json.loads(event.parameters[0]))
|
|
|
+ except:
|
|
|
+ raise
|
|
|
+
|
|
|
+def _exec_on_statechart(statechart):
|
|
|
+ def _exec_sc(controller, inport, outport):
|
|
|
+ global taskname
|
|
|
+ op = controller.addOutputListener(outport)
|
|
|
+
|
|
|
+ while 1:
|
|
|
+ if len(outputs[taskname]) > 1:
|
|
|
+ # Is an output message of the Mv, so put it in the SC
|
|
|
+ del outputs[taskname][0]
|
|
|
+ output_event = outputs[taskname][0]
|
|
|
+
|
|
|
+ if output_event == "Success" or output_event == "Failure":
|
|
|
+ # Is a stop event!
|
|
|
+ controller.addInput(Event("terminate", inport, []))
|
|
|
+ break
|
|
|
+ else:
|
|
|
+ # Is just a normal event!
|
|
|
+ controller.addInput(Event("input", inport, [output_event]))
|
|
|
+
|
|
|
+ input_event = op.fetch(0)
|
|
|
+ if input_event is not None:
|
|
|
+ # Expand the event and make it HTTP input
|
|
|
+ _input(input_event.parameters)
|
|
|
+
|
|
|
+ time.sleep(0.02)
|
|
|
+
|
|
|
+ thrd = threading.Thread(target=_exec_sc, args=statechart)
|
|
|
+ thrd.daemon = True
|
|
|
+ thrd.start()
|
|
|
+
|
|
|
+ return None
|
|
|
|
|
|
def _get_metamodel(model):
|
|
|
global registered_metamodels
|
|
|
@@ -114,77 +157,47 @@ def _goto_mode(new_mode, model_name=None):
|
|
|
# Go to a mode that we have no automatic transfer to: raise exception
|
|
|
raise InvalidMode("Required mode: %s, current mode: %s" % (new_mode, mode))
|
|
|
|
|
|
-def _input(value, port=None):
|
|
|
+def _input(value, task=None):
|
|
|
# Ugly json encoding of primitives
|
|
|
#print("[IN] %s" % value)
|
|
|
- if port is None:
|
|
|
- port = taskname
|
|
|
+ if task is None:
|
|
|
+ task = taskname
|
|
|
if isinstance(value, type([])):
|
|
|
value = json.dumps(value)
|
|
|
- urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": value, "taskname": port}))).read()
|
|
|
+ ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": task, "data": value}), None]))
|
|
|
else:
|
|
|
value = json.dumps(value)
|
|
|
- #print("Set input: " + str(value))
|
|
|
- urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": port}))).read()
|
|
|
+ ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": task, "value": value}), None]))
|
|
|
|
|
|
def _input_raw(value, taskname):
|
|
|
# Ugly json encoding of primitives
|
|
|
- urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname}))).read()
|
|
|
-
|
|
|
- #TODO check that this is actually a Modelverse!
|
|
|
-
|
|
|
-def _compile_AL(code):
|
|
|
- # Compile an action language file and send the compiled code
|
|
|
- code_fragments = code.split("\n")
|
|
|
- code_fragments = [i for i in code_fragments if i.strip() != ""]
|
|
|
- code_fragments = [i.replace(" ", "\t") for i in code_fragments]
|
|
|
- initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
|
|
|
- code_fragments = [i[initial_tabs:] for i in code_fragments]
|
|
|
- code_fragments.append("")
|
|
|
- code = "\n".join(code_fragments)
|
|
|
-
|
|
|
- with open(".code.alc", "w") as f:
|
|
|
- f.write(code)
|
|
|
- f.flush()
|
|
|
-
|
|
|
- compiled = do_compile(".code.alc", COMPILER_PATH + "/grammars/actionlanguage.g", "CS")
|
|
|
- return compiled
|
|
|
-
|
|
|
-def _compile_model(code):
|
|
|
- # Compile a model and send the compiled graph
|
|
|
- # First change multiple spaces to a tab
|
|
|
- code_fragments = code.split("\n")
|
|
|
- code_fragments = [i for i in code_fragments if i.strip() != ""]
|
|
|
- code_fragments = [i.replace(" ", "\t") for i in code_fragments]
|
|
|
- initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
|
|
|
- code_fragments = [i[initial_tabs:] for i in code_fragments]
|
|
|
- code_fragments.append("")
|
|
|
- code = "\n".join(code_fragments)
|
|
|
-
|
|
|
- with open(".model.mvc", "w") as f:
|
|
|
- f.write(code)
|
|
|
- f.flush()
|
|
|
-
|
|
|
- return do_compile(".model.mvc", COMPILER_PATH + "/grammars/modelling.g", "M")
|
|
|
-
|
|
|
-def _output(expected=None,port=None):
|
|
|
- if port is None:
|
|
|
- port = taskname
|
|
|
+ ctrl_input.addInput(Event("HTTP_input", "request_in", [urllib.urlencode({"op": "set_input", "taskname": taskname, "value": value}), None]))
|
|
|
+
|
|
|
+def _output(expected=None, task=None):
|
|
|
+ if task is None:
|
|
|
+ task = taskname
|
|
|
+
|
|
|
try:
|
|
|
- global last_output
|
|
|
- last_output = json.loads(urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "taskname": port}))).read())
|
|
|
- #print("[OUT] %s" % last_output)
|
|
|
+ while len(outputs[task]) < 2:
|
|
|
+ time.sleep(0.02)
|
|
|
+
|
|
|
+ del outputs[task][0]
|
|
|
+ #print("[OUT] %s" % outputs[0])
|
|
|
except:
|
|
|
raise UnknownError()
|
|
|
- if expected is not None and last_output != expected:
|
|
|
- raise InterfaceMismatch(_last_output(), expected)
|
|
|
- return last_output
|
|
|
|
|
|
-def _last_output():
|
|
|
- return last_output
|
|
|
+ if expected is not None and _last_output(task) != expected:
|
|
|
+ raise InterfaceMismatch(_last_output(task), expected)
|
|
|
+ return _last_output(task)
|
|
|
+
|
|
|
+def _last_output(task=None):
|
|
|
+ if task is None:
|
|
|
+ task = taskname
|
|
|
+
|
|
|
+ return outputs[task][0]
|
|
|
|
|
|
# Raise common exceptions
|
|
|
-def _handle_output(requested=None, split=None):
|
|
|
+def _handle_output(requested=None, split=False):
|
|
|
value = _output()
|
|
|
if value.startswith("Model exists: "):
|
|
|
raise ModelExists(value.split(": ", 1)[1])
|
|
|
@@ -199,14 +212,14 @@ def _handle_output(requested=None, split=None):
|
|
|
elif value.startswith("Attribute not found: "):
|
|
|
raise NoSuchAttribute(value.split(": ", 1)[1])
|
|
|
elif requested is not None and value.startswith(requested):
|
|
|
- if split is None:
|
|
|
+ if not split:
|
|
|
return value
|
|
|
else:
|
|
|
- splitted = value.strip().split(split, 1)
|
|
|
- if len(splitted) == 1:
|
|
|
- return ""
|
|
|
+ splitted = value.strip().split(" ", 1)
|
|
|
+ if len(splitted) > 1:
|
|
|
+ return splitted[1].split("\n")
|
|
|
else:
|
|
|
- return splitted[1].rstrip()
|
|
|
+ return []
|
|
|
else:
|
|
|
raise InterfaceMismatch(value)
|
|
|
|
|
|
@@ -251,25 +264,75 @@ def alter_context(model_name, metamodel_name):
|
|
|
global registered_metamodels
|
|
|
registered_metamodels[model_name] = metamodel_name
|
|
|
|
|
|
+def _start_http_client(addr, port, timeout):
|
|
|
+ import http_client
|
|
|
+ ctrl = http_client.Controller()
|
|
|
+ listener = ctrl.addOutputListener("request_out")
|
|
|
+ socket2event.boot_translation_service(ctrl)
|
|
|
+ thrd = threading.Thread(target=ctrl.start)
|
|
|
+ thrd.daemon = True
|
|
|
+ thrd.start()
|
|
|
+
|
|
|
+ evt = listener.fetch(-1)
|
|
|
+ if evt.name != "http_client_initialized":
|
|
|
+ raise Exception("HTTP client did not behave as expected during init: " + str(evt.name))
|
|
|
+
|
|
|
+ ctrl.addInput(Event("connect", "request_in", [(addr, port), timeout]))
|
|
|
+
|
|
|
+ evt = listener.fetch(-1)
|
|
|
+
|
|
|
+ if evt.name == "http_client_timeout":
|
|
|
+ raise Exception("HTTP client timeout")
|
|
|
+ if evt.name != "http_client_ready":
|
|
|
+ raise Exception("HTTP client did not behave as expected during connect: " + str(evt.name))
|
|
|
+
|
|
|
+ return ctrl
|
|
|
+
|
|
|
# Main MvC operations
|
|
|
-def init(address_param="http://127.0.0.1:8001", timeout=20.0):
|
|
|
+def init(address_param="127.0.0.1:8001", timeout=20.0):
|
|
|
"""Starts up the connection to the Modelverse."""
|
|
|
- global mode
|
|
|
+ # Start up the HTTP Client SC
|
|
|
+ global ctrl_input
|
|
|
+ global ctrl_output
|
|
|
+
|
|
|
+ if ctrl_input is not None:
|
|
|
+ ctrl_input.stop()
|
|
|
+ if ctrl_output is not None:
|
|
|
+ ctrl_output.stop()
|
|
|
+
|
|
|
global address
|
|
|
- global taskname
|
|
|
- address = address_param
|
|
|
+ global port
|
|
|
+
|
|
|
+ address, port = address_param.split(":", 1)
|
|
|
+ port = int(port)
|
|
|
+
|
|
|
+ ctrl_input = _start_http_client(address, port, timeout)
|
|
|
+ ctrl_output = _start_http_client(address, port, timeout)
|
|
|
+ controllers = [ctrl_input, ctrl_output]
|
|
|
+
|
|
|
+ global mode
|
|
|
start_time = time.time()
|
|
|
- taskname = random.random()
|
|
|
- while 1:
|
|
|
- try:
|
|
|
- _input_raw('"%s"' % taskname, "task_manager")
|
|
|
- mode = MODE_UNAUTHORIZED
|
|
|
- break
|
|
|
- except URLError as e:
|
|
|
- if time.time() - start_time > timeout:
|
|
|
- raise ConnectionError(e.reason)
|
|
|
- else:
|
|
|
- time.sleep(0.1)
|
|
|
+ task = str(random.random())
|
|
|
+
|
|
|
+ _input_raw('"%s"' % task, "task_manager")
|
|
|
+
|
|
|
+ mode = MODE_UNAUTHORIZED
|
|
|
+
|
|
|
+ global outputs
|
|
|
+ global taskname
|
|
|
+ taskname = task
|
|
|
+ outputs = {}
|
|
|
+
|
|
|
+ _listen_to_output(ctrl_output, task)
|
|
|
+
|
|
|
+def _listen_to_output(controller, task):
|
|
|
+ global outputs
|
|
|
+ outputs[task] = [None]
|
|
|
+
|
|
|
+ # This re-assign also diconnects the previous get_output connections to the outputs variable
|
|
|
+ thrd = threading.Thread(target=_output_thread, args=[controller, outputs, task])
|
|
|
+ thrd.daemon = True
|
|
|
+ thrd.start()
|
|
|
|
|
|
def login(username, password):
|
|
|
"""Log in a user, if user doesn't exist, it is created."""
|
|
|
@@ -305,34 +368,20 @@ def login(username, password):
|
|
|
else:
|
|
|
raise InterfaceMismatch(_last_output())
|
|
|
|
|
|
-def model_add(model_name, metamodel_name, model_code=None):
|
|
|
+def model_add(model_name, metamodel_name, model_code=""):
|
|
|
"""Instantiate a new model."""
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
|
|
|
- # Do this before creating the model, as otherwise compilation errors would make us inconsistent
|
|
|
- if model_code is not None:
|
|
|
- try:
|
|
|
- compiled = _compile_model(model_code)
|
|
|
- except Exception as e:
|
|
|
- raise CompilationError(e)
|
|
|
- else:
|
|
|
- compiled = [0]
|
|
|
-
|
|
|
_input(["model_add", metamodel_name, model_name])
|
|
|
_handle_output("Waiting for model constructors...")
|
|
|
- _input(compiled)
|
|
|
+ _input(model_code)
|
|
|
_output("Success")
|
|
|
|
|
|
global registered_metamodels
|
|
|
registered_metamodels[model_name] = metamodel_name
|
|
|
|
|
|
def upload_code(code):
|
|
|
- try:
|
|
|
- compiled = _compile_AL(code)
|
|
|
- except Exception as e:
|
|
|
- raise CompilationError(e)
|
|
|
-
|
|
|
- _input(compiled)
|
|
|
+ _input(code)
|
|
|
|
|
|
def model_delete(model_name):
|
|
|
"""Delete an existing model."""
|
|
|
@@ -345,19 +394,16 @@ def model_list(location):
|
|
|
"""List all models."""
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
_input(["model_list", location])
|
|
|
- return set(_handle_output("Success: ", split=" ").split("\n"))
|
|
|
+ return set(_handle_output("Success: ", split=True))
|
|
|
|
|
|
def model_list_full(location):
|
|
|
"""List full information on all models."""
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
_input(["model_list_full", location])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
|
|
|
lst = set([])
|
|
|
- value = output.strip().split("\n")
|
|
|
- for v in value:
|
|
|
+ for v in output:
|
|
|
m = v.strip()
|
|
|
perm, own, grp, m = m.split(" ", 3)
|
|
|
lst.add((m, own, grp, perm))
|
|
|
@@ -372,23 +418,15 @@ def verify(model_name, metamodel_name=None):
|
|
|
metamodel_name = _get_metamodel(model_name)
|
|
|
|
|
|
_input(["verify", model_name, metamodel_name])
|
|
|
- return _handle_output("Success: ", split=" ")
|
|
|
+ return _handle_output("Success: ", split=True)[0]
|
|
|
|
|
|
-def model_overwrite(model_name, new_model=None, metamodel_name=None):
|
|
|
+def model_overwrite(model_name, new_model="", metamodel_name=None):
|
|
|
"""Upload a new model and overwrite an existing model."""
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
- if new_model is not None:
|
|
|
- try:
|
|
|
- compiled = _compile_model(new_model)
|
|
|
- except Exception as e:
|
|
|
- raise CompilationError(e)
|
|
|
- else:
|
|
|
- compiled = [0]
|
|
|
-
|
|
|
_input("upload")
|
|
|
_handle_output("Waiting for model constructors...")
|
|
|
- _input(compiled)
|
|
|
+ _input(new_model)
|
|
|
_output("Success")
|
|
|
|
|
|
if metamodel_name is not None:
|
|
|
@@ -414,30 +452,19 @@ def model_render(model_name, mapper_name):
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
|
|
|
_input(["model_render", model_name, mapper_name])
|
|
|
- return json.loads(_handle_output("Success: ", split=" "))
|
|
|
+ return json.loads(_handle_output("Success: ", split=True)[0])
|
|
|
|
|
|
def transformation_between(source, target):
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
|
|
|
_input(["transformation_between", source, target])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- return set([v for v in output.split("\n")])
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ return set(output)
|
|
|
|
|
|
def transformation_add_MT(source_metamodels, target_metamodels, operation_name, code, callback=lambda: None):
|
|
|
"""Create a new model transformation."""
|
|
|
global mode
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
- import time
|
|
|
-
|
|
|
- start = time.time()
|
|
|
- try:
|
|
|
- compiled = _compile_model(code)
|
|
|
- except Exception as e:
|
|
|
- raise CompilationError(e)
|
|
|
- #print("Compilation took: %ss" % (time.time() - start))
|
|
|
- start = time.time()
|
|
|
|
|
|
mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
|
|
|
_input(["transformation_add_MT"] + mv_dict_rep + [operation_name])
|
|
|
@@ -449,25 +476,17 @@ def transformation_add_MT(source_metamodels, target_metamodels, operation_name,
|
|
|
callback()
|
|
|
_input("exit")
|
|
|
mode = MODE_MODELLING
|
|
|
- #print("Callbacks took: %ss" % (time.time() - start))
|
|
|
- start = time.time()
|
|
|
|
|
|
# Done, so RAMify and upload the model
|
|
|
_handle_output("Waiting for model constructors...")
|
|
|
- _input(compiled)
|
|
|
+ _input(code)
|
|
|
_handle_output("Success")
|
|
|
- #print("Upload and RAMify took: %ss" % (time.time() - start))
|
|
|
|
|
|
def transformation_add_AL(source_metamodels, target_metamodels, operation_name, code, callback=lambda: None):
|
|
|
"""Create a new action language model, which can be executed."""
|
|
|
global mode
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
|
|
|
- try:
|
|
|
- compiled = _compile_AL(code)
|
|
|
- except Exception as e:
|
|
|
- raise CompilationError(e)
|
|
|
-
|
|
|
mv_dict_rep = _dict_to_list(source_metamodels) + [""] + _dict_to_list(target_metamodels) + [""]
|
|
|
_input(["transformation_add_AL"] + mv_dict_rep + [operation_name])
|
|
|
|
|
|
@@ -480,7 +499,7 @@ def transformation_add_AL(source_metamodels, target_metamodels, operation_name,
|
|
|
mode = MODE_MODELLING
|
|
|
|
|
|
_handle_output("Waiting for code constructors...")
|
|
|
- _input(compiled)
|
|
|
+ _input(code)
|
|
|
_output("Success")
|
|
|
|
|
|
def transformation_add_MANUAL(source_metamodels, target_metamodels, operation_name, callback=lambda: None):
|
|
|
@@ -501,7 +520,7 @@ def transformation_add_MANUAL(source_metamodels, target_metamodels, operation_na
|
|
|
|
|
|
_handle_output("Success")
|
|
|
|
|
|
-def transformation_execute_AL(operation_name, input_models_dict, output_models_dict, callback=lambda i: None):
|
|
|
+def transformation_execute_AL(operation_name, input_models_dict, output_models_dict, statechart=None):
|
|
|
"""Execute an existing model operation."""
|
|
|
global mode
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
@@ -511,19 +530,21 @@ def transformation_execute_AL(operation_name, input_models_dict, output_models_d
|
|
|
_input(["transformation_execute", operation_name] + mv_dict_rep)
|
|
|
_handle_output("Success: ready for AL execution")
|
|
|
|
|
|
- # We are now executing, so everything we get is part of the dialog, except if it is the string for transformation termination
|
|
|
- while _output() not in ["Success", "Failure"]:
|
|
|
+ if statechart is not None:
|
|
|
+ # We are to delegate this information to another statechart, which wants interaction
|
|
|
+ # As such, we continue on a different thread, where we pipe the information, and let this call return immediately
|
|
|
mode = MODE_DIALOG
|
|
|
- reply = callback(_last_output())
|
|
|
+ _exec_on_statechart(statechart)
|
|
|
mode = MODE_MODELLING
|
|
|
- if reply is not None:
|
|
|
- _input(reply)
|
|
|
-
|
|
|
- # Got termination message, so we are done!
|
|
|
- if _last_output() == "Success":
|
|
|
- return True
|
|
|
+ return None
|
|
|
else:
|
|
|
- return False
|
|
|
+ # No statechart associated, so just wait until we are finished
|
|
|
+ while _output() not in ["Success", "Failure"]:
|
|
|
+ pass
|
|
|
+ if _last_output() == "Success":
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return False
|
|
|
|
|
|
def transformation_execute_MANUAL(operation_name, input_models_dict, output_models_dict, callback=lambda i: None):
|
|
|
"""Execute an existing model operation."""
|
|
|
@@ -552,7 +573,7 @@ def transformation_execute_MANUAL(operation_name, input_models_dict, output_mode
|
|
|
else:
|
|
|
return False
|
|
|
|
|
|
-def transformation_execute_MT(operation_name, input_models_dict, output_models_dict, callback=lambda i: None):
|
|
|
+def transformation_execute_MT(operation_name, input_models_dict, output_models_dict, statechart=None):
|
|
|
"""Execute an existing model operation."""
|
|
|
global mode
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
@@ -562,32 +583,31 @@ def transformation_execute_MT(operation_name, input_models_dict, output_models_d
|
|
|
_input(["transformation_execute", operation_name] + mv_dict_rep)
|
|
|
_handle_output("Success: ready for MT execution")
|
|
|
|
|
|
- # We are now executing, so everything we get is part of the dialog, except if it is the string for transformation termination
|
|
|
- while _output() not in ["Success", "Failure"]:
|
|
|
+ if statechart is not None:
|
|
|
+ # We are to delegate this information to another statechart, which wants interaction
|
|
|
+ # As such, we continue on a different thread, where we pipe the information, and let this call return immediately
|
|
|
mode = MODE_DIALOG
|
|
|
- reply = callback(_last_output())
|
|
|
+ _exec_on_statechart(statechart)
|
|
|
mode = MODE_MODELLING
|
|
|
- if reply is not None:
|
|
|
- _input(reply)
|
|
|
-
|
|
|
- # Got termination message, so we are done!
|
|
|
- if _last_output() == "Success":
|
|
|
- return True
|
|
|
+ return None
|
|
|
else:
|
|
|
- return False
|
|
|
+ # No statechart associated, so just wait until we are finished
|
|
|
+ while _output() not in ["Success", "Failure"]:
|
|
|
+ pass
|
|
|
+ if _last_output() == "Success":
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return False
|
|
|
|
|
|
def transformation_list():
|
|
|
"""List existing model operations."""
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
|
|
|
_input("transformation_list")
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
|
|
|
lst = set([])
|
|
|
- value = output.strip().split("\n")
|
|
|
- for v in value:
|
|
|
+ for v in output:
|
|
|
t, m = v.strip().split(" ", 1)
|
|
|
t = t[1:-1].strip()
|
|
|
m = m.strip().split(":")[0].strip()
|
|
|
@@ -709,10 +729,8 @@ def element_list(model_name):
|
|
|
|
|
|
_input("list_full")
|
|
|
lst = set([])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- for v in output.split("\n"):
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ for v in output:
|
|
|
m, mm = v.split(":")
|
|
|
m = m.strip()
|
|
|
mm = mm.strip()
|
|
|
@@ -725,10 +743,8 @@ def types(model_name):
|
|
|
|
|
|
_input("types")
|
|
|
lst = set([])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- for v in output.split("\n"):
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ for v in output:
|
|
|
m, mm = v.split(":")
|
|
|
m = m.strip()
|
|
|
lst.add(m)
|
|
|
@@ -740,10 +756,8 @@ def types_full(model_name):
|
|
|
|
|
|
_input("types")
|
|
|
lst = set([])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- for v in output.split("\n"):
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ for v in output:
|
|
|
m, mm = v.split(":")
|
|
|
m = m.strip()
|
|
|
mm = mm.strip()
|
|
|
@@ -755,8 +769,7 @@ def read(model_name, ID):
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["read", ID])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- v = output.split("\n")
|
|
|
+ v = _handle_output("Success: ", split=True)
|
|
|
t = v[1].split(":")[1].strip()
|
|
|
if (not v[2].startswith("Source:")):
|
|
|
rval = (t, None)
|
|
|
@@ -771,11 +784,10 @@ def read_attrs(model_name, ID):
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["read", ID])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- v = output.split("\n")
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
searching = True
|
|
|
rval = {}
|
|
|
- for r in v:
|
|
|
+ for r in output:
|
|
|
if searching:
|
|
|
if r == "Attributes:":
|
|
|
# Start working on attributes
|
|
|
@@ -804,7 +816,7 @@ def instantiate(model_name, typename, edge=None, ID=""):
|
|
|
_input(["instantiate_node", typename, ID])
|
|
|
else:
|
|
|
_input(["instantiate_edge", typename, ID, edge[0], edge[1]])
|
|
|
- return _handle_output("Success: ", split=" ")
|
|
|
+ return _handle_output("Success: ", split=True)[0]
|
|
|
|
|
|
def delete_element(model_name, ID):
|
|
|
"""Delete the element with the given ID"""
|
|
|
@@ -824,16 +836,12 @@ def attr_assign(model_name, ID, attr, value):
|
|
|
def attr_assign_code(model_name, ID, attr, code):
|
|
|
"""Assign a piece of Action Language code to the attribute"""
|
|
|
_check_type(code)
|
|
|
- try:
|
|
|
- compiled = _compile_AL(code)
|
|
|
- except Exception as e:
|
|
|
- raise CompilationError(e)
|
|
|
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
- _input(["attr_add", ID, attr])
|
|
|
+ _input(["attr_add_code", ID, attr])
|
|
|
_handle_output("Waiting for code constructors...")
|
|
|
- _input(compiled)
|
|
|
+ _input(code)
|
|
|
_output("Success")
|
|
|
|
|
|
def attr_delete(model_name, ID, attr):
|
|
|
@@ -848,45 +856,47 @@ def read_outgoing(model_name, ID, typename):
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["read_outgoing", ID, typename])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- else:
|
|
|
- return set(output.split("\n"))
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ return set(output)
|
|
|
|
|
|
def read_incoming(model_name, ID, typename):
|
|
|
"""Returns a list of all incoming associations of a specific type ("" = all)"""
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["read_incoming", ID, typename])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- else:
|
|
|
- return set(output.split("\n"))
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ return set(output)
|
|
|
|
|
|
def read_association_source(model_name, ID):
|
|
|
"""Returns the source of an association."""
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["read_association_source", ID])
|
|
|
- return _handle_output("Success: ", split=" ")
|
|
|
+ return _handle_output("Success: ", split=True)[0]
|
|
|
|
|
|
def read_association_destination(model_name, ID):
|
|
|
"""Returns the destination of an association."""
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["read_association_destination", ID])
|
|
|
- return _handle_output("Success: ", split=" ")
|
|
|
+ return _handle_output("Success: ", split=True)[0]
|
|
|
|
|
|
##### To document:
|
|
|
|
|
|
def service_register(name, function):
|
|
|
"""Register a function as a service with a specific name."""
|
|
|
|
|
|
- def service_process(port):
|
|
|
+ def service_process(service_task):
|
|
|
+ global address
|
|
|
+ global port
|
|
|
+
|
|
|
+ ctrl = _start_http_client(address, port, 10.0)
|
|
|
+ _listen_to_output(ctrl, service_task)
|
|
|
while 1:
|
|
|
- thrd = threading.Thread(target=function, args=[service_get(port)])
|
|
|
+ client_task = service_get(service_task)
|
|
|
+ ctrl = _start_http_client(address, port, 10.0)
|
|
|
+ _listen_to_output(ctrl, client_task)
|
|
|
+ thrd = threading.Thread(target=function, args=[client_task])
|
|
|
thrd.daemon = True
|
|
|
thrd.start()
|
|
|
|
|
|
@@ -897,10 +907,12 @@ def service_register(name, function):
|
|
|
|
|
|
# Now we are in service-mode
|
|
|
mode = MODE_SERVICE
|
|
|
- port = _handle_output("Success: ", split=" ")
|
|
|
+ task = _handle_output("Success: ", split=True)[0]
|
|
|
|
|
|
# Process events in the background!
|
|
|
- threading.Thread(target=service_process, args=[port]).start()
|
|
|
+ thrd = threading.Thread(target=service_process, args=[task])
|
|
|
+ thrd.daemon = True
|
|
|
+ thrd.start()
|
|
|
|
|
|
def service_stop():
|
|
|
"""Stop the currently executing process."""
|
|
|
@@ -911,18 +923,19 @@ def service_stop():
|
|
|
global mode
|
|
|
mode = MODE_MODELLING
|
|
|
|
|
|
-def service_get(port):
|
|
|
- """Get the values on the specified port."""
|
|
|
+def service_get(task):
|
|
|
+ """Get the values on the specified task."""
|
|
|
_goto_mode(MODE_SERVICE)
|
|
|
|
|
|
- return _output(port=port)
|
|
|
+ val = _output(task=task)
|
|
|
+ return val
|
|
|
|
|
|
-def service_set(port, value):
|
|
|
- """Set a value on a specified port."""
|
|
|
+def service_set(task, value):
|
|
|
+ """Set a value on a specified task."""
|
|
|
_check_type_list(value)
|
|
|
_goto_mode(MODE_SERVICE)
|
|
|
|
|
|
- _input(value, port=port)
|
|
|
+ _input(value, task=task)
|
|
|
|
|
|
def user_password(user, password):
|
|
|
"""Change a user's password."""
|
|
|
@@ -938,7 +951,7 @@ def element_list_nice(model_name):
|
|
|
|
|
|
_input(["element_list_nice", model_name, _get_metamodel(model_name)])
|
|
|
|
|
|
- data = _handle_output("Success: ", split=" ")
|
|
|
+ data = _handle_output("Success: ", split=True)[0]
|
|
|
try:
|
|
|
return json.loads(data)
|
|
|
except:
|
|
|
@@ -950,29 +963,23 @@ def connections_between(model_name, source_element, target_element):
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["connections_between", source_element, target_element])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- else:
|
|
|
- return set(output.split("\n"))
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ return set(output)
|
|
|
|
|
|
def define_attribute(model_name, node, attr_name, attr_type):
|
|
|
"""Create a new attribute, which can be instantiated one meta-level below."""
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["define_attribute", node, attr_name, attr_type])
|
|
|
- return _handle_output("Success: ", split=" ")
|
|
|
+ return _handle_output("Success: ", split=True)[0]
|
|
|
|
|
|
def all_instances(model_name, type_name):
|
|
|
"""Returns a list of all elements of a specific type."""
|
|
|
_goto_mode(MODE_MODIFY, model_name)
|
|
|
|
|
|
_input(["all_instances", type_name])
|
|
|
- output = _handle_output("Success: ", split=" ")
|
|
|
- if output == "":
|
|
|
- return set([])
|
|
|
- else:
|
|
|
- return set(output.split("\n"))
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ return set(output)
|
|
|
|
|
|
def service_poll(port):
|
|
|
"""Checks whether or not the Modelverse side has any input ready to be processed."""
|
|
|
@@ -996,6 +1003,20 @@ def add_conformance(model_name, metamodel_name, partial_type_mapping=None):
|
|
|
_handle_output("Success")
|
|
|
|
|
|
def folder_create(folder_name):
|
|
|
+ """Create a new folder."""
|
|
|
_goto_mode(MODE_MODELLING)
|
|
|
_input(["folder_create", folder_name])
|
|
|
_handle_output("Success")
|
|
|
+
|
|
|
+def model_types(model_name):
|
|
|
+ """Fetch all typings defined for this specific model."""
|
|
|
+ _goto_mode(MODE_MODELLING)
|
|
|
+ _input(["model_types", model_name])
|
|
|
+ output = _handle_output("Success: ", split=True)
|
|
|
+ return set(output)
|
|
|
+
|
|
|
+import atexit
|
|
|
+def _close_model():
|
|
|
+ if mode == MODE_MODIFY:
|
|
|
+ _model_exit()
|
|
|
+atexit.register(_close_model)
|