使用"适用于所有"使用Uninterpreted排序JAVA API

时间:2014-07-29 18:41:29

标签: java z3 z3py z3c.form

我正在尝试使用java API学习Z3,因为没有文档我一直在查看C API文档但是直到现在我找不到一个如何使用一些基本功能的明确示例。

我正在尝试编码此Z3代码(适用于在线版本)

;general options for getting values when sat
(set-option :produce-models true)
(set-option :produce-assignments true)

;declaring new sorts
(declare-sort Task)
(declare-sort User)

;function for assign an specific user
(declare-fun assignUser (Task) User)
;creating a relation between a task and a usert
(declare-fun TaskUser (Task User) Bool)

;stablishing order
(declare-fun mustPrecede (Task Task) Bool)

(assert(forall((t Task)) (not (mustPrecede t t))))
(assert(forall((t1 Task)(t2 Task)(t3 Task)) (implies (and (mustPrecede t1 t2)(mustPrecede t2 t3)) (mustPrecede t1 t3))))        

;asserting that all task must have one assigned user
(assert(forall((t Task)(u User)) (TaskUser t u)))
;asserting that all task must have one assigned user
;(assert(forall((t1 Task)(t2 Task)) (not(= (assignUser t1) (assignUser t2))))) 

到目前为止,我只是设法声明未解释的排序并声明我的函数如下

      HashMap<String, String> cfg = new HashMap<String, String>();
      cfg.put("proof", "true");
      cfg.put("auto-config", "false");
      Context ctx = new Context(cfg);

    //cfg.put("model", "true");
    Sort USER = ctx.mkUninterpretedSort("USER");
    Sort TASK = ctx.mkUninterpretedSort("TASK");

    FuncDecl assignUser = ctx.mkFuncDecl("assignUser", TASK, USER);
    FuncDecl TaskUser = ctx.mkFuncDecl("TaskUser", new Sort[] { TASK, USER }, ctx.mkBoolSort());
    FuncDecl mustPrecede = ctx.mkFuncDecl("mustPrecede", new Sort[]{TASK,TASK}, ctx.mkBoolSort());

但我找不到表达的例子

(assert(forall((t Task)) (not (mustPrecede t t))))
(assert(forall((t1 Task)(t2 Task)(t3 Task)) (implies (and (mustPrecede t1 t2) (mustPrecede t2 t3)) (mustPrecede t1 t3))))

;asserting that all task must have one assigned user
(assert(forall((t Task)(u User)) (TaskUser t u)))
;asserting that all task must have one assigned user
;(assert(forall((t1 Task)(t2 Task)) (not(= (assignUser t1) (assignUser t2))))) 

有人可以帮助我吗?这是用java API表达这个asserts-foralls的方法吗?

2 个答案:

答案 0 :(得分:2)

作为Z3代码示例的一部分,有一组带有各种Java调用的示例:

https://z3.codeplex.com/SourceControl/latest#examples/java/JavaExample.java

它们包括构建和检查量化公式。

答案 1 :(得分:1)

//creating uninterpreted sorts for function declaration
Sort User = ctx.mkUninterpretedSort("User");
Sort Task = ctx.mkUninterpretedSort("Task");
//function declaration
FuncDecl assignUser = ctx.mkFuncDecl("assignUser", Task, User);
FuncDecl TaskUser = ctx.mkFuncDecl("TaskUser", new Sort[] { Task, User }, ctx.mkBoolSort());
FuncDecl mustPrecede = ctx.mkFuncDecl("mustPrecede", new Sort[]{Task,Task}, ctx.mkBoolSort());

//task for using in quatifiers
Expr task = ctx.mkConst("t", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));

// creating (assert(forall((t Task)) (not (mustPrecede t t))))
//just one task is needed
Sort[] Tasks = new Sort[1];
Tasks[0] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
//setting the name for the task
Symbol[] namess = new Symbol[1];
namess[0] =  ctx.mkSymbol("t");
//Creating a map between mustPrecede and  its two parameters
Expr mtt = ctx.mkApp(mustPrecede, task,task);
//acreating not
Expr body = ctx.mkNot((BoolExpr)mtt);

Expr mustPrecedett = ctx.mkForall(Tasks, namess, body, 1, null, null,
ctx.mkSymbol("Q1"), ctx.mkSymbol("skid1"));

System.out.println("Quantifier mustPrecedett: " + mustPrecedett.toString());

//creating (assert(forall((t1 Task)(t2 Task)(t3 Task)) (implies (and (mustPrecede t1 t2)(mustPrecede t2 t3)) (mustPrecede t1 t3))))

//tree taks will be neede
Sort[] tTask = new Sort[3];
tTask[0] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
tTask[1] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
tTask[2] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));

//setting the names for the tasks
Symbol[] Tnames = new Symbol[3];
Tnames[0] =  ctx.mkSymbol("t1");
Tnames[1] =  ctx.mkSymbol("t2");
Tnames[2] =  ctx.mkSymbol("t3");

//creating tree diferent tasks for the relations
Expr t1 = ctx.mkConst("t1", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));
Expr t2 = ctx.mkConst("t2", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));
Expr t3 = ctx.mkConst("t3", ctx.mkUninterpretedSort(ctx.mkSymbol("Task")));
//creating mappins
Expr mt1t2 = ctx.mkApp(mustPrecede, t1,t2);
Expr mt2t3 = ctx.mkApp(mustPrecede, t2,t3);
Expr mt1t3 = ctx.mkApp(mustPrecede, t1,t3);
//Creating the relation between them        
Expr tbody2= ctx.mkImplies(ctx.mkAnd((BoolExpr)mt1t2,(BoolExpr) mt2t3), (BoolExpr) mt1t3);  
//building quatifier
Expr tra = ctx.mkForall(tTask, Tnames, tbody2, 1, null, null,ctx.mkSymbol("Q1"), ctx.mkSymbol("skid1"));

System.out.println("Quantifier tra: " + tra.toString());



//creating (assert(forall((t Task)(u User)) (TaskUser t u)))
//one user and one task
Sort[] userTask = new Sort[2];
userTask[0] =ctx.mkUninterpretedSort(ctx.mkSymbol("Task"));
userTask[1] =ctx.mkUninterpretedSort(ctx.mkSymbol("User"));
//setting names
Symbol[] userNames = new Symbol[2];
userNames[0] =  ctx.mkSymbol("t");
userNames[1] =  ctx.mkSymbol("u");
//creating one cost for the user        
Expr user = ctx.mkConst("u", ctx.mkUninterpretedSort(ctx.mkSymbol("User")));
//creating the relation between them and TaskUser funct
Expr uTask = ctx.mkApp(TaskUser, task,user);
//building quatificar
Expr userTaskExpr = ctx.mkForall(userTask, userNames, uTask, 1, null, null,ctx.mkSymbol("Q1"), ctx.mkSymbol("skid1"));

System.out.println("Quantifier userTaskExpr: " + userTaskExpr.toString());