-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathHypergraphUnificationsPlot.m
71 lines (58 loc) · 2.47 KB
/
HypergraphUnificationsPlot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Package["SetReplace`"]
PackageImport["GeneralUtilities`"]
PackageExport["HypergraphUnificationsPlot"]
(* Documentation *)
SetUsage @ "
HypergraphUnificationsPlot[hypergraph$1, hypergraph$2] yields a list of plots of all hypergraphs \
containing both hypergraph$1 and hypergraph$2 as rule input matches.
";
Options[HypergraphUnificationsPlot] := Options[HypergraphPlot];
SyntaxInformation[HypergraphUnificationsPlot] = {
"ArgumentsPattern" -> {hypergraph1_, hypergraph2_, OptionsPattern[]},
"OptionNames" -> Options[HypergraphUnificationsPlot][[All, 1]]};
(* Implementation *)
HypergraphUnificationsPlot[args___] := ModuleScope[
result = Catch[hypergraphUnificationsPlot[args]];
result /; result =!= $Failed
];
hypergraphUnificationsPlot[args___] /; !Developer`CheckArgumentCount[HypergraphUnificationsPlot[args], 2, 2] :=
Throw[$Failed];
$color1 = Red;
$color2 = Blue;
HypergraphUnificationsPlot::emptyEdge = "Empty edges are not supported.";
hypergraphUnificationsPlot[e1_, e2_, opts : OptionsPattern[]] := ModuleScope[
If[Length[Cases[Join[e1, e2], {}]] > 0,
Message[HypergraphUnificationsPlot::emptyEdge];
Throw[$Failed];
];
unifications = Check[HypergraphUnifications[e1, e2], Throw[$Failed]];
automaticVertexLabelsList = unificationVertexLabels[e1, e2] @@@ unifications;
{vertexLabels, edgeStyle} =
Check[OptionValue[HypergraphUnificationsPlot, {opts}, #], Throw[$Failed]] & /@ {VertexLabels, EdgeStyle};
MapThread[
Check[
HypergraphPlot[
#1,
VertexLabels -> Replace[vertexLabels, Automatic -> #4],
EdgeStyle -> Replace[edgeStyle, Automatic -> ReplacePart[
Table[Automatic, Length[#]],
Join[
Thread[Intersection[Values[#2], Values[#3]] -> Blend[{$color1, $color2}]],
Thread[Values[#2] -> $color1], Thread[Values[#3] -> $color2]]]],
opts]
,
Throw[$Failed]
] &,
{unifications[[All, 1]], unifications[[All, 2]], unifications[[All, 3]], automaticVertexLabelsList}]
];
unificationVertexLabels[e1_, e2_][unification_, edgeMapping1_, edgeMapping2_] := ModuleScope[
{labels1, labels2} =
Merge[Reverse /@ Union[Catenate[Thread /@ Thread[#1[[Keys[#2]]] -> unification[[Values[#2]]]]]], Identity] & @@@
{{e1, edgeMapping1}, {e2, edgeMapping2}};
Normal @ AssociationMap[
Row[
Catenate[
Function[{unif, color}, Style[#, color] & /@ Lookup[unif, #, {}]] @@@ {{labels1, $color1}, {labels2, $color2}}],
","] &,
vertexList[unification]]
];