-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWolframModelRuleValue.m
119 lines (77 loc) · 4.29 KB
/
WolframModelRuleValue.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
Package["SetReplace`"]
PackageImport["GeneralUtilities`"]
PackageExport["WolframModelRuleValue"]
PackageExport["$WolframModelRuleProperties"]
(* Documentation *)
SetUsage @ "
WolframModelRuleValue[rule$, property$] yields the value for a property$ of a Wolfram model rule$.
WolframModelRuleValue[rule$] yields the values of all available properties.
";
SetUsage @ "
$WolframModelRuleProperties gives the list of all available rule properties.
";
SyntaxInformation[WolframModelRuleValue] = {"ArgumentsPattern" -> {rule_, property_.}};
$WolframModelRuleProperties = Sort @ {
"ConnectedInput", "ConnectedOutput", "ConnectedInputOutputUnion", "MaximumArity", "NodeCounts", "NodesDroppedAdded",
"Signature", "TraditionalSignature", "TransformationCount"};
With[{properties = $WolframModelRuleProperties},
FE`Evaluate[FEPrivate`AddSpecialArgCompletion["WolframModelRuleValue" -> {0, properties}]]];
(* Implementation *)
WolframModelRuleValue[args___] := ModuleScope[
result = Catch[wolframModelRuleValue[args]];
result /; result =!= $Failed
];
wolframModelRuleValue[args___] /; !Developer`CheckArgumentCount[WolframModelRuleValue[args], 1, 2] := Throw[$Failed];
$rulePattern = Rule[_, _] | {Rule[_, _]...};
wolframModelRuleValue[rule : $rulePattern, properties_List] :=
Check[wolframModelRuleValue[rule, #], Throw[$Failed]] & /@ properties;
wolframModelRuleValue[rule : $rulePattern] :=
Association @ Thread[$WolframModelRuleProperties -> wolframModelRuleValue[rule, $WolframModelRuleProperties]];
WolframModelRuleValue::invalidRule = "The rule specification `1` should either be a Rule or a List of rules.";
wolframModelRuleValue[rule : Except[$rulePattern], _ : {}] := (
Message[WolframModelRuleValue::invalidRule, rule];
Throw[$Failed];
);
WolframModelRuleValue::unknownProperty = "Property `1` should be one of $WolframModelRuleProperties.";
wolframModelRuleValue[rule : $rulePattern, property : Except[Alternatives @@ $WolframModelRuleProperties, _String]] := (
Message[WolframModelRuleValue::unknownProperty, property];
Throw[$Failed];
);
WolframModelRuleValue::invalidProperty = "Property `1` should be either a String or a List of properties.";
wolframModelRuleValue[rule : $rulePattern, property : Except[_List | _String]] := (
Message[WolframModelRuleValue::invalidProperty, property];
Throw[$Failed];
);
(* Connectedness *)
wolframModelRuleValue[
rules : {Rule[_, _]...}, property : "ConnectedInput" | "ConnectedOutput" | "ConnectedInputOutputUnion"] :=
And @@ (wolframModelRuleValue[#, property] &) /@ rules;
wolframModelRuleValue[input_ -> _, "ConnectedInput"] := connectedHypergraphQ[input];
wolframModelRuleValue[_ -> output_, "ConnectedOutput"] := connectedHypergraphQ[output];
wolframModelRuleValue[input_ -> output_, "ConnectedInputOutputUnion"] :=
connectedHypergraphQ[Flatten[{input, output}, 1]];
(* Listable properties *)
wolframModelRuleValue[
rules : {Rule[_, _]...},
property : "NodeCounts" | "NodesDroppedAdded" | "Signature" | "TraditionalSignature"] :=
wolframModelRuleValue[#, property] & /@ rules;
(* Arity *)
wolframModelRuleValue[rules : {Rule[_, _]...}, "MaximumArity"] :=
Max[wolframModelRuleValue[#, "MaximumArity"] & /@ rules, 0];
wolframModelRuleValue[input_ -> output_, "MaximumArity"] :=
Max[maximumHypergraphArity /@ toCanonicalHypergraphForm /@ {input, output}, 0];
maximumHypergraphArity[edges_List] := Max[Length /@ edges, 0];
(* Node Counts *)
wolframModelRuleValue[rule : Rule[_, _], "NodeCounts"] := Length @* vertexList /@ rule;
(* Nodes dropped and added *)
wolframModelRuleValue[input_ -> output_, "NodesDroppedAdded"] :=
Length /@ ({Complement[#1, #2], Complement[#2, #1]} &) @@ vertexList /@ {input, output};
(* Rule signature *)
wolframModelRuleValue[rule : Rule[_, _], "Signature"] := hypergraphSignature /@ toCanonicalHypergraphForm /@ rule;
hypergraphSignature[edges_] := SortBy[Reverse /@ Tally[Length /@ edges], Last];
wolframModelRuleValue[rule : Rule[_, _], "TraditionalSignature"] :=
Switch[Length[#], 0, "\[EmptySet]", 1, First[#], _, Row[#]] & /@
Apply[Subscript, wolframModelRuleValue[rule, "Signature"], {2}];
(* Rule count *)
wolframModelRuleValue[rule : Rule[_, _], "TransformationCount"] := wolframModelRuleValue[{rule}, "TransformationCount"];
wolframModelRuleValue[rules : {Rule[_, _]...}, "TransformationCount"] := Length[rules];