Search in sources :

Example 1 with Feature

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

the class ProductDeclarationTest method productUnion.

@Test
public void productUnion() throws WrongProgramArgumentException {
    Model model = assertParse("product P1 = {F1, F2, F3} || {F4};");
    model.evaluateAllProductDeclarations();
    ProductDecl p = model.findProduct("P1");
    Product impl = p.getProduct();
    assertEquals(4, impl.getNumFeature());
    Set<String> expected = new HashSet<>(Arrays.asList("F1", "F2", "F3", "F4"));
    Set<String> actual = new HashSet<>();
    for (Feature f : impl.getFeatures()) actual.add(f.getName());
    assertEquals(expected, actual);
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Model(org.abs_models.frontend.ast.Model) Product(org.abs_models.frontend.ast.Product) Feature(org.abs_models.frontend.ast.Feature) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Feature

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

the class ProductDeclarationTest method oldProductSyntax.

@Test
public void oldProductSyntax() throws WrongProgramArgumentException {
    Model model = assertParse("product P1(F1, F2, F3);");
    model.evaluateAllProductDeclarations();
    ProductDecl p = model.findProduct("P1");
    Product impl = p.getProduct();
    assertEquals(3, impl.getNumFeature());
    Set<String> expected = new HashSet<>(Arrays.asList("F1", "F2", "F3"));
    Set<String> actual = new HashSet<>();
    for (Feature f : impl.getFeatures()) actual.add(f.getName());
    assertEquals(expected, actual);
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Model(org.abs_models.frontend.ast.Model) Product(org.abs_models.frontend.ast.Product) Feature(org.abs_models.frontend.ast.Feature) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Feature

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

the class ProductDeclarationTest method productIntersect.

@Test
public void productIntersect() throws WrongProgramArgumentException {
    Model model = assertParse("product P1 = {F1, F2, F3} && {F2, F3};");
    model.evaluateAllProductDeclarations();
    ProductDecl p = model.findProduct("P1");
    Product impl = p.getProduct();
    assertEquals(2, impl.getNumFeature());
    Set<String> expected = new HashSet<>(Arrays.asList("F2", "F3"));
    Set<String> actual = new HashSet<>();
    for (Feature f : impl.getFeatures()) actual.add(f.getName());
    assertEquals(expected, actual);
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Model(org.abs_models.frontend.ast.Model) Product(org.abs_models.frontend.ast.Product) Feature(org.abs_models.frontend.ast.Feature) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Feature

use of org.abs_models.frontend.ast.Feature 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 5 with Feature

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

the class ProductLineAnalysisHelper method buildAndPrintAllConfigurations.

/*
     * 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 buildAndPrintAllConfigurations(Model m) {
    long timeSum = 0;
    for (Product product : m.getProductList()) {
        long time0 = System.currentTimeMillis();
        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();
        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);
        System.out.println("\u23F1 Time: " + (time1 - time0) + " | " + (time2 - time1) + " | " + (time3 - time2) + " | " + (time4 - time3) + " | " + "Total(s): " + ((time4 - time0) / 1000.0));
    }
    System.out.println("\u23F1 Flattening total time (s): " + timeSum / 1000.0);
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) IntVal(org.abs_models.frontend.ast.IntVal) Product(org.abs_models.frontend.ast.Product) Feature(org.abs_models.frontend.ast.Feature) Model(org.abs_models.frontend.ast.Model) AttrAssignment(org.abs_models.frontend.ast.AttrAssignment) ChocoSolver(org.abs_models.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Aggregations

Feature (org.abs_models.frontend.ast.Feature)11 HashSet (java.util.HashSet)10 Model (org.abs_models.frontend.ast.Model)10 Product (org.abs_models.frontend.ast.Product)10 ProductDecl (org.abs_models.frontend.ast.ProductDecl)10 Test (org.junit.Test)9 Constraint (choco.kernel.model.constraints.Constraint)1 ArrayList (java.util.ArrayList)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1 AttrAssignment (org.abs_models.frontend.ast.AttrAssignment)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 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)1 IntVal (org.abs_models.frontend.ast.IntVal)1 List (org.abs_models.frontend.ast.List)1 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)1