From 97c7e3653690f1e723ccd3ca01689a5b6d4c4601 Mon Sep 17 00:00:00 2001 From: msmock Date: Sun, 24 May 2015 11:08:04 +0200 Subject: [PATCH] added the two core command required to set up a disposition application and refactored to remove code duplication. --- .../command/plan/AbstractPlanCommand.java | 124 +++++++++++ .../command/plan/AssignActionCommand.java | 98 +++++++++ .../command/plan/PlanActionCommand.java | 44 +--- .../command/plan/PlanActivityCommand.java | 94 +-------- .../command/plan/ShiftActionCommand.java | 90 ++++++++ .../li/strolch/command/plan/AllPlanTests.java | 11 + .../command/plan/AssignActionTest.java | 192 ++++++++++++++++++ .../strolch/command/plan/PlanActionTest.java | 25 +-- .../command/plan/PlanActivityTest.java | 74 ++++--- .../strolch/command/plan/ShiftActionTest.java | 167 +++++++++++++++ 10 files changed, 753 insertions(+), 166 deletions(-) create mode 100644 li.strolch.service/src/main/java/li/strolch/command/plan/AbstractPlanCommand.java create mode 100644 li.strolch.service/src/main/java/li/strolch/command/plan/AssignActionCommand.java create mode 100644 li.strolch.service/src/main/java/li/strolch/command/plan/ShiftActionCommand.java create mode 100644 li.strolch.service/src/test/java/li/strolch/command/plan/AllPlanTests.java create mode 100644 li.strolch.service/src/test/java/li/strolch/command/plan/AssignActionTest.java create mode 100644 li.strolch.service/src/test/java/li/strolch/command/plan/ShiftActionTest.java diff --git a/li.strolch.service/src/main/java/li/strolch/command/plan/AbstractPlanCommand.java b/li.strolch.service/src/main/java/li/strolch/command/plan/AbstractPlanCommand.java new file mode 100644 index 000000000..e3ca74e86 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/command/plan/AbstractPlanCommand.java @@ -0,0 +1,124 @@ +/* + * Copyright 2015 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.command.plan; + +import java.util.Iterator; +import java.util.List; +import java.util.Map.Entry; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.exception.StrolchException; +import li.strolch.model.Locator; +import li.strolch.model.Resource; +import li.strolch.model.State; +import li.strolch.model.Tags; +import li.strolch.model.activity.Action; +import li.strolch.model.activity.Activity; +import li.strolch.model.activity.IActivityElement; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.IValueChange; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.service.api.Command; + +/** + * @author Martin Smock + */ +public abstract class AbstractPlanCommand extends Command { + + /** + * @param container + * @param tx + */ + public AbstractPlanCommand(final ComponentContainer container, final StrolchTransaction tx) { + super(container, tx); + } + + /** + * plan an {@link Action}.It iterates the {@link IValueChange} operators and + * registers the changes on the {@link StrolchTimedState} objects assigned + * to the {@link Resource} referenced by type and id. + * + * @param action + */ + @SuppressWarnings({ "unchecked", "rawtypes" }) + protected void plan(final Action action) { + + final Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); + final Resource resource = tx().findElement(locator); + + if (resource == null) + throw new StrolchException("Resource with " + locator + " referenced by " + action.getLocator() + + " cannot be null!"); + + tx().lock(resource); + + final List> changes = action.getChanges(); + for (final IValueChange change : changes) { + final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); + timedState.applyChange(change); + } + + action.setState(State.PLANNED); + } + + /** + * plan an {@link Activity} by navigating to the {#link Action} and + * delegating the planning depending on the {@link IActivityElement} class. + */ + protected void plan(final Activity activity) { + + final Iterator> elementIterator = activity.elementIterator(); + + while (elementIterator.hasNext()) { + final IActivityElement activityElement = elementIterator.next().getValue(); + if (activityElement instanceof Activity) + plan((Activity) activityElement); + else if (activityElement instanceof Action) + plan((Action) activityElement); + } + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + protected void unplan(final Action action) { + + final Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); + final Resource resource = tx().findElement(locator); + + final List> changes = action.getChanges(); + for (final IValueChange change : changes) { + final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); + timedState.applyChange(change.getInverse()); + } + + action.setState(State.CREATED); + } + + protected void unplan(final Activity activity) { + + final Iterator> elementIterator = activity.elementIterator(); + while (elementIterator.hasNext()) { + + final IActivityElement activityElement = elementIterator.next().getValue(); + + if (activityElement instanceof Activity) + unplan((Activity) activityElement); + else if (activityElement instanceof Action) + unplan((Action) activityElement); + + } + } + +} diff --git a/li.strolch.service/src/main/java/li/strolch/command/plan/AssignActionCommand.java b/li.strolch.service/src/main/java/li/strolch/command/plan/AssignActionCommand.java new file mode 100644 index 000000000..a9047d915 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/command/plan/AssignActionCommand.java @@ -0,0 +1,98 @@ +/* + * Copyright 2015 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.command.plan; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.model.Resource; +import li.strolch.model.activity.Action; +import li.strolch.persistence.api.StrolchTransaction; +import ch.eitchnet.utils.dbc.DBC; + +/** + * Command to assign an {@link Action} to a new {@link Resource}. + * + * @author Martin Smock + */ +public class AssignActionCommand extends PlanActionCommand { + + private String targetResourceType; + private String targetResourceId; + + private String initialResourceType; + private String initialResourceId; + + /** + * @param container + * @param tx + */ + public AssignActionCommand(final ComponentContainer container, final StrolchTransaction tx) { + super(container, tx); + } + + @Override + public void validate() { + DBC.PRE.assertNotNull("Action may not be null!", this.action); + DBC.PRE.assertNotNull("Action attribute resourceId may not be null!", action.getResourceId()); + DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", action.getResourceType()); + DBC.PRE.assertNotNull("Target resourceId may not be null!", targetResourceId); + DBC.PRE.assertNotNull("Target resourceType may not be null!", targetResourceType); + } + + @Override + public void doCommand() { + + validate(); + + // bookkeeping for undo + initialResourceId = action.getResourceId(); + initialResourceType = action.getResourceType(); + + // unplan the action + unplan(action); + + // set target resource + action.setResourceId(targetResourceId); + action.setResourceType(targetResourceType); + + // finally plan the action to the assigned resource + plan(action); + } + + @Override + public void undo() { + + validate(); + + // unplan the action + unplan(action); + + // set target resource + action.setResourceId(initialResourceId); + action.setResourceType(initialResourceType); + + // finally plan the action + plan(action); + } + + public void setTargetResourceType(String type) { + this.targetResourceType = type; + } + + public void setTargetResourceId(String id) { + this.targetResourceId = id; + } + +} diff --git a/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActionCommand.java b/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActionCommand.java index 10c94f744..acc4f9dae 100644 --- a/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActionCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActionCommand.java @@ -15,13 +15,8 @@ */ package li.strolch.command.plan; -import java.util.List; - import li.strolch.agent.api.ComponentContainer; -import li.strolch.model.Locator; import li.strolch.model.Resource; -import li.strolch.model.State; -import li.strolch.model.Tags; import li.strolch.model.activity.Action; import li.strolch.model.timedstate.StrolchTimedState; import li.strolch.model.timevalue.IValueChange; @@ -38,7 +33,7 @@ import ch.eitchnet.utils.dbc.DBC; * * @author Martin Smock */ -public class PlanActionCommand extends Command { +public class PlanActionCommand extends AbstractPlanCommand { protected Action action; @@ -53,46 +48,19 @@ public class PlanActionCommand extends Command { @Override public void validate() { DBC.PRE.assertNotNull("Action may not be null!", this.action); - DBC.PRE.assertNotNull("Action attribute resourceId may not be null!", this.action.getResourceId()); - DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", this.action.getResourceType()); - DBC.PRE.assertTrue("Action attribute start must be set!", this.action.getStart() > 0); - DBC.PRE.assertTrue("Action attribute end must later than start!", this.action.getEnd() > this.action.getStart()); + DBC.PRE.assertNotNull("Action attribute resourceId may not be null!", action.getResourceId()); + DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", action.getResourceType()); } @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) public void doCommand() { - - Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); - Resource resource = tx().findElement(locator); - - tx().lock(resource); - - final List> startChanges = action.getChanges(); - for (IValueChange change : startChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change); - } - // finally set the action state - action.setState(State.PLANNED); - action.setResourceId(resource.getId()); + validate(); + plan(action); } @Override - @SuppressWarnings({ "unchecked", "rawtypes" }) public void undo() { - - Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); - Resource resource = tx().findElement(locator); - - final List> startChanges = action.getChanges(); - for (IValueChange change : startChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change.getInverse()); - } - - // finally set the action state - action.setState(State.CREATED); + unplan(action); } public void setAction(Action action) { diff --git a/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActivityCommand.java b/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActivityCommand.java index 418f0cc2d..d90f65a44 100644 --- a/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActivityCommand.java +++ b/li.strolch.service/src/main/java/li/strolch/command/plan/PlanActivityCommand.java @@ -16,15 +16,10 @@ package li.strolch.command.plan; import java.util.Iterator; -import java.util.List; import java.util.Map.Entry; import li.strolch.agent.api.ComponentContainer; -import li.strolch.exception.StrolchException; -import li.strolch.model.Locator; import li.strolch.model.Resource; -import li.strolch.model.State; -import li.strolch.model.Tags; import li.strolch.model.activity.Action; import li.strolch.model.activity.Activity; import li.strolch.model.activity.IActivityElement; @@ -45,7 +40,7 @@ import ch.eitchnet.utils.dbc.DBC; * * @author Martin Smock */ -public class PlanActivityCommand extends Command { +public class PlanActivityCommand extends AbstractPlanCommand { protected Activity activity; @@ -53,7 +48,7 @@ public class PlanActivityCommand extends Command { * @param container * @param tx */ - public PlanActivityCommand(ComponentContainer container, StrolchTransaction tx) { + public PlanActivityCommand(final ComponentContainer container, final StrolchTransaction tx) { super(container, tx); } @@ -63,15 +58,15 @@ public class PlanActivityCommand extends Command { validate(activity); } - private void validate(Action action) { + private void validate(final Action action) { DBC.PRE.assertNotNull("Action attribute resourceId may not be null!", action.getResourceId()); DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", action.getResourceType()); } - private void validate(Activity activity) { - Iterator> elementIterator = activity.elementIterator(); + private void validate(final Activity activity) { + final Iterator> elementIterator = activity.elementIterator(); while (elementIterator.hasNext()) { - IActivityElement activityElement = elementIterator.next().getValue(); + final IActivityElement activityElement = elementIterator.next().getValue(); if (activityElement instanceof Activity) validate((Activity) activityElement); else if (activityElement instanceof Action) @@ -86,85 +81,10 @@ public class PlanActivityCommand extends Command { plan(activity); } - /** - * plan an {@link Activity} by navigating to the {#link Action} and - * delegating the planning depending on the {@link IActivityElement} class. - */ - private void plan(Activity activity) { - - Iterator> elementIterator = activity.elementIterator(); - - while (elementIterator.hasNext()) { - IActivityElement activityElement = elementIterator.next().getValue(); - if (activityElement instanceof Activity) - plan((Activity) activityElement); - else if (activityElement instanceof Action) - plan((Action) activityElement); - } - } - - /** - * plan an {@link Action}.It iterates the {@link IValueChange} operators and - * registers the changes on the {@link StrolchTimedState} objects assigned - * to the {@link Resource} referenced by type and id. - * - * @param action - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - private void plan(Action action) { - - Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); - Resource resource = tx().findElement(locator); - - if (resource == null) - throw new StrolchException("Resource with " + locator + " referenced by " + action.getLocator() - + " cannot be null!"); - - tx().lock(resource); - - final List> startChanges = action.getChanges(); - for (IValueChange change : startChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change); - } - - action.setState(State.PLANNED); - } - @Override public void undo() { tx().lock(activity); - unplanActivity(activity); - } - - private void unplanActivity(Activity activity) { - - Iterator> elementIterator = activity.elementIterator(); - while (elementIterator.hasNext()) { - - IActivityElement activityElement = elementIterator.next().getValue(); - - if (activityElement instanceof Activity) - unplanActivity((Activity) activityElement); - else if (activityElement instanceof Action) - unplanAction((Action) activityElement); - - } - } - - @SuppressWarnings({ "unchecked", "rawtypes" }) - private void unplanAction(Action action) { - - Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); - Resource resource = tx().findElement(locator); - - final List> startChanges = action.getChanges(); - for (IValueChange change : startChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change.getInverse()); - } - - action.setState(State.CREATED); + unplan(activity); } public void setActivity(Activity activity) { diff --git a/li.strolch.service/src/main/java/li/strolch/command/plan/ShiftActionCommand.java b/li.strolch.service/src/main/java/li/strolch/command/plan/ShiftActionCommand.java new file mode 100644 index 000000000..e94d476d0 --- /dev/null +++ b/li.strolch.service/src/main/java/li/strolch/command/plan/ShiftActionCommand.java @@ -0,0 +1,90 @@ +/* + * Copyright 2015 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.command.plan; + +import java.util.List; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.model.State; +import li.strolch.model.timevalue.IValueChange; +import li.strolch.persistence.api.StrolchTransaction; +import ch.eitchnet.utils.dbc.DBC; + +/** + * @author Martin Smock + */ +public class ShiftActionCommand extends PlanActionCommand { + + private Long shift; + + /** + * @param container + * @param tx + */ + public ShiftActionCommand(final ComponentContainer container, final StrolchTransaction tx) { + super(container, tx); + } + + @Override + public void validate() { + DBC.PRE.assertNotNull("Action may not be null!", this.action); + DBC.PRE.assertNotNull("Action attribute resourceId may not be null!", action.getResourceId()); + DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", action.getResourceType()); + DBC.PRE.assertNotNull("The time to shift the action may not be null!", shift); + } + + @Override + public void doCommand() { + + validate(); + + // unplan the action + if (action.getState() == State.PLANNED) + unplan(action); + + // iterate all changes and shift + final List> changes = action.getChanges(); + for (final IValueChange change : changes) { + change.setTime(change.getTime() + shift); + } + + // finally plan the action + plan(action); + + } + + @Override + public void undo() { + + // unplan the action + if (action.getState() == State.PLANNED) + unplan(action); + + // iterate all changes and shift + final List> changes = action.getChanges(); + for (final IValueChange change : changes) { + change.setTime(change.getTime() - shift); + } + + // finally plan the action + plan(action); + } + + public void setShift(Long shift) { + this.shift = shift; + } + +} diff --git a/li.strolch.service/src/test/java/li/strolch/command/plan/AllPlanTests.java b/li.strolch.service/src/test/java/li/strolch/command/plan/AllPlanTests.java new file mode 100644 index 000000000..caefdb637 --- /dev/null +++ b/li.strolch.service/src/test/java/li/strolch/command/plan/AllPlanTests.java @@ -0,0 +1,11 @@ +package li.strolch.command.plan; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; + +@RunWith(Suite.class) +@SuiteClasses({ AssignActionTest.class, PlanActionTest.class, PlanActivityTest.class, ShiftActionTest.class }) +public class AllPlanTests { + +} diff --git a/li.strolch.service/src/test/java/li/strolch/command/plan/AssignActionTest.java b/li.strolch.service/src/test/java/li/strolch/command/plan/AssignActionTest.java new file mode 100644 index 000000000..7ef1e603a --- /dev/null +++ b/li.strolch.service/src/test/java/li/strolch/command/plan/AssignActionTest.java @@ -0,0 +1,192 @@ +/* + * Copyright 2015 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.command.plan; + +import static li.strolch.model.ModelGenerator.STATE_INTEGER_ID; +import static li.strolch.model.ModelGenerator.STATE_INTEGER_NAME; +import static li.strolch.model.ModelGenerator.STATE_INTEGER_TIME_0; +import static li.strolch.model.ModelGenerator.STATE_TIME_0; +import static li.strolch.model.ModelGenerator.STATE_TIME_10; +import static li.strolch.model.ModelGenerator.STATE_TIME_20; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.SortedSet; + +import li.strolch.model.Locator; +import li.strolch.model.ModelGenerator; +import li.strolch.model.ParameterBag; +import li.strolch.model.Resource; +import li.strolch.model.State; +import li.strolch.model.Tags; +import li.strolch.model.activity.Action; +import li.strolch.model.parameter.IntegerParameter; +import li.strolch.model.parameter.Parameter; +import li.strolch.model.timedstate.IntegerTimedState; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.ITimeVariable; +import li.strolch.model.timevalue.IValue; +import li.strolch.model.timevalue.IValueChange; +import li.strolch.model.timevalue.impl.IntegerValue; +import li.strolch.model.timevalue.impl.ValueChange; +import li.strolch.persistence.api.StrolchTransaction; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Martin Smock + */ +public class AssignActionTest { + + Resource initialResource, targetResource; + Action action; + IntegerTimedState initialTimedState, targetTimedState; + StrolchTransaction tx; + + @Before + public void init() { + + // add initial resource with integer state variable + initialResource = ModelGenerator.createResource("initial", "Test With States", "Stated"); + initialTimedState = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME); + initialTimedState.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0))); + initialResource.addTimedState(initialTimedState); + + // add target resource with integer state variable + targetResource = ModelGenerator.createResource("target", "Test With States", "Stated"); + targetTimedState = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME); + targetTimedState.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0))); + targetResource.addTimedState(targetTimedState); + + action = new Action("action", "Action", "Use"); + + Assert.assertEquals(State.CREATED, action.getState()); + + final IntegerParameter iP = new IntegerParameter("quantity", "Occupation", 1); + action.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); + action.addParameter("objective", iP); + + createChanges(action); + + action.setResourceId(initialResource.getId()); + action.setResourceType(initialResource.getType()); + + tx = mock(StrolchTransaction.class); + + Locator locator = Locator.newBuilder(Tags.RESOURCE, initialResource.getType(), initialResource.getId()).build(); + when(tx.findElement(eq(locator))).thenReturn(initialResource); + + locator = Locator.newBuilder(Tags.RESOURCE, targetResource.getType(), targetResource.getId()).build(); + when(tx.findElement(eq(locator))).thenReturn(targetResource); + + // finally plan the action + final PlanActionCommand planCommand = new PlanActionCommand(null, tx); + planCommand.setAction(action); + planCommand.doCommand(); + + } + + @Test + public void test() { + + final AssignActionCommand cmd = new AssignActionCommand(null, tx); + cmd.setTargetResourceId(targetResource.getId()); + cmd.setTargetResourceType(targetResource.getType()); + cmd.setAction(action); + cmd.doCommand(); + + // check the state + Assert.assertEquals(State.PLANNED, action.getState()); + + // check the resource Id + Assert.assertEquals(targetResource.getId(), action.getResourceId()); + + // check if we get the expected result + StrolchTimedState> initialTimedState = initialResource.getTimedState(STATE_INTEGER_ID); + ITimeVariable> initialTimeEvolution = initialTimedState.getTimeEvolution(); + SortedSet>> initialValues = initialTimeEvolution.getValues(); + + Assert.assertEquals(1, initialValues.size()); + + StrolchTimedState> targetTimedState = targetResource.getTimedState(STATE_INTEGER_ID); + ITimeVariable> targetTimeEvolution = targetTimedState.getTimeEvolution(); + SortedSet>> targetValues = targetTimeEvolution.getValues(); + + Assert.assertEquals(3, targetValues.size()); + + ITimeValue> valueAt = targetTimeEvolution.getValueAt(STATE_TIME_10); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(1))); + + valueAt = targetTimeEvolution.getValueAt(STATE_TIME_20); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + cmd.undo(); + + // check the state + Assert.assertEquals(State.PLANNED, action.getState()); + + // check the resource Id + Assert.assertEquals(initialResource.getId(), action.getResourceId()); + + // check if we get the expected result + targetTimedState = targetResource.getTimedState(STATE_INTEGER_ID); + targetTimeEvolution = targetTimedState.getTimeEvolution(); + targetValues = targetTimeEvolution.getValues(); + + Assert.assertEquals(1, targetValues.size()); + + initialTimedState = initialResource.getTimedState(STATE_INTEGER_ID); + initialTimeEvolution = initialTimedState.getTimeEvolution(); + initialValues = initialTimeEvolution.getValues(); + + Assert.assertEquals(3, initialValues.size()); + + valueAt = initialTimeEvolution.getValueAt(STATE_TIME_10); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(1))); + + valueAt = initialTimeEvolution.getValueAt(STATE_TIME_20); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + } + + /** + *

+ * add changes to action start and end time with a value defined in the + * action objective and set the stateId of the state variable to apply the + * change to + *

+ * + * @param action + */ + protected static void createChanges(final Action action) { + + final Parameter parameter = action.getParameter("objective", "quantity"); + final Integer quantity = parameter.getValue(); + + final IValueChange startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity)); + startChange.setStateId(STATE_INTEGER_ID); + action.addChange(startChange); + + final IValueChange endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity)); + endChange.setStateId(STATE_INTEGER_ID); + action.addChange(endChange); + } + +} diff --git a/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActionTest.java b/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActionTest.java index 4704a00d7..c6ce48a25 100644 --- a/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActionTest.java +++ b/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActionTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2014 Martin Smock + * Copyright 2015 Martin Smock * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,11 +67,11 @@ public class PlanActionTest { timedState.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0))); resource.addTimedState(timedState); - action = new Action("action_1", "Action 1", "Use"); + action = new Action("action", "Action", "Use"); Assert.assertEquals(State.CREATED, action.getState()); - IntegerParameter iP = new IntegerParameter("quantity", "Occupation", 1); + final IntegerParameter iP = new IntegerParameter("quantity", "Occupation", 1); action.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action.addParameter("objective", iP); @@ -84,9 +84,9 @@ public class PlanActionTest { @Test public void test() { - StrolchTransaction tx = mock(StrolchTransaction.class); + final StrolchTransaction tx = mock(StrolchTransaction.class); - Locator locator = Locator.newBuilder(Tags.RESOURCE, "Stated", "@1").build(); + final Locator locator = Locator.newBuilder(Tags.RESOURCE, "Stated", "@1").build(); when(tx.findElement(eq(locator))).thenReturn(resource); final PlanActionCommand cmd = new PlanActionCommand(null, tx); @@ -100,8 +100,8 @@ public class PlanActionTest { Assert.assertEquals(resource.getId(), action.getResourceId()); // check if we get the expected result - StrolchTimedState> timedState = resource.getTimedState(STATE_INTEGER_ID); - ITimeVariable> timeEvolution = timedState.getTimeEvolution(); + final StrolchTimedState> timedState = resource.getTimedState(STATE_INTEGER_ID); + final ITimeVariable> timeEvolution = timedState.getTimeEvolution(); SortedSet>> values = timeEvolution.getValues(); Assert.assertEquals(3, values.size()); @@ -132,21 +132,22 @@ public class PlanActionTest { /** *

* add changes to action start and end time with a value defined in the - * action objective and set the stateId of the state variable to apply the change to + * action objective and set the stateId of the state variable to apply the + * change to *

* * @param action */ protected static void createChanges(final Action action) { - Parameter parameter = action.getParameter("objective", "quantity"); - Integer quantity = parameter.getValue(); + final Parameter parameter = action.getParameter("objective", "quantity"); + final Integer quantity = parameter.getValue(); - IValueChange startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity)); + final IValueChange startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity)); startChange.setStateId(STATE_INTEGER_ID); action.addChange(startChange); - IValueChange endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity)); + final IValueChange endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity)); endChange.setStateId(STATE_INTEGER_ID); action.addChange(endChange); } diff --git a/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActivityTest.java b/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActivityTest.java index b72604a80..414dc623f 100644 --- a/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActivityTest.java +++ b/li.strolch.service/src/test/java/li/strolch/command/plan/PlanActivityTest.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package li.strolch.command.plan; import static li.strolch.model.ModelGenerator.STATE_INTEGER_ID; @@ -49,6 +64,7 @@ public class PlanActivityTest { Resource resource_1, resource_2, resource_3; IntegerTimedState timedState_1, timedState_2, timedState_3; Action action_1, action_2, action_3; + StrolchTransaction tx; /** * initialize the resources with states and the activity with 2 actions. @@ -67,7 +83,7 @@ public class PlanActivityTest { timedState_2 = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME); timedState_2.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0))); resource_2.addTimedState(timedState_2); - + // create resource with integer state resource_3 = ModelGenerator.createResource("@3", "Test With States 3", "Stated"); timedState_3 = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME); @@ -80,7 +96,7 @@ public class PlanActivityTest { // create action 1 action_1 = new Action("action_1", "Action 1", "Use"); - IntegerParameter iP1 = new IntegerParameter("quantity", "Occupation", 1); + final IntegerParameter iP1 = new IntegerParameter("quantity", "Occupation", 1); action_1.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action_1.addParameter("objective", iP1); @@ -90,14 +106,14 @@ public class PlanActivityTest { action_1.setResourceType(resource_1.getType()); activity.addElement(action_1); - - // create child activity + + // create child activity childActivity = new Activity("childActivity", "Child Activity", "childType"); // create action 2 action_2 = new Action("action_2", "Action 2", "Use"); - IntegerParameter iP2 = new IntegerParameter("quantity", "Occupation", 1); + final IntegerParameter iP2 = new IntegerParameter("quantity", "Occupation", 1); action_2.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action_2.addParameter("objective", iP2); @@ -107,11 +123,11 @@ public class PlanActivityTest { action_2.setResourceType(resource_2.getType()); childActivity.addElement(action_2); - + // create action 3 action_3 = new Action("action_3", "Action 3", "Use"); - IntegerParameter iP3 = new IntegerParameter("quantity", "Occupation", 1); + final IntegerParameter iP3 = new IntegerParameter("quantity", "Occupation", 1); action_3.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action_3.addParameter("objective", iP3); @@ -121,11 +137,22 @@ public class PlanActivityTest { action_3.setResourceType(resource_3.getType()); childActivity.addElement(action_3); - - activity.addElement(childActivity); + + activity.addElement(childActivity); Assert.assertEquals(2, activity.getElements().size()); + tx = mock(StrolchTransaction.class); + + final Locator locator1 = Locator.newBuilder(Tags.RESOURCE, "Stated", "@1").build(); + when(tx.findElement(eq(locator1))).thenReturn(resource_1); + + final Locator locator2 = Locator.newBuilder(Tags.RESOURCE, "Stated", "@2").build(); + when(tx.findElement(eq(locator2))).thenReturn(resource_2); + + final Locator locator3 = Locator.newBuilder(Tags.RESOURCE, "Stated", "@3").build(); + when(tx.findElement(eq(locator3))).thenReturn(resource_3); + } /** @@ -134,18 +161,7 @@ public class PlanActivityTest { @Test public void test() { - StrolchTransaction tx = mock(StrolchTransaction.class); - - Locator locator1 = Locator.newBuilder(Tags.RESOURCE, "Stated", "@1").build(); - when(tx.findElement(eq(locator1))).thenReturn(resource_1); - - Locator locator2 = Locator.newBuilder(Tags.RESOURCE, "Stated", "@2").build(); - when(tx.findElement(eq(locator2))).thenReturn(resource_2); - - Locator locator3 = Locator.newBuilder(Tags.RESOURCE, "Stated", "@3").build(); - when(tx.findElement(eq(locator3))).thenReturn(resource_3); - - PlanActivityCommand planActivityCommand = new PlanActivityCommand(null, tx); + final PlanActivityCommand planActivityCommand = new PlanActivityCommand(null, tx); planActivityCommand.setActivity(activity); planActivityCommand.doCommand(); @@ -154,8 +170,8 @@ public class PlanActivityTest { Assert.assertEquals(State.PLANNED, action_2.getState()); // check the resource states - StrolchTimedState> timedState_1 = resource_1.getTimedState(STATE_INTEGER_ID); - ITimeVariable> timeEvolution_1 = timedState_1.getTimeEvolution(); + final StrolchTimedState> timedState_1 = resource_1.getTimedState(STATE_INTEGER_ID); + final ITimeVariable> timeEvolution_1 = timedState_1.getTimeEvolution(); SortedSet>> values_1 = timeEvolution_1.getValues(); Assert.assertEquals(3, values_1.size()); @@ -170,8 +186,8 @@ public class PlanActivityTest { Assert.assertEquals(true, valueAt_1.getValue().equals(new IntegerValue(0))); // the second resource - StrolchTimedState> timedState_2 = resource_2.getTimedState(STATE_INTEGER_ID); - ITimeVariable> timeEvolution_2 = timedState_2.getTimeEvolution(); + final StrolchTimedState> timedState_2 = resource_2.getTimedState(STATE_INTEGER_ID); + final ITimeVariable> timeEvolution_2 = timedState_2.getTimeEvolution(); SortedSet>> values_2 = timeEvolution_2.getValues(); Assert.assertEquals(3, values_2.size()); @@ -208,14 +224,14 @@ public class PlanActivityTest { */ protected static void createChanges(Action action, Long start, Long end) { - Parameter parameter = action.getParameter("objective", "quantity"); - Integer quantity = parameter.getValue(); + final Parameter parameter = action.getParameter("objective", "quantity"); + final Integer quantity = parameter.getValue(); - IValueChange startChange = new ValueChange<>(start, new IntegerValue(quantity)); + final IValueChange startChange = new ValueChange<>(start, new IntegerValue(quantity)); startChange.setStateId(STATE_INTEGER_ID); action.addChange(startChange); - IValueChange endChange = new ValueChange<>(end, new IntegerValue(-quantity)); + final IValueChange endChange = new ValueChange<>(end, new IntegerValue(-quantity)); endChange.setStateId(STATE_INTEGER_ID); action.addChange(endChange); diff --git a/li.strolch.service/src/test/java/li/strolch/command/plan/ShiftActionTest.java b/li.strolch.service/src/test/java/li/strolch/command/plan/ShiftActionTest.java new file mode 100644 index 000000000..445648875 --- /dev/null +++ b/li.strolch.service/src/test/java/li/strolch/command/plan/ShiftActionTest.java @@ -0,0 +1,167 @@ +/* + * Copyright 2015 Martin Smock + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package li.strolch.command.plan; + +import static li.strolch.model.ModelGenerator.STATE_INTEGER_ID; +import static li.strolch.model.ModelGenerator.STATE_INTEGER_NAME; +import static li.strolch.model.ModelGenerator.STATE_INTEGER_TIME_0; +import static li.strolch.model.ModelGenerator.STATE_TIME_0; +import static li.strolch.model.ModelGenerator.STATE_TIME_10; +import static li.strolch.model.ModelGenerator.STATE_TIME_20; +import static li.strolch.model.ModelGenerator.STATE_TIME_30; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.SortedSet; + +import li.strolch.model.Locator; +import li.strolch.model.ModelGenerator; +import li.strolch.model.ParameterBag; +import li.strolch.model.Resource; +import li.strolch.model.State; +import li.strolch.model.Tags; +import li.strolch.model.activity.Action; +import li.strolch.model.parameter.IntegerParameter; +import li.strolch.model.parameter.Parameter; +import li.strolch.model.timedstate.IntegerTimedState; +import li.strolch.model.timedstate.StrolchTimedState; +import li.strolch.model.timevalue.ITimeValue; +import li.strolch.model.timevalue.ITimeVariable; +import li.strolch.model.timevalue.IValue; +import li.strolch.model.timevalue.IValueChange; +import li.strolch.model.timevalue.impl.IntegerValue; +import li.strolch.model.timevalue.impl.ValueChange; +import li.strolch.persistence.api.StrolchTransaction; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author Martin Smock + */ +public class ShiftActionTest { + + Resource resource; + Action action; + IntegerTimedState timedState; + StrolchTransaction tx; + + @Before + public void init() { + + // add a resource with integer state variable + resource = ModelGenerator.createResource("@1", "Test With States", "Stated"); + timedState = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME); + timedState.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0))); + resource.addTimedState(timedState); + + action = new Action("action", "Action", "Use"); + + Assert.assertEquals(State.CREATED, action.getState()); + + final IntegerParameter iP = new IntegerParameter("quantity", "Occupation", 1); + action.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); + action.addParameter("objective", iP); + + createChanges(action); + + action.setResourceId(resource.getId()); + action.setResourceType(resource.getType()); + + tx = mock(StrolchTransaction.class); + + final Locator locator = Locator.newBuilder(Tags.RESOURCE, "Stated", "@1").build(); + when(tx.findElement(eq(locator))).thenReturn(resource); + + final PlanActionCommand cmd = new PlanActionCommand(null, tx); + cmd.setAction(action); + cmd.doCommand(); + + } + + @Test + public void test() { + + final ShiftActionCommand cmd = new ShiftActionCommand(null, tx); + cmd.setAction(action); + cmd.setShift(10L); + cmd.doCommand(); + + // check the state + Assert.assertEquals(State.PLANNED, action.getState()); + + // check the resource Id + Assert.assertEquals(resource.getId(), action.getResourceId()); + + // check if we get the expected result + final StrolchTimedState> timedState = resource.getTimedState(STATE_INTEGER_ID); + final ITimeVariable> timeEvolution = timedState.getTimeEvolution(); + final SortedSet>> values = timeEvolution.getValues(); + + Assert.assertEquals(3, values.size()); + + ITimeValue> valueAt = timeEvolution.getValueAt(STATE_TIME_0); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + valueAt = timeEvolution.getValueAt(STATE_TIME_10); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + valueAt = timeEvolution.getValueAt(STATE_TIME_20); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(1))); + + valueAt = timeEvolution.getValueAt(STATE_TIME_30); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + // check the undo functionality + cmd.undo(); + + valueAt = timeEvolution.getValueAt(STATE_TIME_0); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + valueAt = timeEvolution.getValueAt(STATE_TIME_10); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(1))); + + valueAt = timeEvolution.getValueAt(STATE_TIME_20); + Assert.assertEquals(true, valueAt.getValue().equals(new IntegerValue(0))); + + } + + /** + *

+ * add changes to action start and end time with a value defined in the + * action objective and set the stateId of the state variable to apply the + * change to + *

+ * + * @param action + */ + protected static void createChanges(final Action action) { + + final Parameter parameter = action.getParameter("objective", "quantity"); + final Integer quantity = parameter.getValue(); + + final IValueChange startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity)); + startChange.setStateId(STATE_INTEGER_ID); + action.addChange(startChange); + + final IValueChange endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity)); + endChange.setStateId(STATE_INTEGER_ID); + action.addChange(endChange); + } + +}