Z3
 
Loading...
Searching...
No Matches
Model.java
Go to the documentation of this file.
1
18package com.microsoft.z3;
19
20import com.microsoft.z3.enumerations.Z3_sort_kind;
21
22import java.lang.ref.ReferenceQueue;
23
27@SuppressWarnings("unchecked")
28public class Model extends Z3Object {
38 public <R extends Sort> Expr<R> getConstInterp(Expr<R> a)
39 {
40 getContext().checkContextMatch(a);
41 return getConstInterp(a.getFuncDecl());
42 }
43
53 public <R extends Sort> Expr<R> getConstInterp(FuncDecl<R> f)
54 {
55 getContext().checkContextMatch(f);
56 if (f.getArity() != 0)
57 throw new Z3Exception(
58 "Non-zero arity functions have FunctionInterpretations as a model. Use getFuncInterp.");
59
60 long n = Native.modelGetConstInterp(getContext().nCtx(), getNativeObject(),
61 f.getNativeObject());
62 if (n == 0)
63 return null;
64 else
65 return (Expr<R>) Expr.create(getContext(), n);
66 }
67
76 public <R extends Sort> FuncInterp<R> getFuncInterp(FuncDecl<R> f)
77 {
78 getContext().checkContextMatch(f);
79
80 Z3_sort_kind sk = Z3_sort_kind.fromInt(Native.getSortKind(getContext()
81 .nCtx(), Native.getRange(getContext().nCtx(), f.getNativeObject())));
82
83 if (f.getArity() == 0)
84 {
85 long n = Native.modelGetConstInterp(getContext().nCtx(),
86 getNativeObject(), f.getNativeObject());
87
88 if (sk == Z3_sort_kind.Z3_ARRAY_SORT)
89 {
90 if (n == 0)
91 return null;
92 else
93 {
94 if (Native.isAsArray(getContext().nCtx(), n)) {
95 long fd = Native.getAsArrayFuncDecl(getContext().nCtx(), n);
96 return getFuncInterp(new FuncDecl<>(getContext(), fd));
97 }
98 return null;
99 }
100 } else
101 {
102 throw new Z3Exception(
103 "Constant functions do not have a function interpretation; use getConstInterp");
104 }
105 } else
106 {
107 long n = Native.modelGetFuncInterp(getContext().nCtx(),
108 getNativeObject(), f.getNativeObject());
109 if (n == 0)
110 return null;
111 else
112 return new FuncInterp<>(getContext(), n);
113 }
114 }
115
119 public int getNumConsts()
120 {
121 return Native.modelGetNumConsts(getContext().nCtx(), getNativeObject());
122 }
123
130 {
131 int n = getNumConsts();
132 FuncDecl<?>[] res = new FuncDecl[n];
133 for (int i = 0; i < n; i++)
134 res[i] = new FuncDecl<>(getContext(), Native.modelGetConstDecl(getContext()
135 .nCtx(), getNativeObject(), i));
136 return res;
137 }
138
142 public int getNumFuncs()
143 {
144 return Native.modelGetNumFuncs(getContext().nCtx(), getNativeObject());
145 }
146
153 {
154 int n = getNumFuncs();
155 FuncDecl<?>[] res = new FuncDecl[n];
156 for (int i = 0; i < n; i++)
157 res[i] = new FuncDecl<>(getContext(), Native.modelGetFuncDecl(getContext()
158 .nCtx(), getNativeObject(), i));
159 return res;
160 }
161
168 {
169 int nFuncs = getNumFuncs();
170 int nConsts = getNumConsts();
171 int n = nFuncs + nConsts;
172 FuncDecl<?>[] res = new FuncDecl[n];
173 for (int i = 0; i < nConsts; i++)
174 res[i] = new FuncDecl<>(getContext(), Native.modelGetConstDecl(getContext()
175 .nCtx(), getNativeObject(), i));
176 for (int i = 0; i < nFuncs; i++)
177 res[nConsts + i] = new FuncDecl<>(getContext(), Native.modelGetFuncDecl(
178 getContext().nCtx(), getNativeObject(), i));
179 return res;
180 }
181
186 @SuppressWarnings("serial")
188 {
193 {
194 super();
195 }
196 }
197
211 public <R extends Sort> Expr<R> eval(Expr<R> t, boolean completion)
212 {
213 Native.LongPtr v = new Native.LongPtr();
214 if (!Native.modelEval(getContext().nCtx(), getNativeObject(),
215 t.getNativeObject(), (completion), v))
216 throw new ModelEvaluationFailedException();
217 else
218 return (Expr<R>) Expr.create(getContext(), v.value);
219 }
220
226 public <R extends Sort> Expr<R> evaluate(Expr<R> t, boolean completion)
227 {
228 return eval(t, completion);
229 }
230
235 public int getNumSorts()
236 {
237 return Native.modelGetNumSorts(getContext().nCtx(), getNativeObject());
238 }
239
251 public Sort[] getSorts()
252 {
253
254 int n = getNumSorts();
255 Sort[] res = new Sort[n];
256 for (int i = 0; i < n; i++)
257 res[i] = Sort.create(getContext(),
258 Native.modelGetSort(getContext().nCtx(), getNativeObject(), i));
259 return res;
260 }
261
271 public <R extends Sort> Expr<R>[] getSortUniverse(R s)
272 {
273
274 ASTVector nUniv = new ASTVector(getContext(), Native.modelGetSortUniverse(
275 getContext().nCtx(), getNativeObject(), s.getNativeObject()));
276 return (Expr<R>[]) nUniv.ToExprArray();
277 }
278
284 @Override
285 public String toString() {
286 return Native.modelToString(getContext().nCtx(), getNativeObject());
287 }
288
289 Model(Context ctx, long obj)
290 {
291 super(ctx, obj);
292 }
293
294 @Override
295 void incRef() {
296 Native.modelIncRef(getContext().nCtx(), getNativeObject());
297 }
298
299 @Override
300 void addToReferenceQueue() {
301 getContext().getReferenceQueue().storeReference(this, ModelRef::new);
302 }
303
304 private static class ModelRef extends Z3ReferenceQueue.Reference<Model> {
305
306 private ModelRef(Model referent, ReferenceQueue<Z3Object> q) {
307 super(referent, q);
308 }
309
310 @Override
311 void decRef(Context ctx, long z3Obj) {
312 Native.modelDecRef(ctx.nCtx(), z3Obj);
313 }
314 }
315}
FuncDecl< R > getFuncDecl()
Definition Expr.java:76
FuncDecl<?>[] getFuncDecls()
Definition Model.java:152
FuncDecl<?>[] getConstDecls()
Definition Model.java:129
FuncDecl<?>[] getDecls()
Definition Model.java:167
Z3_sort_kind
The different kinds of Z3 types (See Z3_get_sort_kind).
Definition z3_api.h:110