use of com.ge.verdict.synthesis.dtree.DAnd in project VERDICT by ge-high-assurance.
the class VerdictSynthesisTest method multipleRequirementsTest.
@Test
public void multipleRequirementsTest() {
DLeaf.Factory factory = new DLeaf.Factory();
Fraction[] costs = Util.fractionCosts(new double[] { 2, 5, 9, 15, 16, 18, 20, 25, 30, 37 });
DLeaf leaf1 = new DLeaf("S1", "D1", "A2", 0, 3, costs, factory);
DLeaf leaf2 = new DLeaf("S1", "D1", "A2", 0, 4, costs, factory);
DTree dtree = new DAnd(leaf1, leaf2);
CostModel costModel = new CostModel(new Triple<>("S1", "D1", costs));
Optional<ResultsInstance> result = VerdictSynthesis.performSynthesisMultiple(dtree, factory, costModel, false, false, false, false);
Assertions.assertThat(result.isPresent());
Assertions.assertThat(result.get().items).hasSize(1);
Assertions.assertThat(result.get().items).contains(new ResultsInstance.Item("S1", "D1", 0, 4, new Fraction(2), new Fraction(16)));
Assertions.assertThat(result.get().outputCost).isEqualTo(new Fraction(16));
}
use of com.ge.verdict.synthesis.dtree.DAnd 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));
}
}
use of com.ge.verdict.synthesis.dtree.DAnd 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());
}
}
use of com.ge.verdict.synthesis.dtree.DAnd in project VERDICT by ge-high-assurance.
the class DTreeConstructorTest method testConstruct.
@Test
public void testConstruct() {
DLeaf.Factory factory = new DLeaf.Factory();
CostModel dummyCosts = new CostModel(new File(getClass().getResource("dummyCosts.xml").getPath()));
int dal = 5;
SystemModel system = new SystemModel("S1");
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.empty())));
ADTree adtree = new ADOr(new ADAnd(new ADNot(defense1), attack1));
Fraction[] costs = Util.fractionCosts(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
DTree dtree = new DAnd(Collections.singletonList(new DOr(Collections.singletonList(new DOr(Collections.singletonList(new DAnd(Collections.singletonList(new DLeaf("S1", "D1", "A1", 0, dal, costs, factory)))))))));
Assertions.assertThat(DTreeConstructor.construct(adtree, dummyCosts, dal, false, false, factory).prettyPrint()).isEqualTo(dtree.prettyPrint());
}
use of com.ge.verdict.synthesis.dtree.DAnd in project VERDICT by ge-high-assurance.
the class DTreeConstructorTest method testUnmitigatedMixed.
@Test
public void testUnmitigatedMixed() {
DLeaf.Factory factory = new DLeaf.Factory();
CostModel dummyCosts = new CostModel(new File(getClass().getResource("dummyCosts.xml").getPath()));
int dal = 5;
SystemModel system = new SystemModel("S1");
Attack attack1 = new Attack(system.getAttackable(), "A1", "An attack", Prob.certain(), CIA.I);
Attack attack2 = new Attack(system.getAttackable(), "A2", "An attack", Prob.certain(), CIA.I);
Defense defense1 = new Defense(attack1);
defense1.addDefenseClause(Collections.singletonList(new Defense.DefenseLeaf("D1", Optional.empty())));
ADTree adtree = new ADOr(new ADNot(defense1), attack1, attack2);
Fraction[] costs = Util.fractionCosts(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
DTree dtree = new DAnd(Arrays.asList(new DOr(Collections.singletonList(new DAnd(Collections.singletonList(new DLeaf("S1", "D1", "A1", 0, dal, costs, factory))))), new ALeaf(attack2)));
Assertions.assertThat(DTreeConstructor.construct(adtree, dummyCosts, dal, false, false, factory).prettyPrint()).isEqualTo(dtree.prettyPrint());
}
Aggregations