Search in sources :

Example 6 with ProductDecl

use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.

the class Main method rewriteModel.

/**
 * Perform various rewrites that cannot be done in JastAdd.
 *
 * JastAdd rewrite rules can only rewrite the current node using
 * node-local information.  ("The code in the body of the rewrite may
 * access and rearrange the nodes in the subtree rooted at A, but not any
 * other nodes in the AST. Furthermore, the code may not have any other
 * side effects." --
 * http://jastadd.org/web/documentation/reference-manual.php#Rewrites)
 *
 * We use this method to generate Exception constructors and the
 * information in ABS.Productline.
 *
 * @param m the model.
 * @param productname The name of the product or null.
 * @throws WrongProgramArgumentException
 */
private static void rewriteModel(Model m, String productname) throws WrongProgramArgumentException {
    // Generate reflective constructors for all features
    ProductLine pl = m.getProductLine();
    if (pl != null) {
        // Let's assume the module and datatype names in abslang.abs did
        // not get changed, and just crash otherwise.  If you're here
        // because of a NPE: Hi!  Make the standard library and this code
        // agree about what the feature reflection module is called.
        ModuleDecl modProductline = null;
        DataTypeDecl featureDecl = null;
        FunctionDecl currentFeatureFun = null;
        FunctionDecl productNameFun = null;
        for (ModuleDecl d : m.getModuleDecls()) {
            if (d.getName().equals(Constants.PL_NAME)) {
                modProductline = d;
                break;
            }
        }
        if (modProductline == null) {
            throw new WrongProgramArgumentException("Internal error: did not find module " + Constants.PL_NAME + "(should have been defined in the abslang.abs standard library)");
        }
        for (Decl d : modProductline.getDecls()) {
            if (d instanceof DataTypeDecl && d.getName().equals("Feature")) {
                featureDecl = (DataTypeDecl) d;
            } else if (d instanceof FunctionDecl && d.getName().equals("product_features")) {
                currentFeatureFun = (FunctionDecl) d;
            } else if (d instanceof FunctionDecl && d.getName().equals("product_name")) {
                productNameFun = (FunctionDecl) d;
            }
        }
        // Adjust Feature datatype
        featureDecl.setDataConstructorList(new List<>());
        for (Feature f : pl.getFeatures()) {
            // TODO: when/if we incorporate feature parameters into the
            // productline feature declarations (as we should), we need to
            // adjust the DataConstructor arguments here.
            featureDecl.addDataConstructorNoTransform(new DataConstructor(f.getName(), new List<>()));
        }
        // Adjust product_name() function
        productNameFun.setFunctionDef(new ExpFunctionDef(new StringLiteral(productname)));
        // Adjust product_features() function
        ProductDecl p = null;
        if (productname != null)
            p = m.findProduct(productname);
        if (p != null) {
            DataConstructorExp feature_arglist = new DataConstructorExp("Cons", new List<>());
            DataConstructorExp current = feature_arglist;
            for (Feature f : p.getProduct().getFeatures()) {
                DataConstructorExp next = new DataConstructorExp("Cons", new List<>());
                // TODO: when/if we incorporate feature parameters into
                // the productline feature declarations (as we should), we
                // need to adjust the DataConstructorExp arguments here.
                current.addParamNoTransform(new DataConstructorExp(f.getName(), new List<>()));
                current.addParamNoTransform(next);
                current = next;
            }
            current.setConstructor("Nil");
            currentFeatureFun.setFunctionDef(new ExpFunctionDef(feature_arglist));
        }
    }
    m.flushTreeCache();
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) Decl(org.abs_models.frontend.ast.Decl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ProductDecl(org.abs_models.frontend.ast.ProductDecl) DataConstructor(org.abs_models.frontend.ast.DataConstructor) Feature(org.abs_models.frontend.ast.Feature) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) StringLiteral(org.abs_models.frontend.ast.StringLiteral) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) ArrayList(java.util.ArrayList) List(org.abs_models.frontend.ast.List) ProductLine(org.abs_models.frontend.ast.ProductLine) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef)

Example 7 with ProductDecl

use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.

the class Main method mainMethod.

public int mainMethod(Absc arguments) {
    int result = 0;
    boolean done = false;
    this.arguments = arguments;
    try {
        if (arguments.backend != null) {
            if (arguments.backend.maude) {
                result = Math.max(result, MaudeCompiler.doMain(arguments));
                done = true;
            }
            if (arguments.backend.java) {
                result = Math.max(result, JavaBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.erlang) {
                result = Math.max(result, ErlangBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.prolog) {
                result = Math.max(result, PrologBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.coreabs) {
                result = Math.max(result, CoreAbsBackend.doMain(arguments));
                done = true;
            }
            if (arguments.backend.json) {
                result = Math.max(result, Tester.doMain(arguments));
                done = true;
            }
            if (arguments.backend.prettyprint) {
                result = Math.max(result, PrettyPrinterBackEnd.doMain(arguments));
                done = true;
            }
            if (arguments.backend.outline) {
                result = Math.max(result, OutlinePrinterBackEnd.doMain(arguments));
                done = true;
            }
            if (arguments.backend.dumpProducts) {
                Model m = parse(arguments.files);
                if (m.hasParserErrors()) {
                    // parse should have already printed errors
                    result = Math.max(result, 1);
                } else {
                    Iterator<ProductDecl> pi = m.getProductDecls().iterator();
                    while (pi.hasNext()) {
                        System.out.print(pi.next().getName());
                        if (pi.hasNext())
                            System.out.print(' ');
                    }
                }
                done = true;
            }
        }
        if (!done) {
            // no backend selected, just do type-checking
            Model m = parse(arguments.files);
            if (m.hasParserErrors() || m.hasErrors() || m.hasTypeErrors()) {
                printErrorMessage();
                result = 1;
            }
        }
    } catch (InternalBackendException e) {
        // don't print stack trace here
        printError(e.getMessage());
        result = 1;
    } catch (Exception e) {
        if (e.getMessage() == null) {
            e.printStackTrace();
        }
        assert e.getMessage() != null : e.toString();
        printError(e.getMessage());
        result = 1;
    }
    return result;
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) InternalBackendException(org.abs_models.backend.common.InternalBackendException) Model(org.abs_models.frontend.ast.Model) InternalBackendException(org.abs_models.backend.common.InternalBackendException) FileNotFoundException(java.io.FileNotFoundException) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) IOException(java.io.IOException) DeltaModellingException(org.abs_models.frontend.delta.DeltaModellingException)

Example 8 with ProductDecl

use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.

the class CheckSPLCommand method analyzeMTVL.

private void analyzeMTVL(Model m) {
    if (m.hasMTVL()) {
        if (solve) {
            if (parent.verbose)
                System.out.println("Searching for solutions for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.getSolutionsAsString());
        }
        if (minimise != null) {
            if (parent.verbose)
                System.out.println("Searching for minimum solutions of " + minimise + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.minimiseToString(minimise));
        }
        if (maximise != null) {
            if (parent.verbose)
                System.out.println("Searching for maximum solutions of " + maximise + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            // System.out.print(s.maximiseToInt(product));
            s.addConstraint(ChocoSolver.eqeq(s.vars.get(maximise), s.maximiseToInt(maximise)));
            ChocoSolver s1 = m.instantiateCSModel();
            int i = 1;
            while (s1.solveAgain()) {
                System.out.println("------ " + (i++) + "------");
                System.out.print(s1.getSolutionsAsString());
            }
        }
        if (solveall) {
            if (parent.verbose)
                System.out.println("Searching for all solutions for the feature model...");
            ChocoSolver solver = m.instantiateCSModel();
            System.out.print(solver.getSolutionsAsString());
        }
        if (solveWithProduct != null) {
            ProductDecl solveWithDecl = null;
            try {
                solveWithDecl = m.findProduct(solveWithProduct);
            } catch (WrongProgramArgumentException e) {
            // nothing to do
            }
            if (solveWithDecl != null) {
                if (parent.verbose)
                    System.out.println("Searching for solution that includes " + solveWithProduct + "...");
                ChocoSolver s = m.instantiateCSModel();
                HashSet<Constraint> newcs = new HashSet<>();
                solveWithDecl.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 '" + solveWithProduct + "' not found.");
            }
        }
        if (minWith != null) {
            ProductDecl minWithDecl = null;
            try {
                minWithDecl = m.findProduct(minWith);
            } catch (WrongProgramArgumentException e) {
            // nothing to do
            }
            if (minWithDecl != null) {
                if (parent.verbose)
                    System.out.println("Searching for solution that includes " + minWith + "...");
                ChocoSolver s = m.instantiateCSModel();
                HashSet<Constraint> newcs = new HashSet<>();
                s.addIntVar("difference", 0, 50);
                m.getDiffConstraints(minWithDecl.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 '" + minWith + "' not found.");
            }
        }
        if (maxProduct) {
            if (parent.verbose)
                System.out.println("Searching for solution with maximum number of features ...");
            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 (checkProduct != null) {
            ProductDecl checkProductDecl = null;
            try {
                checkProductDecl = m.findProduct(checkProduct);
            } catch (WrongProgramArgumentException e) {
            // nothing to do
            }
            if (checkProductDecl == null) {
                System.out.println("Product '" + checkProduct + "' not found, cannot check.");
            } else {
                ChocoSolver s = m.instantiateCSModel();
                Map<String, Integer> guess = checkProductDecl.getProduct().getSolution();
                System.out.println("checking solution: " + s.checkSolution(guess, m));
            }
        }
        if (numbersol) {
            ChocoSolver s = m.instantiateCSModel();
            // did we call m.dropAttributes() previously?
            if (ignoreattr) {
                System.out.println("Number of solutions found (without attributes): " + s.countSolutions());
            } else {
                System.out.println("Number of solutions found: " + s.countSolutions());
            }
        }
    }
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Constraint(choco.kernel.model.constraints.Constraint) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) Constraint(choco.kernel.model.constraints.Constraint) ChocoSolver(org.abs_models.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Example 9 with ProductDecl

use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.

the class DeltaOrderingTest method properSorting1.

@Test
public void properSorting1() throws DeltaModellingException, WrongProgramArgumentException {
    Model model = assertParse("module Test;" + "delta D1;" + "delta D2;" + "productline PL;" + "features A,B;" + "delta D1 when A;" + "delta D2 after D1 when B;" + "product P1(A);" + "product P2(A, B);");
    model.evaluateAllProductDeclarations();
    ProductDecl prod = model.findProduct("P1");
    ProductLine pl = model.getProductLine();
    Set<String> deltaids = pl.findApplicableDeltas(prod.getProduct());
    List<String> sorted_deltaids = pl.sortDeltas(deltaids);
    assertArrayEquals(new String[] { "D1" }, sorted_deltaids.toArray());
    prod = model.findProduct("P2");
    deltaids = pl.findApplicableDeltas(prod.getProduct());
    sorted_deltaids = pl.sortDeltas(deltaids);
    assertArrayEquals(new String[] { "D1", "D2" }, sorted_deltaids.toArray());
    assertFalse(model.typeCheck().containsErrors());
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Model(org.abs_models.frontend.ast.Model) ProductLine(org.abs_models.frontend.ast.ProductLine) Test(org.junit.Test)

Example 10 with ProductDecl

use of org.abs_models.frontend.ast.ProductDecl in project abstools by abstools.

the class SearchSolutionsTest method CheckEmptyProduct.

@Test
public void CheckEmptyProduct() throws WrongProgramArgumentException {
    Model model = assertParse(withoutProducLine);
    ChocoSolver s = model.instantiateCSModel();
    model.evaluateAllProductDeclarations();
    ProductDecl product = model.findProduct("P");
    Map<String, Integer> guess = product.getProduct().getSolution();
    assertEquals(true, s.checkSolution(guess, model));
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Model(org.abs_models.frontend.ast.Model) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Aggregations

ProductDecl (org.abs_models.frontend.ast.ProductDecl)19 Model (org.abs_models.frontend.ast.Model)17 Test (org.junit.Test)16 HashSet (java.util.HashSet)11 Feature (org.abs_models.frontend.ast.Feature)10 Product (org.abs_models.frontend.ast.Product)9 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)4 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)3 ProductLine (org.abs_models.frontend.ast.ProductLine)3 Constraint (choco.kernel.model.constraints.Constraint)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InternalBackendException (org.abs_models.backend.common.InternalBackendException)1 FrontendTest (org.abs_models.frontend.FrontendTest)1 DataConstructor (org.abs_models.frontend.ast.DataConstructor)1 DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)1 DataTypeDecl (org.abs_models.frontend.ast.DataTypeDecl)1 Decl (org.abs_models.frontend.ast.Decl)1 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)1