From 188a8d94614d94f8fe64f32d3948030654c2b9e3 Mon Sep 17 00:00:00 2001 From: msmock Date: Sat, 23 May 2015 10:14:16 +0200 Subject: [PATCH] simplified model and removed distinction between start and end operators in action. Removed the action start and end attribute. --- .../src/main/java/li/strolch/model/Tags.java | 2 + .../li/strolch/model/activity/Action.java | 109 ++++++------------ .../li/strolch/model/activity/ActionTest.java | 68 ++++++++--- .../strolch/model/activity/ActivityTest.java | 11 -- .../command/plan/PlanActionCommand.java | 17 +-- .../command/plan/PlanActivityCommand.java | 18 +-- .../strolch/command/plan/PlanActionTest.java | 10 +- .../command/plan/PlanActivityTest.java | 23 ++-- 8 files changed, 105 insertions(+), 153 deletions(-) diff --git a/li.strolch.model/src/main/java/li/strolch/model/Tags.java b/li.strolch.model/src/main/java/li/strolch/model/Tags.java index 5a00079b1..96a2569d5 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/Tags.java +++ b/li.strolch.model/src/main/java/li/strolch/model/Tags.java @@ -44,6 +44,8 @@ public class Tags { public static final String ACTIVITY = "Activity"; public static final String ACTION = "Action"; + public static final String START = "Start"; + public static final String END = "End"; public class Audit { public static final String ID = Tags.ID; diff --git a/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java b/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java index 5d5c91e6d..348aed945 100644 --- a/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java +++ b/li.strolch.model/src/main/java/li/strolch/model/activity/Action.java @@ -11,6 +11,7 @@ import li.strolch.model.Resource; import li.strolch.model.State; import li.strolch.model.StrolchElement; import li.strolch.model.StrolchRootElement; +import li.strolch.model.Tags; import li.strolch.model.timevalue.IValueChange; import org.w3c.dom.Document; @@ -28,54 +29,17 @@ public class Action extends GroupedParameterizedElement implements IActivityElem protected static final long serialVersionUID = 1L; - protected Long start; - protected Long end; - + protected Activity parent; protected String resourceId; - - protected final List> startChanges = new ArrayList<>(); - protected final List> endChanges = new ArrayList<>(); - + private String resourceType; protected State state = State.CREATED; - protected Activity parent; - - private String resourceType; + protected final List> changes = new ArrayList<>(); public Action(String id, String name, String type) { super(id, name, type); } - /** - * @return the action start time in unix millisecond time - */ - public Long getStart() { - return start; - } - - /** - * @param start - * the action start time in unix millisecond time - */ - public void setStart(Long start) { - this.start = start; - } - - /** - * @return the action end time in unix millisecond time - */ - public Long getEnd() { - return end; - } - - /** - * @param end - * the action end time in unix millisecond time - */ - public void setEnd(Long end) { - this.end = end; - } - /** * @return the id of the {@link Resource} the {@link Action} acts on */ @@ -124,44 +88,28 @@ public class Action extends GroupedParameterizedElement implements IActivityElem /** * @param add * IValueChange to be applied to the - * Resource to time Action.getStart() + * Resource * * @return true (as specified by {@link Collection#add}) */ - public boolean addStartChange(IValueChange change) { - return startChanges.add(change); - } - - /** - * @param change - * IValueChange to be applied to the - * Resource at time Action.getEnd() - * @return true (as specified by {@link Collection#add}) - */ - public boolean addEndChange(IValueChange change) { - return endChanges.add(change); + public boolean addChange(IValueChange change) { + return changes.add(change); } /** * @return the list of IValueChange attached to the * Action start */ - public List> getStartChanges() { - return startChanges; - } - - /** - * @return the list of IValueChange attached to the - * Action end - */ - public List> getEndChanges() { - return endChanges; + public List> getChanges() { + return changes; } @Override public Element toDom(Document doc) { - // TODO Auto-generated method stub - return null; + Element element = doc.createElement(Tags.ACTION); + fillElement(element); + element.setAttribute(Tags.STATE, this.state.toString()); + return element; } @Override @@ -183,16 +131,11 @@ public class Action extends GroupedParameterizedElement implements IActivityElem public StrolchElement getClone() { Action clone = new Action(getId(), getName(), getType()); clone.setDbid(getDbid()); - clone.setEnd(end); clone.setResourceId(resourceId); clone.setResourceType(resourceType); - clone.setStart(start); clone.setState(state); - for (IValueChange change : getStartChanges()) { - clone.startChanges.add(change.getClone()); - } - for (IValueChange change : getEndChanges()) { - clone.endChanges.add(change.getClone()); + for (IValueChange change : getChanges()) { + clone.changes.add(change.getClone()); } return clone; } @@ -223,10 +166,6 @@ public class Action extends GroupedParameterizedElement implements IActivityElem builder.append(this.resourceId); builder.append(", state="); builder.append(this.state); - builder.append(", start="); - builder.append(this.start); - builder.append(", end="); - builder.append(this.end); builder.append("]"); return builder.toString(); } @@ -236,4 +175,22 @@ public class Action extends GroupedParameterizedElement implements IActivityElem this.parent = activity; } + @Override + public Long getStart() { + Long start = Long.MAX_VALUE; + for (IValueChange change : changes){ + start = Math.min(start, change.getTime()); + } + return start; + } + + @Override + public Long getEnd() { + Long end = 0L; + for (IValueChange change : changes){ + end = Math.max(end, change.getTime()); + } + return end; + } + } diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java index 585a45c33..17f662e10 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActionTest.java @@ -3,6 +3,19 @@ package li.strolch.model.activity; import static li.strolch.model.ModelGenerator.STATE_INTEGER_ID; import static li.strolch.model.ModelGenerator.STATE_TIME_10; import static li.strolch.model.ModelGenerator.STATE_TIME_30; + +import java.io.StringWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + import li.strolch.model.timevalue.IValueChange; import li.strolch.model.timevalue.impl.IntegerValue; import li.strolch.model.timevalue.impl.ValueChange; @@ -10,6 +23,8 @@ import li.strolch.model.timevalue.impl.ValueChange; import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; public class ActionTest { @@ -22,31 +37,54 @@ public class ActionTest { public void init() { // create action action = new Action("action_1", "Action 1", "Use"); - action.setStart(STATE_TIME_10); - action.setEnd(STATE_TIME_30); - IValueChange startChange = new ValueChange<>(action.getStart(), new IntegerValue(1)); + IValueChange startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(1)); startChange.setStateId(STATE_INTEGER_ID); - action.addStartChange(startChange); + action.addChange(startChange); - IValueChange endChange = new ValueChange<>(action.getEnd(), new IntegerValue(-1)); + IValueChange endChange = new ValueChange<>(STATE_TIME_30, new IntegerValue(-1)); endChange.setStateId(STATE_INTEGER_ID); - action.addEndChange(endChange); + action.addChange(endChange); } + + @Test + public void testGetStart() { + Assert.assertTrue(STATE_TIME_10 == action.getStart()); + } + + @Test + public void testGetEnd() { + Assert.assertTrue(STATE_TIME_30 == action.getEnd()); + } + @Test public void testClone() { Action clone = (Action) action.getClone(); Assert.assertEquals(action.toString(), clone.toString()); - Assert.assertEquals(action.startChanges.size(), clone.startChanges.size()); - Assert.assertEquals(action.endChanges.size(), clone.endChanges.size()); - for (int i = 0; i < action.startChanges.size(); i++) { - Assert.assertEquals(action.startChanges.get(i).getTime(), clone.startChanges.get(i).getTime()); - Assert.assertEquals(action.startChanges.get(i).getValue(), clone.startChanges.get(i).getValue()); - } - for (int i = 0; i < action.endChanges.size(); i++) { - Assert.assertEquals(action.endChanges.get(i).getTime(), clone.endChanges.get(i).getTime()); - Assert.assertEquals(action.endChanges.get(i).getValue(), clone.endChanges.get(i).getValue()); + Assert.assertEquals(action.changes.size(), clone.changes.size()); + for (int i = 0; i < action.changes.size(); i++) { + Assert.assertEquals(action.changes.get(i).getTime(), clone.changes.get(i).getTime()); } } + + @Test + public void testToDOM() throws ParserConfigurationException, TransformerException { + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder db = dbf.newDocumentBuilder(); + Document document = db.newDocument(); + Element dom = action.toDom(document); + document.appendChild(dom); + + TransformerFactory tf = TransformerFactory.newInstance(); + Transformer transformer = tf.newTransformer(); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + StringWriter writer = new StringWriter(); + transformer.transform(new DOMSource(document), new StreamResult(writer)); + String content = writer.getBuffer().toString(); + System.out.println(content); + + } + } diff --git a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java index 496fdfaf6..8917edd89 100644 --- a/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java +++ b/li.strolch.model/src/test/java/li/strolch/model/activity/ActivityTest.java @@ -1,10 +1,5 @@ package li.strolch.model.activity; -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 li.strolch.model.ModelGenerator.STATE_TIME_40; - import li.strolch.exception.StrolchException; import li.strolch.model.State; @@ -28,8 +23,6 @@ public class ActivityTest { // create action 1 action_1 = new Action("action_1", "Action 1", "Use"); - action_1.setStart(STATE_TIME_10); - action_1.setEnd(STATE_TIME_20); action_1.setState(State.CREATED); activity.addElement(action_1); @@ -38,16 +31,12 @@ public class ActivityTest { // create action 2 action_2 = new Action("action_2", "Action 2", "Use"); - action_2.setStart(STATE_TIME_20); - action_2.setEnd(STATE_TIME_30); action_2.setState(State.PLANNED); childActivity.addElement(action_2); // create action 3 action_3 = new Action("action_3", "Action 3", "Use"); - action_3.setStart(STATE_TIME_20); - action_3.setEnd(STATE_TIME_40); action_3.setState(State.CREATED); childActivity.addElement(action_3); 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 5da38270e..10c94f744 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 @@ -68,17 +68,11 @@ public class PlanActionCommand extends Command { tx().lock(resource); - final List> startChanges = action.getStartChanges(); + final List> startChanges = action.getChanges(); for (IValueChange change : startChanges) { final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); timedState.applyChange(change); } - - final List> endChanges = action.getEndChanges(); - for (IValueChange change : endChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change); - } // finally set the action state action.setState(State.PLANNED); action.setResourceId(resource.getId()); @@ -91,17 +85,12 @@ public class PlanActionCommand extends Command { Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); Resource resource = tx().findElement(locator); - final List> startChanges = action.getStartChanges(); + final List> startChanges = action.getChanges(); for (IValueChange change : startChanges) { final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); timedState.applyChange(change.getInverse()); } - - final List> endChanges = action.getEndChanges(); - for (IValueChange change : endChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change.getInverse()); - } + // finally set the action state action.setState(State.CREATED); } 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 dee04e228..418f0cc2d 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 @@ -66,8 +66,6 @@ public class PlanActivityCommand extends Command { private void validate(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()); - DBC.PRE.assertTrue("Action attribute start must be set!", action.getStart() > 0); - DBC.PRE.assertTrue("Action attribute end must later than start!", action.getEnd() > action.getStart()); } private void validate(Activity activity) { @@ -124,18 +122,12 @@ public class PlanActivityCommand extends Command { tx().lock(resource); - final List> startChanges = action.getStartChanges(); + final List> startChanges = action.getChanges(); for (IValueChange change : startChanges) { final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); timedState.applyChange(change); } - final List> endChanges = action.getEndChanges(); - for (IValueChange change : endChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change); - } - action.setState(State.PLANNED); } @@ -166,18 +158,12 @@ public class PlanActivityCommand extends Command { Locator locator = Locator.newBuilder(Tags.RESOURCE, action.getResourceType(), action.getResourceId()).build(); Resource resource = tx().findElement(locator); - final List> startChanges = action.getStartChanges(); + final List> startChanges = action.getChanges(); for (IValueChange change : startChanges) { final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); timedState.applyChange(change.getInverse()); } - final List> endChanges = action.getEndChanges(); - for (IValueChange change : endChanges) { - final StrolchTimedState timedState = resource.getTimedState(change.getStateId()); - timedState.applyChange(change.getInverse()); - } - action.setState(State.CREATED); } 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 a8dfee491..4704a00d7 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 @@ -68,8 +68,6 @@ public class PlanActionTest { resource.addTimedState(timedState); action = new Action("action_1", "Action 1", "Use"); - action.setStart(STATE_TIME_10); - action.setEnd(STATE_TIME_20); Assert.assertEquals(State.CREATED, action.getState()); @@ -144,13 +142,13 @@ public class PlanActionTest { Parameter parameter = action.getParameter("objective", "quantity"); Integer quantity = parameter.getValue(); - IValueChange startChange = new ValueChange<>(action.getStart(), new IntegerValue(quantity)); + IValueChange startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity)); startChange.setStateId(STATE_INTEGER_ID); - action.addStartChange(startChange); + action.addChange(startChange); - IValueChange endChange = new ValueChange<>(action.getEnd(), new IntegerValue(-quantity)); + IValueChange endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity)); endChange.setStateId(STATE_INTEGER_ID); - action.addEndChange(endChange); + 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 3bc935712..b72604a80 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 @@ -8,7 +8,6 @@ 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 li.strolch.model.ModelGenerator.STATE_TIME_40; - import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -80,14 +79,12 @@ public class PlanActivityTest { // create action 1 action_1 = new Action("action_1", "Action 1", "Use"); - action_1.setStart(STATE_TIME_10); - action_1.setEnd(STATE_TIME_20); IntegerParameter iP1 = new IntegerParameter("quantity", "Occupation", 1); action_1.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action_1.addParameter("objective", iP1); - createChanges(action_1); + createChanges(action_1, STATE_TIME_10, STATE_TIME_20); action_1.setResourceId(resource_1.getId()); action_1.setResourceType(resource_1.getType()); @@ -99,14 +96,12 @@ public class PlanActivityTest { // create action 2 action_2 = new Action("action_2", "Action 2", "Use"); - action_2.setStart(STATE_TIME_20); - action_2.setEnd(STATE_TIME_30); IntegerParameter iP2 = new IntegerParameter("quantity", "Occupation", 1); action_2.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action_2.addParameter("objective", iP2); - createChanges(action_2); + createChanges(action_2, STATE_TIME_20, STATE_TIME_30); action_2.setResourceId(resource_2.getId()); action_2.setResourceType(resource_2.getType()); @@ -115,14 +110,12 @@ public class PlanActivityTest { // create action 3 action_3 = new Action("action_3", "Action 3", "Use"); - action_3.setStart(STATE_TIME_20); - action_3.setEnd(STATE_TIME_40); IntegerParameter iP3 = new IntegerParameter("quantity", "Occupation", 1); action_3.addParameterBag(new ParameterBag("objective", "Objective", "Don't know")); action_3.addParameter("objective", iP3); - createChanges(action_3); + createChanges(action_3, STATE_TIME_20, STATE_TIME_40); action_3.setResourceId(resource_3.getId()); action_3.setResourceType(resource_3.getType()); @@ -213,18 +206,18 @@ public class PlanActivityTest { * action objective and set the stateId of the state variable to apply the * change to */ - protected static void createChanges(final Action action) { + protected static void createChanges(Action action, Long start, Long end) { Parameter parameter = action.getParameter("objective", "quantity"); Integer quantity = parameter.getValue(); - IValueChange startChange = new ValueChange<>(action.getStart(), new IntegerValue(quantity)); + IValueChange startChange = new ValueChange<>(start, new IntegerValue(quantity)); startChange.setStateId(STATE_INTEGER_ID); - action.addStartChange(startChange); + action.addChange(startChange); - IValueChange endChange = new ValueChange<>(action.getEnd(), new IntegerValue(-quantity)); + IValueChange endChange = new ValueChange<>(end, new IntegerValue(-quantity)); endChange.setStateId(STATE_INTEGER_ID); - action.addEndChange(endChange); + action.addChange(endChange); }