Преглед изворни кода

Make sure that users are removed when they are no longer needed

Yentl Van Tendeloo пре 7 година
родитељ
комит
6cea091d72
1 измењених фајлова са 17 додато и 27 уклоњено
  1. 17 27
      model/model.py

+ 17 - 27
model/model.py

@@ -11,7 +11,7 @@ from hutn_compiler.compiler import main as do_compile
 
 from pypdevs.minimal import AtomicDEVS, CoupledDEVS, Simulator
 
-PROFILE = False
+PROFILE = True
 
 import json
 import random
@@ -222,7 +222,6 @@ class ModelverseKernel(AtomicDEVS):
 
         if self.from_mvs in inputs:
             # Got input from MvS, so we can continue processing
-            #print(" --> " + str(inputs[self.from_mvs]))
             for mvs_input in inputs[self.from_mvs]:
                 if self.state.mvk is None:
                     # No MvK, so set it with the root we have just received (or should have received)
@@ -273,7 +272,6 @@ class ModelverseKernel(AtomicDEVS):
                     commands = [("RV", [self.state.tasks[0]])]
                 else:
                     self.state.current_task = self.state.reply[0]
-                    #print("Processing task %s at time %s" % (self.state.current_task, self.time_last))
                     self.state.start_task_time = self.state.current_time
                     if self.state.current_task.startswith("__"):
                         # Don't process this task and force termination of task
@@ -285,6 +283,7 @@ class ModelverseKernel(AtomicDEVS):
                     value = self.state.inputs[self.state.current_task][0]
                     start = time.time()
                     commands = self.state.mvk.execute_yields(self.state.current_task, "set_input", [value], self.state.reply)
+                    #print("EXECUTEYIELDS %s %.17f" % (self.state.current_task, time.time() - start))
                     if PROFILE:
                         print("rule_generation: %.17f" % ((time.time() - start)))
                     #self.state.rule_generation = time.time() - start
@@ -298,6 +297,7 @@ class ModelverseKernel(AtomicDEVS):
                 try:
                     start = time.time()
                     commands = self.state.mvk.execute_yields(self.state.current_task, "execute_rule", [], self.state.reply)
+                    #print("EXECUTEYIELDS %s %.17f" % (self.state.current_task, time.time() - start))
                     if PROFILE:
                         print("rule_generation: %.17f" % ((time.time() - start)))
                     #self.state.rule_generation = time.time() - start
@@ -309,6 +309,7 @@ class ModelverseKernel(AtomicDEVS):
             elif self.state.phase == "output":
                 start = time.time()
                 commands = self.state.mvk.execute_yields(self.state.current_task, "get_output", [], self.state.reply)
+                #print("EXECUTEYIELDS %s %.17f" % (self.state.current_task, time.time() - start))
                 if PROFILE:
                     print("rule_generation: %.17f" % ((time.time() - start)))
                 #self.state.rule_generation = time.time() - start
@@ -324,7 +325,7 @@ class ModelverseKernel(AtomicDEVS):
                 elif self.state.phase == "input":
                     self.state.phase = "computation"
                 elif self.state.phase == "computation":
-                    if not self.state.mvk.success or (self.state.current_time - self.state.start_task_time > self.time_per_phase):
+                    if (not self.state.mvk.success) or (self.state.current_time - self.state.start_task_time > self.time_per_phase):
                         self.state.phase = "output"
                 elif self.state.phase == "output":
                     self.state.tasks.pop(0)
@@ -336,7 +337,6 @@ class ModelverseKernel(AtomicDEVS):
 
             # Send the commands to the MvS
             self.state.commands = commands
-            #print(" <-- " + str(commands))
         return self.state
 
     def outputFnc(self):
@@ -404,7 +404,7 @@ class ModelverseInterface(AtomicDEVS):
 
     def extTransition(self, inputs):
         for inp in inputs[self.from_mvk]:
-            print("Got input: " + str(inp))
+            #print(inp)
             self.state.blocked = False
 
             self.state.output.setdefault(inp[0], []).append(inp[1])
@@ -412,63 +412,53 @@ class ModelverseInterface(AtomicDEVS):
                 self.state.finished = True
             elif inp[0] == self.taskname and self.state.operations[0] is None:
                 # We have to block for now, and modify a model first...
-                print("Blocking for now...")
                 prev_output = self.state.output[inp[0]][-1]
                 if prev_output.startswith("Please edit this model before sending next input: "):
                     _, model_name = prev_output.split("Please edit this model before sending next input: ", 1)
                     new_taskname = str(uuid.uuid4())
-                    self.state.send_operations[new_taskname] = [[], ["admin"], ["admin"], ["quiet"], ["model_modify", model_name, ""]] + self.state.additional_operations[0] + [["exit"]]
-                    print("Modifying model " + str(model_name))
-                    print("On taskname " + str(new_taskname))
+                    self.state.send_operations[new_taskname] = [[], ["admin"], ["admin"], ["quiet"], ["model_modify", model_name, ""]] + self.state.additional_operations[0] + [["exit"], ["exit"]]
                     self.state.create_additional_task.append(new_taskname)
                     self.state.task_to_operation[new_taskname] = None
                     self.state.task_to_spawner[new_taskname] = None
                 elif prev_output.startswith("Spawned activity on task: "):
-                    print("Spawned activity")
                     _, task_name = prev_output.split("Spawned activity on task: ", 1)
                     self.state.blocked = True
                     self.state.task_to_spawner[task_name] = None
                     # NOTE We now know that there is something to do on an other task, so we just wait for that event to come in
-                    print("Adding 3 empty lists")
                     self.state.operations.insert(0, None)
                     self.state.operations.insert(0, None)
                 elif prev_output.startswith("Finished task: "):
                     self.state.blocked = True
                 elif " : " in prev_output:
                     task_name, _ = prev_output.split(" : ", 1)
-                    print("Got new task to monitor: " + task_name)
-                    print("Adding 3 empty lists")
                     self.state.blocked = True
                     self.state.task_to_spawner[task_name] = None
                     # NOTE We now know that there is something to do on an other task, so we just wait for that event to come in
                     self.state.operations.insert(0, None)
                     self.state.operations.insert(0, None)
                 elif prev_output == "Success":
-                    print("Remaining operations: " + str(self.state.operations))
                     self.state.operations.pop(0)
                     self.state.blocked = False
-                else:
-                    print("UNKNOWN: " + str(prev_output))
 
             elif inp[0] != self.taskname:
                 # Got some output on another task
                 # If the task is not registered yet, it is likely not important to communicate with it, so ignore it
+                #print("Send operations: " + str(self.state.send_operations))
                 if inp[0] in self.state.send_operations:
                     self.state.send_operations[inp[0]].pop(0)
 
                 if inp[1].startswith("Please edit this model before sending next input: "):
                     _, model_name = inp[1].split("Please edit this model before sending next input: ", 1)
                     new_taskname = str(uuid.uuid4())
-                    self.state.send_operations[new_taskname] = [[], ["admin"], ["admin"], ["quiet"], ["model_modify", model_name, ""]] + self.state.keyed_operations.get(self.state.task_to_operation[inp[0]], []) + [["exit"]]
-                    print("Modifying model " + str(model_name))
-                    print("On taskname " + str(new_taskname))
+                    self.state.send_operations[new_taskname] = [[], ["admin"], ["admin"], ["quiet"], ["model_modify", model_name, ""]] + self.state.keyed_operations.get(self.state.task_to_operation[inp[0]], []) + [["exit"], ["exit"]]
+                    #print("DO EXIT2")
                     self.state.create_additional_task.append(new_taskname)
                     self.state.task_to_spawner[new_taskname] = inp[0]
                 elif inp[1].startswith("Please perform manual operation "):
                     _, op_name = inp[1].split("Please perform manual operation ", 1)
                     self.state.task_to_operation[inp[0]] = op_name[1:-1]
 
-                if inp[0] in self.state.send_operations and not self.state.send_operations[inp[0]]:
+                if inp[0] in self.state.send_operations and len(self.state.send_operations[inp[0]]) == 0:
                     del self.state.send_operations[inp[0]]
 
                     # At the end of these operations, so finish up!
@@ -480,7 +470,6 @@ class ModelverseInterface(AtomicDEVS):
                         else:
                             self.state.additional_operations.pop(0)
                             self.state.operations.pop(0)
-                            print("Clear up to the normal task!")
 
         return self.state
 
@@ -492,13 +481,13 @@ class ModelverseInterface(AtomicDEVS):
             for k, v in self.state.send_operations.items():
                 if v and v[0] is not None:
                     outp.append((k, v[0]))
-            print("REQUEST special: " + str(outp))
             if outp:
+                #print("SEND " + str(outp))
                 return {self.to_mvk: outp}
             else:
                 return {}
         elif self.state.operations and self.state.operations[0] is not None:
-            print("REQUEST: " + str(self.state.operations[0]))
+            #print("SEND " + str([(self.taskname, self.state.operations[0])]))
             return {self.to_mvk: [(self.taskname, self.state.operations[0])]}
         else:
             return {}
@@ -548,7 +537,6 @@ class Network(AtomicDEVS):
         return {self.output_port: [self.state.processing[0]]}
 
     def timeAdvance(self):
-        #print("Timer: " + str(self.state.timer))
         return self.state.timer
 
 class System(CoupledDEVS):
@@ -656,7 +644,7 @@ class System(CoupledDEVS):
 taskname = "test_task"
 
 operations = json.loads(open("model/operations", 'r').read())
-#operations = json.loads(open("model/operations_simple", 'r').read())
+operations = json.loads(open("model/operations_simple", 'r').read())
 additional_operations = [[], # revise_req
                          [], # revise_environment
                          [], # revise_plant
@@ -731,6 +719,8 @@ model = System(**args)
 sim = Simulator(model)
 sim.setTerminationCondition(lambda t, m: m.mvi.state.finished)
 #sim.setVerbose()
+start = time.time()
 tn = sim.simulate()
 
-print("Simulation finished at time %s" % tn)
+print("Simulation time: %s" % tn)
+print("Execution time: %s" % (time.time() - start))