1 /*******************************************************************************
2 * Copyright (c) 2017 Politecnico di Torino and others.
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;
11 import java.util.ArrayList;
12 import java.util.List;
14 import com.microsoft.z3.BoolExpr;
15 import com.microsoft.z3.Context;
16 import com.microsoft.z3.DatatypeExpr;
17 import com.microsoft.z3.Expr;
18 import com.microsoft.z3.FuncDecl;
19 import com.microsoft.z3.IntExpr;
20 import com.microsoft.z3.Solver;
21 import com.microsoft.z3.Sort;
23 import it.polito.verigraph.mcnet.components.NetContext;
24 import it.polito.verigraph.mcnet.components.Network;
25 import it.polito.verigraph.mcnet.components.NetworkObject;
27 /** First example of custom network function: a simple filter
31 public class PolitoNF extends NetworkObject{
33 List<BoolExpr> constraints;
35 DatatypeExpr politoNF;
38 FuncDecl isInBlacklist;
41 public PolitoNF(Context ctx, Object[]... args) {
46 protected void init(Context ctx, Object[]... args) {
49 constraints = new ArrayList<BoolExpr>();
50 z3Node = ((NetworkObject)args[0][0]).getZ3Node();
52 net = (Network)args[0][1];
53 nctx = (NetContext)args[0][2];
57 public void politoNFRules (DatatypeExpr ipA,DatatypeExpr ipB){
58 // System.out.println("[PolitoNf] Installing rules");
59 Expr n_0 = ctx.mkConst("politoNF_"+politoNF+"_n_0", nctx.node);
60 Expr n_1 = ctx.mkConst("politoNF_"+politoNF+"_n_1", nctx.node);
61 Expr p_0 = ctx.mkConst("politoNF_"+politoNF+"_p_0", nctx.packet);
62 IntExpr t_0 = ctx.mkIntConst("politoNF_"+politoNF+"_t_0");
63 IntExpr t_1 = ctx.mkIntConst("politoNF_"+politoNF+"_t_1");
64 Expr a_0 = ctx.mkConst(politoNF+"_politoNF_a_0", nctx.address);
65 Expr a_1 = ctx.mkConst(politoNF+"_politoNF_a_1", nctx.address);
67 FuncDecl myFunction = ctx.mkFuncDecl(politoNF+"_myFunction", new Sort[]{nctx.address,nctx.address}, ctx.mkBoolSort());
69 BoolExpr myConstraint = ctx.mkForall(new Expr[]{n_0, p_0, t_0},
70 ctx.mkImplies((BoolExpr)nctx.send.apply(politoNF, n_0, p_0, t_0),
71 ctx.mkExists(new Expr[]{n_1, t_1},
72 ctx.mkAnd((BoolExpr)nctx.recv.apply(n_1, politoNF, p_0, t_1),
74 (BoolExpr)myFunction.apply(nctx.pf.get("src").apply(p_0), nctx.pf.get("dest").apply(p_0))),1,null,null,null,null)),1,null,null,null,null);
76 BoolExpr funcConstraint = ctx.mkOr(ctx.mkAnd(ctx.mkEq(a_0, ipA), ctx.mkEq(a_1, ipB)), ctx.mkAnd(ctx.mkEq(a_0,ipB), ctx.mkEq(a_1,ipA)));
78 // Constraint1myFunction(a_0,a_1) == ((a_0 == ipA && a_1 == ipB) || (a_0 == ipB && a_1 == ipA))
80 ctx.mkForall(new Expr[]{a_0,a_1},
81 ctx.mkEq(myFunction.apply(a_0, a_1), funcConstraint),1,null,null,null,null));
83 //Constraint2send(politoNF, n_0, p, t_0) ->
84 //(exist n_1,t_1 : (t_1 < t_0 && recv(n_1, politoNF, p, t_1) && myFunction(p.src,p.dest))
85 constraints.add(myConstraint);
90 protected void addConstraints(Solver solver) {
91 BoolExpr[] constr = new BoolExpr[constraints.size()];
92 solver.add(constraints.toArray(constr));
96 public DatatypeExpr getZ3Node() {