-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloy.py
More file actions
116 lines (102 loc) · 3.5 KB
/
alloy.py
File metadata and controls
116 lines (102 loc) · 3.5 KB
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
from lark import lark, Visitor, Tree
alloy_grammar = r"""
%import common.WS
%ignore WS
%import common.CNAME -> NAME
start: paragraph*
paragraph: signature | fact | predicate
signature: [ABSTRACT] [MULTIPLICITY] "sig" names [RELATION NAME] "{" [field ("," field)*] "}"
names: NAME ("," NAME)*
field: NAME ":" TYPE
fact: "fact" NAME "{" [expr] "}"
predicate: "pred" NAME "{" [expr] "}"
expr: ANY? ("{" expr "}")? ANY?
MULTIPLICITY: "one" | "lone" | "some"
RELATION: "extends" | "in"
ABSTRACT: "abstract"
TYPE: /[^,}]+/
ANY: /[^{}]+/
"""
parser = lark.Lark(alloy_grammar, start='start', parser='earley')
def expr2string(tree):
result = ""
for child in tree.children:
if isinstance(child, Tree):
if child.children:
result += "{" + expr2string(child) + "}"
else:
result += child.value
return result
class Parent(Visitor):
def __default__(self, tree):
for subtree in tree.children:
if isinstance(subtree, Tree):
subtree.parent = tree
class Collector(Visitor):
def __init__(self):
self.predicates = {}
self.signatures = set()
self.subsets = {}
self.toplevel = set()
self.fields = {}
self.extensions = {}
self.multiplicities = {}
self.abstract = set()
self.facts = {}
def signature(self, tree):
for n in tree.children[2].children:
self.signatures.add(n.value)
if not tree.children[3]:
self.toplevel.add(n.value)
else:
parent = tree.children[4].value
self.subsets[n.value] = parent
if tree.children[3].value == "extends":
self.extensions.setdefault(parent,set()).add(n)
if tree.children[1]:
self.multiplicities[n.value] = tree.children[1].value
if tree.children[0]:
self.abstract.add(n.value)
def field(self, tree):
for p in tree.parent.children[2].children:
parent = p.value
field_name = tree.children[0].value
self.fields[field_name] = parent + " -> " + tree.children[1].value.strip()
def predicate(self, tree):
if tree.children[1]:
self.predicates[tree.children[0].value] = expr2string(tree.children[1])
else:
self.predicates[tree.children[0].value] = ""
def fact(self, tree):
self.facts[tree.children[0].value] = expr2string(tree.children[1])
def parse(content):
tree = parser.parse(content)
parent_setter = Parent()
parent_setter.visit(tree)
collector = Collector()
collector.visit(tree)
return collector
def build_instance(collector, value):
atoms = set()
for sig in collector.toplevel:
for tuple in value[sig]:
atoms.add(str(tuple[0]).replace('$',''))
if atoms:
instance = "\tsome disj " + ",".join(sorted(atoms)) + " : univ {\n"
else:
instance = "\t{\n"
for rel in sorted(value):
tuples = value[rel]
if tuples:
relation = []
for tuple in tuples:
relation.append("->".join([str(atom).replace('$','') for atom in tuple]))
instance += f"\t\t{rel} = " + " + ".join(relation) + "\n"
else:
instance += f"\t\tno {rel}\n"
instance += "\t}\n"
return instance
def print_instances(instances, scope):
for i, inst in enumerate(instances):
command = f"run Test{i+1} {{\n{inst}\n}} for {scope}"
print(command)