简体   繁体   中英

How to define a function in Z3 java API?

I am learning to run the similar codes in the matching case, which is for matching the trucks for transportation( https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/nbjorner-nuz.pdf ) in JAVA API. There is a code for defining the max function:

(define-fun imax ((a Int) (b Int)) Int (if (> a b) a b))

I translate that to Java:

     public static ArithExpr maxFunc(ArithExpr a, ArithExpr b)
     {
         Context ctx = new Context();
         ArithExpr result = (ArithExpr) ctx.mkITE(ctx.mkGe(a, b), a, b);
         return result;
     }

And try to use it in the same way(the following is just a demo)

        ArithExpr per1 = maxFunc(X0101, maxFunc(X0201, maxFunc(X0301, maxFunc(X0401, X0501))));
        BoolExpr minPer1 = ctx.mkEq(per1, constant0);
        o.AssertSoft(minPer1,1 , "a");

There are errors for the above codes:

Exception in thread "main" com.microsoft.z3.Z3Exception: Context mismatch

The error refers to the function body:

ArithExpr result = (ArithExpr) ctx.mkITE(ctx.mkGe(a, b), a, b);

I definitely have doubts in constructing the z3 function in JAVA. Does there exist special function form for Z3 Java API? How to fix the function?

"Context mismatch" means that you're trying to use expressions from multiple contexts together, which is not supported. Instead of Context ctx = new Context(); , make sure you use the same context that you use in the rest of your program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM