Search in sources :

Example 1 with WrongProgramArgumentException

use of org.abs_models.common.WrongProgramArgumentException in project abstools by abstools.

the class ABSTest method assertParseFilesOk.

protected Model assertParseFilesOk(Set<String> fileNames, Config... config) throws IOException, InternalBackendException, DeltaModellingException, WrongProgramArgumentException {
    Main main = new Main();
    java.util.List<File> files = fileNames.stream().map(f -> new File(resolveFileName(f))).collect(Collectors.toList());
    Model m = main.parse(files);
    return assertParseModelOk(m, config);
}
Also used : FilenameFilter(java.io.FilenameFilter) Arrays(java.util.Arrays) SemanticCondition(org.abs_models.frontend.analyser.SemanticCondition) Set(java.util.Set) IOException(java.io.IOException) EXPECT_TYPE_ERROR(org.abs_models.ABSTest.Config.EXPECT_TYPE_ERROR) Collectors(java.util.stream.Collectors) EXPECT_PARSE_ERROR(org.abs_models.ABSTest.Config.EXPECT_PARSE_ERROR) WITHOUT_DESUGARING_AFTER_TYPECHECK(org.abs_models.ABSTest.Config.WITHOUT_DESUGARING_AFTER_TYPECHECK) File(java.io.File) LocationTypeExtension(org.abs_models.frontend.typechecker.locationtypes.LocationTypeExtension) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) TYPE_CHECK(org.abs_models.ABSTest.Config.TYPE_CHECK) Main(org.abs_models.frontend.parser.Main) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) StringReader(java.io.StringReader) Model(org.abs_models.frontend.ast.Model) WITH_LOC_INF(org.abs_models.ABSTest.Config.WITH_LOC_INF) Assert.fail(org.junit.Assert.fail) InternalBackendException(org.abs_models.backend.common.InternalBackendException) WITHOUT_MODULE_NAME(org.abs_models.ABSTest.Config.WITHOUT_MODULE_NAME) DeltaModellingException(org.abs_models.frontend.delta.DeltaModellingException) Model(org.abs_models.frontend.ast.Model) Main(org.abs_models.frontend.parser.Main) File(java.io.File)

Example 2 with WrongProgramArgumentException

use of org.abs_models.common.WrongProgramArgumentException 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 3 with WrongProgramArgumentException

use of org.abs_models.common.WrongProgramArgumentException 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)

Aggregations

WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)3 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)2 ProductDecl (org.abs_models.frontend.ast.ProductDecl)2 Constraint (choco.kernel.model.constraints.Constraint)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 EXPECT_PARSE_ERROR (org.abs_models.ABSTest.Config.EXPECT_PARSE_ERROR)1 EXPECT_TYPE_ERROR (org.abs_models.ABSTest.Config.EXPECT_TYPE_ERROR)1 TYPE_CHECK (org.abs_models.ABSTest.Config.TYPE_CHECK)1 WITHOUT_DESUGARING_AFTER_TYPECHECK (org.abs_models.ABSTest.Config.WITHOUT_DESUGARING_AFTER_TYPECHECK)1 WITHOUT_MODULE_NAME (org.abs_models.ABSTest.Config.WITHOUT_MODULE_NAME)1 WITH_LOC_INF (org.abs_models.ABSTest.Config.WITH_LOC_INF)1 InternalBackendException (org.abs_models.backend.common.InternalBackendException)1