00001
00018 package com.microsoft.z3;
00019
00020 import com.microsoft.z3.enumerations.Z3_sort_kind;
00021
00025 public class Model extends Z3Object
00026 {
00036 public Expr getConstInterp(Expr a)
00037 {
00038 getContext().checkContextMatch(a);
00039 return getConstInterp(a.getFuncDecl());
00040 }
00041
00051 public Expr getConstInterp(FuncDecl f)
00052 {
00053 getContext().checkContextMatch(f);
00054 if (f.getArity() != 0
00055 || Native.getSortKind(getContext().nCtx(),
00056 Native.getRange(getContext().nCtx(), f.getNativeObject())) == Z3_sort_kind.Z3_ARRAY_SORT
00057 .toInt())
00058 throw new Z3Exception(
00059 "Non-zero arity functions and arrays have FunctionInterpretations as a model. Use FuncInterp.");
00060
00061 long n = Native.modelGetConstInterp(getContext().nCtx(), getNativeObject(),
00062 f.getNativeObject());
00063 if (n == 0)
00064 return null;
00065 else
00066 return Expr.create(getContext(), n);
00067 }
00068
00077 public FuncInterp getFuncInterp(FuncDecl f)
00078 {
00079 getContext().checkContextMatch(f);
00080
00081 Z3_sort_kind sk = Z3_sort_kind.fromInt(Native.getSortKind(getContext()
00082 .nCtx(), Native.getRange(getContext().nCtx(), f.getNativeObject())));
00083
00084 if (f.getArity() == 0)
00085 {
00086 long n = Native.modelGetConstInterp(getContext().nCtx(),
00087 getNativeObject(), f.getNativeObject());
00088
00089 if (sk == Z3_sort_kind.Z3_ARRAY_SORT)
00090 {
00091 if (n == 0)
00092 return null;
00093 else
00094 {
00095 if (Native.isAsArray(getContext().nCtx(), n) ^ true)
00096 throw new Z3Exception(
00097 "Argument was not an array constant");
00098 long fd = Native.getAsArrayFuncDecl(getContext().nCtx(), n);
00099 return getFuncInterp(new FuncDecl(getContext(), fd));
00100 }
00101 } else
00102 {
00103 throw new Z3Exception(
00104 "Constant functions do not have a function interpretation; use ConstInterp");
00105 }
00106 } else
00107 {
00108 long n = Native.modelGetFuncInterp(getContext().nCtx(),
00109 getNativeObject(), f.getNativeObject());
00110 if (n == 0)
00111 return null;
00112 else
00113 return new FuncInterp(getContext(), n);
00114 }
00115 }
00116
00120 public int getNumConsts()
00121 {
00122 return Native.modelGetNumConsts(getContext().nCtx(), getNativeObject());
00123 }
00124
00130 public FuncDecl[] getConstDecls()
00131 {
00132 int n = getNumConsts();
00133 FuncDecl[] res = new FuncDecl[n];
00134 for (int i = 0; i < n; i++)
00135 res[i] = new FuncDecl(getContext(), Native.modelGetConstDecl(getContext()
00136 .nCtx(), getNativeObject(), i));
00137 return res;
00138 }
00139
00143 public int getNumFuncs()
00144 {
00145 return Native.modelGetNumFuncs(getContext().nCtx(), getNativeObject());
00146 }
00147
00153 public FuncDecl[] getFuncDecls()
00154 {
00155 int n = getNumFuncs();
00156 FuncDecl[] res = new FuncDecl[n];
00157 for (int i = 0; i < n; i++)
00158 res[i] = new FuncDecl(getContext(), Native.modelGetFuncDecl(getContext()
00159 .nCtx(), getNativeObject(), i));
00160 return res;
00161 }
00162
00168 public FuncDecl[] getDecls()
00169 {
00170 int nFuncs = getNumFuncs();
00171 int nConsts = getNumConsts();
00172 int n = nFuncs + nConsts;
00173 FuncDecl[] res = new FuncDecl[n];
00174 for (int i = 0; i < nConsts; i++)
00175 res[i] = new FuncDecl(getContext(), Native.modelGetConstDecl(getContext()
00176 .nCtx(), getNativeObject(), i));
00177 for (int i = 0; i < nFuncs; i++)
00178 res[nConsts + i] = new FuncDecl(getContext(), Native.modelGetFuncDecl(
00179 getContext().nCtx(), getNativeObject(), i));
00180 return res;
00181 }
00182
00187 @SuppressWarnings("serial")
00188 public class ModelEvaluationFailedException extends Z3Exception
00189 {
00193 public ModelEvaluationFailedException()
00194 {
00195 super();
00196 }
00197 }
00198
00211 public Expr eval(Expr t, boolean completion)
00212 {
00213 Native.LongPtr v = new Native.LongPtr();
00214 if (Native.modelEval(getContext().nCtx(), getNativeObject(),
00215 t.getNativeObject(), (completion) ? true : false, v) ^ true)
00216 throw new ModelEvaluationFailedException();
00217 else
00218 return Expr.create(getContext(), v.value);
00219 }
00220
00226 public Expr evaluate(Expr t, boolean completion)
00227 {
00228 return eval(t, completion);
00229 }
00230
00235 public int getNumSorts()
00236 {
00237 return Native.modelGetNumSorts(getContext().nCtx(), getNativeObject());
00238 }
00239
00251 public Sort[] getSorts()
00252 {
00253
00254 int n = getNumSorts();
00255 Sort[] res = new Sort[n];
00256 for (int i = 0; i < n; i++)
00257 res[i] = Sort.create(getContext(),
00258 Native.modelGetSort(getContext().nCtx(), getNativeObject(), i));
00259 return res;
00260 }
00261
00271 public Expr[] getSortUniverse(Sort s)
00272 {
00273
00274 ASTVector nUniv = new ASTVector(getContext(), Native.modelGetSortUniverse(
00275 getContext().nCtx(), getNativeObject(), s.getNativeObject()));
00276 return nUniv.ToExprArray();
00277 }
00278
00284 public String toString()
00285 {
00286 try
00287 {
00288 return Native.modelToString(getContext().nCtx(), getNativeObject());
00289 } catch (Z3Exception e)
00290 {
00291 return "Z3Exception: " + e.getMessage();
00292 }
00293 }
00294
00295 Model(Context ctx, long obj)
00296 {
00297 super(ctx, obj);
00298 }
00299
00300 void incRef(long o)
00301 {
00302 getContext().getModelDRQ().incAndClear(getContext(), o);
00303 super.incRef(o);
00304 }
00305
00306 void decRef(long o)
00307 {
00308 getContext().getModelDRQ().add(o);
00309 super.decRef(o);
00310 }
00311 }