瀏覽代碼

current version

Simon Van Mierlo 9 年之前
父節點
當前提交
f73d12e9fc

+ 3 - 3
integration/code/pdevs_client.alc

@@ -14,19 +14,19 @@ Boolean function main(model : Element):
     log("(PDEVS) task name = " + get_taskname())
     log("(PDEVS) Sending model...")
     comm_set(port, read_attribute(model, set_pop(allInstances(model, "MyString/MyString")), "value"))
-    log(port)
+    log("(PDEVS) " + port)
     
     while (True):
         if (comm_hasInput(port)):
             log(cast_v2s(has_input()))
             the_input = comm_get(port)
             log("(PDEVS) Got input from simulator!")
-            log(the_input)
+            log("(PDEVS) " + the_input)
             output(the_input)
         if (has_input()):
             the_input = input()
             log("(PDEVS) Got input from console/ps simulator!")
-            log(cast_v2s(the_input))
+            log("(PDEVS) " + cast_v2s(the_input))
             if (is_physical_string(the_input)):
                 comm_set(port, the_input)
             else:

+ 111 - 9
integration/code/ps_simulator.alc

@@ -3,29 +3,131 @@ include "services.alh"
 include "modelling.alh"
 include "object_operations.alh"
 include "io.alh"
+include "json.alh"
 
 Boolean function main(model : Element):
 	String pdevs_client_taskname
     String the_output
     String the_input
     String devs_model
+    String simulation_state
+    String command
+    Element input_for_devs_sim
+    Element params
+    Element parsed_value
+    Element curr_output
+    Element constructed_output
+    Element to_output
+    Element new_items
     
     pdevs_client_taskname = input()
     log("(PS) Got task name from console!")
-    log(pdevs_client_taskname)
+    log("(PS) " + pdevs_client_taskname)
+    
+    simulation_state = "paused"
     
     while (True):
         if (has_input()):
             the_input = input()
             log("(PS) Got input from console!")
-            log(cast_v2s(the_input))
-            if (is_physical_string(the_input)):
-                give_input_to_other(pdevs_client_taskname, the_input)
-            else:
-                give_input_to_other(pdevs_client_taskname, cast_v2s(the_input))
+            log("(PS) " + cast_v2s(the_input))
+            if (bool_not(is_physical_string(the_input))):
+                the_input = cast_v2s(the_input)
+            parsed_value = json_deserialize(the_input)
+            log("(PS) Parsed input!")
+            log("(PS) " + list_to_string(parsed_value))
+            // TODO: look at simulation state: figure out what to send to simulator (either nothing or big_step). Also save the end condition probably.
+            command = list_read(parsed_value, 0)
+            if (command == "big_step"):
+                if (simulation_state == "paused"):
+                    simulation_state = "big_step"
+                    input_for_devs_sim = list_create()
+                    list_append(input_for_devs_sim, "big_step")
+                    list_append(input_for_devs_sim, list_read(parsed_value, 1))
+            give_input_to_other(pdevs_client_taskname, json_serialize(input_for_devs_sim))
         if (other_has_output(pdevs_client_taskname)):
             the_output = get_output_from_other(pdevs_client_taskname)
             log("(PS) Got output from pdevs simulator!")
-            log(the_output)
-            output(the_output)
-        sleep(0.05)
+            log("(PS) " + the_output)
+            parsed_value = json_deserialize(the_output)
+            log("(PS) Parsed output!")
+            log("(PS) " + dict_to_string(parsed_value))
+            if (cast_v2s(parsed_value['name']) == 'new_states'):
+                constructed_output = list_create()
+                curr_output = detect_convbelt_new_item(parsed_value, model)
+                if (read_nr_out(dict_keys(curr_output)) > 0):
+                    new_items = dict_create()
+                    dict_add_fast(new_items, "new_items", curr_output)
+                    list_append(constructed_output, new_items)
+            if (list_len(constructed_output) > 0):
+                to_output = dict_create()
+                dict_add_fast(to_output, 'name', 'state_update')
+                dict_add_fast(to_output, 'parameters', constructed_output)
+                output(json_serialize(to_output))
+            else:
+                // TODO temporary
+                output(the_output)
+        sleep(0.05)
+        
+Element function detect_convbelt_new_item(state_update: Element, model: Element):
+    Element params
+    Element curr_param
+    Element keys
+    Element curr_key
+    Element devs_instances
+    Element curr_devs_instance
+    Element generators
+    Element curr_convbelt
+    Element splitted_key
+    Element connected_items
+    Element curr_item
+    Element curr_value
+    String curr_id
+    Element to_output
+    Element temp_dict
+    Boolean item_found
+    String devs_name
+        
+    params = state_update['parameters']
+    to_output = dict_create()
+    while (list_len(params) > 0):
+        curr_param = list_pop(params, 0)
+        keys = dict_keys(curr_param)
+        while (read_nr_out(keys) > 0):
+            curr_key = set_pop(keys)
+            curr_value = curr_param[curr_key]
+            if (dict_in(curr_value, 'product')):
+                devs_instances = allInstances(model, 'ParallelDEVS/DEVSInstance')
+                while (read_nr_out(devs_instances) > 0):
+                    curr_devs_instance = set_pop(devs_instances)
+                    splitted_key = string_split(curr_key, ".")
+                    if (list_read(splitted_key, list_len(splitted_key) - 1) == read_attribute(model, curr_devs_instance, "name")):
+                        break!
+                    log("(PS) We shouldn't reach this point...")
+                curr_convbelt = set_pop(allAssociationOrigins(model, curr_devs_instance, "PS2DEVS_instancelink"))
+                if (read_type(model, curr_convbelt) == "ProductionSystemRuntime/ConveyorBelt"):
+                    connected_items = allAssociationDestinations(model, curr_convbelt, "ProductionSystemRuntime/Conv_To_I")
+                    curr_id = cast_i2s(curr_value['product']['id'])
+                    item_found = False
+                    while (read_nr_out(connected_items) > 0):
+                        if (cast_v2s(set_pop(connected_items)) == curr_id):
+                            item_found = True
+                            log("(PS) Item " + curr_id + " found...")
+                            break!
+                    if (bool_not(item_found)):
+                        log("(PS) Item not found, creating...")
+                        instantiate_node(model, ("ProductionSystemRuntime/" + cast_i2s(curr_value['product']['type'])), curr_id)
+                        instantiate_attribute(model, curr_id, "broken", curr_value['product']['broken'])
+                        instantiate_link(model, "ProductionSystemRuntime/Conv_To_I", "", curr_convbelt, curr_id)
+                        temp_dict = dict_create()
+                        dict_add_fast(temp_dict, 'type', ("ProductionSystemRuntime/" + cast_i2s(curr_value['product']['type'])))
+                        dict_add_fast(temp_dict, 'name', curr_id)
+                        dict_add_fast(temp_dict, 'broken', curr_value['product']['broken'])
+                        dict_add_fast(to_output, curr_convbelt, temp_dict)
+    return to_output!
+        
+Boolean function is_big_step_done(state_update: Element):
+    return True!
+    
+Boolean function is_small_step_done(state_update: Element):
+    return True!

+ 20 - 17
interface/PDEVS/console.py

@@ -13,7 +13,8 @@ def register_service():
 
     print 'Registering service...'
 
-    init('msdl.uantwerpen.be:8001')
+    #init('msdl.uantwerpen.be:8001')
+    init()
     login("pypdevs_service", "my_password")
 
     def pypdevs_service(port):
@@ -61,7 +62,8 @@ def register_service():
         service_stop()
 
 def start_pypdevs_client(conn):
-    init('msdl.uantwerpen.be:8001')
+    #init('msdl.uantwerpen.be:8001')
+    init()
     login("admin", "admin")
 
     conn.send(get_taskname())
@@ -91,7 +93,8 @@ if __name__ == '__main__':
     p.start()
     
     print("Init")
-    init("msdl.uantwerpen.be:8001")
+    #init('msdl.uantwerpen.be:8001')
+    init()
     print("Login")
     login("admin", "admin")
 
@@ -108,6 +111,20 @@ if __name__ == '__main__':
             model_add("formalisms/ProductionSystem", "formalisms/SimpleClassDiagrams", open("../../models/production_system_design.mvc").read())
         except ModelExists:
             pass
+        
+        # Remove the runtime metamodel for ProductionSystem
+        print("Remove ProductionSystem runtime metamodel")
+        try:
+            model_delete("formalisms/ProductionSystemRuntime")
+        except ModelverseException:
+            pass
+            
+        # Add the runtime metamodel for ProductionSystem
+        print("Add ProductionSystem runtime metamodel")
+        try:
+            model_add("formalisms/ProductionSystemRuntime", "formalisms/SimpleClassDiagrams", open("../../models/production_system_runtime.mvc").read())
+        except ModelExists:
+            pass
             
         print("Remove model")
         try:
@@ -179,20 +196,6 @@ if __name__ == '__main__':
             
         print("Execute DEVS->String transformation")
         transformation_execute_AL("models/paralleldevs_to_string", {"ParallelDEVS": "models/example_PS_DEVS"}, {"MyString": "models/example_PS_DEVS_String"})
-        
-        # Remove the runtime metamodel for ProductionSystem
-        print("Remove ProductionSystem runtime metamodel")
-        try:
-            model_delete("formalisms/ProductionSystemRuntime")
-        except ModelverseException:
-            pass
-            
-        # Add the runtime metamodel for ProductionSystem
-        print("Add ProductionSystem runtime metamodel")
-        try:
-            model_add("formalisms/ProductionSystemRuntime", "formalisms/SimpleClassDiagrams", open("../../models/production_system_runtime.mvc").read())
-        except ModelExists:
-            pass
             
         print("Remove model")
         try:

+ 4 - 1
models/production_system_runtime.mvc

@@ -1,5 +1,6 @@
 SimpleAttribute String {}
 SimpleAttribute Integer {}
+SimpleAttribute Boolean {}
 
 Class Machine {}
 Class Processor: Machine {}
@@ -15,7 +16,9 @@ Class ConveyorBelt {
     capacity: Integer
 }
 Class Operator {}
-Class Item {}
+Class Item {
+    broken: Boolean
+}
 Class Part: Item {}
 Class Wheels: Part {}
 Class Tracks: Part {}

+ 1 - 1
scripts/HUTN_service.py

@@ -40,7 +40,7 @@ def compile_service(port):
             f.flush()
 
         compiled = do_compile(temp_file, COMPILER_PATH + "/grammars/actionlanguage.g", "CS")
-        os.remove(temp_file)
+        # os.remove(temp_file)
         return compiled
 
     def compile_model(code):