Search in sources :

Example 6 with Variable

use of org.whole.lang.commons.model.Variable in project whole by wholeplatform.

the class GenericTemplateInterpreterVisitor method visit.

public void visit(IEntity entity) {
    IEntity adaptee = entity.wGetAdaptee(false);
    EntityDescriptor<?> ed = adaptee.wGetEntityDescriptor();
    // TODO workaround move into a separate visitor
    if (ed.getLanguageKit().getURI().equals(CommonsLanguageKit.URI)) {
        switch(ed.getOrdinal()) {
            case CommonsEntityDescriptorEnum.Resolver_ord:
                break;
            case CommonsEntityDescriptorEnum.SameStageFragment_ord:
                stagedVisit(adaptee.wGetRoot());
                if (getStage() > 0)
                    setResult(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.SameStageFragment, // CommonsEntityFactory.instance.createSameStageFragment(
                    EntityUtils.cloneIfParented(getResult())));
                return;
            case CommonsEntityDescriptorEnum.RootFragment_ord:
            case CommonsEntityDescriptorEnum.StageDownFragment_ord:
                setResult(null);
                IEntity oldSelfEntity = getBindings().wGet("self");
                final int stageShift0 = -1;
                stagedVisit(adaptee.wGetRoot(), stageShift0);
                if (getBindings().wGet("self") != oldSelfEntity)
                    getBindings().wDef("self", oldSelfEntity);
                if (getStage() + stageShift0 > 0 && !isResultIterator())
                    setResult(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.StageDownFragment, // CommonsEntityFactory.instance.createStageDownFragment(
                    EntityUtils.cloneIfParented(getResult())));
                return;
            case CommonsEntityDescriptorEnum.StageUpFragment_ord:
                final int stageShift = +1;
                stagedVisit(adaptee.wGetRoot(), stageShift);
                if (getStage() > 0) {
                    setResult(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.StageUpFragment, // CommonsEntityFactory.instance.createStageUpFragment(
                    EntityUtils.cloneIfParented(BehaviorUtils.evaluateResult(getBindings()))));
                }
                return;
            case CommonsEntityDescriptorEnum.Variable_ord:
            case CommonsEntityDescriptorEnum.InlineVariable_ord:
                Variable variable = (Variable) adaptee;
                String varName = variable.getVarName().getValue();
                IEntity value = getBindings().wGet(varName);
                if (value != null) {
                    if (BindingManagerFactory.instance.isVoid(value))
                        setResult(value);
                    else {
                        QuantifierEnum.Value quantifierValue = variable.getQuantifier().getValue();
                        if (quantifierValue.isComposite()) {
                            Variable newVariable = EntityUtils.clone(variable);
                            newVariable.getQuantifier().setValue(quantifierValue.toOptional());
                            if (EntityUtils.isInlineVariable(variable)) {
                                // TODO constantChildIterator should call EntityUtils.convert(childValue, varType)
                                setResultIterator(IteratorFactory.sequenceIterator(IteratorFactory.constantChildIterator(value), IteratorFactory.constantIterator(newVariable, true)));
                            } else {
                                EntityDescriptor<?> varType = variable.getVarType().getValue();
                                try {
                                    setResultIterator(IteratorFactory.sequenceIterator(IteratorFactory.constantIterator(EntityUtils.convertCloneIfParented(value, varType), true), IteratorFactory.constantIterator(newVariable, true)));
                                } catch (IllegalArgumentException e) {
                                    throw new SubstituteException(variable, value.wGetEntityDescriptor());
                                }
                            }
                        } else {
                            if (EntityUtils.isInlineVariable(variable)) {
                                // TODO constantChildIterator should call EntityUtils.convert(childValue, varType)
                                setResultIterator(IteratorFactory.constantChildIterator(value));
                            } else {
                                EntityDescriptor<?> varType = variable.getVarType().getValue();
                                try {
                                    setResult(EntityUtils.convertCloneIfParented(value, varType));
                                } catch (IllegalArgumentException e) {
                                    throw new SubstituteException(variable, value.wGetEntityDescriptor());
                                }
                            }
                        }
                    }
                    return;
                }
        }
    }
    IEntity result = ((InternalIEntity) adaptee).wShallowClone();
    IEntity oldSelfEntity2 = getBindings().wGet("self");
    for (int i = 0; i < result.wSize(); i++) {
        int resultSize = result.wSize();
        visit(result.wGet(i));
        int nextResultSize = result.wSize();
        i += (nextResultSize - resultSize);
        if (isResultIterator()) {
            IEntityIterator<?> iterator = getResultIterator();
            setResultIterator(null);
            IEntity selfEntity = getBindings().wGet("self");
            if (selfEntity != oldSelfEntity2)
                getBindings().wDef("self", selfEntity = oldSelfEntity2);
            iterator.reset(selfEntity);
            FeatureDescriptor resultChildDescriptor = result.wGetFeatureDescriptor(i);
            if (EntityUtils.isComposite(result)) {
                result.wRemove(i--);
                if (iterator.hasNext()) {
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    resultSize = result.wSize();
                    for (IEntity e : iterator) {
                        nextResultSize = result.wSize();
                        i += (nextResultSize - resultSize);
                        if (BindingManagerFactory.instance.isVoid(e))
                            resultSize = nextResultSize;
                        else {
                            result.wAdd(++i, EntityUtils.convertCloneIfReparenting(e, resultChildDescriptor));
                            resultSize = result.wSize();
                        }
                        resettableScope.commit();
                    }
                    resettableScope.rollback();
                    getBindings().wExitScope();
                }
            } else {
                IEntity e = null;
                if (iterator.hasNext()) {
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    for (IEntity r : iterator) {
                        if (!BindingManagerFactory.instance.isVoid(r))
                            e = r;
                        resettableScope.commit();
                    }
                    resettableScope.rollback();
                    getBindings().wExitScope();
                }
                if (e != null)
                    result.wSet(i, EntityUtils.convertCloneIfReparenting(e, resultChildDescriptor));
                else
                    result.wRemove(i);
            }
        } else {
            IEntity child = getResult();
            if (child != null && !BindingManagerFactory.instance.isVoid(child)) {
                FeatureDescriptor resultChildDescriptor = result.wGetAdaptee(false).wGetFeatureDescriptor(i);
                result.wSet(i, EntityUtils.convertCloneIfReparenting(child, resultChildDescriptor));
            } else
                result.wRemove(i);
        }
        IEntity selfEntity = getBindings().wGet("self");
        if (selfEntity != oldSelfEntity2)
            getBindings().wDef("self", selfEntity = oldSelfEntity2);
    }
    setResult(result);
}
Also used : Variable(org.whole.lang.commons.model.Variable) ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) InternalIEntity(org.whole.lang.model.InternalIEntity) SubstituteException(org.whole.lang.matchers.SubstituteException) QuantifierEnum(org.whole.lang.commons.model.QuantifierEnum) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) InternalIEntity(org.whole.lang.model.InternalIEntity)

Example 7 with Variable

use of org.whole.lang.commons.model.Variable in project whole by wholeplatform.

the class Matcher method vars.

public static Set<String> vars(IEntity pattern, boolean includeOptionals) {
    Set<String> names = new HashSet<String>();
    AbstractPatternFilterIterator<IEntity> variableIterator = IteratorFactory.descendantOrSelfMatcherIterator().withPattern(GenericMatcherFactory.instance.isVariableMatcher());
    variableIterator.reset(pattern);
    for (IEntity variableAdapter : variableIterator) {
        Variable variable = (Variable) variableAdapter.wGetAdaptee(false);
        if (includeOptionals || !variable.getQuantifier().getValue().isOptional())
            names.add(variable.getVarName().getValue());
    }
    return names;
}
Also used : Variable(org.whole.lang.commons.model.Variable) IEntity(org.whole.lang.model.IEntity) HashSet(java.util.HashSet)

Example 8 with Variable

use of org.whole.lang.commons.model.Variable in project whole by wholeplatform.

the class Matcher method removeVars.

public static boolean removeVars(IEntity pattern, final Set<String> names, boolean useNamesComplement) {
    if (names.isEmpty() && !useNamesComplement)
        return true;
    boolean allVarsAreOptional = true;
    AbstractPatternFilterIterator<IEntity> variableIterator = IteratorFactory.descendantOrSelfMatcherIterator().withPattern(GenericMatcherFactory.instance.isVariableMatcher());
    variableIterator.reset(pattern);
    for (IEntity variableAdapter : variableIterator) {
        Variable variable = (Variable) variableAdapter.wGetAdaptee(false);
        if (names.contains(variable.getVarName().getValue()) ^ useNamesComplement) {
            boolean isOptional = variable.getQuantifier().getValue().isOptional();
            allVarsAreOptional &= isOptional;
            if (isOptional)
                variableIterator.remove();
        }
    }
    return allVarsAreOptional;
}
Also used : Variable(org.whole.lang.commons.model.Variable) IEntity(org.whole.lang.model.IEntity)

Example 9 with Variable

use of org.whole.lang.commons.model.Variable in project whole by wholeplatform.

the class GenericMatcherFactory method substitute.

// NB substitute a copy of the binding values
public IVisitor substitute(final IBindingScope inBindings, final IBindingScope outBindings) {
    return new AbstractVariableVisitor() {

        private Set<Variable> vars = new HashSet<Variable>();

        public void visitVariable(Variable variable) {
            if (!EntityUtils.hasParent(variable) || !vars.add(variable))
                return;
            String varName = variable.getVarName().getValue();
            IEntity value = inBindings.wGet(varName);
            if (value != null && !BindingManagerFactory.instance.isVoid(value)) {
                boolean isInline = EntityUtils.isInlineVariable(variable);
                EntityDescriptor<?> varType = variable.getVarType().getValue();
                if (!isInline)
                    // TODO convertCloneIfReparenting variable reference
                    value = EntityUtils.convertCloneIfParented(value, varType);
                outBindings.wDef(varName, value);
                // TODO if has not a parent and has only one inverseAdjacent use it and use convertCloneIfReparenting
                IEntity parentEntity = variable.wGetParent();
                int variableIndex = parentEntity.wIndexOf(variable);
                QuantifierEnum.Value quantifierValue = variable.getQuantifier().getValue();
                boolean isCompositeVar = quantifierValue.isComposite();
                if (isCompositeVar)
                    variable.getQuantifier().setValue(quantifierValue.toOptional());
                // TODO ? use EntityUtils.convert(value, varType?) instead of cloneIfParented
                if (isInline) {
                    if (!isCompositeVar)
                        parentEntity.wRemove(variableIndex);
                    for (int i = 0, size = value.wSize(); i < size; i++) parentEntity.wAdd(variableIndex + i, EntityUtils.clone(value.wGet(i)));
                } else if (isCompositeVar)
                    parentEntity.wAdd(variableIndex, value);
                else
                    parentEntity.wSet(variableIndex, value);
            }
        }
    };
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Variable(org.whole.lang.commons.model.Variable) QuantifierEnum(org.whole.lang.commons.model.QuantifierEnum) IEntity(org.whole.lang.model.IEntity)

Example 10 with Variable

use of org.whole.lang.commons.model.Variable in project whole by wholeplatform.

the class QueriesDynamicCompilerVisitor method visitAdapter.

@Override
public boolean visitAdapter(IEntityAdapter entity) {
    IEntity adaptee = entity.wGetAdaptee(false);
    @Deprecated EntityDescriptor<?> adapteeEd = adaptee.wGetEntityDescriptor();
    if (adapteeEd.getLanguageKit().getURI().equals(CommonsLanguageKit.URI)) {
        switch(adapteeEd.getOrdinal()) {
            case CommonsEntityDescriptorEnum.Resolver_ord:
                setResultIterator(IteratorFactory.emptyIterator().withSourceEntity(adaptee));
                return false;
            case CommonsEntityDescriptorEnum.Variable_ord:
            case CommonsEntityDescriptorEnum.InlineVariable_ord:
                throw new MissingVariableException(((Variable) adaptee).getVarName().toString()).withSourceEntity(adaptee).withBindings(getBindings());
            case CommonsEntityDescriptorEnum.RootFragment_ord:
            case CommonsEntityDescriptorEnum.StageDownFragment_ord:
                setResultIterator(IteratorFactory.templateInterpreterIterator(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.StageDownFragment, EntityUtils.clone(entity))).withSourceEntity(adaptee));
                return false;
        }
    }
    stagedVisit(adaptee, 0);
    return false;
}
Also used : Variable(org.whole.lang.commons.model.Variable) IEntity(org.whole.lang.model.IEntity) MissingVariableException(org.whole.lang.visitors.MissingVariableException)

Aggregations

Variable (org.whole.lang.commons.model.Variable)13 IEntity (org.whole.lang.model.IEntity)10 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 QuantifierEnum (org.whole.lang.commons.model.QuantifierEnum)2 Feature (org.whole.lang.models.model.Feature)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)1 ClasspathPersistenceProvider (org.whole.lang.codebase.ClasspathPersistenceProvider)1 AbstractVariableVisitor (org.whole.lang.matchers.GenericMatcherFactory.AbstractVariableVisitor)1 SubstituteException (org.whole.lang.matchers.SubstituteException)1 InternalIEntity (org.whole.lang.model.InternalIEntity)1 Model (org.whole.lang.models.model.Model)1 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)1 ModelTemplate (org.whole.lang.templates.ModelTemplate)1 TestingModel (org.whole.lang.testentities.codebase.TestingModel)1 MissingVariableException (org.whole.lang.visitors.MissingVariableException)1