Search in sources :

Example 1 with ComponentDefense

use of com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense in project VERDICT by ge-high-assurance.

the class VerdictSynthesisTest method partialSolutionTest.

@Test
public void partialSolutionTest() {
    CostModel costModel = new CostModel(new File(getClass().getResource("partialCosts.xml").getPath()));
    int dal = 2;
    SystemModel system = new SystemModel("C1");
    Attack attack1 = new Attack(system.getAttackable(), "A1", "An attack", Prob.certain(), CIA.I);
    Defense defense1 = new Defense(attack1);
    defense1.addDefenseClause(Collections.singletonList(new Defense.DefenseLeaf("D1", Optional.of(new com.ge.verdict.attackdefensecollector.Pair<>("D1", 1)))));
    ADTree adtree = new ADOr(new ADAnd(new ADNot(defense1), attack1));
    for (Approach approach : Approach.values()) {
        {
            DLeaf.Factory factoryPartial = new DLeaf.Factory();
            Optional<Pair<Set<ComponentDefense>, Double>> resultPartial = VerdictSynthesis.performSynthesisSingle(DTreeConstructor.construct(adtree, costModel, dal, true, false, factoryPartial), dal, factoryPartial, approach);
            Assertions.assertThat(resultPartial.isPresent());
            Assertions.assertThat(resultPartial.get().right).isEqualTo(1);
        }
        {
            DLeaf.Factory factoryTotal = new DLeaf.Factory();
            Optional<Pair<Set<ComponentDefense>, Double>> resultTotal = VerdictSynthesis.performSynthesisSingle(DTreeConstructor.construct(adtree, costModel, dal, false, false, factoryTotal), dal, factoryTotal, approach);
            Assertions.assertThat(resultTotal.isPresent());
            Assertions.assertThat(resultTotal.get().right).isEqualTo(2);
        }
    }
}
Also used : DLeaf(com.ge.verdict.synthesis.dtree.DLeaf) Set(java.util.Set) ADNot(com.ge.verdict.attackdefensecollector.adtree.ADNot) Optional(java.util.Optional) ADAnd(com.ge.verdict.attackdefensecollector.adtree.ADAnd) Attack(com.ge.verdict.attackdefensecollector.adtree.Attack) Defense(com.ge.verdict.attackdefensecollector.adtree.Defense) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) ADTree(com.ge.verdict.attackdefensecollector.adtree.ADTree) SystemModel(com.ge.verdict.attackdefensecollector.model.SystemModel) Approach(com.ge.verdict.synthesis.VerdictSynthesis.Approach) ADOr(com.ge.verdict.attackdefensecollector.adtree.ADOr) File(java.io.File) Pair(com.ge.verdict.synthesis.util.Pair) Test(org.junit.Test)

Example 2 with ComponentDefense

use of com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense in project VERDICT by ge-high-assurance.

the class VerdictSynthesisTest method unmitigatedMixedTest.

@Test
public void unmitigatedMixedTest() {
    DLeaf.Factory factory = new DLeaf.Factory();
    SystemModel system = new SystemModel("S1");
    int targetDal = 1;
    Fraction[] costs = Util.fractionCosts(new double[] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 });
    DLeaf dleaf = new DLeaf("S1", "D1", "A2", 0, targetDal, costs, factory);
    DTree dtree = new DOr(new ALeaf(new Attack(system.getAttackable(), "A1", "An attack", Prob.certain(), CIA.I)), dleaf);
    for (Approach approach : Approach.values()) {
        Optional<Pair<Set<ComponentDefense>, Double>> result = VerdictSynthesis.performSynthesisSingle(dtree, targetDal, factory, approach);
        Assertions.assertThat(result.isPresent());
        Assertions.assertThat(result.get().left).hasSize(1);
        Assertions.assertThat(result.get().left).contains(dleaf.componentDefense);
        Assertions.assertThat(result.get().right).isEqualTo(5);
    }
}
Also used : DLeaf(com.ge.verdict.synthesis.dtree.DLeaf) DTree(com.ge.verdict.synthesis.dtree.DTree) ADTree(com.ge.verdict.attackdefensecollector.adtree.ADTree) ADOr(com.ge.verdict.attackdefensecollector.adtree.ADOr) DOr(com.ge.verdict.synthesis.dtree.DOr) ALeaf(com.ge.verdict.synthesis.dtree.ALeaf) Fraction(org.apache.commons.math3.fraction.Fraction) Attack(com.ge.verdict.attackdefensecollector.adtree.Attack) SystemModel(com.ge.verdict.attackdefensecollector.model.SystemModel) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) Approach(com.ge.verdict.synthesis.VerdictSynthesis.Approach) Pair(com.ge.verdict.synthesis.util.Pair) Test(org.junit.Test)

Example 3 with ComponentDefense

use of com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense in project VERDICT by ge-high-assurance.

the class VerdictSynthesisTest method performSynthesisTestInternal.

private void performSynthesisTestInternal(int[] costs, String[] selected, Approach approach) {
    if (costs.length != 3) {
        throw new RuntimeException("hey!");
    }
    DLeaf.Factory factory = new DLeaf.Factory();
    double[] doubleCosts0 = new double[10];
    double[] doubleCosts1 = new double[10];
    double[] doubleCosts2 = new double[10];
    for (int i = 0; i < 10; i++) {
        doubleCosts0[i] = costs[0];
        doubleCosts1[i] = costs[1];
        doubleCosts2[i] = costs[2];
    }
    Fraction[] fractionCosts0 = Util.fractionCosts(doubleCosts0);
    Fraction[] fractionCosts1 = Util.fractionCosts(doubleCosts1);
    Fraction[] fractionCosts2 = Util.fractionCosts(doubleCosts2);
    int targetDal = 1;
    DLeaf cd1 = new DLeaf("C1", "D1", "A1", 0, targetDal, fractionCosts0, factory);
    DLeaf cd2 = new DLeaf("C2", "D2", "A2", 0, targetDal, fractionCosts1, factory);
    DLeaf cd3 = new DLeaf("C3", "D3", "A3", 0, targetDal, fractionCosts2, factory);
    DTree tree = new DOr(new DAnd(cd1, cd2), new DAnd(cd2, cd3), new DAnd(cd1, cd3));
    Optional<Pair<Set<ComponentDefense>, Double>> output = VerdictSynthesis.performSynthesisSingle(tree, targetDal, factory, approach);
    Assertions.assertThat(output.isPresent()).isTrue();
    Assertions.assertThat(output.get().left.size()).isEqualTo(selected.length);
    for (String comp : selected) {
        Assertions.assertThat(output.get().left.stream()).withFailMessage("Expected: " + stringOfArray(selected) + ", output: " + stringOfIterable(output.get().left) + ", does not contain component: " + comp + ", approach: " + approach.toString()).anyMatch(pair -> pair.component.equals(comp));
    }
}
Also used : DLeaf(com.ge.verdict.synthesis.dtree.DLeaf) DTree(com.ge.verdict.synthesis.dtree.DTree) ADTree(com.ge.verdict.attackdefensecollector.adtree.ADTree) ADOr(com.ge.verdict.attackdefensecollector.adtree.ADOr) DOr(com.ge.verdict.synthesis.dtree.DOr) Fraction(org.apache.commons.math3.fraction.Fraction) ADAnd(com.ge.verdict.attackdefensecollector.adtree.ADAnd) DAnd(com.ge.verdict.synthesis.dtree.DAnd) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) Pair(com.ge.verdict.synthesis.util.Pair)

Example 4 with ComponentDefense

use of com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense in project VERDICT by ge-high-assurance.

the class VerdictSynthesis method performSynthesisMaxSmt.

/**
 * Perform synthesis using Z3 MaxSMT.
 *
 * @param tree
 * @param targetDal
 * @param factory
 * @return
 * @deprecated use the multi-requirement approach instead
 */
@Deprecated
public static Optional<Pair<Set<ComponentDefense>, Double>> performSynthesisMaxSmt(DTree tree, int targetDal, DLeaf.Factory factory) {
    Context context = new Context();
    Optimize optimizer = context.mkOptimize();
    Collection<ComponentDefense> pairs = factory.allComponentDefensePairs();
    int costLcd = normalizeCosts(pairs);
    for (ComponentDefense pair : pairs) {
        if (pair.dalToNormCost(targetDal) > 0) {
            // this id ("cover") doesn't matter but we have to specify something
            optimizer.AssertSoft(context.mkNot(pair.toZ3(context)), pair.dalToNormCost(targetDal), "cover");
        }
    }
    optimizer.Assert(tree.toZ3(context));
    if (optimizer.Check().equals(Status.SATISFIABLE)) {
        Set<ComponentDefense> output = new LinkedHashSet<>();
        int totalNormalizedCost = 0;
        Model model = optimizer.getModel();
        for (ComponentDefense pair : pairs) {
            Expr expr = model.eval(pair.toZ3(context), true);
            switch(expr.getBoolValue()) {
                case Z3_L_TRUE:
                    output.add(pair);
                    totalNormalizedCost += pair.dalToNormCost(targetDal);
                    break;
                case Z3_L_FALSE:
                    break;
                case Z3_L_UNDEF:
                default:
                    throw new RuntimeException("Synthesis: Undefined variable in output model: " + pair.toString());
            }
        }
        return Optional.of(new Pair<>(output, ((double) totalNormalizedCost) / costLcd));
    } else {
        System.err.println("Synthesis: SMT not satisfiable, perhaps there are unmitigatable attacks");
        return Optional.empty();
    }
}
Also used : Context(com.microsoft.z3.Context) LinkedHashSet(java.util.LinkedHashSet) BoolExpr(com.microsoft.z3.BoolExpr) ArithExpr(com.microsoft.z3.ArithExpr) Expr(com.microsoft.z3.Expr) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) Model(com.microsoft.z3.Model) Optimize(com.microsoft.z3.Optimize)

Example 5 with ComponentDefense

use of com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense in project VERDICT by ge-high-assurance.

the class VerdictSynthesis method performSynthesisMaxSat.

/**
 * Perform synthesis using LogicNG MaxSAT.
 *
 * @param tree
 * @param targetDal
 * @param dleafFactory
 * @return
 * @deprecated use the multi-requirement approach instead
 */
@Deprecated
public static Optional<Pair<Set<ComponentDefense>, Double>> performSynthesisMaxSat(DTree tree, int targetDal, DLeaf.Factory dleafFactory) {
    Collection<ComponentDefense> pairs = dleafFactory.allComponentDefensePairs();
    FormulaFactory factory = new FormulaFactory();
    MaxSATSolver solver = MaxSATSolver.wbo();
    Formula cnf = tree.toLogicNG(factory).cnf();
    int costLcd = normalizeCosts(pairs);
    for (ComponentDefense pair : pairs) {
        if (pair.dalToNormCost(targetDal) > 0) {
            solver.addSoftFormula(factory.not(pair.toLogicNG(factory)), pair.dalToNormCost(targetDal));
        }
    }
    // implicitly converts formula to CNF
    solver.addHardFormula(cnf);
    switch(solver.solve()) {
        case OPTIMUM:
            Set<ComponentDefense> output = new LinkedHashSet<>();
            int totalNormalizedCost = 0;
            Assignment model = solver.model();
            for (ComponentDefense pair : pairs) {
                if (model.evaluateLit(pair.toLogicNG(factory))) {
                    output.add(pair);
                    totalNormalizedCost += pair.dalToNormCost(targetDal);
                }
            }
            return Optional.of(new Pair<>(output, ((double) totalNormalizedCost) / costLcd));
        case UNDEF:
            System.err.println("Synthesis: SAT undefined, is input tree valid?");
            return Optional.empty();
        case UNSATISFIABLE:
            System.err.println("Synthesis: SAT not satisfiable, perhaps there are unmitigatable attacks");
            return Optional.empty();
        default:
            throw new RuntimeException("impossible");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) MaxSATSolver(org.logicng.solvers.MaxSATSolver)

Aggregations

ComponentDefense (com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense)8 DLeaf (com.ge.verdict.synthesis.dtree.DLeaf)5 Fraction (org.apache.commons.math3.fraction.Fraction)5 Pair (com.ge.verdict.synthesis.util.Pair)4 ADOr (com.ge.verdict.attackdefensecollector.adtree.ADOr)3 ADTree (com.ge.verdict.attackdefensecollector.adtree.ADTree)3 DTree (com.ge.verdict.synthesis.dtree.DTree)3 Context (com.microsoft.z3.Context)3 Model (com.microsoft.z3.Model)3 Optimize (com.microsoft.z3.Optimize)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 Test (org.junit.Test)3 ADAnd (com.ge.verdict.attackdefensecollector.adtree.ADAnd)2 Attack (com.ge.verdict.attackdefensecollector.adtree.Attack)2 SystemModel (com.ge.verdict.attackdefensecollector.model.SystemModel)2 Approach (com.ge.verdict.synthesis.VerdictSynthesis.Approach)2 DOr (com.ge.verdict.synthesis.dtree.DOr)2 ResultsInstance (com.ge.verdict.vdm.synthesis.ResultsInstance)2 ArithExpr (com.microsoft.z3.ArithExpr)2