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);
}
}
}
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);
}
}
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));
}
}
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();
}
}
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");
}
}
Aggregations