update verigraph
[parser.git] / verigraph / src / it / polito / verigraph / mcnet / netobjs / PolitoFieldModifier.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Politecnico di Torino and others.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Apache License, Version 2.0
6  * which accompanies this distribution, and is available at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *******************************************************************************/
9 package it.polito.verigraph.mcnet.netobjs;
10
11 import java.util.ArrayList;
12 import java.util.List;
13 import com.microsoft.z3.BoolExpr;
14 import com.microsoft.z3.Context;
15 import com.microsoft.z3.DatatypeExpr;
16 import com.microsoft.z3.Expr;
17 import com.microsoft.z3.IntExpr;
18 import com.microsoft.z3.Solver;
19 import it.polito.verigraph.mcnet.components.NetContext;
20 import it.polito.verigraph.mcnet.components.Network;
21 import it.polito.verigraph.mcnet.components.NetworkObject;
22
23 public class PolitoFieldModifier extends NetworkObject {
24
25     List<BoolExpr> constraints;
26     Context ctx;
27     DatatypeExpr politoFieldModifier;
28     Network net;
29     NetContext nctx;
30
31     public PolitoFieldModifier(Context ctx, Object[]... args) {
32         super(ctx, args);
33     }
34
35     @Override
36     public DatatypeExpr getZ3Node() {
37         return politoFieldModifier;
38     }
39
40     @Override
41     protected void init(Context ctx, Object[]... args) {
42         this.ctx = ctx;
43         isEndHost=false;
44         constraints = new ArrayList<BoolExpr>();
45         z3Node = ((NetworkObject)args[0][0]).getZ3Node();
46         politoFieldModifier = z3Node;
47         net = (Network)args[0][1];
48         nctx = (NetContext)args[0][2];
49     }
50
51     @Override
52     protected void addConstraints(Solver solver) {
53         BoolExpr[] constr = new BoolExpr[constraints.size()];
54         solver.add(constraints.toArray(constr));
55     }
56
57     public void installFieldModifier (){
58         Expr x = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_x", nctx.node);
59         Expr y = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_y", nctx.node);
60
61         Expr p_0 = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_p_0", nctx.packet);
62         Expr p_1 = ctx.mkConst("politoFieldModifier_"+politoFieldModifier+"_p_1", nctx.packet);
63
64         IntExpr t_0 = ctx.mkIntConst("politoFieldModifier_"+politoFieldModifier+"_t_0");
65         IntExpr t_1 = ctx.mkIntConst("politoFieldModifier_"+politoFieldModifier+"_t_1");
66
67         constraints.add(
68                 ctx.mkForall(new Expr[]{t_0, p_0, x},
69                         ctx.mkImplies((BoolExpr)nctx.send.apply(politoFieldModifier,x,p_0,t_0),
70                                 ctx.mkExists(new Expr[]{y, p_1, t_1},
71                                         ctx.mkAnd(ctx.mkLt(t_1, t_0),
72                                                 (BoolExpr)nctx.recv.apply(y, politoFieldModifier, p_1, t_1),
73                                                 ctx.mkEq(nctx.pf.get("encrypted").apply(p_0), nctx.pf.get("encrypted").apply(p_1)),
74                                                 ctx.mkEq(nctx.pf.get("src").apply(p_0), nctx.pf.get("src").apply(p_1)),
75                                                 ctx.mkEq(nctx.pf.get("dest").apply(p_0), nctx.pf.get("dest").apply(p_1)),
76                                                 ctx.mkEq(nctx.pf.get("inner_src").apply(p_0), nctx.pf.get("inner_src").apply(p_1)),
77                                                 ctx.mkEq(nctx.pf.get("inner_dest").apply(p_0), nctx.pf.get("inner_dest").apply(p_1)),
78                                                 ctx.mkEq(nctx.pf.get("origin").apply(p_0), nctx.pf.get("origin").apply(p_1)),
79                                                 ctx.mkEq(nctx.pf.get("orig_body").apply(p_0), nctx.pf.get("orig_body").apply(p_1)),
80                                                 ctx.mkEq(nctx.pf.get("body").apply(p_0), nctx.pf.get("body").apply(p_1)),
81                                                 ctx.mkEq(nctx.pf.get("seq").apply(p_0), nctx.pf.get("seq").apply(p_1)),
82                                                 ctx.mkEq(nctx.pf.get("proto").apply(p_0), nctx.pf.get("proto").apply(p_1)),
83                                                 ctx.mkEq(nctx.pf.get("emailFrom").apply(p_0), nctx.pf.get("emailFrom").apply(p_1)),
84                                                 ctx.mkNot(ctx.mkEq(nctx.pf.get("url").apply(p_0), nctx.pf.get("url").apply(p_1))),
85                                                 ctx.mkEq(nctx.pf.get("options").apply(p_0), nctx.pf.get("options").apply(p_1))),1,null,null,null,null)),
86                         1,null,null,null,null));
87     }
88
89 }