Search in sources :

Example 6 with InternalIEntity

use of org.whole.lang.model.InternalIEntity in project whole by wholeplatform.

the class LifecycleAPITest method testUndoCommands.

@Test
public void testUndoCommands() {
    IEntity id = model.wGet(0);
    id.wSetValue("newLangName");
    ICommand lastCommand = ((InternalIEntity) id).wGetLastCommand();
    List<ICommand> changes = history.getUndoCommands();
    ICommand command = changes.get(changes.size() - 1);
    assertSame(lastCommand, command);
    assertEquals(id, command.getSource());
    assertEquals("newLangName", command.getNewObject());
}
Also used : IEntity(org.whole.lang.model.IEntity) InternalIEntity(org.whole.lang.model.InternalIEntity) ICommand(org.whole.lang.commands.ICommand) InternalIEntity(org.whole.lang.model.InternalIEntity) Test(org.junit.Test)

Example 7 with InternalIEntity

use of org.whole.lang.model.InternalIEntity in project whole by wholeplatform.

the class AbstractEntityContext method wIndexedFeatures.

@Deprecated
public List<IEntity> wIndexedFeatures(IEntity e, int contextTime) {
    List<IEntity> result = childrenCache.get(e);
    if (result != null && this.contextTime == contextTime)
        return result;
    result = new ArrayList<IEntity>(e.wFeatures());
    ICommand cmd = ((InternalIEntity) e).wGetLastCommand();
    while (contextTime < cmd.getExecutionTime()) {
        switch(cmd.getKind()) {
            case ADD:
                result.remove(cmd.getSourceIndex());
                break;
            case REMOVE:
                result.add(cmd.getSourceIndex(), cmd.getOldEntity());
                break;
            case CHANGE:
                result.set(cmd.getSourceIndex(), cmd.getOldEntity());
                break;
        }
        cmd = cmd.getPrevCommand();
    }
    if (this.contextTime == contextTime)
        childrenCache.put(e, result);
    return result;
}
Also used : IEntity(org.whole.lang.model.IEntity) InternalIEntity(org.whole.lang.model.InternalIEntity) ICommand(org.whole.lang.commands.ICommand) InternalIEntity(org.whole.lang.model.InternalIEntity)

Example 8 with InternalIEntity

use of org.whole.lang.model.InternalIEntity in project whole by wholeplatform.

the class AbstractEntityContext method wGetPrevVersion.

protected IEntity wGetPrevVersion(IEntity e) {
    ICommand cmd = ((InternalIEntity) e).wGetBindingCommand();
    if (cmd.getKind().isComposite()) {
        IEntity source = cmd.getSource();
        List<IEntity> children = wIndexedFeatures(source, ((InternalIEntity) source).wGetLastCommand().getPrevCommand().getExecutionTime());
        return children.get(cmd.getSourceIndex());
    } else
        return cmd.getPrevCommand().getNewEntity();
}
Also used : IEntity(org.whole.lang.model.IEntity) InternalIEntity(org.whole.lang.model.InternalIEntity) ICommand(org.whole.lang.commands.ICommand) InternalIEntity(org.whole.lang.model.InternalIEntity)

Example 9 with InternalIEntity

use of org.whole.lang.model.InternalIEntity in project whole by wholeplatform.

the class AbstractFragment method wSetChildrenModel.

protected void wSetChildrenModel(IFragmentModel model) {
    IFragmentModel newModel = (IFragmentModel) model.clone();
    newModel.setFragment(this);
    ((InternalIEntity) rootEntity).wSetModel(newModel);
}
Also used : IFragmentModel(org.whole.lang.model.IFragmentModel) InternalIEntity(org.whole.lang.model.InternalIEntity)

Example 10 with InternalIEntity

use of org.whole.lang.model.InternalIEntity 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)

Aggregations

InternalIEntity (org.whole.lang.model.InternalIEntity)25 IEntity (org.whole.lang.model.IEntity)19 Test (org.junit.Test)13 ICommand (org.whole.lang.commands.ICommand)6 IFragmentModel (org.whole.lang.model.IFragmentModel)2 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)2 Serializable (java.io.Serializable)1 Category (org.junit.experimental.categories.Category)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)1 QuantifierEnum (org.whole.lang.commons.model.QuantifierEnum)1 Variable (org.whole.lang.commons.model.Variable)1 SubstituteException (org.whole.lang.matchers.SubstituteException)1