فهرست منبع

Added a script to do the sending to the Mv in a generic way: all other
tools should now just return lists to comply with this format!

Yentl Van Tendeloo 9 سال پیش
والد
کامیت
924ef92df0
2فایلهای تغییر یافته به همراه48 افزوده شده و 3 حذف شده
  1. 23 3
      model/model.py
  2. 25 0
      scripts/process.py

+ 23 - 3
model/model.py

@@ -9,6 +9,13 @@ from pypdevs.simulator import Simulator
 
 import json
 
+def get_object_constructor(code):
+    with open(".code.alc", "w") as f:
+        f.write(code)
+        f.flush()
+    constructors = do_compile(".code.alc", "interface/HUTN/grammars/actionlanguage.g", "CD")
+    return constructors
+
 def translate(operation):
     return {
             "CN": "create_node",
@@ -134,6 +141,7 @@ class MvKState(object):
         self.current_user = None
         self.loaded_primitives = False
         self.execution_counter = 0
+        self.all_failed = True
 
     def __str__(self):
         return "\nMvK: %s\n" % self.mvk + \
@@ -273,6 +281,9 @@ class ModelverseKernel(AtomicDEVS):
         return {}
 
     def timeAdvance(self):
+        if self.state.phase == "init_user" and self.state.all_failed:
+            # Make this a parameter
+            return 200
         if self.state.commands is not None:
             return 0
         elif self.state.waiting:
@@ -470,9 +481,18 @@ class System(CoupledDEVS):
         self.connectPorts(self.mvk.to_mvi, self.mvk2mvi.input_port)
         self.connectPorts(self.mvk2mvi.output_port, self.mvi.from_mvk)
 
-operations = [
-        "test",
-    ]
+code = \
+    """
+include "primitives.alh"
+
+Element function main():
+\tlog("Executed the code!")
+\treturn
+    """
+
+operations = [get_object_constructor(f.read()) for f in files] + \
+              get_object_constructor(code) + \
+              get_link_and_load("main", files + ["code.alc"])
 
 args = {
         "username":             "test_user",

+ 25 - 0
scripts/process.py

@@ -0,0 +1,25 @@
+def flush_data(address, data):
+    if data:
+        urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "data": json.dumps(data), "username": username})), timeout=10).read()
+    return []
+
+def fetch_output(address):
+    val = urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "get_output", "username": username})), timeout=10).read()
+    l, r = output.split("&", 1)
+    if "value" in l:
+        output = r
+    else:
+        output = l
+    _, output = output.split("=", 1)
+    return output
+
+def send_to_mv(address, lst):
+    data = []
+    variables = {}
+
+    for c in lst:
+        if isinstance(c, int):
+            data = flush_data(data)
+            variables[c] = fetch_output(address)
+        else:
+            data.append(c)