Search in sources :

Example 1 with DNot

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

the class DTreeConstructor method constructInternal.

/**
 * Inductively-defined function over attack-defense trees.
 *
 * <p>The mapping from attack-defense tree to defense tree is pretty straightforward. One of the
 * most important things to note is that AND and OR nodes are transposed in the transformation
 * because they mean opposite things in a defense tree compared to an attack-defense tree. (An
 * attack-defense tree is "how to attack", whereas a defense tree is "how to defend".)
 *
 * @param adtree
 * @return
 */
private Optional<DTree> constructInternal(ADTree adtree) {
    if (adtree instanceof Attack) {
        Attack attack = (Attack) adtree;
        ALeaf aleaf = new ALeaf(attack);
        // keep track of all attack leaves
        if (!attackALeafMap.containsKey(attack)) {
            attackALeafMap.put(attack, new LinkedHashSet<>());
        }
        attackALeafMap.get(attack).add(aleaf);
        return Optional.of(aleaf);
    } else if (adtree instanceof Defense) {
        Defense defense = (Defense) adtree;
        defenses.add(defense);
        return Optional.of(new DNot(constructDefenseTree(defense)));
    } else if (adtree instanceof ADAnd) {
        ADAnd adand = (ADAnd) adtree;
        // Transpose and/or
        return Optional.of(new DOr(adand.children().stream().map(this::constructInternal).flatMap(elem -> elem.isPresent() ? Stream.of(elem.get()) : Stream.empty()).collect(Collectors.toList())));
    } else if (adtree instanceof ADOr) {
        ADOr ador = (ADOr) adtree;
        // Transpose and/or
        return Optional.of(new DAnd(ador.children().stream().map(this::constructInternal).flatMap(elem -> elem.isPresent() ? Stream.of(elem.get()) : Stream.empty()).collect(Collectors.toList())));
    } else if (adtree instanceof ADNot) {
        ADNot adnot = (ADNot) adtree;
        return constructInternal(adnot.child()).map(DNot::new);
    } else if (adtree instanceof DefenseCondition) {
        DCondition dcond = new DCondition((DefenseCondition) adtree);
        dconditions.add(dcond);
        return Optional.of(dcond);
    } else {
        throw new RuntimeException("got invalid adtree type: " + adtree.getClass().getCanonicalName());
    }
}
Also used : DCondition(com.ge.verdict.synthesis.dtree.DCondition) ADOr(com.ge.verdict.attackdefensecollector.adtree.ADOr) DefenseCondition(com.ge.verdict.attackdefensecollector.adtree.DefenseCondition) ArrayList(java.util.ArrayList) Attack(com.ge.verdict.attackdefensecollector.adtree.Attack) LinkedHashMap(java.util.LinkedHashMap) Defense(com.ge.verdict.attackdefensecollector.adtree.Defense) Map(java.util.Map) DTree(com.ge.verdict.synthesis.dtree.DTree) ADTree(com.ge.verdict.attackdefensecollector.adtree.ADTree) LinkedHashSet(java.util.LinkedHashSet) DLeaf(com.ge.verdict.synthesis.dtree.DLeaf) DNot(com.ge.verdict.synthesis.dtree.DNot) AttackDefenseCollector(com.ge.verdict.attackdefensecollector.AttackDefenseCollector) Set(java.util.Set) Collectors(java.util.stream.Collectors) ALeaf(com.ge.verdict.synthesis.dtree.ALeaf) ADAnd(com.ge.verdict.attackdefensecollector.adtree.ADAnd) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) DAnd(com.ge.verdict.synthesis.dtree.DAnd) DOr(com.ge.verdict.synthesis.dtree.DOr) Collections(java.util.Collections) ADNot(com.ge.verdict.attackdefensecollector.adtree.ADNot) ADNot(com.ge.verdict.attackdefensecollector.adtree.ADNot) ADOr(com.ge.verdict.attackdefensecollector.adtree.ADOr) DOr(com.ge.verdict.synthesis.dtree.DOr) ALeaf(com.ge.verdict.synthesis.dtree.ALeaf) ADAnd(com.ge.verdict.attackdefensecollector.adtree.ADAnd) DCondition(com.ge.verdict.synthesis.dtree.DCondition) Attack(com.ge.verdict.attackdefensecollector.adtree.Attack) ADAnd(com.ge.verdict.attackdefensecollector.adtree.ADAnd) DAnd(com.ge.verdict.synthesis.dtree.DAnd) Defense(com.ge.verdict.attackdefensecollector.adtree.Defense) DNot(com.ge.verdict.synthesis.dtree.DNot) ADNot(com.ge.verdict.attackdefensecollector.adtree.ADNot) ADOr(com.ge.verdict.attackdefensecollector.adtree.ADOr) DefenseCondition(com.ge.verdict.attackdefensecollector.adtree.DefenseCondition)

Example 2 with DNot

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

the class DTreeConstructorTest method testFlattenNot.

@Test
public void testFlattenNot() {
    // Note: comparing the results of prettyPrint() instead of the trees directly
    // because we have not implemented equals for the DTree classes
    DLeaf.Factory factory = new DLeaf.Factory();
    Fraction[] costs = Util.fractionCosts(new double[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 });
    DTree leaf = new DLeaf("A", "A", "A", 0, 1, costs, factory);
    DTree dtree = new DNot(new DNot(leaf));
    Assertions.assertThat(dtree.prepare().get().prettyPrint()).isEqualTo(leaf.prettyPrint());
    DTree dtree2 = new DAnd(Collections.singletonList(new DOr(Collections.singletonList(dtree))));
    DTree dtree3 = new DAnd(Collections.singletonList(new DOr(Collections.singletonList(leaf))));
    Assertions.assertThat(dtree2.prepare().get().prettyPrint()).isEqualTo(dtree3.prettyPrint());
}
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) DNot(com.ge.verdict.synthesis.dtree.DNot) ADNot(com.ge.verdict.attackdefensecollector.adtree.ADNot) Test(org.junit.Test)

Aggregations

ADAnd (com.ge.verdict.attackdefensecollector.adtree.ADAnd)2 ADNot (com.ge.verdict.attackdefensecollector.adtree.ADNot)2 ADOr (com.ge.verdict.attackdefensecollector.adtree.ADOr)2 ADTree (com.ge.verdict.attackdefensecollector.adtree.ADTree)2 DAnd (com.ge.verdict.synthesis.dtree.DAnd)2 DLeaf (com.ge.verdict.synthesis.dtree.DLeaf)2 DNot (com.ge.verdict.synthesis.dtree.DNot)2 DOr (com.ge.verdict.synthesis.dtree.DOr)2 DTree (com.ge.verdict.synthesis.dtree.DTree)2 AttackDefenseCollector (com.ge.verdict.attackdefensecollector.AttackDefenseCollector)1 Attack (com.ge.verdict.attackdefensecollector.adtree.Attack)1 Defense (com.ge.verdict.attackdefensecollector.adtree.Defense)1 DefenseCondition (com.ge.verdict.attackdefensecollector.adtree.DefenseCondition)1 ALeaf (com.ge.verdict.synthesis.dtree.ALeaf)1 DCondition (com.ge.verdict.synthesis.dtree.DCondition)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1