Abstract away Object Diagram pattern matching in a function that does all the magic
This commit is contained in:
parent
13f19cdec6
commit
f45872d3f7
3 changed files with 89 additions and 101 deletions
|
|
@ -137,18 +137,6 @@ def main():
|
|||
conf4 = Conformance(state, rhs_id, ramified_mm_id)
|
||||
print("RHS conforms?", conf4.check_nominal(log=True))
|
||||
|
||||
# Convert to format understood by matching algorithm
|
||||
host = mvs_adapter.model_to_graph(state, dsl_m_id, dsl_mm_id)
|
||||
guest = mvs_adapter.model_to_graph(state, lhs_id, ramified_mm_id)
|
||||
|
||||
print("HOST:")
|
||||
print(host.vtxs)
|
||||
print(host.edges)
|
||||
|
||||
print("GUEST:")
|
||||
print(guest.vtxs)
|
||||
print(guest.edges)
|
||||
|
||||
def render_ramification():
|
||||
uml = (""
|
||||
# Render original and RAMified meta-models
|
||||
|
|
@ -177,16 +165,9 @@ def main():
|
|||
uml += plantuml.render_trace_conformance(state, dsl_m_id, dsl_mm_id)
|
||||
|
||||
print("matching...")
|
||||
matcher = MatcherVF2(host, guest, mvs_adapter.RAMCompare(Bottom(state), dsl_m_od))
|
||||
for m, color in zip(matcher.match(), ["red", "orange"]):
|
||||
print("\nMATCH:\n", m)
|
||||
name_mapping = {}
|
||||
# id_mapping = {}
|
||||
for guest_vtx, host_vtx in m.mapping_vtxs.items():
|
||||
if isinstance(guest_vtx, mvs_adapter.NamedNode) and isinstance(host_vtx, mvs_adapter.NamedNode):
|
||||
# id_mapping[guest_vtx.node_id] = host_vtx.node_id
|
||||
name_mapping[guest_vtx.name] = host_vtx.name
|
||||
print(name_mapping)
|
||||
generator = mvs_adapter.match_od(state, dsl_m_id, dsl_mm_id, lhs_id, ramified_mm_id)
|
||||
for name_mapping, color in zip(generator, ["red", "orange"]):
|
||||
print("\nMATCH:\n", name_mapping)
|
||||
|
||||
# Render every match
|
||||
uml += plantuml.render_trace_match(state, name_mapping, lhs_id, dsl_m_id, color)
|
||||
|
|
@ -197,17 +178,8 @@ def main():
|
|||
def render_rewrite():
|
||||
uml = render_ramification()
|
||||
|
||||
matcher = MatcherVF2(host, guest, mvs_adapter.RAMCompare(Bottom(state), dsl_m_od))
|
||||
for m in matcher.match():
|
||||
name_mapping = {}
|
||||
# id_mapping = {}
|
||||
for guest_vtx, host_vtx in m.mapping_vtxs.items():
|
||||
if isinstance(guest_vtx, mvs_adapter.NamedNode) and isinstance(host_vtx, mvs_adapter.NamedNode):
|
||||
# id_mapping[guest_vtx.node_id] = host_vtx.node_id
|
||||
name_mapping[guest_vtx.name] = host_vtx.name
|
||||
print(name_mapping)
|
||||
|
||||
|
||||
generator = mvs_adapter.match_od(state, dsl_m_id, dsl_mm_id, lhs_id, ramified_mm_id)
|
||||
for name_mapping in generator:
|
||||
rewriter.rewrite(state, lhs_id, rhs_id, ramified_mm_id, name_mapping, dsl_m_id, dsl_mm_id)
|
||||
|
||||
# Render match
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from uuid import UUID
|
|||
from services.bottom.V0 import Bottom
|
||||
from services.scd import SCD
|
||||
from services.od import OD
|
||||
from pattern_matching.matcher import Graph, Edge, Vertex
|
||||
from pattern_matching.matcher import Graph, Edge, Vertex, MatcherVF2
|
||||
from transformation import ramify
|
||||
import itertools
|
||||
import re
|
||||
|
|
@ -188,8 +188,10 @@ def model_to_graph(state: State, model: UUID, metamodel: UUID, prefix=""):
|
|||
|
||||
return graph
|
||||
|
||||
# Function object for pattern matching. Decides whether to match host and guest vertices, where guest is a RAMified instance (e.g., the attributes are all strings with Python expressions), and the host is an instance (=object diagram) of the original model (=class diagram)
|
||||
class RAMCompare:
|
||||
|
||||
def match_od(state, host_m, host_mm, pattern_m, pattern_mm):
|
||||
# Function object for pattern matching. Decides whether to match host and guest vertices, where guest is a RAMified instance (e.g., the attributes are all strings with Python expressions), and the host is an instance (=object diagram) of the original model (=class diagram)
|
||||
class RAMCompare:
|
||||
def __init__(self, bottom, host_od):
|
||||
self.bottom = bottom
|
||||
self.host_od = host_od
|
||||
|
|
@ -272,3 +274,17 @@ class RAMCompare:
|
|||
})
|
||||
except Exception as e:
|
||||
return False
|
||||
|
||||
# Convert to format understood by matching algorithm
|
||||
host = model_to_graph(state, host_m, host_mm)
|
||||
guest = model_to_graph(state, pattern_m, pattern_mm)
|
||||
|
||||
matcher = MatcherVF2(host, guest, RAMCompare(Bottom(state), OD(host_mm, host_m, state)))
|
||||
for m in matcher.match():
|
||||
# print("\nMATCH:\n", m)
|
||||
# Convert mapping
|
||||
name_mapping = {}
|
||||
for guest_vtx, host_vtx in m.mapping_vtxs.items():
|
||||
if isinstance(guest_vtx, NamedNode) and isinstance(host_vtx, NamedNode):
|
||||
name_mapping[guest_vtx.name] = host_vtx.name
|
||||
yield name_mapping
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ def process_rule(state, lhs: UUID, rhs: UUID):
|
|||
|
||||
# Rewrite is performed in-place
|
||||
# Also updates the mapping in-place, to become RHS -> host
|
||||
def rewrite(state, lhs: UUID, rhs: UUID, rhs_mm: UUID, match_mapping: dict, m_to_transform: UUID, mm: UUID) -> dict:
|
||||
def rewrite(state, lhs: UUID, rhs: UUID, rhs_mm: UUID, match_mapping: dict, m_to_transform: UUID, mm: UUID):
|
||||
bottom = Bottom(state)
|
||||
|
||||
scd_metamodel_id = state.read_dict(state.read_root(), "SCD")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue