Search in sources :

Example 1 with Formula

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);
}
Also used : Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) DNFFactorization(org.logicng.transformations.dnf.DNFFactorization)

Example 2 with Formula

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");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) ComponentDefense(com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense) MaxSATSolver(org.logicng.solvers.MaxSATSolver)

Example 3 with Formula

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));
}
Also used : Formula(org.logicng.formulas.Formula)

Example 4 with Formula

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)));
}
Also used : Formula(org.logicng.formulas.Formula) Literal(org.logicng.formulas.Literal)

Example 5 with Formula

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));
}
Also used : Formula(org.logicng.formulas.Formula) FutureTask(java.util.concurrent.FutureTask) Expression(fr.lip6.move.gal.structural.expr.Expression) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

Formula (org.logicng.formulas.Formula)12 FormulaFactory (org.logicng.formulas.FormulaFactory)3 Literal (org.logicng.formulas.Literal)3 Variable (org.logicng.formulas.Variable)3 BooleanExpression (fr.lip6.move.gal.BooleanExpression)2 ExecutorService (java.util.concurrent.ExecutorService)2 FutureTask (java.util.concurrent.FutureTask)2 Assignment (org.logicng.datastructures.Assignment)2 CFalse (org.logicng.formulas.CFalse)2 CTrue (org.logicng.formulas.CTrue)2 SATSolver (org.logicng.solvers.SATSolver)2 DNFFactorization (org.logicng.transformations.dnf.DNFFactorization)2 IBooleanFormula (org.matheclipse.core.interfaces.IBooleanFormula)2 ComponentDefense (com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense)1 ASTFeatureConfiguration (de.monticore.featureconfiguration._ast.ASTFeatureConfiguration)1 FeatureDiagramTraverser (de.monticore.featurediagram._visitor.FeatureDiagramTraverser)1 And (fr.lip6.move.gal.And)1 Not (fr.lip6.move.gal.Not)1 Or (fr.lip6.move.gal.Or)1 Ag (fr.lip6.move.gal.logic.Ag)1