Search in sources :

Example 1 with ChocoSolver

use of abs.frontend.mtvl.ChocoSolver in project abstools by abstools.

the class Main method analyzeMTVL.

/**
 * TODO: Should probably be introduced in Model through JastAdd by MTVL package.
 * However, the command-line argument handling will have to stay in Main. Pity.
 */
private void analyzeMTVL(Model m) {
    ProductDecl productDecl = null;
    try {
        productDecl = product == null ? null : m.findProduct(product);
    } catch (WrongProgramArgumentException e) {
    // ignore in case we're just solving.
    }
    if (m.hasMTVL()) {
        if (solve) {
            if (verbose)
                System.out.println("Searching for solutions for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.getSolutionsAsString());
        }
        if (minimise) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for minimum solutions of " + product + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.minimiseToString(product));
        }
        if (maximise) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for maximum solutions of " + product + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            // System.out.print(s.maximiseToInt(product));
            s.addConstraint(ChocoSolver.eqeq(s.vars.get(product), s.maximiseToInt(product)));
            ChocoSolver s1 = m.instantiateCSModel();
            int i = 1;
            while (s1.solveAgain()) {
                System.out.println("------ " + (i++) + "------");
                System.out.print(s1.getSolutionsAsString());
            }
        }
        if (solveall) {
            if (verbose)
                System.out.println("Searching for all solutions for the feature model...");
            ChocoSolver solver = m.instantiateCSModel();
            System.out.print(solver.getSolutionsAsString());
        }
        if (solveWith) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for solution that includes " + product + "...");
            if (productDecl != null) {
                ChocoSolver s = m.instantiateCSModel();
                HashSet<Constraint> newcs = new HashSet<>();
                productDecl.getProduct().getProdConstraints(s.vars, newcs);
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution:\n" + s.getSolutionsAsString());
            } else {
                System.out.println("Product '" + product + "' not found.");
            }
        }
        if (minWith) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for solution that includes " + product + "...");
            ChocoSolver s = m.instantiateCSModel();
            HashSet<Constraint> newcs = new HashSet<>();
            s.addIntVar("difference", 0, 50);
            if (productDecl != null) {
                m.getDiffConstraints(productDecl.getProduct(), s.vars, newcs, "difference");
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution: " + s.minimiseToString("difference"));
            } else {
                System.out.println("Product '" + product + "' not found.");
            }
        }
        if (maxProduct) {
            assert product != null;
            if (verbose)
                System.out.println("Searching for solution that includes " + product + "...");
            ChocoSolver s = m.instantiateCSModel();
            HashSet<Constraint> newcs = new HashSet<>();
            s.addIntVar("noOfFeatures", 0, 50);
            if (m.getMaxConstraints(s.vars, newcs, "noOfFeatures")) {
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution: " + s.maximiseToString("noOfFeatures"));
            } else {
                System.out.println("---No solution-------------");
            }
        }
        if (check) {
            assert product != null;
            ChocoSolver s = m.instantiateCSModel();
            if (productDecl == null) {
                System.out.println("Product '" + product + "' not found.");
            } else {
                Map<String, Integer> guess = productDecl.getProduct().getSolution();
                System.out.println("checking solution: " + s.checkSolution(guess, m));
            }
        }
        if (numbersol && !ignoreattr) {
            ChocoSolver s = m.instantiateCSModel();
            System.out.println("Number of solutions found: " + s.countSolutions());
        } else if (numbersol && ignoreattr) {
            ChocoSolver s = m.instantiateCSModel();
            System.out.println("Number of solutions found (without attributes): " + s.countSolutions());
        }
    }
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) WrongProgramArgumentException(abs.common.WrongProgramArgumentException) Constraint(choco.kernel.model.constraints.Constraint) ChocoSolver(abs.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Example 2 with ChocoSolver

use of abs.frontend.mtvl.ChocoSolver in project abstools by abstools.

the class ProductLineAnalysisHelper method buildAllConfigurations.

/*
     * Build all SPL configurations (valid feature selections, ignoring attributes), one by one
     * The purpose is to measure how long this takes, so we can compare it with the performance of type checking the SPL.
     *
     */
public static void buildAllConfigurations(Model m) {
    long timeSum = 0;
    for (Product product : m.getProductList()) {
        long time0 = System.currentTimeMillis();
        if (m.debug)
            System.out.println("\u23F1 Flattening product: " + product.getFeatureSetAsString());
        // Find a solution to the feature model that satisfies the product feature selection
        ChocoSolver s = m.instantiateCSModel();
        HashSet<Constraint> newcs = new HashSet<>();
        product.getProdConstraints(s.vars, newcs);
        for (Constraint c : newcs) s.addConstraint(c);
        Map<String, Integer> solution = s.getSolution();
        if (m.debug)
            System.out.println("\u23F1 Full product configuration: " + solution);
        long time1 = System.currentTimeMillis();
        // i.e. add attribute assignments to features
        for (String fname : solution.keySet()) {
            if (// ignore internal ChocoSolver variable
            fname.startsWith("$"))
                continue;
            if (fname.contains(".")) {
                String[] parts = fname.split("\\.");
                String fid = parts[0];
                String aid = parts[1];
                Integer val = solution.get(fname);
                for (Feature feature : product.getFeatures()) {
                    if (feature.getName().equals(fid)) {
                        feature.addAttrAssignment(new AttrAssignment(aid, new IntVal(val)));
                        break;
                    }
                }
            }
        }
        long time2 = System.currentTimeMillis();
        Model thisModel = m.treeCopyNoTransform();
        long time3 = System.currentTimeMillis();
        thisModel.flattenForProduct(product);
        long time4 = System.currentTimeMillis();
        timeSum += (time4 - time3);
        if (m.debug) {
            System.out.println("\u23F1 Time: " + (time1 - time0) + " | " + (time2 - time1) + " | " + (time3 - time2) + " | " + (time4 - time3) + " | " + "Total(s): " + ((time4 - time0) / 1000.0));
        }
    }
    if (m.debug)
        System.out.println("\u23F1 Flattening total time (s): " + timeSum / 1000.0);
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) ChocoSolver(abs.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Aggregations

ChocoSolver (abs.frontend.mtvl.ChocoSolver)2 Constraint (choco.kernel.model.constraints.Constraint)2 HashSet (java.util.HashSet)2 WrongProgramArgumentException (abs.common.WrongProgramArgumentException)1