-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.java
More file actions
302 lines (276 loc) · 9.8 KB
/
Model.java
File metadata and controls
302 lines (276 loc) · 9.8 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/**
* This file was automatically generated from Model.cs
* w/ further modifications by:
* @author Christoph M. Wintersteiger (cwinter)
**/
package com.microsoft.z3;
import com.microsoft.z3.enumerations.Z3_sort_kind;
/**
* A Model contains interpretations (assignments) of constants and functions.
**/
public class Model extends Z3Object
{
/**
* Retrieves the interpretation (the assignment) of <paramref name="a"/> in
* the model. <param name="a">A Constant</param>
*
* @return An expression if the constant has an interpretation in the model,
* null otherwise.
* @throws Z3Exception
**/
public Expr getConstInterp(Expr a) throws Z3Exception
{
getContext().checkContextMatch(a);
return getConstInterp(a.getFuncDecl());
}
/**
* Retrieves the interpretation (the assignment) of <paramref name="f"/> in
* the model. <param name="f">A function declaration of zero arity</param>
*
* @return An expression if the function has an interpretation in the model,
* null otherwise.
* @throws Z3Exception
**/
public Expr getConstInterp(FuncDecl f) throws Z3Exception
{
getContext().checkContextMatch(f);
if (f.getArity() != 0
|| Native.getSortKind(getContext().nCtx(),
Native.getRange(getContext().nCtx(), f.getNativeObject())) == Z3_sort_kind.Z3_ARRAY_SORT
.toInt())
throw new Z3Exception(
"Non-zero arity functions and arrays have FunctionInterpretations as a model. Use FuncInterp.");
long n = Native.modelGetConstInterp(getContext().nCtx(), getNativeObject(),
f.getNativeObject());
if (n == 0)
return null;
else
return Expr.create(getContext(), n);
}
/**
* Retrieves the interpretation (the assignment) of a non-constant <paramref
* name="f"/> in the model. <param name="f">A function declaration of
* non-zero arity</param>
*
* @return A FunctionInterpretation if the function has an interpretation in
* the model, null otherwise.
* @throws Z3Exception
**/
public FuncInterp getFuncInterp(FuncDecl f) throws Z3Exception
{
getContext().checkContextMatch(f);
Z3_sort_kind sk = Z3_sort_kind.fromInt(Native.getSortKind(getContext()
.nCtx(), Native.getRange(getContext().nCtx(), f.getNativeObject())));
if (f.getArity() == 0)
{
long n = Native.modelGetConstInterp(getContext().nCtx(),
getNativeObject(), f.getNativeObject());
if (sk == Z3_sort_kind.Z3_ARRAY_SORT)
{
if (n == 0)
return null;
else
{
if (Native.isAsArray(getContext().nCtx(), n) ^ true)
throw new Z3Exception(
"Argument was not an array constant");
long fd = Native.getAsArrayFuncDecl(getContext().nCtx(), n);
return getFuncInterp(new FuncDecl(getContext(), fd));
}
} else
{
throw new Z3Exception(
"Constant functions do not have a function interpretation; use ConstInterp");
}
} else
{
long n = Native.modelGetFuncInterp(getContext().nCtx(),
getNativeObject(), f.getNativeObject());
if (n == 0)
return null;
else
return new FuncInterp(getContext(), n);
}
}
/**
* The number of constants that have an interpretation in the model.
**/
public int getNumConsts() throws Z3Exception
{
return Native.modelGetNumConsts(getContext().nCtx(), getNativeObject());
}
/**
* The function declarations of the constants in the model.
*
* @throws Z3Exception
**/
public FuncDecl[] getConstDecls() throws Z3Exception
{
int n = getNumConsts();
FuncDecl[] res = new FuncDecl[n];
for (int i = 0; i < n; i++)
res[i] = new FuncDecl(getContext(), Native.modelGetConstDecl(getContext()
.nCtx(), getNativeObject(), i));
return res;
}
/**
* The number of function interpretations in the model.
**/
public int getNumFuncs() throws Z3Exception
{
return Native.modelGetNumFuncs(getContext().nCtx(), getNativeObject());
}
/**
* The function declarations of the function interpretations in the model.
*
* @throws Z3Exception
**/
public FuncDecl[] getFuncDecls() throws Z3Exception
{
int n = getNumFuncs();
FuncDecl[] res = new FuncDecl[n];
for (int i = 0; i < n; i++)
res[i] = new FuncDecl(getContext(), Native.modelGetFuncDecl(getContext()
.nCtx(), getNativeObject(), i));
return res;
}
/**
* All symbols that have an interpretation in the model.
*
* @throws Z3Exception
**/
public FuncDecl[] getDecls() throws Z3Exception
{
int nFuncs = getNumFuncs();
int nConsts = getNumConsts();
int n = nFuncs + nConsts;
FuncDecl[] res = new FuncDecl[n];
for (int i = 0; i < nConsts; i++)
res[i] = new FuncDecl(getContext(), Native.modelGetConstDecl(getContext()
.nCtx(), getNativeObject(), i));
for (int i = 0; i < nFuncs; i++)
res[nConsts + i] = new FuncDecl(getContext(), Native.modelGetFuncDecl(
getContext().nCtx(), getNativeObject(), i));
return res;
}
/**
* A ModelEvaluationFailedException is thrown when an expression cannot be
* evaluated by the model.
**/
@SuppressWarnings("serial")
public class ModelEvaluationFailedException extends Z3Exception
{
/**
* An exception that is thrown when model evaluation fails.
**/
public ModelEvaluationFailedException()
{
super();
}
}
/**
* Evaluates the expression <paramref name="t"/> in the current model.
* <remarks> This function may fail if <paramref name="t"/> contains
* quantifiers, is partial (MODEL_PARTIAL enabled), or if <paramref
* name="t"/> is not well-sorted. In this case a
* <code>ModelEvaluationFailedException</code> is thrown. </remarks> <param
* name="t">An expression</param> <param name="completion"> When this flag
* is enabled, a model value will be assigned to any constant or function
* that does not have an interpretation in the model. </param>
*
* @return The evaluation of <paramref name="t"/> in the model.
* @throws Z3Exception
**/
public Expr eval(Expr t, boolean completion) throws Z3Exception
{
Native.LongPtr v = new Native.LongPtr();
if (Native.modelEval(getContext().nCtx(), getNativeObject(),
t.getNativeObject(), (completion) ? true : false, v) ^ true)
throw new ModelEvaluationFailedException();
else
return Expr.create(getContext(), v.value);
}
/**
* Alias for <code>Eval</code>.
*
* @throws Z3Exception
**/
public Expr evaluate(Expr t, boolean completion) throws Z3Exception
{
return eval(t, completion);
}
/**
* The number of uninterpreted sorts that the model has an interpretation
* for.
**/
public int getNumSorts() throws Z3Exception
{
return Native.modelGetNumSorts(getContext().nCtx(), getNativeObject());
}
/**
* The uninterpreted sorts that the model has an interpretation for.
* <remarks> Z3 also provides an intepretation for uninterpreted sorts used
* in a formula. The interpretation for a sort is a finite set of distinct
* values. We say this finite set is the "universe" of the sort. </remarks>
* <seealso cref="NumSorts"/> <seealso cref="SortUniverse"/>
*
* @throws Z3Exception
**/
public Sort[] getSorts() throws Z3Exception
{
int n = getNumSorts();
Sort[] res = new Sort[n];
for (int i = 0; i < n; i++)
res[i] = Sort.create(getContext(),
Native.modelGetSort(getContext().nCtx(), getNativeObject(), i));
return res;
}
/**
* The finite set of distinct values that represent the interpretation for
* sort <paramref name="s"/>. <seealso cref="Sorts"/> <param name="s">An
* uninterpreted sort</param>
*
* @return An array of expressions, where each is an element of the universe
* of <paramref name="s"/>
* @throws Z3Exception
**/
public Expr[] getSortUniverse(Sort s) throws Z3Exception
{
ASTVector nUniv = new ASTVector(getContext(), Native.modelGetSortUniverse(
getContext().nCtx(), getNativeObject(), s.getNativeObject()));
int n = nUniv.size();
Expr[] res = new Expr[n];
for (int i = 0; i < n; i++)
res[i] = Expr.create(getContext(), nUniv.get(i).getNativeObject());
return res;
}
/**
* Conversion of models to strings.
*
* @return A string representation of the model.
**/
public String toString()
{
try
{
return Native.modelToString(getContext().nCtx(), getNativeObject());
} catch (Z3Exception e)
{
return "Z3Exception: " + e.getMessage();
}
}
Model(Context ctx, long obj) throws Z3Exception
{
super(ctx, obj);
}
void incRef(long o) throws Z3Exception
{
getContext().model_DRQ().incAndClear(getContext(), o);
super.incRef(o);
}
void decRef(long o) throws Z3Exception
{
getContext().model_DRQ().add(o);
super.decRef(o);
}
}