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
|
/* FIXME: finer imports. */
import java.util.*;
import kodkod.ast.*;
import kodkod.instance.*;
public class VariableManager
{
private final Map<String, Expression> from_string;
private final Map<String, TaggedVariable> tagged_variables;
private int next_id;
public VariableManager (final String var_prefix)
{
from_string = new HashMap<String, Expression>();
tagged_variables = new HashMap<String, TaggedVariable>();
}
private String generate_new_anonymous_variable_name ()
{
final String result;
result = "_var" + next_id;
next_id += 1;
return result;
}
public void add_tag
(
final String var_name,
final String var_type,
final String tag_name
)
throws Exception
{
final TaggedVariable tg;
System.out.println("[D] Skolemizing: " + var_name);
if (from_string.containsKey(var_name))
{
throw
new Exception
(
"[F] Invalid property: the variable name \""
+ var_name
+ "\" is bound multiple times in the \"tag_existing\""
+ " operator."
);
}
tg = new TaggedVariable(var_name, var_type, tag_name);
from_string.put(var_name, tg.as_relation);
tagged_variables.put(var_name, tg);
}
public Variable add_variable (final String var_name)
throws Exception
{
final Variable result;
if (from_string.containsKey(var_name))
{
throw
new Exception
(
"[F] Invalid property: the variable name \""
+ var_name
+ "\" is declared multiple times."
);
}
result = Variable.unary(var_name);
from_string.put(var_name, result);
return result;
}
public Expression get_variable (final String var_name)
throws Exception
{
final Expression result;
result = from_string.get(var_name);
if (result == null)
{
throw
new Exception
(
"[F] Variable \""
+ var_name
+ "\" is used, but not declared."
);
}
return result;
}
public Variable generate_new_anonymous_variable ()
{
return Variable.unary(generate_new_anonymous_variable_name());
}
public void add_tagged_variables_to_bounds
(
final Bounds b,
final TupleFactory f
)
{
for (final TaggedVariable tg: tagged_variables.values())
{
b.bound
(
tg.as_relation,
f.setOf(new Object[0]),
f.setOf
(
Main.get_model().get_type
(
tg.type
).get_all_members_as_atoms().toArray()
)
);
}
}
public Formula generate_tagged_variable_constraints ()
{
Formula result;
result = Formula.TRUE;
for (final TaggedVariable tg: tagged_variables.values())
{
result = result.and(tg.as_relation.one());
}
return result;
}
private static class TaggedVariable
{
private final String name;
private final String type;
private final String tag;
private final Relation as_relation;
private TaggedVariable
(
final String name,
final String type,
final String tag
)
{
this.name = name;
this.type = type;
this.tag = tag;
as_relation = Relation.unary(name);
}
}
}
|