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