-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_configuration.py
209 lines (170 loc) · 6.94 KB
/
gen_configuration.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# MIT License
#
# Copyright (c) 2022 Nicola Dardanis, Lucas Weitzendorf
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import random
import importlib
from typing import Dict, List
# A map from theories name to theory operation.
# Each operation specifies either the number of input parameters (having the same theory operator type).
# or the list of parameters with the names and types of each one.
# Insert here new theories and their operations!
main_operators: Dict[str, Dict[str, List[str]]] = {
"BooleanOperator": {
"BooleanNot": ["BooleanOperator"],
"BooleanXor": ["BooleanOperator", "BooleanOperator"],
},
"IntegerOperator": {
"IntegerAddition": ["IntegerOperator", "IntegerOperator"],
"IntegerSubtraction": ["IntegerOperator", "IntegerOperator"],
"IntegerMultiplication": ["IntegerOperator", "IntegerOperator"],
},
"RealOperator": {
"RealAddition": ["RealOperator", "RealOperator"],
"RealSubtraction": ["RealOperator", "RealOperator"],
"RealMultiplication": ["RealOperator", "RealOperator"],
},
"StringOperator": {
"StringConcatenation1n1": ["StringOperator", "StringOperator"],
"StringConcatenation1n2": ["StringOperator", "StringOperator"],
"StringConcatenation1n3": ["StringOperator", "StringOperator"],
"StringConcatenation2n1": ["StringOperator", "StringOperator"],
"StringConcatenation2n2": ["StringOperator", "StringOperator"],
"StringConcatenation2n3": ["StringOperator", "StringOperator"],
},
"BitVectorOperator": {
"BitVectorNot": ["BitVectorOperator"],
"BitVectorNegation": ["BitVectorOperator"],
"BitVectorXor": ["BitVectorOperator", "BitVectorOperator"],
"BitVectorConcatenation": ["BitVectorOperator", "BitVectorOperator"],
},
}
# These operators are not invertible and thus not used in generation, but necessary
# to define the inverse of some main operators
fringe_operators: Dict[str, Dict[str, List[str]]] = {
"BooleanOperator": {},
"IntegerOperator": {
"IntegerDivision": ["IntegerOperator", "IntegerOperator"],
"StringLength": ["StringOperator"],
"StringIndexof": ["StringOperator", "StringOperator", "IntegerOperator"],
},
"RealOperator": {
"RealDivision": ["RealOperator", "RealOperator"],
},
"StringOperator": {
"StringReplacement": ["StringOperator", "StringOperator", "StringOperator"],
"Substring": ["StringOperator", "IntegerOperator", "IntegerOperator"],
},
"BitVectorOperator": {
"BitVectorExtraction": ["BitVectorOperator", "IntegerOperator", "IntegerOperator"],
},
}
theory_declarations: Dict[str, Dict[str, List[str]]] = {
k: {**main_operators[k], **fringe_operators[k]} for k in main_operators
}
leaf_operators: Dict[str, Dict[str, str]] = {
"BooleanOperator": {
"const": "BooleanConstant",
"var": "BooleanVariable",
"lit": "BooleanLiteral"
},
"IntegerOperator": {
"const": "IntegerConstant",
"var": "IntegerVariable",
"lit": "IntegerLiteral"
},
"RealOperator": {
"const": "RealConstant",
"var": "RealVariable",
"lit": "RealLiteral"
},
"StringOperator": {
"const": "StringConstant",
"var": "StringVariable",
"lit": "StringLiteral"
},
"BitVectorOperator": {
"const": "BitVectorConstant",
"var": "BitVectorVariable",
"lit": "BitVectorLiteral"
},
}
root_operators: Dict[str, str] = {
"BooleanOperator": "BooleanEquality",
"IntegerOperator": "IntegerEquality",
"RealOperator": "RealEquality",
"StringOperator": "StringEquality",
"BitVectorOperator": "BitVectorEquality",
}
# A map from command line options to the internal representation.
option_to_operator_type: Dict[str, str] = {
"bool": "BooleanOperator",
"int": "IntegerOperator",
"real": "RealOperator",
"string": "StringOperator",
"bitvector": "BitVectorOperator",
}
def get_theories() -> List[str]:
return list(theory_declarations.keys())
def get_operators(theory: str) -> List[str]:
return list(theory_declarations[theory].keys())
def get_all_nodes(theory: str) -> List[str]:
return [*get_operators(theory), get_variable(theory),
get_constant(theory), get_literal(theory), get_root(theory)]
def get_operator_types(theory: str) -> List[str]:
type_set = {theory}
for op in theory_declarations[theory]:
for in_type in theory_declarations[theory][op]:
type_set.add(in_type)
return list(type_set)
def get_arities(theory: str) -> List[int]:
arities = []
for op in main_operators[theory].keys():
arities.append(len(get_operator_parameters(theory, op)))
return arities
def get_operator_parameters(theory: str, operator: str) -> List[str]:
params = theory_declarations[theory][operator]
return params
def get_constant(theory: str) -> str:
return leaf_operators[theory]["const"]
def get_variable(theory: str) -> str:
return leaf_operators[theory]["var"]
def get_literal(theory: str) -> str:
return leaf_operators[theory]["lit"]
def get_root(theory: str) -> str:
return root_operators[theory]
def get_eligible_operator(theory: str, arity: int) -> str:
if theory is None:
theory = random.choice(get_theories())
theory = main_operators[theory]
operator_choices = []
for operator in theory.keys():
n = len([p for p in theory[operator] if "Operator" in p])
if n == arity:
operator_choices.append(operator)
return random.choice(operator_choices)
def get_theory_name(theory: str) -> str:
return theory.split("Operator")[0]
def get_module_name(theory: str) -> str:
return get_theory_name(theory).lower() + "_theory"
def get_operator_class(theory: str, name: str) -> 'Operator.__class__':
module_name = get_module_name(theory)
module = importlib.import_module('src.operators.' + module_name)
return getattr(module, name)