Search in sources :

Example 1 with Select

use of org.lara.language.specification.dsl.Select in project lara-framework by specs-feup.

the class ASTPointcut method organize.

@Override
public Object organize(Object obj) {
    final ASTSelect select = ((ASTSelect) obj);
    final ASTAspectDef aspdef = this.getAspectDefForDeclStmt("Select");
    // final ASTAspectDef aspdef = (ASTAspectDef) (select.parent);
    final LaraC lara = aspdef.getLara();
    // final JoinPointModel joinPointModel = lara.languageSpec().getJpModel();
    // This type was set previously, I hope!
    // setType(joinPointModel.getLastPointcutType());
    var = new Variable(reference, Types.Joinpoint);
    select.putPointcut(reference, this);
    if (getChildren() == null) {
        return null;
    }
    final SimpleNode firstChild = (SimpleNode) getChildren()[0];
    SimpleNode pointcutNode = null;
    if (firstChild instanceof ASTPointcutFilters) {
        final ASTPointcutFilters pcp = (ASTPointcutFilters) firstChild;
        pcp.organize(this, type, lara);
        if (getChildren().length > 1) {
            pointcutNode = (SimpleNode) getChildren()[1];
        } else {
            return null;
        }
    } else {
        pointcutNode = firstChild;
    }
    final String jpType = getType();
    final String selectName = pointcutNode.value.toString();
    // final PairList<String, String> path2 = joinPointModel.getPath(jpType, selectName, validateChain);
    final SelectionPathV2 selPath = new Selector(lara.languageSpec()).selectionPath(jpType, selectName, validateChain);
    if ((!selPath.hasPath() || selPath.getPath().isEmpty()) && validateChain) {
        throw newException("The following pointcut chain is not correct: \"" + value + "\"->\"" + pointcutNode.value + "\"");
    }
    if (selPath.hasSecondaryPath()) {
        getLara().warnln("More than one path was found for select: " + selPath.toString());
    }
    List<Select> path = selPath.getReversedPath();
    ASTPointcut pointcutNode2 = (ASTPointcut) pointcutNode;
    if (path != null) {
        if (path.size() > 1) {
            int i = getChildren().length - 1;
            SimpleNode nextParent = this;
            for (int j = 0; j < path.size() - 1; j++) {
                final ASTPointcut pc = new ASTPointcut(id);
                final String pcName = path.get(j).getSelectName();
                // final String pcName = path.get(j).getAlias();
                // final String pcType = path.get(j).getClazz().getClazz();
                final String pcType = path.get(j).getClazz().getName();
                pc.jjtSetValue(pcName);
                pc.setReference("$" + pcName + LARACConstantPool.HIDDEN_TAG + select.getHiddenCount());
                pc.setType(pcType);
                nextParent.associateChild(pc, i);
                nextParent = pc;
                i = 0;
            }
            nextParent.associateChild(pointcutNode, i);
        }
        if (!path.isEmpty()) {
            // pointcutNode2.setType(path.get(path.size() - 1).getClazz().getClazz());
            pointcutNode2.setType(path.get(path.size() - 1).getClazz().getName());
        } else {
            pointcutNode2.setType(selectName);
        }
    } else {
        pointcutNode2.setValidateChain(false);
    }
    // ASTPointcut pointcutNode2 = (ASTPointcut) pointcutNode;
    // if (path != null) {
    // if (path.size() > 1) {
    // int i = getChildren().length - 1;
    // SimpleNode nextParent = this;
    // for (int j = 0; j < path.size() - 1; j++) {
    // final ASTPointcut pc = new ASTPointcut(id);
    // final String pcName = path.get(j).getLeft();
    // final String pcType = path.get(j).getRight();
    // pc.jjtSetValue(pcName);
    // pc.setReference("$" + pcName + LARACConstantPool.HIDDEN_TAG + select.getHiddenCount());
    // pc.setType(pcType);
    // nextParent.associateChild(pc, i);
    // nextParent = pc;
    // i = 0;
    // }
    // nextParent.associateChild(pointcutNode, i);
    // }
    // if (!path.isEmpty()) {
    // pointcutNode2.setType(path.last().getRight());
    // } else {
    // pointcutNode2.setType(selectName);
    // }
    // } else {
    // pointcutNode2.setValidateChain(false);
    // }
    pointcutNode.organize(obj);
    return null;
}
Also used : LaraC(larac.LaraC) Variable(larac.objects.Variable) SelectionPathV2(utils.SelectionPathV2) Select(org.lara.language.specification.dsl.Select) Selector(utils.Selector)

Example 2 with Select

use of org.lara.language.specification.dsl.Select in project lara-framework by specs-feup.

the class LangSpecsXmlParser method parse.

public static LanguageSpecificationV2 parse(InputStream joinPointModel, InputStream attributeModel, InputStream actionModel, boolean validate) {
    // System.out.println("JP SCHEMA: " + SchemaResource.JOIN_POINT_SCHEMA.read());
    // System.out.println("JP SCHEMA: " + SchemaResource.JOIN_POINT_SCHEMA.getResource());
    var jpSchema = validate ? SchemaResource.JOIN_POINT_SCHEMA.toStream() : null;
    var attrSchema = validate ? SchemaResource.ATTRIBUTE_SCHEMA.toStream() : null;
    var actionSchema = validate ? SchemaResource.ACTION_SCHEMA.toStream() : null;
    var joinPointModelNode = XmlDocument.newInstance(joinPointModel, jpSchema);
    var attributeModelNode = XmlDocument.newInstance(attributeModel, attrSchema);
    var actionModelNode = XmlDocument.newInstance(actionModel, actionSchema);
    // Setup global JoinPointClass
    LanguageSpecificationV2 langSpecV2 = new LanguageSpecificationV2();
    JoinPointClass global = JoinPointClass.globalJoinPoint(langSpecV2);
    langSpecV2.setGlobal(global);
    // Initialize types (typedef, enums), to have access to available names
    for (var type : attributeModelNode.getElementsByName("object")) {
        var typeDef = new TypeDef(type.getAttribute("name"));
        langSpecV2.add(typeDef);
        setOptional(type.getAttribute("tooltip"), typeDef::setToolTip);
    }
    for (var type : attributeModelNode.getElementsByName("enum")) {
        var enumDef = new EnumDef(type.getAttribute("name"));
        langSpecV2.add(enumDef);
        setOptional(type.getAttribute("tooltip"), enumDef::setToolTip);
        List<EnumValue> valuesList = toEnumValues(type.getElementsByName("value"), langSpecV2);
        enumDef.setValues(valuesList);
    }
    List<JoinPointClass> jps = new ArrayList<>();
    for (var jpNode : joinPointModelNode.getElementsByName("joinpoint")) {
        var jp = new JoinPointClass(jpNode.getAttribute("class"), langSpecV2);
        setOptional(jpNode.getAttribute("tooltip"), jp::setToolTip);
        jps.add(jp);
    }
    Collections.sort(jps);
    jps.stream().forEach(langSpecV2::add);
    var joinpoints = joinPointModelNode.getElementsByName("joinpoints").get(0);
    langSpecV2.setRoot(joinpoints.getAttribute("root_class"));
    setOptional(joinpoints.getAttribute("root_alias"), langSpecV2::setRootAlias);
    // Map of actions according to class
    MultiMap<String, XmlElement> joinPointActions = new MultiMap<>();
    List<XmlElement> globalActions = new ArrayList<>();
    for (var actionNode : actionModelNode.getElementsByName("action")) {
        var classNames = actionNode.getAttribute("class");
        // Global actions do not have a class value, or its value is '*'
        if (classNames.isEmpty() || classNames.equals("*")) {
            globalActions.add(actionNode);
            continue;
        }
        // System.out.println("CLASS NAMES: " + classNames);
        for (String className : classNames.split(",")) {
            // System.out.println("NAME: " + className);
            joinPointActions.add(className.strip(), actionNode);
        }
    }
    populateGlobal(joinPointModelNode, attributeModelNode, actionModelNode, langSpecV2, global, globalActions);
    // Populate TypeDef
    for (var typeNode : attributeModelNode.getElementsByName("object")) {
        TypeDef typeDef = langSpecV2.getTypeDefs().get(typeNode.getAttribute("name"));
        List<Attribute> attributesList = convertAttributes(typeNode.getElementsByName("attribute"), langSpecV2);
        typeDef.setFields(attributesList);
    }
    for (var jpNode : joinPointModelNode.getElementsByName("joinpoint")) {
        String jpClass = jpNode.getAttribute("class");
        JoinPointClass jp = langSpecV2.getJoinPoint(jpClass);
        String extendsType = jpNode.getAttribute("extends");
        if (!extendsType.isEmpty()) {
            jp.setExtend(langSpecV2.getJoinPoint(extendsType));
        } else {
            jp.setExtend(global);
        }
        // Obtain attribute nodes from artifacts
        List<XmlElement> artifactNodes = attributeModelNode.getElementsByName("artifact").stream().filter(attribute -> attribute.getAttribute("class").equals(jpClass)).collect(Collectors.toList());
        var attributeNodes = artifactNodes.stream().flatMap(art -> art.getElementsByName("attribute").stream()).collect(Collectors.toList());
        // Add attributes
        jp.setAttributes(convertAttributes(attributeNodes, langSpecV2));
        // Add selects
        jp.setSelects(convertSelects(langSpecV2, jpNode.getElementsByName("select")));
        // Add actions
        jp.setActions(convertActions(langSpecV2, joinPointActions.get(jpClass)));
        // Set default attributes
        for (var artifact : attributeModelNode.getElementsByName("artifact")) {
            var defaultValue = artifact.getAttribute("default");
            if (defaultValue.isEmpty()) {
                continue;
            }
            // Get corresponding join point and set default
            // System.out.println("ARTIFACT CLASS: " + artifact.getAttribute("class"));
            // System.out.println("JP: " + langSpecV2.getJoinPoint(artifact.getAttribute("class")));
            var artifactJp = langSpecV2.getJoinPoint(artifact.getAttribute("class"));
            if (artifactJp == null) {
                SpecsLogs.info("Artifact without join point: " + artifact.getAttribute("class"));
                continue;
            }
            artifactJp.setDefaultAttribute(defaultValue);
        // System.out.println("SETTING DEFAULT '" + defaultValue + "' for JP " +
        // artifact.getAttribute("class"));
        }
    }
    // Add default global attributes (e.g., joinPointType, instanceOf)
    addDefaultGlobalAttributes(langSpecV2);
    return langSpecV2;
}
Also used : Arrays(java.util.Arrays) IType(org.lara.language.specification.dsl.types.IType) Attribute(org.lara.language.specification.dsl.Attribute) ResourceProvider(pt.up.fe.specs.util.providers.ResourceProvider) LanguageSpecificationV2(org.lara.language.specification.dsl.LanguageSpecificationV2) ArrayList(java.util.ArrayList) Action(org.lara.language.specification.dsl.Action) Parameter(org.lara.language.specification.dsl.Parameter) Select(org.lara.language.specification.dsl.Select) PrimitiveClasses(org.lara.language.specification.dsl.types.PrimitiveClasses) XmlDocument(pt.up.fe.specs.util.xml.XmlDocument) JoinPointClass(org.lara.language.specification.dsl.JoinPointClass) TypeDef(org.lara.language.specification.dsl.types.TypeDef) Declaration(org.lara.language.specification.dsl.Declaration) MultiMap(pt.up.fe.specs.util.collections.MultiMap) SpecsIo(pt.up.fe.specs.util.SpecsIo) EnumValue(org.lara.language.specification.dsl.types.EnumValue) XmlElement(pt.up.fe.specs.util.xml.XmlElement) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) List(java.util.List) SpecsLogs(pt.up.fe.specs.util.SpecsLogs) Collections(java.util.Collections) InputStream(java.io.InputStream) EnumDef(org.lara.language.specification.dsl.types.EnumDef) EnumDef(org.lara.language.specification.dsl.types.EnumDef) Attribute(org.lara.language.specification.dsl.Attribute) EnumValue(org.lara.language.specification.dsl.types.EnumValue) ArrayList(java.util.ArrayList) LanguageSpecificationV2(org.lara.language.specification.dsl.LanguageSpecificationV2) MultiMap(pt.up.fe.specs.util.collections.MultiMap) TypeDef(org.lara.language.specification.dsl.types.TypeDef) JoinPointClass(org.lara.language.specification.dsl.JoinPointClass) XmlElement(pt.up.fe.specs.util.xml.XmlElement)

Example 3 with Select

use of org.lara.language.specification.dsl.Select in project lara-framework by specs-feup.

the class Selector method selectWithMiddlePath.

private Stack<Select> selectWithMiddlePath(String targetJP, JoinPointClass source, SelectionPathV2 selPath) {
    Stack<Select> path = new Stack<>();
    final Set<JoinPointClass> ignoreSet = new HashSet<>();
    ignoreSet.add(source);
    // for (final Select select : source.getSelect()) {
    for (final Select select : source.getSelects()) {
        // final JoinPointType next = select.getClazz();
        final JoinPointClass next = select.getClazz();
        final Stack<Select> pathAux = selectionPathAux(next, targetJP, new ArrayList<JoinPointClass>(), ignoreSet, selPath);
        if (!pathAux.isEmpty()) {
            pathAux.push(select);
            if (!path.isEmpty()) {
                if (!alreadyFound) {
                    // System.out.println(
                    // "More than one path for inital join point '" + targetJP + "'. Two of then are: ");
                    // System.out.println("\t1. " + path + "->" + targetJP);
                    // System.out.println("\t2. " + pathAux + "->" + targetJP);
                    int pos = 1;
                    Select first = path.get(0);
                    Select second = pathAux.get(0);
                    while (first.equals(second)) {
                        // && pos < max) { <-- this should not happen in these conditions
                        first = path.get(pos);
                        second = pathAux.get(pos);
                        pos++;
                    }
                    // boolean firstWOalias = first.getAlias().equals(first.getClazz().getClazz());
                    // boolean secondWOAlias = second.getAlias().equals(second.getClazz().getClazz());
                    boolean firstWOalias = first.getSelectName().equals(first.getClazz().getName());
                    boolean secondWOAlias = second.getSelectName().equals(second.getClazz().getName());
                    if (first.getClazz().equals(second.getClazz())) {
                        selPath.setTieBreakReason("use the one with specific join point type (i.e. without label)");
                        if (!firstWOalias && secondWOAlias) {
                            selPath.setSecondaryPath(path);
                            path = pathAux;
                        } else {
                            selPath.setSecondaryPath(pathAux);
                        }
                    } else {
                        selPath.setTieBreakReason("use the first path found as primary (from a depth first search)");
                        selPath.setSecondaryPath(pathAux);
                    }
                    alreadyFound = true;
                }
                break;
            }
            path = pathAux;
        } else {
            ignoreSet.add(next);
        }
    }
    // If extends another join point
    if (path.isEmpty() && source.getExtend().isPresent()) {
        // source = (JoinPointType) source.getExtends();
        source = source.getExtend().get();
        return selectWithMiddlePath(targetJP, source, selPath);
    }
    return path;
}
Also used : Select(org.lara.language.specification.dsl.Select) JoinPointClass(org.lara.language.specification.dsl.JoinPointClass) Stack(java.util.Stack) HashSet(java.util.HashSet)

Example 4 with Select

use of org.lara.language.specification.dsl.Select in project lara-framework by specs-feup.

the class ASTPointcut method organizeFirst.

@Override
public Object organizeFirst(Object obj, int i) {
    final ASTSelect select = ((ASTSelect) obj);
    // final ASTAspectDef aspdef = (ASTAspectDef) (select.parent);
    final ASTAspectDef aspdef = this.getAspectDefForDeclStmt("Select");
    final LaraC lara = aspdef.getLara();
    // if (!joinPointModel.contains(value.toString())) {
    if (!lara.languageSpec().hasJoinPointName(value.toString())) {
        final Variable var = lookup(value.toString());
        if (var.isNotFound() && !getLara().getOptions().isDocumentationMode()) {
            throw new LARACompilerException("The join point \"" + value + "\" does not exist in the join point model");
        }
        // getLara().warnln("Using variable '" + value + "' to create select " + select.label
        // + ": Auto-complete could not be used. Pointcut chain has to be complete and attributes must be
        // defined.");
        type = LARACConstantPool.USER_DEFINED;
        // point
        if (getChildren() != null) {
            final SimpleNode firstChild = (SimpleNode) getChildren()[0];
            if (firstChild instanceof ASTPointcutFilters) {
                throw new LARACompilerException("Cannot use join point filters in the variable used as starting join point");
            }
            if (!(firstChild instanceof ASTPointcut)) {
                throw new LARACompilerException("Only a pointcut can be used when using a variable as starting join point. Type used: " + firstChild.toString());
            }
            ASTPointcut childAsPointCut = (ASTPointcut) firstChild;
            childAsPointCut.setType(firstChild.value.toString());
            childAsPointCut.setValidateChain(false);
            // TODO- if the next child cannot be found in the join point model then
            childAsPointCut.organize(obj);
        // do not
        // validate the child. This should be recursive for the rest of the chain,
        // until a valid join point is found
        }
        return null;
    // getLara().warnln("Could not solve select for join point
    // "+value+"");
    // throw newException("The join point \"" + value + "\" does not
    // exist in the join point model");
    }
    // final PairList<String, String> path2 = joinPointModel.getPath(value.toString());
    final SelectionPathV2 selPath = new Selector(lara.languageSpec()).selectionPath(value.toString());
    if (!selPath.hasPath() || selPath.getPath().isEmpty()) {
        throw new LARACompilerException("No path exists for was found for join point '" + value + "'");
    }
    if (selPath.hasSecondaryPath()) {
        getLara().warnln("More than one path was found for select: " + selPath.toString());
    }
    List<Select> path = selPath.getReversedPath();
    for (int j = 0; j < path.size() - 1; j++) {
        final ASTPointcut pc = new ASTPointcut(id);
        final String pcName = path.get(j).getSelectName();
        // final String pcName = path.get(j).getAlias();
        // final String pcType = path.get(j).getClazz().getClazz();
        final String pcType = path.get(j).getClazz().getName();
        pc.jjtSetValue(pcName);
        pc.setReference("$" + pcName + LARACConstantPool.HIDDEN_TAG + select.getHiddenCount());
        pc.setType(pcType);
        ((SimpleNode) parent).associateChild(pc, i);
        pc.associateChild(this, 0);
        i = 0;
    }
    // setType(path.get(path.size() - 1).getClazz().getClazz());
    setType(path.get(path.size() - 1).getClazz().getName());
    // if (path == null || path.isEmpty()) {
    // throw new LARACompilerException("No path exists for was found for join pont '" + value + "'");
    // }
    // 
    // for (int j = 0; j < path.size() - 1; j++) {
    // final ASTPointcut pc = new ASTPointcut(id);
    // final String pcName = path.get(j).getLeft();
    // final String pcType = path.get(j).getRight();
    // pc.jjtSetValue(pcName);
    // pc.setReference("$" + pcName + LARACConstantPool.HIDDEN_TAG + select.getHiddenCount());
    // pc.setType(pcType);
    // ((SimpleNode) parent).associateChild(pc, i);
    // pc.associateChild(this, 0);
    // i = 0;
    // }
    // setType(path.last().getRight());
    this.organize(obj);
    return null;
}
Also used : LaraC(larac.LaraC) Variable(larac.objects.Variable) SelectionPathV2(utils.SelectionPathV2) LARACompilerException(larac.exceptions.LARACompilerException) Select(org.lara.language.specification.dsl.Select) Selector(utils.Selector)

Example 5 with Select

use of org.lara.language.specification.dsl.Select in project lara-framework by specs-feup.

the class LanguageSpecificationSideBar method getSelects.

private List<Select> getSelects(JoinPointClass joinPoint) {
    switch(sortingMethod) {
        case ALPHABETICALLY:
            return getAlphabetical(joinPoint, jp -> jp.getSelects());
        case HIERARCHICALLY:
            List<Select> selects = new ArrayList<>();
            getHierarchical(joinPoint, selects, jp -> jp.getSelectsSelf(), jp -> new Select(jp, SEPARATOR_TYPE));
            return selects;
        default:
            throw new NotImplementedException(sortingMethod);
    }
}
Also used : NotImplementedException(pt.up.fe.specs.util.exceptions.NotImplementedException) Select(org.lara.language.specification.dsl.Select) ArrayList(java.util.ArrayList)

Aggregations

Select (org.lara.language.specification.dsl.Select)11 JoinPointClass (org.lara.language.specification.dsl.JoinPointClass)7 ArrayList (java.util.ArrayList)4 Stack (java.util.Stack)4 HashSet (java.util.HashSet)2 LaraC (larac.LaraC)2 Variable (larac.objects.Variable)2 Action (org.lara.language.specification.dsl.Action)2 Attribute (org.lara.language.specification.dsl.Attribute)2 SelectionPathV2 (utils.SelectionPathV2)2 Selector (utils.Selector)2 InputStream (java.io.InputStream)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 LARACompilerException (larac.exceptions.LARACompilerException)1 Declaration (org.lara.language.specification.dsl.Declaration)1 LanguageSpecificationV2 (org.lara.language.specification.dsl.LanguageSpecificationV2)1