added the two core command required to set up a disposition application
and refactored to remove code duplication.
This commit is contained in:
parent
19c6d0827f
commit
97c7e36536
|
@ -0,0 +1,124 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
|
*
|
||||||
|
* 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 <martin.smock@bluewin.ch>
|
||||||
|
*/
|
||||||
|
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<IValueChange<?>> 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<Entry<String, IActivityElement>> 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<IValueChange<?>> 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<Entry<String, IActivityElement>> 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,98 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
|
*
|
||||||
|
* 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 <martin.smock@bluewin.ch>
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -15,13 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package li.strolch.command.plan;
|
package li.strolch.command.plan;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
import li.strolch.agent.api.ComponentContainer;
|
||||||
import li.strolch.model.Locator;
|
|
||||||
import li.strolch.model.Resource;
|
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.Action;
|
||||||
import li.strolch.model.timedstate.StrolchTimedState;
|
import li.strolch.model.timedstate.StrolchTimedState;
|
||||||
import li.strolch.model.timevalue.IValueChange;
|
import li.strolch.model.timevalue.IValueChange;
|
||||||
|
@ -38,7 +33,7 @@ import ch.eitchnet.utils.dbc.DBC;
|
||||||
*
|
*
|
||||||
* @author Martin Smock <martin.smock@bluewin.ch>
|
* @author Martin Smock <martin.smock@bluewin.ch>
|
||||||
*/
|
*/
|
||||||
public class PlanActionCommand extends Command {
|
public class PlanActionCommand extends AbstractPlanCommand {
|
||||||
|
|
||||||
protected Action action;
|
protected Action action;
|
||||||
|
|
||||||
|
@ -53,46 +48,19 @@ public class PlanActionCommand extends Command {
|
||||||
@Override
|
@Override
|
||||||
public void validate() {
|
public void validate() {
|
||||||
DBC.PRE.assertNotNull("Action may not be null!", this.action);
|
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 resourceId may not be null!", action.getResourceId());
|
||||||
DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", this.action.getResourceType());
|
DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", 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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
public void doCommand() {
|
public void doCommand() {
|
||||||
|
validate();
|
||||||
Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build();
|
plan(action);
|
||||||
Resource resource = tx().findElement(locator);
|
|
||||||
|
|
||||||
tx().lock(resource);
|
|
||||||
|
|
||||||
final List<IValueChange<?>> 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());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
|
||||||
public void undo() {
|
public void undo() {
|
||||||
|
unplan(action);
|
||||||
Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build();
|
|
||||||
Resource resource = tx().findElement(locator);
|
|
||||||
|
|
||||||
final List<IValueChange<?>> 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAction(Action action) {
|
public void setAction(Action action) {
|
||||||
|
|
|
@ -16,15 +16,10 @@
|
||||||
package li.strolch.command.plan;
|
package li.strolch.command.plan;
|
||||||
|
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import li.strolch.agent.api.ComponentContainer;
|
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.Resource;
|
||||||
import li.strolch.model.State;
|
|
||||||
import li.strolch.model.Tags;
|
|
||||||
import li.strolch.model.activity.Action;
|
import li.strolch.model.activity.Action;
|
||||||
import li.strolch.model.activity.Activity;
|
import li.strolch.model.activity.Activity;
|
||||||
import li.strolch.model.activity.IActivityElement;
|
import li.strolch.model.activity.IActivityElement;
|
||||||
|
@ -45,7 +40,7 @@ import ch.eitchnet.utils.dbc.DBC;
|
||||||
*
|
*
|
||||||
* @author Martin Smock <martin.smock@bluewin.ch>
|
* @author Martin Smock <martin.smock@bluewin.ch>
|
||||||
*/
|
*/
|
||||||
public class PlanActivityCommand extends Command {
|
public class PlanActivityCommand extends AbstractPlanCommand {
|
||||||
|
|
||||||
protected Activity activity;
|
protected Activity activity;
|
||||||
|
|
||||||
|
@ -53,7 +48,7 @@ public class PlanActivityCommand extends Command {
|
||||||
* @param container
|
* @param container
|
||||||
* @param tx
|
* @param tx
|
||||||
*/
|
*/
|
||||||
public PlanActivityCommand(ComponentContainer container, StrolchTransaction tx) {
|
public PlanActivityCommand(final ComponentContainer container, final StrolchTransaction tx) {
|
||||||
super(container, tx);
|
super(container, tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,15 +58,15 @@ public class PlanActivityCommand extends Command {
|
||||||
validate(activity);
|
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 resourceId may not be null!", action.getResourceId());
|
||||||
DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", action.getResourceType());
|
DBC.PRE.assertNotNull("Action attribute resourceType may not be null!", action.getResourceType());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void validate(Activity activity) {
|
private void validate(final Activity activity) {
|
||||||
Iterator<Entry<String, IActivityElement>> elementIterator = activity.elementIterator();
|
final Iterator<Entry<String, IActivityElement>> elementIterator = activity.elementIterator();
|
||||||
while (elementIterator.hasNext()) {
|
while (elementIterator.hasNext()) {
|
||||||
IActivityElement activityElement = elementIterator.next().getValue();
|
final IActivityElement activityElement = elementIterator.next().getValue();
|
||||||
if (activityElement instanceof Activity)
|
if (activityElement instanceof Activity)
|
||||||
validate((Activity) activityElement);
|
validate((Activity) activityElement);
|
||||||
else if (activityElement instanceof Action)
|
else if (activityElement instanceof Action)
|
||||||
|
@ -86,85 +81,10 @@ public class PlanActivityCommand extends Command {
|
||||||
plan(activity);
|
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<Entry<String, IActivityElement>> 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<IValueChange<?>> startChanges = action.getChanges();
|
|
||||||
for (IValueChange<?> change : startChanges) {
|
|
||||||
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
|
|
||||||
timedState.applyChange(change);
|
|
||||||
}
|
|
||||||
|
|
||||||
action.setState(State.PLANNED);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void undo() {
|
public void undo() {
|
||||||
tx().lock(activity);
|
tx().lock(activity);
|
||||||
unplanActivity(activity);
|
unplan(activity);
|
||||||
}
|
|
||||||
|
|
||||||
private void unplanActivity(Activity activity) {
|
|
||||||
|
|
||||||
Iterator<Entry<String, IActivityElement>> 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<IValueChange<?>> startChanges = action.getChanges();
|
|
||||||
for (IValueChange<?> change : startChanges) {
|
|
||||||
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
|
|
||||||
timedState.applyChange(change.getInverse());
|
|
||||||
}
|
|
||||||
|
|
||||||
action.setState(State.CREATED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setActivity(Activity activity) {
|
public void setActivity(Activity activity) {
|
||||||
|
|
|
@ -0,0 +1,90 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
|
*
|
||||||
|
* 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 <martin.smock@bluewin.ch>
|
||||||
|
*/
|
||||||
|
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<IValueChange<?>> 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<IValueChange<?>> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,192 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
|
*
|
||||||
|
* 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 <martin.smock@bluewin.ch>
|
||||||
|
*/
|
||||||
|
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<IValue<Integer>> initialTimedState = initialResource.getTimedState(STATE_INTEGER_ID);
|
||||||
|
ITimeVariable<IValue<Integer>> initialTimeEvolution = initialTimedState.getTimeEvolution();
|
||||||
|
SortedSet<ITimeValue<IValue<Integer>>> initialValues = initialTimeEvolution.getValues();
|
||||||
|
|
||||||
|
Assert.assertEquals(1, initialValues.size());
|
||||||
|
|
||||||
|
StrolchTimedState<IValue<Integer>> targetTimedState = targetResource.getTimedState(STATE_INTEGER_ID);
|
||||||
|
ITimeVariable<IValue<Integer>> targetTimeEvolution = targetTimedState.getTimeEvolution();
|
||||||
|
SortedSet<ITimeValue<IValue<Integer>>> targetValues = targetTimeEvolution.getValues();
|
||||||
|
|
||||||
|
Assert.assertEquals(3, targetValues.size());
|
||||||
|
|
||||||
|
ITimeValue<IValue<Integer>> 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)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param action
|
||||||
|
*/
|
||||||
|
protected static void createChanges(final Action action) {
|
||||||
|
|
||||||
|
final Parameter<Integer> parameter = action.getParameter("objective", "quantity");
|
||||||
|
final Integer quantity = parameter.getValue();
|
||||||
|
|
||||||
|
final IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity));
|
||||||
|
startChange.setStateId(STATE_INTEGER_ID);
|
||||||
|
action.addChange(startChange);
|
||||||
|
|
||||||
|
final IValueChange<IntegerValue> endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity));
|
||||||
|
endChange.setStateId(STATE_INTEGER_ID);
|
||||||
|
action.addChange(endChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2014 Martin Smock <smock.martin@gmail.com>
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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)));
|
timedState.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0)));
|
||||||
resource.addTimedState(timedState);
|
resource.addTimedState(timedState);
|
||||||
|
|
||||||
action = new Action("action_1", "Action 1", "Use");
|
action = new Action("action", "Action", "Use");
|
||||||
|
|
||||||
Assert.assertEquals(State.CREATED, action.getState());
|
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.addParameterBag(new ParameterBag("objective", "Objective", "Don't know"));
|
||||||
action.addParameter("objective", iP);
|
action.addParameter("objective", iP);
|
||||||
|
|
||||||
|
@ -84,9 +84,9 @@ public class PlanActionTest {
|
||||||
@Test
|
@Test
|
||||||
public void 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);
|
when(tx.findElement(eq(locator))).thenReturn(resource);
|
||||||
|
|
||||||
final PlanActionCommand cmd = new PlanActionCommand(null, tx);
|
final PlanActionCommand cmd = new PlanActionCommand(null, tx);
|
||||||
|
@ -100,8 +100,8 @@ public class PlanActionTest {
|
||||||
Assert.assertEquals(resource.getId(), action.getResourceId());
|
Assert.assertEquals(resource.getId(), action.getResourceId());
|
||||||
|
|
||||||
// check if we get the expected result
|
// check if we get the expected result
|
||||||
StrolchTimedState<IValue<Integer>> timedState = resource.getTimedState(STATE_INTEGER_ID);
|
final StrolchTimedState<IValue<Integer>> timedState = resource.getTimedState(STATE_INTEGER_ID);
|
||||||
ITimeVariable<IValue<Integer>> timeEvolution = timedState.getTimeEvolution();
|
final ITimeVariable<IValue<Integer>> timeEvolution = timedState.getTimeEvolution();
|
||||||
SortedSet<ITimeValue<IValue<Integer>>> values = timeEvolution.getValues();
|
SortedSet<ITimeValue<IValue<Integer>>> values = timeEvolution.getValues();
|
||||||
|
|
||||||
Assert.assertEquals(3, values.size());
|
Assert.assertEquals(3, values.size());
|
||||||
|
@ -132,21 +132,22 @@ public class PlanActionTest {
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
* add changes to action start and end time with a value defined in the
|
* 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
|
||||||
* </p>
|
* </p>
|
||||||
*
|
*
|
||||||
* @param action
|
* @param action
|
||||||
*/
|
*/
|
||||||
protected static void createChanges(final Action action) {
|
protected static void createChanges(final Action action) {
|
||||||
|
|
||||||
Parameter<Integer> parameter = action.getParameter("objective", "quantity");
|
final Parameter<Integer> parameter = action.getParameter("objective", "quantity");
|
||||||
Integer quantity = parameter.getValue();
|
final Integer quantity = parameter.getValue();
|
||||||
|
|
||||||
IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity));
|
final IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity));
|
||||||
startChange.setStateId(STATE_INTEGER_ID);
|
startChange.setStateId(STATE_INTEGER_ID);
|
||||||
action.addChange(startChange);
|
action.addChange(startChange);
|
||||||
|
|
||||||
IValueChange<IntegerValue> endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity));
|
final IValueChange<IntegerValue> endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity));
|
||||||
endChange.setStateId(STATE_INTEGER_ID);
|
endChange.setStateId(STATE_INTEGER_ID);
|
||||||
action.addChange(endChange);
|
action.addChange(endChange);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
|
*
|
||||||
|
* 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;
|
package li.strolch.command.plan;
|
||||||
|
|
||||||
import static li.strolch.model.ModelGenerator.STATE_INTEGER_ID;
|
import static li.strolch.model.ModelGenerator.STATE_INTEGER_ID;
|
||||||
|
@ -49,6 +64,7 @@ public class PlanActivityTest {
|
||||||
Resource resource_1, resource_2, resource_3;
|
Resource resource_1, resource_2, resource_3;
|
||||||
IntegerTimedState timedState_1, timedState_2, timedState_3;
|
IntegerTimedState timedState_1, timedState_2, timedState_3;
|
||||||
Action action_1, action_2, action_3;
|
Action action_1, action_2, action_3;
|
||||||
|
StrolchTransaction tx;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initialize the resources with states and the activity with 2 actions.
|
* 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 = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME);
|
||||||
timedState_2.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0)));
|
timedState_2.applyChange(new ValueChange<>(STATE_TIME_0, new IntegerValue(STATE_INTEGER_TIME_0)));
|
||||||
resource_2.addTimedState(timedState_2);
|
resource_2.addTimedState(timedState_2);
|
||||||
|
|
||||||
// create resource with integer state
|
// create resource with integer state
|
||||||
resource_3 = ModelGenerator.createResource("@3", "Test With States 3", "Stated");
|
resource_3 = ModelGenerator.createResource("@3", "Test With States 3", "Stated");
|
||||||
timedState_3 = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME);
|
timedState_3 = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME);
|
||||||
|
@ -80,7 +96,7 @@ public class PlanActivityTest {
|
||||||
// create action 1
|
// create action 1
|
||||||
action_1 = new Action("action_1", "Action 1", "Use");
|
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.addParameterBag(new ParameterBag("objective", "Objective", "Don't know"));
|
||||||
action_1.addParameter("objective", iP1);
|
action_1.addParameter("objective", iP1);
|
||||||
|
|
||||||
|
@ -90,14 +106,14 @@ public class PlanActivityTest {
|
||||||
action_1.setResourceType(resource_1.getType());
|
action_1.setResourceType(resource_1.getType());
|
||||||
|
|
||||||
activity.addElement(action_1);
|
activity.addElement(action_1);
|
||||||
|
|
||||||
// create child activity
|
// create child activity
|
||||||
childActivity = new Activity("childActivity", "Child Activity", "childType");
|
childActivity = new Activity("childActivity", "Child Activity", "childType");
|
||||||
|
|
||||||
// create action 2
|
// create action 2
|
||||||
action_2 = new Action("action_2", "Action 2", "Use");
|
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.addParameterBag(new ParameterBag("objective", "Objective", "Don't know"));
|
||||||
action_2.addParameter("objective", iP2);
|
action_2.addParameter("objective", iP2);
|
||||||
|
|
||||||
|
@ -107,11 +123,11 @@ public class PlanActivityTest {
|
||||||
action_2.setResourceType(resource_2.getType());
|
action_2.setResourceType(resource_2.getType());
|
||||||
|
|
||||||
childActivity.addElement(action_2);
|
childActivity.addElement(action_2);
|
||||||
|
|
||||||
// create action 3
|
// create action 3
|
||||||
action_3 = new Action("action_3", "Action 3", "Use");
|
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.addParameterBag(new ParameterBag("objective", "Objective", "Don't know"));
|
||||||
action_3.addParameter("objective", iP3);
|
action_3.addParameter("objective", iP3);
|
||||||
|
|
||||||
|
@ -121,11 +137,22 @@ public class PlanActivityTest {
|
||||||
action_3.setResourceType(resource_3.getType());
|
action_3.setResourceType(resource_3.getType());
|
||||||
|
|
||||||
childActivity.addElement(action_3);
|
childActivity.addElement(action_3);
|
||||||
|
|
||||||
activity.addElement(childActivity);
|
activity.addElement(childActivity);
|
||||||
|
|
||||||
Assert.assertEquals(2, activity.getElements().size());
|
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
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
|
||||||
StrolchTransaction tx = mock(StrolchTransaction.class);
|
final PlanActivityCommand planActivityCommand = new PlanActivityCommand(null, tx);
|
||||||
|
|
||||||
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);
|
|
||||||
planActivityCommand.setActivity(activity);
|
planActivityCommand.setActivity(activity);
|
||||||
planActivityCommand.doCommand();
|
planActivityCommand.doCommand();
|
||||||
|
|
||||||
|
@ -154,8 +170,8 @@ public class PlanActivityTest {
|
||||||
Assert.assertEquals(State.PLANNED, action_2.getState());
|
Assert.assertEquals(State.PLANNED, action_2.getState());
|
||||||
|
|
||||||
// check the resource states
|
// check the resource states
|
||||||
StrolchTimedState<IValue<Integer>> timedState_1 = resource_1.getTimedState(STATE_INTEGER_ID);
|
final StrolchTimedState<IValue<Integer>> timedState_1 = resource_1.getTimedState(STATE_INTEGER_ID);
|
||||||
ITimeVariable<IValue<Integer>> timeEvolution_1 = timedState_1.getTimeEvolution();
|
final ITimeVariable<IValue<Integer>> timeEvolution_1 = timedState_1.getTimeEvolution();
|
||||||
SortedSet<ITimeValue<IValue<Integer>>> values_1 = timeEvolution_1.getValues();
|
SortedSet<ITimeValue<IValue<Integer>>> values_1 = timeEvolution_1.getValues();
|
||||||
|
|
||||||
Assert.assertEquals(3, values_1.size());
|
Assert.assertEquals(3, values_1.size());
|
||||||
|
@ -170,8 +186,8 @@ public class PlanActivityTest {
|
||||||
Assert.assertEquals(true, valueAt_1.getValue().equals(new IntegerValue(0)));
|
Assert.assertEquals(true, valueAt_1.getValue().equals(new IntegerValue(0)));
|
||||||
|
|
||||||
// the second resource
|
// the second resource
|
||||||
StrolchTimedState<IValue<Integer>> timedState_2 = resource_2.getTimedState(STATE_INTEGER_ID);
|
final StrolchTimedState<IValue<Integer>> timedState_2 = resource_2.getTimedState(STATE_INTEGER_ID);
|
||||||
ITimeVariable<IValue<Integer>> timeEvolution_2 = timedState_2.getTimeEvolution();
|
final ITimeVariable<IValue<Integer>> timeEvolution_2 = timedState_2.getTimeEvolution();
|
||||||
SortedSet<ITimeValue<IValue<Integer>>> values_2 = timeEvolution_2.getValues();
|
SortedSet<ITimeValue<IValue<Integer>>> values_2 = timeEvolution_2.getValues();
|
||||||
|
|
||||||
Assert.assertEquals(3, values_2.size());
|
Assert.assertEquals(3, values_2.size());
|
||||||
|
@ -208,14 +224,14 @@ public class PlanActivityTest {
|
||||||
*/
|
*/
|
||||||
protected static void createChanges(Action action, Long start, Long end) {
|
protected static void createChanges(Action action, Long start, Long end) {
|
||||||
|
|
||||||
Parameter<Integer> parameter = action.getParameter("objective", "quantity");
|
final Parameter<Integer> parameter = action.getParameter("objective", "quantity");
|
||||||
Integer quantity = parameter.getValue();
|
final Integer quantity = parameter.getValue();
|
||||||
|
|
||||||
IValueChange<IntegerValue> startChange = new ValueChange<>(start, new IntegerValue(quantity));
|
final IValueChange<IntegerValue> startChange = new ValueChange<>(start, new IntegerValue(quantity));
|
||||||
startChange.setStateId(STATE_INTEGER_ID);
|
startChange.setStateId(STATE_INTEGER_ID);
|
||||||
action.addChange(startChange);
|
action.addChange(startChange);
|
||||||
|
|
||||||
IValueChange<IntegerValue> endChange = new ValueChange<>(end, new IntegerValue(-quantity));
|
final IValueChange<IntegerValue> endChange = new ValueChange<>(end, new IntegerValue(-quantity));
|
||||||
endChange.setStateId(STATE_INTEGER_ID);
|
endChange.setStateId(STATE_INTEGER_ID);
|
||||||
action.addChange(endChange);
|
action.addChange(endChange);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,167 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2015 Martin Smock <martin.smock@bluewin.ch>
|
||||||
|
*
|
||||||
|
* 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 <martin.smock@bluewin.ch>
|
||||||
|
*/
|
||||||
|
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<IValue<Integer>> timedState = resource.getTimedState(STATE_INTEGER_ID);
|
||||||
|
final ITimeVariable<IValue<Integer>> timeEvolution = timedState.getTimeEvolution();
|
||||||
|
final SortedSet<ITimeValue<IValue<Integer>>> values = timeEvolution.getValues();
|
||||||
|
|
||||||
|
Assert.assertEquals(3, values.size());
|
||||||
|
|
||||||
|
ITimeValue<IValue<Integer>> 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)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* @param action
|
||||||
|
*/
|
||||||
|
protected static void createChanges(final Action action) {
|
||||||
|
|
||||||
|
final Parameter<Integer> parameter = action.getParameter("objective", "quantity");
|
||||||
|
final Integer quantity = parameter.getValue();
|
||||||
|
|
||||||
|
final IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity));
|
||||||
|
startChange.setStateId(STATE_INTEGER_ID);
|
||||||
|
action.addChange(startChange);
|
||||||
|
|
||||||
|
final IValueChange<IntegerValue> endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity));
|
||||||
|
endChange.setStateId(STATE_INTEGER_ID);
|
||||||
|
action.addChange(endChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue