Search in sources :

Example 1 with FormulaFactory

use of org.logicng.formulas.FormulaFactory 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 FormulaFactory

use of org.logicng.formulas.FormulaFactory 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 FormulaFactory

use of org.logicng.formulas.FormulaFactory in project feature-diagram by MontiCore.

the class FDSemDiff method semDiff.

/**
 * Calculates the semantic difference witness between two feature diagrams.
 *
 * Either uses open-world or close-world semantics of FDs as presented here:
 * https://se-rwth.de/publications/Semantic-Evolution-Analysis-of-Feature-Models.pdf
 *
 * @param fd1 The first feature diagram
 * @param fd2 The second feature diagram
 * @param closedWorld if true, then closed-world semantics is used, otherwise open-world semantics is used
 * @return The (optional) semantic diff witness
 */
private Optional<ASTFeatureConfiguration> semDiff(ASTFeatureDiagram fd1, ASTFeatureDiagram fd2, boolean closedWorld) {
    final FormulaFactory ff = new FormulaFactory();
    final Set<String> features = Sets.union(Sets.newHashSet(fd1.getAllFeatures()), Sets.newHashSet(fd2.getAllFeatures()));
    final Map<Variable, String> vars = features.stream().collect(Collectors.toMap(ff::variable, Function.identity()));
    FeatureDiagramTraverser traverser = FeatureDiagramMill.traverser();
    FD2Formula trafo = new FD2Formula(ff);
    traverser.add4FeatureDiagram(trafo);
    // traverser.setFeatureDiagramHandler(trafo);
    // trafo.setTraverser(traverser);
    fd1.accept(traverser);
    Formula phi_1 = trafo.getFormula();
    fd2.accept(traverser);
    Formula phi_2 = trafo.getFormula();
    if (closedWorld) {
        // features of FD1 that are not features of FD2 must not be selected in FD2
        Set<String> featuresInFD1NotInFD2 = new HashSet<>(fd1.getAllFeatures());
        featuresInFD1NotInFD2.removeAll(fd2.getAllFeatures());
        for (String feature : featuresInFD1NotInFD2) {
            // This feature must not be chosen for FD2
            Formula notFeature = ff.literal(feature, false);
            phi_2 = ff.and(phi_2, notFeature);
        }
        // features of FD2 that are not features of FD1 must not be selected in FD1
        Set<String> featuresInFD2NotInFD1 = new HashSet<>(fd2.getAllFeatures());
        featuresInFD2NotInFD1.removeAll(fd1.getAllFeatures());
        for (String feature : featuresInFD2NotInFD1) {
            // This feature must not be chosen for fd1
            Formula notFeature = ff.literal(feature, false);
            phi_1 = ff.and(phi_1, notFeature);
        }
    }
    Formula phi = ff.and(phi_1, ff.not(phi_2));
    final SATSolver miniSat = MiniSat.miniSat(ff);
    miniSat.add(phi);
    miniSat.sat();
    final Assignment assignment = miniSat.model();
    Optional<ASTFeatureConfiguration> result = Optional.empty();
    if (assignment != null) {
        Set<String> selectedFeatures = assignment.positiveLiterals().stream().map(vars::get).filter(Objects::nonNull).collect(Collectors.toSet());
        return Optional.of(calculateConfiguration(selectedFeatures, new HashSet<>(fd1.getAllFeatures()), fd1.getName()));
    }
    return result;
}
Also used : SATSolver(org.logicng.solvers.SATSolver) Variable(org.logicng.formulas.Variable) ASTFeatureConfiguration(de.monticore.featureconfiguration._ast.ASTFeatureConfiguration) Assignment(org.logicng.datastructures.Assignment) Formula(org.logicng.formulas.Formula) FormulaFactory(org.logicng.formulas.FormulaFactory) FeatureDiagramTraverser(de.monticore.featurediagram._visitor.FeatureDiagramTraverser)

Aggregations

Formula (org.logicng.formulas.Formula)3 FormulaFactory (org.logicng.formulas.FormulaFactory)3 Assignment (org.logicng.datastructures.Assignment)2 ComponentDefense (com.ge.verdict.synthesis.dtree.DLeaf.ComponentDefense)1 ASTFeatureConfiguration (de.monticore.featureconfiguration._ast.ASTFeatureConfiguration)1 FeatureDiagramTraverser (de.monticore.featurediagram._visitor.FeatureDiagramTraverser)1 LinkedHashSet (java.util.LinkedHashSet)1 Variable (org.logicng.formulas.Variable)1 MaxSATSolver (org.logicng.solvers.MaxSATSolver)1 SATSolver (org.logicng.solvers.SATSolver)1 DNFFactorization (org.logicng.transformations.dnf.DNFFactorization)1