Sfoglia il codice sorgente

Transformation bugfixes

rparedis 2 anni fa
parent
commit
04d3b302ea

BIN
core/__pycache__/himesis.cpython-38.pyc


BIN
core/__pycache__/match_algo.cpython-38.pyc


+ 17 - 7
core/himesis.py

@@ -53,9 +53,12 @@ class Himesis:
 
         self.state = mvs
         self.attrib = {
-            Himesis.Constants.GUID: self.state.new_id()
+            Himesis.Constants.GUID: self.state.new_id(),
+            Himesis.Constants.METAMODELS: [],
+            Himesis.Constants.MISSING_METAMODELS: lambda: []
         }
         self.vcount = 0
+        self.attrs = set()
 
         self.match_sets = []
 
@@ -93,9 +96,9 @@ class Himesis:
     def __setitem__(self, key, value):
         self.attrib[key] = value
 
-    def __str__(self):
-        s = super(Himesis, self).__str__()
-        return self.name + ' ' + s[s.index('('):] + ' ' + str(self[Himesis.Constants.GUID])
+    # def __str__(self):
+    #     s = super(Himesis, self).__str__()
+    #     return self.name + ' ' + s[s.index('('):] + ' ' + str(self[Himesis.Constants.GUID])
 
     def get_id(self):
         """
@@ -128,7 +131,11 @@ class Himesis:
         res = []
         for node in self.node_guid_iter():
             res.extend(self.state.read_outgoing(node))
-        return [e for e in res if self.edge_get_label(e) != self.Constants.ATTRIBUTES]
+            res.extend(self.state.read_incoming(node))
+        return list(set([e for e in res if self.edge_get_label(e) != self.Constants.ATTRIBUTES]))
+
+    def attr_iter(self):
+        return self.attrs
 
     def follow_edge(self, node, label):
         return self.state.read_dict(node, label)
@@ -146,6 +153,7 @@ class Himesis:
             self.state.delete_node(target)
         target = self.state.create_nodevalue(value)
         self.state.create_dict(attr, key, target)
+        self.attrs.add(key)
 
     def node_get_attribute(self, node, key):
         attrs = [self.state.read_value(v) for v in self.state.read_dict_keys(node)]
@@ -717,13 +725,15 @@ class HimesisPostConditionPattern(HimesisPattern):
             for edge in edges:
                 # src_label = self.node_get_attribute(self.edge_get_source(edge), Himesis.Constants.MT_LABEL)
                 # tgt_label = self.node_get_attribute(self.edge_get_target(edge), Himesis.Constants.MT_LABEL)
-                graph.link_nodes(self.edge_get_source(edge), self.edge_get_target(edge))
+                sl = self.node_get_attribute(self.edge_get_source(edge), Himesis.Constants.MT_LABEL)
+                tl = self.node_get_attribute(self.edge_get_target(edge), Himesis.Constants.MT_LABEL)
+                graph.link_nodes(labels[sl], labels[tl], self.edge_get_label(edge))
                 # graph.add_edges([(labels[src_label], labels[tgt_label])])
                 packet.deltas.append(
                     {'op':'MKEDGE',
                      'guid1':self.edge_get_source(edge),
                      'guid2':self.edge_get_target(edge)})
-                visited_edges.append(edge.index)
+                visited_edges.append(edge)
 
         # Set the output pivots
         for node in self.node_guid_iter():

+ 2 - 0
core/match_algo.py

@@ -452,6 +452,8 @@ class HimesisMatcher(object):
             yield self.mapping
         else:
             for src_node, patt_node in self.candidate_pairs_iter():
+                if src_node not in self.G1.node_guid_iter(): continue
+                if patt_node not in self.G2.node_guid_iter(): continue
 
                 # Cache the predecessors and successors of the candidate pairs on the fly 
                 self.pred1, self.succ1, self.pred2, self.succ2 = {}, {}, {}, {}

BIN
rules/__pycache__/arule.cpython-38.pyc


+ 1 - 0
rules/arule.py

@@ -31,6 +31,7 @@ class ARule(Composer):
         packet = self.M.packet_in(packet)
         if not self.M.is_success:
             self.exception = self.M.exception
+            self.M.packet_in(packet)
             return packet
 
         # Choose the only match

BIN
tcore/__pycache__/messages.cpython-38.pyc


BIN
tcore/__pycache__/rewriter.cpython-38.pyc


+ 8 - 8
tcore/messages.py

@@ -232,13 +232,13 @@ class Match(dict):
                 node[Himesis.Constants.MT_DIRTY] = False
 
     def to_label_mapping(self, source_graph):
-        '''
-            Converts the match to a mapping dictionary {label: source node index}.
-        '''
+        """
+        Converts the match to a mapping dictionary {label: source node index}.
+        """
         mapping = {}
         for label in self.keys():
             try:
-                sourceNode = source_graph.get_node(self[label])
+                sourceNode = self[label]
             except KeyError:
                 raise Exception('The matched node %s does not exist' % label)
             if sourceNode is not None:
@@ -248,14 +248,14 @@ class Match(dict):
         return mapping
 
     def to_mapping(self, source_graph, pattern_graph):
-        '''
-            Converts the match to a mapping dictionary {pattern node index: source node index}.
-        '''
+        """
+        Converts the match to a mapping dictionary {pattern node index: source node index}.
+        """
         mapping = {}
         for label in self.keys():
             patternNode = pattern_graph.get_node_with_label(label)
             if patternNode is not None:
-                sourceNode = source_graph.get_node(self[label])
+                sourceNode = self[label]
                 mapping[patternNode] = sourceNode
         return mapping
 

+ 7 - 7
tcore/rewriter.py

@@ -13,14 +13,14 @@ if sys.version_info[0] >= 3:
     from functools import reduce
 
 class Rewriter(RulePrimitive):
-    '''
-        Transforms the matched source model elements according to the specified post-condition pattern.
-    '''
+    """
+    Transforms the matched source model elements according to the specified post-condition pattern.
+    """
     def __init__(self, condition,sendAndApplyDeltaFunc):
-        '''
-            Transforms the bound graph of the source graph into what the specification of the post-condition pattern.
-            @param condition: The the post-condition pattern.
-        '''
+        """
+        Transforms the bound graph of the source graph into what the specification of the post-condition pattern.
+        @param condition: The the post-condition pattern.
+        """
         super(Rewriter, self).__init__()
         self.condition = condition
 

+ 38 - 19
transformation.py

@@ -17,7 +17,7 @@ def print_results(graph):
 	for edge in graph.edge_iter():
 		label = graph.edge_get_label(edge)
 		s, t = graph.get_edge(edge)
-		print(" ", s, "-[ %s ]->" % label, t)
+		print(" ", s, "-[ %s ]->" % str(label), t)
 
 # TODO: Read from DrawIO
 # TODO: Select graph based on Formalism name and instance name
@@ -53,27 +53,46 @@ 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')
-r1n4 = r1NAC.add_node("Link", True)
-r1NAC.node_set_attribute(r1n4, Himesis.Constants.MT_LABEL, '3')
-r1NAC.link_nodes(r1n2, r1n4)
-r1NAC.link_nodes(r1n4, r1n3)
+r1NAC.link_nodes(r1n2, r1n3)
 r1LHS.addNAC(r1NAC)
 
 r1RHS = HimesisPostConditionPattern("r1RHS", getMvSBackend(MvSKernel.IGRAPH))
-r1n5 = r1RHS.add_node("State", False)
-r1RHS.node_set_attribute(r1n5, Himesis.Constants.MT_LABEL, '1')
-r1n6 = r1RHS.add_node("Place", False)
-r1RHS.node_set_attribute(r1n6, Himesis.Constants.MT_LABEL, '2')
-r1n7 = r1RHS.add_node("Link", True)
-r1RHS.node_set_attribute(r1n7, Himesis.Constants.MT_LABEL, '3')
-r1RHS.link_nodes(r1n5, r1n7)
-r1RHS.link_nodes(r1n7, r1n6)
-
-print("Applying FRule 1")
-frule = FRule(r1LHS, r1RHS)
-FSA2PN = frule.packet_in(Packet(FSA))
-
-print_results(FSA2PN.graph)
+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)