use of org.logicng.formulas.Formula in project VERDICT by ge-high-assurance.
the class CutSetGenerator method generate.
/**
* Perform cut set generation for the given attack-defense tree.
*
* @param adtree
* @return
*/
public static ADTree generate(ADTree adtree) {
FormulaFactory factory = new FormulaFactory();
Cache cache = new Cache();
Formula formula = adtree.toLogicNg(factory, cache);
long startTime = System.currentTimeMillis();
// this is terribly inefficient for any non-trivial system
// and it has not yet been observed to terminate
// Formula minimal = QuineMcCluskeyAlgorithm.compute(formula);
// this should be inefficient too, but it finishes trivially for trees already in DNF form
// not yet tested on non-DNF trees because we don't have a model that produces one
Formula minimal = (new DNFFactorization()).apply(formula, false);
// for comparing approaches
System.out.println("converted to DNF in " + (System.currentTimeMillis() - startTime) + " ms");
return extract(minimal, cache);
}
use of org.logicng.formulas.Formula 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");
}
}
use of org.logicng.formulas.Formula in project feature-diagram by MontiCore.
the class FD2Formula method visit.
@Override
public void visit(ASTOrGroup node) {
Set<String> orGroup = getFeatures(node);
Formula r = ff.or(orGroup.stream().map(this::Var).collect(Collectors.toList()));
formulas.add(ff.implication(Var(current), r));
}
use of org.logicng.formulas.Formula in project feature-diagram by MontiCore.
the class FD2Formula method visit.
@Override
public void visit(ASTXorGroup node) {
Set<String> xorGroup = getFeatures(node);
Set<Literal> G = xorGroup.stream().map(this::Var).collect(Collectors.toSet());
Formula min1 = ff.or(G);
Formula max1 = ff.and(Sets.combinations(G, 2).stream().map(ff::and).map(ff::not).collect(Collectors.toList()));
formulas.add(ff.implication(Var(current), ff.and(min1, max1)));
}
use of org.logicng.formulas.Formula in project ITSTools by lip6.
the class ExpressionToLogicNG method simplify.
public Expression simplify(Expression be) {
ExecutorService pool = Executors.newCachedThreadPool();
// System.out.println("Before : "+ SerializationUtil.getText(be, true));
FutureTask<Expression> task = new FutureTask<>(() -> {
Formula ff = toFormula(be);
// new DNFFactorization().apply(ff, false);
Formula fs = QuineMcCluskeyAlgorithm.compute(ff);
if (fs.numberOfNodes() >= 4 * ff.numberOfNodes() + 1) {
fs = ff;
}
Expression newbe = toExpression(fs);
return newbe;
});
pool.execute(task);
try {
return task.get(500, TimeUnit.MILLISECONDS);
} catch (Exception e) {
task.cancel(true);
return be;
}
// QuineMcCluskeyAlgorithm.compute(ff);
// System.out.println("After LogicNG : "+ SerializationUtil.getText(newbe, true));
}
Aggregations