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)
|
conf4 = Conformance(state, rhs_id, ramified_mm_id)
|
||||||
print("RHS conforms?", conf4.check_nominal(log=True))
|
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():
|
def render_ramification():
|
||||||
uml = (""
|
uml = (""
|
||||||
# Render original and RAMified meta-models
|
# Render original and RAMified meta-models
|
||||||
|
|
@ -177,16 +165,9 @@ def main():
|
||||||
uml += plantuml.render_trace_conformance(state, dsl_m_id, dsl_mm_id)
|
uml += plantuml.render_trace_conformance(state, dsl_m_id, dsl_mm_id)
|
||||||
|
|
||||||
print("matching...")
|
print("matching...")
|
||||||
matcher = MatcherVF2(host, guest, mvs_adapter.RAMCompare(Bottom(state), dsl_m_od))
|
generator = mvs_adapter.match_od(state, dsl_m_id, dsl_mm_id, lhs_id, ramified_mm_id)
|
||||||
for m, color in zip(matcher.match(), ["red", "orange"]):
|
for name_mapping, color in zip(generator, ["red", "orange"]):
|
||||||
print("\nMATCH:\n", m)
|
print("\nMATCH:\n", name_mapping)
|
||||||
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)
|
|
||||||
|
|
||||||
# Render every match
|
# Render every match
|
||||||
uml += plantuml.render_trace_match(state, name_mapping, lhs_id, dsl_m_id, color)
|
uml += plantuml.render_trace_match(state, name_mapping, lhs_id, dsl_m_id, color)
|
||||||
|
|
@ -197,17 +178,8 @@ def main():
|
||||||
def render_rewrite():
|
def render_rewrite():
|
||||||
uml = render_ramification()
|
uml = render_ramification()
|
||||||
|
|
||||||
matcher = MatcherVF2(host, guest, mvs_adapter.RAMCompare(Bottom(state), dsl_m_od))
|
generator = mvs_adapter.match_od(state, dsl_m_id, dsl_mm_id, lhs_id, ramified_mm_id)
|
||||||
for m in matcher.match():
|
for name_mapping in generator:
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
rewriter.rewrite(state, lhs_id, rhs_id, ramified_mm_id, name_mapping, dsl_m_id, dsl_mm_id)
|
rewriter.rewrite(state, lhs_id, rhs_id, ramified_mm_id, name_mapping, dsl_m_id, dsl_mm_id)
|
||||||
|
|
||||||
# Render match
|
# Render match
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ from uuid import UUID
|
||||||
from services.bottom.V0 import Bottom
|
from services.bottom.V0 import Bottom
|
||||||
from services.scd import SCD
|
from services.scd import SCD
|
||||||
from services.od import OD
|
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
|
from transformation import ramify
|
||||||
import itertools
|
import itertools
|
||||||
import re
|
import re
|
||||||
|
|
@ -188,6 +188,8 @@ def model_to_graph(state: State, model: UUID, metamodel: UUID, prefix=""):
|
||||||
|
|
||||||
return graph
|
return graph
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
# 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:
|
class RAMCompare:
|
||||||
def __init__(self, bottom, host_od):
|
def __init__(self, bottom, host_od):
|
||||||
|
|
@ -272,3 +274,17 @@ class RAMCompare:
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return False
|
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
|
# Rewrite is performed in-place
|
||||||
# Also updates the mapping in-place, to become RHS -> host
|
# 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)
|
bottom = Bottom(state)
|
||||||
|
|
||||||
scd_metamodel_id = state.read_dict(state.read_root(), "SCD")
|
scd_metamodel_id = state.read_dict(state.read_root(), "SCD")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue