Search in sources :

Example 1 with Pair

use of com.ge.verdict.synthesis.util.Pair 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 Pair

use of com.ge.verdict.synthesis.util.Pair 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 Pair

use of com.ge.verdict.synthesis.util.Pair 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 Pair

use of com.ge.verdict.synthesis.util.Pair in project VERDICT by ge-high-assurance.

the class VerdictSynthesis method addExtraImplDefenses.

/**
 * Returns a new results instance with any extraneous defenses from the provided list added to
 * the results items liset as removals.
 *
 * @param results the input results to use as a baseline
 * @param implCompDefPairs the set of all implemented component-defense pairs
 * @param costModel the cost model
 * @return a new results instance with the extraneous defenses included
 */
public static ResultsInstance addExtraImplDefenses(ResultsInstance results, Map<com.ge.verdict.attackdefensecollector.Pair<String, String>, Integer> implCompDefPairs, CostModel costModel) {
    List<ResultsInstance.Item> items = new ArrayList<>(results.items);
    Fraction inputCost = results.inputCost;
    Fraction outputCost = results.outputCost;
    // find all of the pairs already in the defense tree
    Set<com.ge.verdict.attackdefensecollector.Pair<String, String>> accountedCompDefPairs = results.items.stream().map(item -> new com.ge.verdict.attackdefensecollector.Pair<>(item.component, item.defenseProperty)).collect(Collectors.toSet());
    for (Map.Entry<com.ge.verdict.attackdefensecollector.Pair<String, String>, Integer> entry : implCompDefPairs.entrySet()) {
        // only add if not already accounted for
        if (!accountedCompDefPairs.contains(entry.getKey())) {
            String comp = entry.getKey().left;
            String defProp = entry.getKey().right;
            int implDal = entry.getValue();
            // compute cost of the implemented pair
            Fraction pairInputCost = costModel.cost(defProp, comp, implDal);
            Fraction pairOutputCost = costModel.cost(defProp, comp, 0);
            // the item will be a removal, so from implDal -> 0 DAL
            items.add(new ResultsInstance.Item(comp, defProp, implDal, 0, pairInputCost, pairOutputCost));
            inputCost = inputCost.add(pairInputCost);
            // this may seem silly, but in theory we can have a non-zero cost for DAL 0
            outputCost = outputCost.add(pairOutputCost);
        }
    }
    return new ResultsInstance(results.partialSolution, results.meritAssignment, results.inputSat, inputCost, outputCost, items);
}
Also used : IntStream(java.util.stream.IntStream) Optimize(com.microsoft.z3.Optimize) Context(com.microsoft.z3.Context) Formula(org.logicng.formulas.Formula) Assignment(org.logicng.datastructures.Assignment) MaxSATSolver(org.logicng.solvers.MaxSATSolver) RatNum(com.microsoft.z3.RatNum) ArrayList(java.util.ArrayList) Map(java.util.Map) FormulaFactory(org.logicng.formulas.FormulaFactory) BoolExpr(com.microsoft.z3.BoolExpr) Status(com.microsoft.z3.Status) DTree(com.ge.verdict.synthesis.dtree.DTree) ArithExpr(com.microsoft.z3.ArithExpr) LinkedHashSet(java.util.LinkedHashSet) DLeaf(com.ge.verdict.synthesis.dtree.DLeaf) PrintWriter(java.io.PrintWriter) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) Model(com.microsoft.z3.Model) List(java.util.List) Fraction(org.apache.commons.math3.fraction.Fraction) ArithmeticUtils(org.apache.commons.math3.util.ArithmeticUtils) Pair(com.ge.verdict.synthesis.util.Pair) ResultsInstance(com.ge.verdict.vdm.synthesis.ResultsInstance) Expr(com.microsoft.z3.Expr) Optional(java.util.Optional) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) Fraction(org.apache.commons.math3.fraction.Fraction) ResultsInstance(com.ge.verdict.vdm.synthesis.ResultsInstance) Map(java.util.Map) Pair(com.ge.verdict.synthesis.util.Pair)

Example 5 with Pair

use of com.ge.verdict.synthesis.util.Pair in project VERDICT by ge-high-assurance.

the class CostModel method load.

/**
 * Loads the cost model from XML.
 *
 * @param costModelXml
 */
private void load(File costModelXml) {
    try {
        DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document xml = parser.parse(costModelXml);
        for (Element rule : extractElements(xml.getDocumentElement(), "cost")) {
            String component = rule.getAttribute("component");
            String defense = rule.getAttribute("defense");
            String dalStr = rule.getAttribute("dal");
            if (component.contains(":::")) {
                // TODO this is temporary, until we support fully qualified names throughout the
                // tool. basically we can just remove this once qualified names are supported
                String[] parts = component.split(":::");
                if (parts.length != 2) {
                    throw new RuntimeException("invalid number of parts in qualified component name (should be 2): " + component);
                }
                component = parts[1];
            }
            for (int i = 0; i < rule.getAttributes().getLength(); i++) {
                String name = rule.getAttributes().item(i).getNodeName();
                if (!name.equals("component") && !name.equals("defense") && !name.equals("dal")) {
                    throw new ParseException("Unrecognized tag: " + name);
                }
            }
            Fraction cost = parseCost(rule.getTextContent());
            if (isEmptyStr(component) && isEmptyStr(defense) && isEmptyStr(dalStr)) {
                defaultModel = cost;
            } else if (isEmptyStr(component) && isEmptyStr(defense)) {
                dalModel.put(parseDal(dalStr), cost);
            } else if (isEmptyStr(component) && isEmptyStr(dalStr)) {
                defModel.put(defense, cost);
            } else if (isEmptyStr(defense) && isEmptyStr(dalStr)) {
                compModel.put(component, cost);
            } else if (isEmptyStr(component)) {
                defDalModel.put(new Pair<>(defense, parseDal(dalStr)), cost);
            } else if (isEmptyStr(defense)) {
                compDalModel.put(new Pair<>(component, parseDal(dalStr)), cost);
            } else if (isEmptyStr(dalStr)) {
                compDefModel.put(new Pair<>(component, defense), cost);
            } else {
                compDefDalModel.put(new Triple<>(component, defense, parseDal(dalStr)), cost);
            }
        }
    } catch (IOException | SAXException | ParserConfigurationException e) {
        throw new ParseException(e);
    }
}
Also used : Element(org.w3c.dom.Element) Fraction(org.apache.commons.math3.fraction.Fraction) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Pair(com.ge.verdict.synthesis.util.Pair)

Aggregations

Pair (com.ge.verdict.synthesis.util.Pair)7 Fraction (org.apache.commons.math3.fraction.Fraction)6 DLeaf (com.ge.verdict.synthesis.dtree.DLeaf)5 ComponentDefense (com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense)5 DTree (com.ge.verdict.synthesis.dtree.DTree)4 ADOr (com.ge.verdict.attackdefensecollector.adtree.ADOr)3 ADTree (com.ge.verdict.attackdefensecollector.adtree.ADTree)3 Optional (java.util.Optional)3 Set (java.util.Set)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 BoolExpr (com.microsoft.z3.BoolExpr)2 Context (com.microsoft.z3.Context)2 Expr (com.microsoft.z3.Expr)2 Model (com.microsoft.z3.Model)2