Search in sources :

Example 6 with ModifyClassModifier

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

the class ProductLineAnalysisHelper method isStronglyUnambiguous.

/*
     * A product line is strongly unambiguous if each set in the partition of
     * the delta modules specified in the product-line declaration is
     * consistent, that is, if one delta module in a set adds or removes a
     * class, no other delta module in the same set may add, remove or modify
     * the same class, and the modifications of the same class in different
     * delta modules in the same set have to be disjoint.
     */
public static boolean isStronglyUnambiguous(ProductLine pl, SemanticConditionList errors) {
    boolean result = true;
    Model model = pl.getModel();
    /*
        System.out.print("Delta partition: ");
        for (Set<String> set : pl.getDeltaPartition()) {
            System.out.print("{");
            for (String el : set)
                System.out.print(" " + el + " ");
            System.out.print("}   ");
        }
        System.out.println();
         */
    assert pl.getDeltaPartition() != null;
    for (Set<String> set : pl.getDeltaPartition()) {
        // Remember the names of classes and methods modified by deltas in
        // current set
        // { Module.Class -> { Method -> Delta } }
        // { Module.Class -> { "CLASS" -> Delta } }
        Map<String, Map<String, String>> cache = new HashMap<>();
        for (String deltaID : set) {
            // assumes the DeltaDecl corresponding to deltaID exists (wellFormedProductLine)
            DeltaDecl delta = model.getDeltaDeclsMap().get(deltaID);
            assert delta.getModuleModifiers() != null;
            for (ModuleModifier moduleModifier : delta.getModuleModifiers()) {
                if (moduleModifier instanceof ModifyClassModifier) {
                    // String methodID;
                    String prefix = ((ClassModifier) moduleModifier).getQualifiedName();
                    for (Modifier mod : ((ModifyClassModifier) moduleModifier).getModifiers()) {
                        if (mod instanceof DeltaTraitModifier) {
                            HashSet<String> methodIDSet = new HashSet<>();
                            ((DeltaTraitModifier) mod).collectMethodIDs(methodIDSet, model);
                            for (String methodID : methodIDSet) {
                                if (cache.containsKey(prefix)) {
                                    if (cache.get(prefix).containsKey(methodID)) {
                                        result = false;
                                        String otherDeltaID = cache.get(prefix).get(methodID);
                                        if (!deltaID.equals(otherDeltaID))
                                            errors.add(new TypeError(pl, ErrorMessage.AMBIGUOUS_PRODUCTLINE, pl.getName(), deltaID, otherDeltaID, prefix + ", method " + methodID));
                                        else
                                            // FIXME also a kind of ambiguity but needs a different error message
                                            ;
                                    } else if (cache.get(prefix).containsKey("CLASS")) {
                                        result = false;
                                        String otherDeltaID = cache.get(prefix).get("CLASS");
                                        if (!deltaID.equals(otherDeltaID))
                                            errors.add(new TypeError(pl, ErrorMessage.AMBIGUOUS_PRODUCTLINE, pl.getName(), deltaID, otherDeltaID, prefix));
                                        else
                                            // FIXME also a kind of ambiguity but needs a different error message
                                            ;
                                    } else {
                                        cache.get(prefix).put(methodID, deltaID);
                                    }
                                } else {
                                    cache.put(prefix, new HashMap<>());
                                    cache.get(prefix).put(methodID, deltaID);
                                }
                            }
                        }
                    }
                } else if (moduleModifier instanceof AddClassModifier || moduleModifier instanceof RemoveClassModifier) {
                    String prefix = ((ClassModifier) moduleModifier).getQualifiedName();
                    if (cache.containsKey(prefix)) {
                        result = false;
                        assert !cache.get(prefix).isEmpty();
                        String otherDeltaID = cache.get(prefix).values().iterator().next();
                        errors.add(new TypeError(pl, ErrorMessage.AMBIGUOUS_PRODUCTLINE, pl.getName(), deltaID, otherDeltaID, prefix));
                        System.out.println("3 ambiguity due to " + deltaID + "<>" + otherDeltaID);
                    } else {
                        cache.put(prefix, new HashMap<>());
                        cache.get(prefix).put("CLASS", deltaID);
                    }
                }
            }
        // TODO apply same reasoning to other elements: fields,
        // functions, ADTs, etc
        }
    }
    // TODO remove boolean result unless needed
    return result;
}
Also used : HashMap(java.util.HashMap) RemoveClassModifier(org.abs_models.frontend.ast.RemoveClassModifier) AddClassModifier(org.abs_models.frontend.ast.AddClassModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) ClassModifier(org.abs_models.frontend.ast.ClassModifier) AddClassModifier(org.abs_models.frontend.ast.AddClassModifier) RemoveClassModifier(org.abs_models.frontend.ast.RemoveClassModifier) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) ModuleModifier(org.abs_models.frontend.ast.ModuleModifier) Model(org.abs_models.frontend.ast.Model) TypeError(org.abs_models.frontend.analyser.TypeError) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) HashMap(java.util.HashMap) Map(java.util.Map) Modifier(org.abs_models.frontend.ast.Modifier) RemoveClassModifier(org.abs_models.frontend.ast.RemoveClassModifier) AddClassModifier(org.abs_models.frontend.ast.AddClassModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) ClassModifier(org.abs_models.frontend.ast.ClassModifier) ModuleModifier(org.abs_models.frontend.ast.ModuleModifier) HashSet(java.util.HashSet)

Example 7 with ModifyClassModifier

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

the class TraitTest method resolveTest4.

@Test
public void resolveTest4() {
    Model model = assertParse("module TestMod;" + "interface Inter {}" + "trait T = { " + "  Unit printLine_1(){println(\"I'm 1!\");}" + "  Unit printLine_2(){println(\"I'm 2!\");}" + "}" + "trait T2 = { " + "  Unit printLine_2(){println(\"I'm 2!\");}" + "  Unit printLine_3(){println(\"I'm 3!\");}" + "}" + "class InterImpl(Inter inter) implements Inter {   }" + "" + "delta D3;" + "modifies class TestMod.InterImpl{" + "    adds Int i = 0;" + "    modifies T adds T2 removes {Unit printLine_2();}" + "}");
    ClassDecl cls = (ClassDecl) findDecl(model, "TestMod", "InterImpl");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    model.applyTraits();
    DeltaDecl delta = findDelta(model, "D3");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    ModifyClassModifier mm = (ModifyClassModifier) delta.getModuleModifier(0);
    DeltaTraitModifier dml = (DeltaTraitModifier) mm.getModifier(1);
    ModifyMethodModifier mcl = (ModifyMethodModifier) dml.getMethodModifier();
    TraitExpr expr = mcl.getTraitExpr();
    TraitExpr set = expr.resolve(cls.getModuleDecl());
    assertTrue("expected 2, got " + set.getChild(0).getNumChild(), set.getChild(0).getNumChild() == 2);
    assertThat(set, instanceOf(TraitSetExpr.class));
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) ModifyMethodModifier(org.abs_models.frontend.ast.ModifyMethodModifier) TraitExpr(org.abs_models.frontend.ast.TraitExpr) Model(org.abs_models.frontend.ast.Model) TraitSetExpr(org.abs_models.frontend.ast.TraitSetExpr) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 8 with ModifyClassModifier

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

the class TraitTest method collapseTest3.

@Test
public void collapseTest3() {
    Model model = assertParse("module TestMod;" + "trait T = {" + "Unit printLine_0(){println(\"I'm 0!\");}" + "Unit printLine_1(){println(\"I'm 1!\");}" + "}" + "trait T2 = {" + "Unit printLine_1(){println(\"I'm 1!\");}" + "}" + "class InterImpl(Inter inter){ }" + "" + "delta D3;" + "modifies class TestMod.InterImpl{" + "adds Int i = 0;" + "adds T " + "modifies T2 " + "removes { Unit printLine_0(); }" + "                removes { Unit printLine_1(); }" + "adds {" + "Unit printLine_2(){println(\"I'm 2!\");}" + "Unit printLine_3(){println(\"I'm 3!\");}" + "}" + "removes { Unit printLine_2(); }" + "removes { Unit printLine_3(); }" + "}");
    ClassDecl cls = (ClassDecl) findDecl(model, "TestMod", "InterImpl");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    model.applyTraits();
    model.collapseTraitModifiers();
    DeltaDecl delta = findDelta(model, "D3");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    ModifyClassModifier mm = (ModifyClassModifier) delta.getModuleModifier(0);
    assertTrue(mm.getModifierList().getNumChild() == 8);
    DeltaTraitModifier dml = (DeltaTraitModifier) mm.getModifier(1);
    AddMethodModifier mcl = (AddMethodModifier) dml.getMethodModifier();
    TraitExpr set = mcl.getTraitExpr();
    assertTrue(set.getChild(0).getNumChild() == 2);
    assertThat(set, instanceOf(TraitSetExpr.class));
    DeltaTraitModifier dml2 = (DeltaTraitModifier) mm.getModifier(2);
    ModifyMethodModifier mcl2 = (ModifyMethodModifier) dml2.getMethodModifier();
    TraitExpr set2 = mcl2.getTraitExpr();
    assertTrue(set2.getChild(0).getNumChild() == 1);
    assertThat(set2, instanceOf(TraitSetExpr.class));
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) TraitExpr(org.abs_models.frontend.ast.TraitExpr) ModifyMethodModifier(org.abs_models.frontend.ast.ModifyMethodModifier) AddMethodModifier(org.abs_models.frontend.ast.AddMethodModifier) Model(org.abs_models.frontend.ast.Model) TraitSetExpr(org.abs_models.frontend.ast.TraitSetExpr) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 9 with ModifyClassModifier

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

the class TraitTest method resolveTest2.

@Test
public void resolveTest2() {
    // this tests that the given delta is wrong (as we take T union T2 and thus have printLine_2 twice)
    Model model = assertParse("module TestMod;" + "interface Inter {}" + "trait T = { " + "  Unit printLine_1(){println(\"I'm 1!\");}" + "  Unit printLine_2(){println(\"I'm 2!\");}" + "}" + "trait T2 = { " + "  Unit printLine_2(){println(\"I'm 2!\");}" + "  Unit printLine_3(){println(\"I'm 3!\");}" + "}" + "class InterImpl(Inter inter) implements Inter {   }" + "" + "delta D3;" + "modifies class TestMod.InterImpl{" + "    adds Int i = 0;" + "    modifies T adds T2" + "}");
    ClassDecl cls = (ClassDecl) findDecl(model, "TestMod", "InterImpl");
    assertNotNull(cls);
    assertTrue(cls.getMethods().getNumChild() == 0);
    model.applyTraits();
    DeltaDecl delta = findDelta(model, "D3");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    ModifyClassModifier mm = (ModifyClassModifier) delta.getModuleModifier(0);
    DeltaTraitModifier dml = (DeltaTraitModifier) mm.getModifier(1);
    ModifyMethodModifier mcl = (ModifyMethodModifier) dml.getMethodModifier();
    TraitExpr expr = mcl.getTraitExpr();
    TraitExpr set = expr.resolve(cls.getModuleDecl());
    assertTrue("expected 2, got " + set.getChild(0).getNumChild(), set.getChild(0).getNumChild() == 2);
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) ModifyMethodModifier(org.abs_models.frontend.ast.ModifyMethodModifier) TraitExpr(org.abs_models.frontend.ast.TraitExpr) Model(org.abs_models.frontend.ast.Model) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) DeltaTraitModifier(org.abs_models.frontend.ast.DeltaTraitModifier) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 10 with ModifyClassModifier

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

the class TraitTest method addModifyModifierAtRuntimeBackComp.

@Test
public void addModifyModifierAtRuntimeBackComp() {
    Model model = assertParse("module M;" + "class C { Unit m(){skip;} }");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    MethodSig sig = AbsASTBuilderUtil.createMethodSig("m", AbsASTBuilderUtil.getUnit());
    MethodImpl impl = new MethodImpl(sig, new Block(new List<>(), new List<>(new SkipStmt(), new SkipStmt())));
    ModifyMethodModifier opr = new ModifyMethodModifier(impl);
    assertNotNull(opr.getMethodImpl());
    ModifyClassModifier mcn = new ModifyClassModifier();
    mcn.setName("M.C");
    DeltaAccess acc = new DeltaAccess(cls.getModuleDecl().getName());
    DeltaDecl dd = new DeltaDecl();
    dd.setName("MyDelta");
    dd.setImportedModule(acc);
    dd.addModuleModifier(mcn);
    mcn.addModifier(opr);
    mcn.setParent(dd);
    acc.setParent(dd);
    opr.setParent(mcn);
    sig.setParent(opr);
    CompilationUnit cu = model.getCompilationUnitList().getChild(0);
    cu.addDeltaDecl(dd);
    dd.setParent(cu);
    model.applyDelta(dd);
    assertEquals(1, cls.getMethods().getNumChild());
    assertEquals(2, cls.getMethod(0).getBlock().getNumChild());
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ModifyMethodModifier(org.abs_models.frontend.ast.ModifyMethodModifier) DeltaAccess(org.abs_models.frontend.ast.DeltaAccess) Model(org.abs_models.frontend.ast.Model) Block(org.abs_models.frontend.ast.Block) List(org.abs_models.frontend.ast.List) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) SkipStmt(org.abs_models.frontend.ast.SkipStmt) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Aggregations

DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)13 Model (org.abs_models.frontend.ast.Model)13 ModifyClassModifier (org.abs_models.frontend.ast.ModifyClassModifier)13 ClassDecl (org.abs_models.frontend.ast.ClassDecl)12 Test (org.junit.Test)12 DeltaTraitModifier (org.abs_models.frontend.ast.DeltaTraitModifier)8 ModifyMethodModifier (org.abs_models.frontend.ast.ModifyMethodModifier)7 TraitExpr (org.abs_models.frontend.ast.TraitExpr)7 AddMethodModifier (org.abs_models.frontend.ast.AddMethodModifier)5 TraitSetExpr (org.abs_models.frontend.ast.TraitSetExpr)5 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)3 DeltaAccess (org.abs_models.frontend.ast.DeltaAccess)3 List (org.abs_models.frontend.ast.List)3 MethodSig (org.abs_models.frontend.ast.MethodSig)3 Block (org.abs_models.frontend.ast.Block)2 MethodImpl (org.abs_models.frontend.ast.MethodImpl)2 SkipStmt (org.abs_models.frontend.ast.SkipStmt)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1