""" A test TCore transformation: an FSA to a PN, based on https://homepages.ecs.vuw.ac.nz/~tk/publications/papers/explicitly-modeling-transformations.pdf """ from core.himesis import Himesis, HimesisPreConditionPatternNAC, HimesisPreConditionPatternLHS, HimesisPostConditionPattern from tcore.messages import Packet from rules.frule import FRule from state import MvSKernel, getMvSBackend def print_results(graph): print("GRAPH:") for node in graph.node_guid_iter(): print(" ", node) for k, v in graph.node_get_attributes(node).items(): print(" ", k, "=", v) for edge in graph.edge_iter(): label = graph.edge_get_label(edge) s, t = graph.get_edge(edge) print(" ", s, "-[ %s ]->" % str(label), t) # TODO: Read from DrawIO # TODO: Select graph based on Formalism name and instance name FSA = Himesis("FSA", getMvSBackend(MvSKernel.IGRAPH)) # Create the nodes s1 = FSA.add_node("State", False) FSA.node_set_attribute(s1, "name", "S1") s2 = FSA.add_node("State", False) FSA.node_set_attribute(s2, "name", "S2") s3 = FSA.add_node("State", False) FSA.node_set_attribute(s3, "name", "S3") # Create the edges FSA.link_nodes(s1, s2, "y") FSA.link_nodes(s2, s2, "e") FSA.link_nodes(s2, s3, "s") # TODO: conformance check w.r.t. Formalism # Create the rules # TODO: Also obtain these from DrawIO # RULE 1: State2Place r1LHS = HimesisPreConditionPatternLHS("r1LHS", getMvSBackend(MvSKernel.IGRAPH)) r1n1 = r1LHS.add_node("State", False) r1LHS.node_set_attribute(r1n1, Himesis.Constants.MT_LABEL, '1') r1NAC = HimesisPreConditionPatternNAC(None, "r1NAC", getMvSBackend(MvSKernel.IGRAPH)) r1n2 = r1NAC.add_node("State", False) r1NAC.node_set_attribute(r1n2, Himesis.Constants.MT_LABEL, '1') r1n3 = r1NAC.add_node("Place", False) r1NAC.node_set_attribute(r1n3, Himesis.Constants.MT_LABEL, '2') r1NAC.link_nodes(r1n2, r1n3) r1LHS.addNAC(r1NAC) r1RHS = HimesisPostConditionPattern("r1RHS", getMvSBackend(MvSKernel.IGRAPH)) r1n4 = r1RHS.add_node("State", False) r1RHS.node_set_attribute(r1n4, Himesis.Constants.MT_LABEL, '1') r1n5 = r1RHS.add_node("Place", False) r1RHS.node_set_attribute(r1n5, Himesis.Constants.MT_LABEL, '2') r1RHS.link_nodes(r1n4, r1n5) def action(mapper, H: Himesis): attr = H.node_get_attribute(mapper['1'], "name") H.node_set_attribute(mapper['2'], "name", "P(%s)" % attr) return {} # TODO? r1RHS[Himesis.Constants.MT_ACTION] = action # print("NAC:") # print_results(r1NAC) # print("\nLHS:") # print_results(r1LHS) # print("\nRHS:") # print_results(r1RHS) print("Applying ARule") r1NAC[Himesis.Constants.MT_CONSTRAINT] = lambda a, b: True r1LHS[Himesis.Constants.MT_CONSTRAINT] = lambda a, b: True r1RHS.pre = r1LHS r1RHS.pre_labels = [] for v in r1LHS.node_guid_iter(): r1RHS.pre_labels.append(r1LHS.node_get_attribute(v, Himesis.Constants.MT_LABEL)) arule = FRule(r1LHS, r1RHS, sendAndApplyDeltaFunc=lambda x: None) FSA2PN = arule.packet_in(Packet(FSA)) if arule.exception: print(arule.exception) else: # import igraph as ig # ig.plot(FSA2PN.graph.state.graph, "result.png") print_results(FSA2PN.graph)