Search in sources :

Example 16 with ICommand

use of org.whole.lang.commands.ICommand in project whole by wholeplatform.

the class HistoryManager method reduce.

// TODO called by model to delta Stream if changes.size > model.size
protected List<ICommand> reduce(List<ICommand> commands) {
    if (commands.isEmpty())
        return commands;
    List<ICommand> list = new ArrayList<ICommand>();
    for (int i = 0, size = commands.size(); i < size; i++) {
        ICommand command = commands.get(i);
        if (CommandKind.COMPOUND == command.getKind()) {
            for (ICommand child : ((CompoundCommand) command).commands) list.add(child);
        } else
            list.add(command);
    }
    int index = list.size() - 1;
    while (index > 0) {
        ICommand lastCommand = list.get(index--);
        if (CommandKind.CHANGE == lastCommand.getKind()) {
            IEntity lastSource = lastCommand.getSource();
            FeatureDescriptor lastSourceFD = lastCommand.getSourceFeatureDescriptor();
            int firstIndex = index;
            while (firstIndex >= 0) {
                ICommand firstCommand = list.get(firstIndex);
                if (CommandKind.CHANGE != firstCommand.getKind() || firstCommand.getSource() != lastSource || firstCommand.getSourceFeatureDescriptor() != lastSourceFD)
                    break;
                else
                    firstIndex--;
            }
            if (firstIndex < index) {
                if (lastSourceFD == null)
                    lastCommand.setOldObject(list.get(firstIndex + 1).getOldObject());
                else
                    lastCommand.setOldEntity(list.get(firstIndex + 1).getOldEntity());
                for (int i = firstIndex + 1; i <= index; i++) list.remove(firstIndex + 1).dispose();
                index = firstIndex;
            }
        }
    }
    return list;
// iterate over model tree to exclude overridden commands
// for each command discard overridden prevCommands
}
Also used : IEntity(org.whole.lang.model.IEntity) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) ICommand(org.whole.lang.commands.ICommand) ArrayList(java.util.ArrayList) CompoundCommand(org.whole.lang.commands.CompoundCommand)

Example 17 with ICommand

use of org.whole.lang.commands.ICommand in project whole by wholeplatform.

the class LifecycleAPITest method testCommit.

@Test
public void testCommit() {
    ITransaction transaction = ReflectionFactory.getTransaction(model);
    IEntity e1 = model.wGet(0);
    IEntity e2 = model.wGet(3);
    transaction.begin();
    e1.wSetValue("newLangName");
    ICommand c1 = ((InternalIEntity) e1).wGetLastCommand();
    e2.wSetValue("newNamespace");
    ICommand c2 = ((InternalIEntity) e2).wGetLastCommand();
    String langName = e1.wStringValue();
    int size = history.getUndoSize();
    assertTrue(c1.getExecutionTime() <= c2.getExecutionTime());
    transaction.commit();
    assertEquals(e1.wStringValue(), langName);
    assertTrue(history.getUndoSize() < size);
    assertEquals(c1.getExecutionTime(), c2.getExecutionTime());
}
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)

Aggregations

ICommand (org.whole.lang.commands.ICommand)17 IEntity (org.whole.lang.model.IEntity)7 InternalIEntity (org.whole.lang.model.InternalIEntity)6 CompoundCommand (org.whole.lang.commands.CompoundCommand)3 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)1