simplified model and removed distinction between start and end operators

in action. Removed the action start and end attribute.
This commit is contained in:
msmock 2015-05-23 10:14:16 +02:00
parent 1109a7be2d
commit 188a8d9461
8 changed files with 105 additions and 153 deletions

View File

@ -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;

View File

@ -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<IValueChange<?>> startChanges = new ArrayList<>();
protected final List<IValueChange<?>> endChanges = new ArrayList<>();
private String resourceType;
protected State state = State.CREATED;
protected Activity parent;
private String resourceType;
protected final List<IValueChange<?>> 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
* <code>IValueChange</code> to be applied to the
* <code>Resource</code> to time <code>Action.getStart()<code>
* <code>Resource</code>
*
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean addStartChange(IValueChange<?> change) {
return startChanges.add(change);
}
/**
* @param change
* <code>IValueChange</code> to be applied to the
* <code>Resource</code> at time <code>Action.getEnd()<code>
* @return <tt>true</tt> (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 <code>IValueChange</code> attached to the
* <code>Action</code> start
*/
public List<IValueChange<?>> getStartChanges() {
return startChanges;
}
/**
* @return the list of <code>IValueChange</code> attached to the
* <code>Action</code> end
*/
public List<IValueChange<?>> getEndChanges() {
return endChanges;
public List<IValueChange<?>> 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;
}
}

View File

@ -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<IntegerValue> startChange = new ValueChange<>(action.getStart(), new IntegerValue(1));
IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(1));
startChange.setStateId(STATE_INTEGER_ID);
action.addStartChange(startChange);
action.addChange(startChange);
IValueChange<IntegerValue> endChange = new ValueChange<>(action.getEnd(), new IntegerValue(-1));
IValueChange<IntegerValue> 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);
}
}

View File

@ -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);

View File

@ -68,17 +68,11 @@ public class PlanActionCommand extends Command {
tx().lock(resource);
final List<IValueChange<?>> startChanges = action.getStartChanges();
final List<IValueChange<?>> startChanges = action.getChanges();
for (IValueChange<?> change : startChanges) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change);
}
final List<IValueChange<?>> 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<IValueChange<?>> startChanges = action.getStartChanges();
final List<IValueChange<?>> startChanges = action.getChanges();
for (IValueChange<?> change : startChanges) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change.getInverse());
}
final List<IValueChange<?>> 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);
}

View File

@ -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<IValueChange<?>> startChanges = action.getStartChanges();
final List<IValueChange<?>> startChanges = action.getChanges();
for (IValueChange<?> change : startChanges) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change);
}
final List<IValueChange<?>> 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<IValueChange<?>> startChanges = action.getStartChanges();
final List<IValueChange<?>> startChanges = action.getChanges();
for (IValueChange<?> change : startChanges) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change.getInverse());
}
final List<IValueChange<?>> endChanges = action.getEndChanges();
for (IValueChange<?> change : endChanges) {
final StrolchTimedState timedState = resource.getTimedState(change.getStateId());
timedState.applyChange(change.getInverse());
}
action.setState(State.CREATED);
}

View File

@ -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<Integer> parameter = action.getParameter("objective", "quantity");
Integer quantity = parameter.getValue();
IValueChange<IntegerValue> startChange = new ValueChange<>(action.getStart(), new IntegerValue(quantity));
IValueChange<IntegerValue> startChange = new ValueChange<>(STATE_TIME_10, new IntegerValue(quantity));
startChange.setStateId(STATE_INTEGER_ID);
action.addStartChange(startChange);
action.addChange(startChange);
IValueChange<IntegerValue> endChange = new ValueChange<>(action.getEnd(), new IntegerValue(-quantity));
IValueChange<IntegerValue> endChange = new ValueChange<>(STATE_TIME_20, new IntegerValue(-quantity));
endChange.setStateId(STATE_INTEGER_ID);
action.addEndChange(endChange);
action.addChange(endChange);
}
}

View File

@ -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<Integer> parameter = action.getParameter("objective", "quantity");
Integer quantity = parameter.getValue();
IValueChange<IntegerValue> startChange = new ValueChange<>(action.getStart(), new IntegerValue(quantity));
IValueChange<IntegerValue> startChange = new ValueChange<>(start, new IntegerValue(quantity));
startChange.setStateId(STATE_INTEGER_ID);
action.addStartChange(startChange);
action.addChange(startChange);
IValueChange<IntegerValue> endChange = new ValueChange<>(action.getEnd(), new IntegerValue(-quantity));
IValueChange<IntegerValue> endChange = new ValueChange<>(end, new IntegerValue(-quantity));
endChange.setStateId(STATE_INTEGER_ID);
action.addEndChange(endChange);
action.addChange(endChange);
}