소스 검색

Updated FSA semantics

Yentl Van Tendeloo 8 년 전
부모
커밋
4684b20d72
1개의 변경된 파일54개의 추가작업 그리고 77개의 파일을 삭제
  1. 54 77
      integration/code/fsa_semantics.alc

+ 54 - 77
integration/code/fsa_semantics.alc

@@ -19,7 +19,6 @@ Element function retype_to_runtime(design_model : Element):
 	String src
 	String dst
 	String time
-	Element all_attributes
 
 	runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
 
@@ -45,94 +44,61 @@ Element function retype_to_runtime(design_model : Element):
 
 	return runtime_model!
 
-Element function sanitize(new_runtime_model : Element, old_runtime_model : Element):
-	Element all_blocks
-	Element all_links
-	String element_name
-	String attr_name
-	String attr_value
-	String attribute
-	String time
-	Element all_attributes
-	Float current_time
-
-	all_blocks = allInstances(new_runtime_model, "Block")
-	while (list_len(all_blocks) > 0):
-		element_name = set_pop(all_blocks)
-		if (dict_in(old_runtime_model["model"], element_name)):
-			if (is_nominal_instance(new_runtime_model, element_name, "ICBlock")):
-				instantiate_attribute(new_runtime_model, element_name, "last_in", read_attribute(old_runtime_model, element_name, "last_in"))
-			if (is_nominal_instance(new_runtime_model, element_name, "IntegratorBlock")):
-				instantiate_attribute(new_runtime_model, element_name, "last_out", read_attribute(old_runtime_model, element_name, "last_out"))
-			instantiate_attribute(new_runtime_model, element_name, "signal", read_attribute(old_runtime_model, element_name, "signal"))
+Element function sanitize(new_runtime_model : Element, old_runtime_model : Element, auto : Boolean):
+	String cstate
+	String cstate_obj
+
+	cstate_obj = instantiate_node(new_runtime_model, "CurrentState", "")
+	cstate = readAssociationDestination(old_runtime_model, set_pop(allInstances(old_runtime_model, "CurrentStateLink")))
+	if (bool_not(dict_in(new_runtime_model["model"], cstate))):
+		// Current state removed, so fix
+		if (auto):
+			cstate = set_pop(allInstances(new_runtime_model, "InitialState"))
 		else:
-			instantiate_attribute(new_runtime_model, element_name, "signal", 0.0)
-
-	if (dict_in(old_runtime_model["model"], "time")):
-		current_time = read_attribute(old_runtime_model, "time", "current_time")
-	else:
-		current_time = 0
-
-	time = instantiate_node(new_runtime_model, "Time", "time")
-	instantiate_attribute(new_runtime_model, time, "start_time", current_time)
-	instantiate_attribute(new_runtime_model, time, "current_time", current_time)
+			while (bool_not(dict_in(new_runtime_model["model"], cstate))):
+				output("REQUEST_CURRENT_STATE")
+				cstate = input()
+	
+	instantiate_link(new_runtime_model, "CurrentStateLink", "", cstate_obj, cstate)
 
 	return new_runtime_model!
 
-Void function dict_overwrite(d : Element, key : Element, value : Element):
-	if (dict_in(d, key)):
-		dict_delete(d, key)
-	if (dict_in_node(d, key)):
-		dict_delete_node(d, key)
-	dict_add(d, key, value)
-
-	return !
-
-Integer function min(a : Integer, b : Integer):
-	if (a < b):
-		return a!
-	else:
-		return b!
-
-Element function list_pop(list : Element):
-	Integer top
-	Element t
-	top = list_len(list) - 1
-	t = list_read(list, top)
-	list_delete(list, top)
-	return t!
-
-String function readType(model : Element, name : String):
-	return reverseKeyLookup(model["metamodel"]["model"], dict_read_node(model["type_mapping"], model["model"][name]))!
-
-Integer function list_index_of(lst : Element, elem : Element):
-	Integer i
-	i = 0
-	while (i < read_nr_out(lst)):
-		if (value_eq(list_read(lst, i), elem)):
-			return i!
-		else:
-			i = i + 1
-	return -1!
-
 Void function do_transition(model : Element, start_time : Float, event : String):
 	// Read out current state
 	String cstate_link
+	String cstate_obj
 	String cstate
+	String transition
+	String new_state
+
 	cstate_link = set_pop(allInstances(model, "CurrentStateLink"))
 	cstate = readAssociationDestination(model, cstate_link)
+	cstate_obj = readAssociationSource(model, cstate_link)
 
 	// Read out all outgoing transitions
 	Element all_transitions
 	all_transitions = allOutgoingAssociationInstances(model, cstate, "Transition")
 
+	output("SIMTIME " + cast_v2s(time() - start_time))
+	output("EVENT " + cast_v2s(event))
+
 	while (read_nr_out(all_transitions) > 0):
-		if (value_eq(read_attribute(model, cstate, "event"), event)):
+		transition = set_pop(all_transitions)
+		if (value_eq(read_attribute(model, transition, "event"), event)):
 			// Found a match
 			model_delete_element(model, cstate_link)
-			// TODO
+
 			// Set destination of state
+			instantiate_link(model, "CurrentStateLink", "", cstate_obj, new_state)
+
+			output("STATE " + cast_v2s(new_state))
+
 			// Raise "raise" attribute of transition
+			raise = read_attribute(model, transition, "raise")
+			if (element_neq(raise, read_root())):
+				// Raise the event
+				output("RAISE " + cast_v2s(raise))
+
 			return !
 
 	return!
@@ -151,7 +117,7 @@ Void function execute_fsa(design_model : Element):
 	simulation_time = 0.0
 	old_runtime_model = instantiate_model(import_node("models/FiniteStateAutomata_Runtime"))
 	runtime_model = retype_to_runtime(design_model)
-	runtime_model = sanitize(runtime_model, old_runtime_model)
+	runtime_model = sanitize(runtime_model, old_runtime_model, True)
 
 	conforming = conformance_scd(design_model)
 	if (conforming == "OK"):
@@ -160,10 +126,17 @@ Void function execute_fsa(design_model : Element):
 		output("CONFORMANCE_FAIL")
 
 	while (True):
-		cmd = input()
+		if (has_input()):
+			cmd = input()
+		else:
+			cmd = "skip"
 
 		// Process input
-		if (cmd == "pause"):
+		if (cmd == "skip"):
+			output("SIMTIME " + cast_v2s(time() - start_time))
+			output("STATE " + cast_v2s(readAssociationDestination(model, set_pop(allInstances(model, "CurrentStateLink")))))
+			
+		elif (cmd == "pause"):
 			// Pausing merely stops a running simulation
 			simulation_time = time() - start_time
 
@@ -179,7 +152,7 @@ Void function execute_fsa(design_model : Element):
 			evt = input()
 
 			do_transition(runtime_model, start_time, evt)
-			
+
 		elif (cmd == "read_available_attributes"):
 			// Returns a list of all available attributes
 			Element attr_list
@@ -197,7 +170,7 @@ Void function execute_fsa(design_model : Element):
 			// Returns the value of an attribute
 			output("ATTR_VALUE " + cast_v2s(read_attribute(design_model, input(), input())))
 
-		elif (bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association"))):
+		elif (bool_or(cmd == "switch_initial", bool_or(bool_or(cmd == "set_attribute", cmd == "instantiate_node"), bool_or(cmd == "delete_element", cmd == "instantiate_association")))):
 			// Modify the structure
 			if (cmd == "set_attribute"):
 				// Setting an attribute
@@ -225,6 +198,13 @@ Void function execute_fsa(design_model : Element):
 				// Delete the provided element
 				model_delete_element(design_model, input())
 
+			elif (cmd == "switch_initial"):
+				// Switch the initial state
+				if (read_nr_out(allInstances(design_model, "InitialState")) > 0):
+					retype(design_model, set_pop(allInstances(design_model, "InitialState")), "State")
+
+				retype(design_model, input(), "InitialState")
+
 			// After changes, we check whether or not the design model conforms
 			if (conforming == "OK"):
 				// Was correct, so store just to make sure
@@ -244,6 +224,3 @@ Void function execute_fsa(design_model : Element):
 				output("CONFORMANCE_FAIL " + conforming)
 		else:
 			log("Did not understand command: " + cmd)
-
-Float function v2f(i : Element):
-	return cast_s2f(cast_v2s(i))!