浏览代码

Merge branch 'master' into testing

Yentl Van Tendeloo 7 年之前
父节点
当前提交
cd0f93982c

+ 1 - 0
bootstrap/bootstrap.py

@@ -71,6 +71,7 @@ def bootstrap():
                     "is_physical_string": ["Boolean", "Element"],
                     "is_physical_action": ["Boolean", "Element"],
                     "is_physical_float": ["Boolean", "Element"],
+                    "is_physical_none": ["Boolean", "Element"],
                     "create_node": ["Element"],
                     "create_edge": ["Element", "Element", "Element"],
                     "create_value": ["Element", "Element"],

+ 6 - 1
bootstrap/json.alc

@@ -14,7 +14,10 @@ Void function json_send_data(port : String, data : Element):
 	// Send the current value to the service
 	// First determine what it is!
 
-	if (has_value(data)):
+	if (is_physical_none(data)):
+		comm_set(port, "N")
+		
+	elif (has_value(data)):
 		// Primitive value, so send as-is
 		comm_set(port, "P")
 		comm_set(port, data)
@@ -59,6 +62,8 @@ Element function process_JSON_data(port : String):
 	if (type == "P"):
 		// Primitive data type, so just return as-is
 		result = comm_get(port)
+	elif (type == "N"):
+		result = !none
 	elif (type == "L"):
 		// List, so fetch elements in turn
 		Integer length

+ 2 - 1
bootstrap/primitives.alc

@@ -64,7 +64,8 @@ Boolean function is_physical_float(a: Element) = ?primitives/is_physical_float
 Boolean function is_physical_string(a: Element) = ?primitives/is_physical_string
 Boolean function is_physical_boolean(a: Element) = ?primitives/is_physical_boolean
 Boolean function is_physical_action(a: Element) = ?primitives/is_physical_action
+Boolean function is_physical_none(a: Element) = ?primitives/is_physical_none
 Float function time() = ?primitives/time
 String function hash(a : String) = ?primitives/hash
 Float function __sleep(a : Float, b : Boolean) = ?primitives/__sleep
-Boolean function is_error() = ?primitives/is_error
+Boolean function is_error(a : Element) = ?primitives/is_error

+ 1 - 1
bootstrap/semi_primitives.alc

@@ -129,7 +129,7 @@ Boolean function string_startswith(a: String, b: String):
 	return True!
 
 Boolean function has_value(a: Element):
-	return bool_or(bool_or(bool_or(is_physical_action, is_physical_int(a)), is_physical_float(a)), bool_or(is_physical_string(a), is_physical_boolean(a)))!
+	return bool_or(bool_or(bool_or(is_physical_action, is_physical_int(a)), bool_or(is_physical_none(a), is_physical_float(a))), bool_or(is_physical_string(a), is_physical_boolean(a)))!
 
 Boolean function float_gte(a: Float, b: Float):
 	return bool_or(float_gt(a, b), value_eq(a, b))!

+ 2 - 1
interface/HUTN/grammars/actionlanguage.g

@@ -86,7 +86,7 @@ grammar{
 
     type_specifier: INT | FLOAT | BOOL | STRING | TYPE | ACTION | ELEMENT;
 
-    actionname: IF_NODE | WHILE_NODE | ASSIGN_NODE | CALL_NODE | BREAK_NODE | CONTINUE_NODE | RETURN_NODE | RESOLVE_NODE | ACCESS_NODE | CONSTANT_NODE | GLOBAL_NODE | DECLARE_NODE | INPUT_NODE | OUTPUT_NODE;
+    actionname: IF_NODE | WHILE_NODE | ASSIGN_NODE | CALL_NODE | BREAK_NODE | CONTINUE_NODE | RETURN_NODE | RESOLVE_NODE | ACCESS_NODE | CONSTANT_NODE | GLOBAL_NODE | DECLARE_NODE | INPUT_NODE | OUTPUT_NODE | NONE_NODE;
 
     string: (STRVALUE|LONG_STRVALUE);
 
@@ -147,6 +147,7 @@ grammar{
         OUTPUT_NODE: '!output';
         GLOBAL_NODE: '!global';
         DECLARE_NODE: '!declare';
+        NONE_NODE: '!none';
 
         FUNCTION: 'function';
         RETURN: 'return';

+ 1 - 0
interface/HUTN/includes/primitives.alh

@@ -93,6 +93,7 @@ Boolean function is_physical_float(a : Element)
 Boolean function is_physical_string(a : Element)
 Boolean function is_physical_action(a : Element)
 Boolean function is_physical_boolean(a : Element)
+Boolean function is_physical_none(a : Element)
 Boolean function has_value(a : Element)
 Float function time()
 String function hash(a : String)

+ 5 - 0
kernel/modelverse_kernel/primitives.py

@@ -327,6 +327,11 @@ def is_physical_action(a, **remainder):
     result, = yield [("CNV", [isinstance(t, dict) and t["value"] in ["if", "while", "assign", "call", "break", "continue", "return", "resolve", "access", "constant", "global", "declare"]])]
     raise PrimitiveFinished(result)
 
+def is_physical_none(a, **remainder):
+    t, = yield [("RV", [a])]
+    result, = yield [("CNV", [isinstance(t, dict) and t["value"] == "none"])]
+    raise PrimitiveFinished(result)
+
 def create_node(**remainder):
     result, = yield [("CN", [])]
     raise PrimitiveFinished(result)

+ 91 - 0
scripts/JSON_service.py

@@ -0,0 +1,91 @@
+import sys
+sys.path.append("wrappers")
+from modelverse import *
+import os
+import json
+
+import time
+time.sleep(1)
+init(sys.argv[1])
+login("JSON", "JSON")
+
+def json_service(port):
+    def print_out_json(data):
+        if isinstance(data, dict):
+            # Serialize dictionary
+            service_set(port, "D")
+            service_set(port, len(data))
+            for key, value in data.items():
+                service_set(port, key)
+                print_out_json(value)
+        elif isinstance(data, list):
+            # Serialize list
+            service_set(port, "L")
+            service_set(port, len(data))
+            for value in data:
+                print_out_json(value)
+        elif data is None:
+            service_set(port, "N")
+        else:
+            # Is a primitive value (normally), so send as-is
+            service_set(port, "P")
+            service_set(port, data)
+
+    def fetch_data():
+        data = service_get(port)
+        if data == "D":
+            rval = {}
+            length = service_get(port)
+            for _ in range(length):
+                key = service_get(port)
+                rval[key] = fetch_data()
+        elif data == "L":
+            rval = []
+            length = service_get(port)
+            for _ in range(length):
+                rval.append(fetch_data())
+        elif data == "P":
+            rval = service_get(port)
+        elif data == "N":
+            rval = None
+        else:
+            raise Exception("Unknown data type: " + data)
+        return rval
+
+    start = time.time()
+
+    mode = service_get(port)
+    try:
+        if mode == "decode":
+            service_set(port, "OK")
+            json_str = service_get(port)
+            json_data = json.loads(json_str)
+            print_out_json(json_data)
+            print("Decoded %s" % json_str)
+
+        elif mode == "encode":
+            service_set(port, "OK")
+            json_data = fetch_data()
+            json_str = json.dumps(json_data)
+            service_set(port, json_str)
+            print("Encoded %s" % json_str)
+            
+        else:
+            raise Exception("No such mode: " + mode)
+
+    except Exception as e:
+        service_set(port, str(e))
+        raise
+    print("JSON took %ss" % (time.time() - start))
+
+service_register("JSON", json_service)
+
+try:
+    while raw_input() != "STOP":
+        pass
+except EOFError:
+    import time
+    while 1:
+        time.sleep(1.0)
+finally:
+    service_stop()

+ 1 - 1
state/modelverse_state/main.py

@@ -13,7 +13,7 @@ if sys.version > '3': # pragma: no cover
 else: # pragma: no cover
     integer_types = (int, long)
     primitive_types = (int, long, float, str, bool, unicode)
-complex_primitives = frozenset(["if", "while", "assign", "call", "break", "continue", "return","resolve","access", "constant", "input", "output", "declare", "global"])
+complex_primitives = frozenset(["if", "while", "assign", "call", "break", "continue", "return","resolve","access", "constant", "input", "output", "declare", "global", "none"])
 
 def instance_to_string(value):
     return value["value"]