[New] TimedStates are now properly incorporated into the Strolch Model

The TimedStates have been added to Resources and can be added, removed and retrieved using the following methods:
- addTimedState(StrolchTimedState):void
- removeTimedState(String):<T extends StrolchTimedState>
- getTimedState(String):<T extends StrolchTimedState>
- getTimedStates():List<StrolchTimedState<?>>
- hasTimedState(String):boolean

Also created a new Boolean StateVariables type

The TimedStates are added to Resources using the StrolchTimedState interface and there is a concrete implementation for each type:
- BooleanTimedState
- IntegerTimedState
- FloatTimedState
- StringSetTimedState

Each timed state is tested in a unit test
Each timed state is (de)serializable from/to XML
This commit is contained in:
Robert von Burg 2014-03-24 15:02:58 +01:00
parent 0dae330e72
commit 7ca7bc7ca7
28 changed files with 1876 additions and 816 deletions

View File

@ -15,10 +15,13 @@
*/
package li.strolch.model;
import ch.eitchnet.utils.helper.StringHelper;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.FloatParameter;
@ -27,224 +30,319 @@ import li.strolch.model.parameter.LongParameter;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import ch.eitchnet.utils.helper.StringHelper;
import li.strolch.model.timedstate.BooleanTimedState;
import li.strolch.model.timedstate.FloatTimedState;
import li.strolch.model.timedstate.IntegerTimedState;
import li.strolch.model.timedstate.StringSetTimedState;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.impl.AString;
import li.strolch.model.timevalue.impl.BooleanValue;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.IntegerValue;
import li.strolch.model.timevalue.impl.StringSetValue;
import li.strolch.model.timevalue.impl.ValueChange;
/**
* Class which can be used to generate objects which implement {@link StrolchElement}. These generated classes can then
* be used in test classes etc.
*
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("nls")
public class ModelGenerator {
public static final String PARAM_BOOLEAN_ID = "@param1";
public static final String PARAM_BOOLEAN_NAME = "Boolean Param";
public static final String PARAM_BOOLEAN_ID = "@param1";
public static final String PARAM_BOOLEAN_NAME = "Boolean Param";
public static final String PARAM_FLOAT_ID = "@param2";
public static final String PARAM_FLOAT_NAME = "Float Param";
public static final String PARAM_FLOAT_ID = "@param2";
public static final String PARAM_FLOAT_NAME = "Float Param";
public static final String PARAM_INTEGER_ID = "@param3";
public static final String PARAM_INTEGER_NAME = "Integer Param";
public static final String PARAM_INTEGER_ID = "@param3";
public static final String PARAM_INTEGER_NAME = "Integer Param";
public static final String PARAM_LONG_ID = "@param4";
public static final String PARAM_LONG_NAME = "Long Param";
public static final String PARAM_LONG_ID = "@param4";
public static final String PARAM_LONG_NAME = "Long Param";
public static final String PARAM_STRING_ID = "@param5";
public static final String PARAM_STRING_NAME = "String Param";
public static final String PARAM_STRING_ID = "@param5";
public static final String PARAM_STRING_NAME = "String Param";
public static final String PARAM_DATE_ID = "@param6";
public static final String PARAM_DATE_NAME = "Date Param";
public static final String PARAM_DATE_ID = "@param6";
public static final String PARAM_DATE_NAME = "Date Param";
public static final String PARAM_LIST_STRING_ID = "@param7";
public static final String PARAM_LIST_STRING_NAME = "StringList Param";
public static final String PARAM_LIST_STRING_ID = "@param7";
public static final String PARAM_LIST_STRING_NAME = "StringList Param";
public static final String BAG_ID = "@bag01";
public static final String BAG_NAME = "Test Bag";
public static final String BAG_TYPE = "TestBag";
public static final String STATE_FLOAT_ID = "@state1";
public static final String STATE_FLOAT_NAME = "Float State";
/**
* Creates an {@link Resource} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param id
* the id of the {@link Resource}
* @param name
* the name of the {@link Resource}
* @param type
* the type of the {@link Resource}
*
* @return the newly created {@link Resource}
*/
public static Resource createResource(String id, String name, String type) {
Resource resource = new Resource(id, name, type);
ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
resource.addParameterBag(bag);
public static final String STATE_INTEGER_ID = "@state2";
public static final String STATE_INTEGER_NAME = "Float State";
return resource;
}
public static final String STATE_STRING_ID = "@state3";
public static final String STATE_STRING_NAME = "Float State";
/**
* Creates a list of {@link Resource Resources} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param idStart
* id range start
* @param count
* the number of elements to create
* @param idPrefix
* the prefix to generate IDs for the {@link Resource Resources}
* @param name
* the name of the {@link Resource}
* @param type
* the type of the {@link Resource}
*
* @return the list of newly created {@link Resource Resources}
*/
public static List<Resource> createResources(int idStart, int count, String idPrefix, String name, String type) {
List<Resource> resources = new ArrayList<>();
for (int i = 0; i < count; i++) {
String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0');
resources.add(createResource(idPrefix + "_" + id, name + " " + i, type));
}
return resources;
}
public static final String STATE_BOOLEAN_ID = "@state4";
public static final String STATE_BOOLEAN_NAME = "Float State";
/**
* Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param id
* the id of the {@link Order}
* @param name
* the name of the {@link Order}
* @param type
* the type of the {@link Order}
*
* @return the newly created {@link Order}
*/
public static Order createOrder(String id, String name, String type) {
return createOrder(id, name, type, new Date(), State.CREATED);
}
public static final long STATE_TIME_0 = 0L;
public static final long STATE_TIME_10 = 10L;
public static final long STATE_TIME_20 = 20L;
public static final long STATE_TIME_30 = 30L;
/**
* Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param id
* the id of the {@link Order}
* @param name
* the name of the {@link Order}
* @param type
* the type of the {@link Order}
* @param date
* the date of the {@link Order}
* @param state
* the {@link State} of the {@link Order}
*
* @return the newly created {@link Order}
*/
public static Order createOrder(String id, String name, String type, Date date, State state) {
public static final Double STATE_FLOAT_TIME_0 = 0.0D;
public static final Double STATE_FLOAT_TIME_10 = 10.0D;
public static final Double STATE_FLOAT_TIME_20 = 20.0D;
public static final Double STATE_FLOAT_TIME_30 = 30.0D;
Order order = new Order(id, name, type, date, state);
ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
order.addParameterBag(bag);
public static final Integer STATE_INTEGER_TIME_0 = 0;
public static final Integer STATE_INTEGER_TIME_10 = 10;
public static final Integer STATE_INTEGER_TIME_20 = 20;
public static final Integer STATE_INTEGER_TIME_30 = 30;
return order;
}
public static final String STATE_STRING_TIME_0 = "empty";
public static final String STATE_STRING_TIME_10 = "a";
public static final String STATE_STRING_TIME_20 = "b";
public static final String STATE_STRING_TIME_30 = "c";
/**
* Creates a list of {@link Order Orders} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param idStart
* id range start
* @param count
* the number of elements to create
* @param idPrefix
* the prefix to generate IDs for the {@link Order Orders}
* @param name
* the name of the {@link Order}
* @param type
* the type of the {@link Order}
*
* @return the list of newly created {@link Order Orders}
*/
public static List<Order> createOrders(int idStart, int count, String idPrefix, String name, String type) {
List<Order> orders = new ArrayList<>();
for (int i = 0; i < count; i++) {
String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0');
orders.add(createOrder(idPrefix + "_" + id, name + " " + i, type));
}
return orders;
}
public static final Boolean STATE_BOOLEAN_TIME_0 = Boolean.FALSE;
public static final Boolean STATE_BOOLEAN_TIME_10 = Boolean.TRUE;
public static final Boolean STATE_BOOLEAN_TIME_20 = Boolean.FALSE;
public static final Boolean STATE_BOOLEAN_TIME_30 = Boolean.TRUE;
/**
* Creates a {@link ParameterBag} with the given values and calls {@link #addAllParameters(ParameterBag)} to add
* {@link Parameter}s
*
* @param id
* the id of the {@link ParameterBag}
* @param name
* the name of the {@link ParameterBag}
* @param type
* the type of the {@link ParameterBag}
*
* @return the newly created {@link ParameterBag}
*/
public static ParameterBag createParameterBag(String id, String name, String type) {
public static final String BAG_ID = "@bag01";
public static final String BAG_NAME = "Test Bag";
public static final String BAG_TYPE = "TestBag";
ParameterBag bag = new ParameterBag(id, name, type);
addAllParameters(bag);
return bag;
}
/**
* Creates an {@link Resource} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param id the id of the {@link Resource}
* @param name the name of the {@link Resource}
* @param type the type of the {@link Resource}
*
* @return the newly created {@link Resource}
*/
public static Resource createResource(String id, String name, String type) {
Resource resource = new Resource(id, name, type);
ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
resource.addParameterBag(bag);
addTimedStates(resource);
/**
* Adds the following {@link Parameter}s to the given {@link ParameterBag}:
* <ul>
* <li>BooleanParameter - true</li>
* <li>FloatParameter - 44.3</li>
* <li>IntegerParameter - 77</li>
* <li>LongParameter - 4453234566L</li>
* <li>StringParameter - "Strolch"</li>
* <li>DateParameter - 1354295525628L</li>
* <li>StringListParameter - Hello, World</li>
* </ul>
*
* @param bag
*/
public static void addAllParameters(ParameterBag bag) {
return resource;
}
BooleanParameter boolParam = new BooleanParameter(PARAM_BOOLEAN_ID, PARAM_BOOLEAN_NAME, true);
boolParam.setIndex(1);
bag.addParameter(boolParam);
/**
* Creates {@link StrolchTimedState} instances and adds them to the {@link Resource}
*
* @param resource the resource to which to addd the newly created {@link StrolchTimedState}
*/
public static void addTimedStates(Resource resource) {
FloatParameter floatParam = new FloatParameter(PARAM_FLOAT_ID, PARAM_FLOAT_NAME, 44.3);
floatParam.setIndex(2);
bag.addParameter(floatParam);
// float state
FloatTimedState floatTimedState = new FloatTimedState(STATE_FLOAT_ID, STATE_FLOAT_NAME);
floatTimedState.applyChange(new ValueChange<>(STATE_TIME_0, new FloatValue(STATE_FLOAT_TIME_0)));
FloatValue floatValueChange = new FloatValue(STATE_FLOAT_TIME_10);
floatTimedState.applyChange(new ValueChange<>(STATE_TIME_10, floatValueChange));
floatTimedState.applyChange(new ValueChange<>(STATE_TIME_20, floatValueChange));
floatTimedState.applyChange(new ValueChange<>(STATE_TIME_30, floatValueChange));
resource.addTimedState(floatTimedState);
IntegerParameter integerParam = new IntegerParameter(PARAM_INTEGER_ID, PARAM_INTEGER_NAME, 77);
integerParam.setIndex(3);
bag.addParameter(integerParam);
// integer state
IntegerTimedState integerTimedState = new IntegerTimedState(STATE_INTEGER_ID, STATE_INTEGER_NAME);
integerTimedState.applyChange(new ValueChange<>(STATE_TIME_0,
new IntegerValue(STATE_INTEGER_TIME_0)));
IntegerValue integerValueChange = new IntegerValue(STATE_INTEGER_TIME_10);
integerTimedState.applyChange(new ValueChange<>(STATE_TIME_10, integerValueChange));
integerTimedState.applyChange(new ValueChange<>(STATE_TIME_20, integerValueChange));
integerTimedState.applyChange(new ValueChange<>(STATE_TIME_30, integerValueChange));
resource.addTimedState(integerTimedState);
LongParameter longParam = new LongParameter(PARAM_LONG_ID, PARAM_LONG_NAME, 4453234566L);
longParam.setIndex(4);
bag.addParameter(longParam);
// boolean state
BooleanTimedState booleanTimedState = new BooleanTimedState(STATE_BOOLEAN_ID, STATE_BOOLEAN_NAME);
booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_0,
new BooleanValue(STATE_BOOLEAN_TIME_0)));
BooleanValue booleanValueChange = new BooleanValue(STATE_BOOLEAN_TIME_10);
booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_10, booleanValueChange));
booleanValueChange = booleanValueChange.getInverse();
booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_20, booleanValueChange));
booleanValueChange = booleanValueChange.getInverse();
booleanTimedState.applyChange(new ValueChange<>(STATE_TIME_30, booleanValueChange));
resource.addTimedState(booleanTimedState);
StringParameter stringParam = new StringParameter(PARAM_STRING_ID, PARAM_STRING_NAME, "Strolch");
stringParam.setIndex(5);
bag.addParameter(stringParam);
// string state
StringSetTimedState stringTimedState = new StringSetTimedState(STATE_STRING_ID, STATE_STRING_NAME);
StringSetValue change = new StringSetValue(asSet(STATE_STRING_TIME_0));
stringTimedState.applyChange(new ValueChange<>(STATE_TIME_0, change));
change = change.getInverse();
change.add(asSet(STATE_STRING_TIME_10));
stringTimedState.applyChange(new ValueChange<>(STATE_TIME_10, change));
removeInverted(change.getValue());
change = change.getInverse();
change.add(asSet(STATE_STRING_TIME_20));
stringTimedState.applyChange(new ValueChange<>(STATE_TIME_20, change));
removeInverted(change.getValue());
change = change.getInverse();
change.add(asSet(STATE_STRING_TIME_30));
stringTimedState.applyChange(new ValueChange<>(STATE_TIME_30, change));
resource.addTimedState(stringTimedState);
}
DateParameter dateParam = new DateParameter(PARAM_DATE_ID, PARAM_DATE_NAME, new Date(1354295525628L));
dateParam.setIndex(6);
bag.addParameter(dateParam);
private static Set<AString> asSet(String value) {
HashSet<AString> hashSet = new HashSet<>();
hashSet.add(new AString(value));
return hashSet;
}
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Hello");
stringList.add("World");
StringListParameter stringListP = new StringListParameter(PARAM_LIST_STRING_ID, PARAM_LIST_STRING_NAME,
stringList);
stringListP.setIndex(7);
bag.addParameter(stringListP);
}
private static void removeInverted(Set<AString> set) {
for (Iterator<AString> iter = set.iterator(); iter.hasNext();) {
AString aString = iter.next();
if (aString.isInverse()) {
iter.remove();
}
}
}
/**
* Creates a list of {@link Resource Resources} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param idStart id range start
* @param count the number of elements to create
* @param idPrefix the prefix to generate IDs for the {@link Resource Resources}
* @param name the name of the {@link Resource}
* @param type the type of the {@link Resource}
*
* @return the list of newly created {@link Resource Resources}
*/
public static List<Resource> createResources(int idStart, int count, String idPrefix, String name, String type) {
List<Resource> resources = new ArrayList<>();
for (int i = 0; i < count; i++) {
String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0');
resources.add(createResource(idPrefix + "_" + id, name + " " + i, type));
}
return resources;
}
/**
* Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param id the id of the {@link Order}
* @param name the name of the {@link Order}
* @param type the type of the {@link Order}
*
* @return the newly created {@link Order}
*/
public static Order createOrder(String id, String name, String type) {
return createOrder(id, name, type, new Date(), State.CREATED);
}
/**
* Creates an {@link Order} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param id the id of the {@link Order}
* @param name the name of the {@link Order}
* @param type the type of the {@link Order}
* @param date the date of the {@link Order}
* @param state the {@link State} of the {@link Order}
*
* @return the newly created {@link Order}
*/
public static Order createOrder(String id, String name, String type, Date date, State state) {
Order order = new Order(id, name, type, date, state);
ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
order.addParameterBag(bag);
return order;
}
/**
* Creates a list of {@link Order Orders} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
*
* @param idStart id range start
* @param count the number of elements to create
* @param idPrefix the prefix to generate IDs for the {@link Order Orders}
* @param name the name of the {@link Order}
* @param type the type of the {@link Order}
*
* @return the list of newly created {@link Order Orders}
*/
public static List<Order> createOrders(int idStart, int count, String idPrefix, String name, String type) {
List<Order> orders = new ArrayList<>();
for (int i = 0; i < count; i++) {
String id = StringHelper.normalizeLength(String.valueOf((i + idStart)), 8, true, '0');
orders.add(createOrder(idPrefix + "_" + id, name + " " + i, type));
}
return orders;
}
/**
* Creates a {@link ParameterBag} with the given values and calls {@link #addAllParameters(ParameterBag)} to add
* {@link Parameter}s
*
* @param id the id of the {@link ParameterBag}
* @param name the name of the {@link ParameterBag}
* @param type the type of the {@link ParameterBag}
*
* @return the newly created {@link ParameterBag}
*/
public static ParameterBag createParameterBag(String id, String name, String type) {
ParameterBag bag = new ParameterBag(id, name, type);
addAllParameters(bag);
return bag;
}
/**
* Adds the following {@link Parameter}s to the given {@link ParameterBag}:
* <ul>
* <li>BooleanParameter - true</li>
* <li>FloatParameter - 44.3</li>
* <li>IntegerParameter - 77</li>
* <li>LongParameter - 4453234566L</li>
* <li>StringParameter - "Strolch"</li>
* <li>DateParameter - 1354295525628L</li>
* <li>StringListParameter - Hello, World</li>
* </ul>
*
* @param bag
*/
public static void addAllParameters(ParameterBag bag) {
BooleanParameter boolParam = new BooleanParameter(PARAM_BOOLEAN_ID, PARAM_BOOLEAN_NAME, true);
boolParam.setIndex(1);
bag.addParameter(boolParam);
FloatParameter floatParam = new FloatParameter(PARAM_FLOAT_ID, PARAM_FLOAT_NAME, 44.3);
floatParam.setIndex(2);
bag.addParameter(floatParam);
IntegerParameter integerParam = new IntegerParameter(PARAM_INTEGER_ID, PARAM_INTEGER_NAME, 77);
integerParam.setIndex(3);
bag.addParameter(integerParam);
LongParameter longParam = new LongParameter(PARAM_LONG_ID, PARAM_LONG_NAME, 4453234566L);
longParam.setIndex(4);
bag.addParameter(longParam);
StringParameter stringParam = new StringParameter(PARAM_STRING_ID, PARAM_STRING_NAME, "Strolch");
stringParam.setIndex(5);
bag.addParameter(stringParam);
DateParameter dateParam = new DateParameter(PARAM_DATE_ID, PARAM_DATE_NAME, new Date(1354295525628L));
dateParam.setIndex(6);
bag.addParameter(dateParam);
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Hello");
stringList.add("World");
StringListParameter stringListP = new StringListParameter(PARAM_LIST_STRING_ID, PARAM_LIST_STRING_NAME,
stringList);
stringListP.setIndex(7);
bag.addParameter(stringListP);
}
}

View File

@ -15,6 +15,7 @@
*/
package li.strolch.model;
import ch.eitchnet.utils.dbc.DBC;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
@ -45,253 +46,255 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public abstract class ParameterizedElement extends AbstractStrolchElement {
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 0L;
protected GroupedParameterizedElement parent;
protected Map<String, Parameter<?>> parameterMap;
protected String type;
protected GroupedParameterizedElement parent;
protected Map<String, Parameter<?>> parameterMap;
protected String type;
/**
* Empty Constructor
*/
protected ParameterizedElement() {
//
}
/**
* Empty Constructor
*/
protected ParameterizedElement() {
//
}
/**
* Default Constructor
*
* @param id
* @param name
* @param type
*/
public ParameterizedElement(String id, String name, String type) {
setId(id);
setName(name);
setType(type);
}
/**
* Default Constructor
*
* @param id
* @param name
* @param type
*/
public ParameterizedElement(String id, String name, String type) {
setId(id);
setName(name);
setType(type);
}
@Override
public String getType() {
return this.type;
}
@Override
public String getType() {
return this.type;
}
/**
* Sets the type of this {@link ParameterizedElement}
*
* @param type
* the type to set
*/
public void setType(String type) {
if (StringHelper.isEmpty(type)) {
String msg = "Type may not be empty on element {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, getLocator());
throw new StrolchException(msg);
}
/**
* Sets the type of this {@link ParameterizedElement}
*
* @param type the type to set
*/
public void setType(String type) {
if (StringHelper.isEmpty(type)) {
String msg = "Type may not be empty on element {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, getLocator());
throw new StrolchException(msg);
}
this.type = type;
}
this.type = type;
}
/**
* Returns the {@link Parameter} with the given id, or null if it does not exist
*
* @param key
* the id of the parameter to return
*
* @return the {@link Parameter} with the given id, or null if it does not exist
*/
@SuppressWarnings("unchecked")
public <T> T getParameter(String key) {
if (this.parameterMap == null)
return null;
return (T) this.parameterMap.get(key);
}
/**
* Returns the {@link Parameter} with the given id, or null if it does not exist
*
* @param key the id of the parameter to return
*
* @return the {@link Parameter} with the given id, or null if it does not exist
*/
@SuppressWarnings("unchecked")
public <T> T getParameter(String key) {
if (this.parameterMap == null) {
return null;
}
return (T) this.parameterMap.get(key);
}
/**
* Adds the given {@link Parameter} to the {@link ParameterizedElement}
*
* @param parameter
* the {@link Parameter} to add
*/
public void addParameter(Parameter<?> parameter) {
if (this.parameterMap == null)
this.parameterMap = new HashMap<String, Parameter<?>>();
this.parameterMap.put(parameter.getId(), parameter);
parameter.setParent(this);
}
/**
* Adds the given {@link Parameter} to the {@link ParameterizedElement}
*
* @param parameter the {@link Parameter} to add
*/
public void addParameter(Parameter<?> parameter) {
if (this.parameterMap == null) {
this.parameterMap = new HashMap<String, Parameter<?>>();
}
this.parameterMap.put(parameter.getId(), parameter);
parameter.setParent(this);
}
/**
* Removes the {@link Parameter} with the given key
*
* @param key
* the key of the {@link Parameter} to remove
*
* @return the removed {@link Parameter}, or null if it does not exist
*/
@SuppressWarnings("unchecked")
public <T> Parameter<T> removeParameter(String key) {
if (this.parameterMap == null)
return null;
return (Parameter<T>) this.parameterMap.remove(key);
}
/**
* Removes the {@link Parameter} with the given key
*
* @param key the key of the {@link Parameter} to remove
*
* @return the removed {@link Parameter}, or null if it does not exist
*/
@SuppressWarnings("unchecked")
public <T> Parameter<T> removeParameter(String key) {
if (this.parameterMap == null) {
return null;
}
return (Parameter<T>) this.parameterMap.remove(key);
}
/**
* Returns a list of all the {@link Parameter}s in this {@link ParameterizedElement}
*
* @return a list of all the {@link Parameter}s in this {@link ParameterizedElement}
*/
public List<Parameter<?>> getParameters() {
if (this.parameterMap == null)
return Collections.emptyList();
return new ArrayList<Parameter<?>>(this.parameterMap.values());
}
/**
* Returns a list of all the {@link Parameter}s in this {@link ParameterizedElement}
*
* @return a list of all the {@link Parameter}s in this {@link ParameterizedElement}
*/
public List<Parameter<?>> getParameters() {
if (this.parameterMap == null) {
return Collections.emptyList();
}
return new ArrayList<Parameter<?>>(this.parameterMap.values());
}
/**
* Returns true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise
*
* @return true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise
*/
public boolean hasParameters() {
return this.parameterMap != null && !this.parameterMap.isEmpty();
}
/**
* Returns true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise
*
* @return true, if the this {@link ParameterizedElement} has any {@link Parameter Parameters}, false otherwise
*/
public boolean hasParameters() {
return this.parameterMap != null && !this.parameterMap.isEmpty();
}
/**
* Returns true, if the {@link Parameter} exists with the given key, false otherwise
*
* @param key
* the key of the {@link Parameter} to check for
*
* @return true, if the {@link Parameter} exists with the given key, false otherwise
*/
public boolean hasParameter(String key) {
if (this.parameterMap == null)
return false;
return this.parameterMap.containsKey(key);
}
/**
* Returns true, if the {@link Parameter} exists with the given key, false otherwise
*
* @param key the key of the {@link Parameter} to check for
*
* @return true, if the {@link Parameter} exists with the given key, false otherwise
*/
public boolean hasParameter(String key) {
if (this.parameterMap == null) {
return false;
}
return this.parameterMap.containsKey(key);
}
/**
* Returns a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement}
*
* @return a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement}
*/
public Set<String> getParameterKeySet() {
if (this.parameterMap == null)
return Collections.emptySet();
return new HashSet<String>(this.parameterMap.keySet());
}
/**
* Returns a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement}
*
* @return a {@link Set} of all the {@link Parameter} keys in this {@link ParameterizedElement}
*/
public Set<String> getParameterKeySet() {
if (this.parameterMap == null) {
return Collections.emptySet();
}
return new HashSet<String>(this.parameterMap.keySet());
}
@Override
public void fillLocator(LocatorBuilder lb) {
this.parent.fillLocator(lb);
lb.append(this.id);
}
@Override
public void fillLocator(LocatorBuilder lb) {
this.parent.fillLocator(lb);
lb.append(this.id);
}
@Override
public Locator getLocator() {
LocatorBuilder lb = new LocatorBuilder();
fillLocator(lb);
return lb.build();
}
@Override
public Locator getLocator() {
LocatorBuilder lb = new LocatorBuilder();
fillLocator(lb);
return lb.build();
}
@Override
protected void fromDom(Element element) {
super.fromDom(element);
@Override
protected void fromDom(Element element) {
super.fromDom(element);
String type = element.getAttribute(Tags.TYPE);
setType(type);
String type = element.getAttribute(Tags.TYPE);
setType(type);
// add all the parameters
NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER);
for (int i = 0; i < parameterElements.getLength(); i++) {
Element paramElement = (Element) parameterElements.item(i);
String paramtype = paramElement.getAttribute(Tags.TYPE);
// add all the parameters
NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER);
for (int i = 0; i < parameterElements.getLength(); i++) {
Element paramElement = (Element) parameterElements.item(i);
String paramtype = paramElement.getAttribute(Tags.TYPE);
if (paramtype.equals(StringParameter.TYPE)) {
StringParameter param = new StringParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(IntegerParameter.TYPE)) {
IntegerParameter param = new IntegerParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(FloatParameter.TYPE)) {
FloatParameter param = new FloatParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(LongParameter.TYPE)) {
LongParameter param = new LongParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(DateParameter.TYPE)) {
DateParameter param = new DateParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(BooleanParameter.TYPE)) {
BooleanParameter param = new BooleanParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(StringListParameter.TYPE)) {
StringListParameter param = new StringListParameter(paramElement);
addParameter(param);
} else {
String msg = "What kind of parameter is this: {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, paramtype);
throw new StrolchException(msg);
}
}
}
DBC.PRE.assertNotEmpty("Type must be set on Parameter for bag with id " + id, paramtype);
@Override
protected void fillElement(Element element) {
super.fillElement(element);
if (paramtype.equals(StringParameter.TYPE)) {
StringParameter param = new StringParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(IntegerParameter.TYPE)) {
IntegerParameter param = new IntegerParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(FloatParameter.TYPE)) {
FloatParameter param = new FloatParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(LongParameter.TYPE)) {
LongParameter param = new LongParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(DateParameter.TYPE)) {
DateParameter param = new DateParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(BooleanParameter.TYPE)) {
BooleanParameter param = new BooleanParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(StringListParameter.TYPE)) {
StringListParameter param = new StringListParameter(paramElement);
addParameter(param);
} else {
String msg = "What kind of parameter is this: {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, paramtype);
throw new StrolchException(msg);
}
}
}
if (this.parameterMap != null) {
for (Parameter<?> parameter : this.parameterMap.values()) {
element.appendChild(parameter.toDom(element.getOwnerDocument()));
}
}
}
@Override
protected void fillElement(Element element) {
super.fillElement(element);
@Override
protected void fillClone(StrolchElement clone) {
super.fillClone(clone);
ParameterizedElement peClone = (ParameterizedElement) clone;
peClone.setType(this.type);
if (this.parameterMap != null) {
for (Parameter<?> param : this.parameterMap.values()) {
peClone.addParameter(param.getClone());
}
}
}
if (this.parameterMap != null) {
for (Parameter<?> parameter : this.parameterMap.values()) {
element.appendChild(parameter.toDom(element.getOwnerDocument()));
}
}
}
@Override
public GroupedParameterizedElement getParent() {
return this.parent;
}
@Override
protected void fillClone(StrolchElement clone) {
super.fillClone(clone);
ParameterizedElement peClone = (ParameterizedElement) clone;
peClone.setType(this.type);
if (this.parameterMap != null) {
for (Parameter<?> param : this.parameterMap.values()) {
peClone.addParameter(param.getClone());
}
}
}
/**
* Set the parent for this {@link ParameterizedElement}
*
* @param parent
* the parent to set
*/
public void setParent(GroupedParameterizedElement parent) {
this.parent = parent;
}
@Override
public GroupedParameterizedElement getParent() {
return this.parent;
}
@Override
public StrolchRootElement getRootElement() {
return this.parent.getRootElement();
}
/**
* Set the parent for this {@link ParameterizedElement}
*
* @param parent the parent to set
*/
public void setParent(GroupedParameterizedElement parent) {
this.parent = parent;
}
@SuppressWarnings("nls")
@Override
public String toString() {
@Override
public StrolchRootElement getRootElement() {
return this.parent.getRootElement();
}
StringBuilder builder = new StringBuilder();
@SuppressWarnings("nls")
@Override
public String toString() {
builder.append("ParameterizedElement [id=");
builder.append(this.id);
builder.append(", name=");
builder.append(this.name);
builder.append(", type=");
builder.append(this.type);
builder.append("]");
StringBuilder builder = new StringBuilder();
return builder.toString();
}
builder.append("ParameterizedElement [id=");
builder.append(this.id);
builder.append(", name=");
builder.append(this.name);
builder.append(", type=");
builder.append(this.type);
builder.append("]");
return builder.toString();
}
}

View File

@ -15,105 +15,202 @@
*/
package li.strolch.model;
import ch.eitchnet.utils.dbc.DBC;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import li.strolch.exception.StrolchException;
import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.timedstate.BooleanTimedState;
import li.strolch.model.timedstate.FloatTimedState;
import li.strolch.model.timedstate.IntegerTimedState;
import li.strolch.model.timedstate.StringSetTimedState;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class Resource extends GroupedParameterizedElement implements StrolchRootElement {
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 0L;
/**
* Empty constructor
*/
public Resource() {
//
}
private Map<String, StrolchTimedState<?>> timedStateMap;
/**
* Default constructor
*
* @param id
* @param name
* @param type
*/
public Resource(String id, String name, String type) {
super(id, name, type);
}
/**
* Empty constructor
*/
public Resource() {
//
}
/**
* DOM Constructor
*
* @param element
*/
public Resource(Element element) {
super.fromDom(element);
}
/**
* Default constructor
*
* @param id
* @param name
* @param type
*/
public Resource(String id, String name, String type) {
super(id, name, type);
}
@Override
public Element toDom(Document doc) {
/**
* DOM Constructor
*
* @param element
*/
public Resource(Element element) {
super.fromDom(element);
Element element = doc.createElement(Tags.RESOURCE);
fillElement(element);
NodeList timedStateElems = element.getElementsByTagName(Tags.TIMED_STATE);
for (int i = 0; i < timedStateElems.getLength(); i++) {
Element timedStateElem = (Element) timedStateElems.item(i);
String typeS = timedStateElem.getAttribute(Tags.TYPE);
return element;
}
DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + id, typeS);
@Override
public Resource getClone() {
Resource clone = new Resource();
if (typeS.equals(FloatTimedState.TYPE)) {
StrolchTimedState timedState = new FloatTimedState(timedStateElem);
addTimedState(timedState);
} else if (typeS.equals(IntegerTimedState.TYPE)) {
StrolchTimedState timedState = new IntegerTimedState(timedStateElem);
addTimedState(timedState);
} else if (typeS.equals(BooleanTimedState.TYPE)) {
StrolchTimedState timedState = new BooleanTimedState(timedStateElem);
addTimedState(timedState);
} else if (typeS.equals(StringSetTimedState.TYPE)) {
StrolchTimedState timedState = new StringSetTimedState(timedStateElem);
addTimedState(timedState);
} else {
String msg = "What kind of TimedState is this: {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, typeS);
throw new StrolchException(msg);
}
}
}
super.fillClone(clone);
public void addTimedState(StrolchTimedState<?> strolchTimedState) {
if (this.timedStateMap == null) {
this.timedStateMap = new HashMap<>();
}
return clone;
}
this.timedStateMap.put(strolchTimedState.getId(), strolchTimedState);
strolchTimedState.setParent(this);
}
@Override
protected void fillLocator(LocatorBuilder lb) {
lb.append(Tags.RESOURCE).append(getType()).append(getId());
}
@SuppressWarnings({"rawtypes", "unchecked"})
public <T extends StrolchTimedState> T getTimedState(String id) {
if (this.timedStateMap == null) {
return null;
}
return (T) this.timedStateMap.get(id);
}
@Override
public Locator getLocator() {
LocatorBuilder lb = new LocatorBuilder();
fillLocator(lb);
return lb.build();
}
@SuppressWarnings({"unchecked", "rawtypes"})
public <T extends StrolchTimedState> T removeTimedState(String id) {
if (this.timedStateMap == null) {
return null;
}
return (T) this.timedStateMap.remove(id);
}
@Override
public StrolchElement getParent() {
return null;
}
public Set<String> getTimedStateKeySet() {
if (this.timedStateMap == null) {
return Collections.emptySet();
}
return new HashSet<>(this.timedStateMap.keySet());
}
@Override
public Resource getRootElement() {
return this;
}
public List<StrolchTimedState<?>> getTimedStates() {
if (this.timedStateMap == null) {
return Collections.emptyList();
}
return new ArrayList<>(this.timedStateMap.values());
}
@Override
public <T> T accept(StrolchRootElementVisitor visitor) {
return visitor.visitResource(this);
}
public boolean hasTimedStates() {
return this.timedStateMap != null && !this.timedStateMap.isEmpty();
}
@SuppressWarnings("nls")
@Override
public String toString() {
public boolean hasTimedState(String id) {
return this.timedStateMap != null && this.timedStateMap.containsKey(id);
}
StringBuilder builder = new StringBuilder();
@Override
public Element toDom(Document doc) {
builder.append("Resource [id=");
builder.append(this.id);
builder.append(", name=");
builder.append(this.name);
builder.append(", type=");
builder.append(this.type);
builder.append("]");
Element element = doc.createElement(Tags.RESOURCE);
fillElement(element);
return builder.toString();
}
if (this.timedStateMap != null) {
for (StrolchTimedState<?> state : this.timedStateMap.values()) {
Element timedStateElem = state.toDom(element.getOwnerDocument());
element.appendChild(timedStateElem);
}
}
return element;
}
@Override
public Resource getClone() {
Resource clone = new Resource();
super.fillClone(clone);
return clone;
}
@Override
public void fillLocator(LocatorBuilder lb) {
lb.append(Tags.RESOURCE).append(getType()).append(getId());
}
@Override
public Locator getLocator() {
LocatorBuilder lb = new LocatorBuilder();
fillLocator(lb);
return lb.build();
}
@Override
public StrolchElement getParent() {
return null;
}
@Override
public Resource getRootElement() {
return this;
}
@Override
public <T> T accept(StrolchRootElementVisitor visitor) {
return visitor.visitResource(this);
}
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Resource [id=");
builder.append(this.id);
builder.append(", name=");
builder.append(this.name);
builder.append(", type=");
builder.append(this.type);
builder.append("]");
return builder.toString();
}
}

View File

@ -25,11 +25,13 @@ public class Tags {
public static final String DATE = "Date";
public static final String STATE = "State";
public static final String VALUE = "Value";
public static final String TIME = "Time";
public static final String INTERPRETATION = "Interpretation";
public static final String UOM = "Uom";
public static final String HIDDEN = "Hidden";
public static final String INDEX = "Index";
public static final String PARAMETER = "Parameter";
public static final String TIMED_STATE = "TimedState";
public static final String PARAMETERIZED_ELEMENT = "ParameterizedElement";
public static final String RESOURCE = "Resource";
public static final String ORDER = "Order";

View File

@ -200,7 +200,7 @@ public abstract class AbstractParameter<T> extends AbstractStrolchElement implem
public Locator getLocator() {
LocatorBuilder lb = new LocatorBuilder();
this.parent.fillLocator(lb);
this.fillLocator(lb);
fillLocator(lb);
return lb.build();
}

View File

@ -0,0 +1,127 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import li.strolch.model.AbstractStrolchElement;
import li.strolch.model.Locator;
import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.Resource;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.ITimeVariable;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("rawtypes")
public abstract class AbstractStrolchTimedState<T extends IValue> extends AbstractStrolchElement implements
StrolchTimedState<T> {
private static final long serialVersionUID = 1L;
protected Resource parent;
protected ITimedState<T> state;
public AbstractStrolchTimedState() {
this.state = new TimedState<>();
}
public AbstractStrolchTimedState(String id, String name) {
super(id, name);
this.state = new TimedState<>();
}
@Override
public ITimeValue<T> getNextMatch(Long time, T value) {
return this.state.getNextMatch(time, value);
}
@Override
public ITimeValue<T> getPreviousMatch(Long time, T value) {
return this.state.getPreviousMatch(time, value);
}
@Override
public <U extends IValueChange<T>> void applyChange(U change) {
this.state.applyChange(change);
}
@Override
public ITimeValue<T> getStateAt(Long time) {
return this.state.getStateAt(time);
}
@Override
public ITimeVariable<T> getTimeEvolution() {
return this.state.getTimeEvolution();
}
@Override
public StrolchElement getParent() {
return this.parent;
}
@Override
public void setParent(Resource parent) {
this.parent = parent;
}
@Override
public StrolchRootElement getRootElement() {
return this.parent;
}
@Override
protected void fillLocator(LocatorBuilder locatorBuilder) {
locatorBuilder.append(Tags.TIMED_STATE);
locatorBuilder.append(getId());
}
@Override
public Locator getLocator() {
LocatorBuilder lb = new LocatorBuilder();
this.parent.fillLocator(lb);
fillLocator(lb);
return lb.build();
}
protected void fillClone(AbstractStrolchTimedState<T> clone) {
super.fillClone(clone);
clone.state = this.state.getCopy();
}
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(getClass().getSimpleName());
builder.append(" [id=");
builder.append(this.id);
builder.append(", name=");
builder.append(this.name);
builder.append(", valueNow=");
builder.append(this.state.getStateAt(System.currentTimeMillis()));
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import java.util.SortedSet;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.impl.BooleanValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class BooleanTimedState extends AbstractStrolchTimedState<BooleanValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "BooleanState";
public BooleanTimedState() {
super();
}
public BooleanTimedState(String id, String name) {
super(id, name);
}
public BooleanTimedState(Element element) {
super.fromDom(element);
this.state = new TimedState<>();
NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE);
for (int i = 0; i < timeValueElems.getLength(); i++) {
Element timeValueElem = (Element) timeValueElems.item(i);
Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME));
Boolean value = Boolean.valueOf(timeValueElem.getAttribute(Tags.VALUE));
BooleanValue booleanValue = new BooleanValue(value);
this.state.getTimeEvolution().setValueAt(time, booleanValue);
}
}
@Override
public Element toDom(Document doc) {
Element stateElement = doc.createElement(Tags.TIMED_STATE);
super.fillElement(stateElement);
SortedSet<ITimeValue<BooleanValue>> values = this.state.getTimeEvolution().getValues();
for (ITimeValue<BooleanValue> timeValue : values) {
Long time = timeValue.getTime();
BooleanValue value = timeValue.getValue();
Element valueElem = doc.createElement(Tags.VALUE);
valueElem.setAttribute(Tags.TIME, time.toString());
valueElem.setAttribute(Tags.VALUE, value.getValue().toString());
stateElement.appendChild(valueElem);
}
return stateElement;
}
@Override
public String getType() {
return TYPE;
}
@Override
public StrolchElement getClone() {
BooleanTimedState clone = new BooleanTimedState();
fillClone(clone);
return clone;
}
}

View File

@ -0,0 +1,88 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import java.util.SortedSet;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.impl.FloatValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class FloatTimedState extends AbstractStrolchTimedState<FloatValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "FloatState";
public FloatTimedState() {
super();
}
public FloatTimedState(String id, String name) {
super(id, name);
}
public FloatTimedState(Element element) {
super.fromDom(element);
this.state = new TimedState<>();
NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE);
for (int i = 0; i < timeValueElems.getLength(); i++) {
Element timeValueElem = (Element) timeValueElems.item(i);
Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME));
Double value = Double.valueOf(timeValueElem.getAttribute(Tags.VALUE));
FloatValue floatValue = new FloatValue(value);
this.state.getTimeEvolution().setValueAt(time, floatValue);
}
}
@Override
public Element toDom(Document doc) {
Element stateElement = doc.createElement(Tags.TIMED_STATE);
super.fillElement(stateElement);
SortedSet<ITimeValue<FloatValue>> values = this.state.getTimeEvolution().getValues();
for (ITimeValue<FloatValue> timeValue : values) {
Long time = timeValue.getTime();
FloatValue value = timeValue.getValue();
Element valueElem = doc.createElement(Tags.VALUE);
valueElem.setAttribute(Tags.TIME, time.toString());
valueElem.setAttribute(Tags.VALUE, value.getValue().toString());
stateElement.appendChild(valueElem);
}
return stateElement;
}
@Override
public String getType() {
return TYPE;
}
@Override
public StrolchElement getClone() {
FloatTimedState clone = new FloatTimedState();
fillClone(clone);
return clone;
}
}

View File

@ -45,7 +45,7 @@ public interface ITimedState<T extends IValue> {
* @param change
* the state change to be applied
*/
void applyChange(final IValueChange<T> change);
<U extends IValueChange<T>> void applyChange(final U change);
/**
* @return the state at the given time
@ -57,4 +57,8 @@ public interface ITimedState<T extends IValue> {
*/
ITimeVariable<T> getTimeEvolution();
/**
* @return a copy of this timed state
*/
ITimedState<T> getCopy();
}

View File

@ -0,0 +1,88 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import java.util.SortedSet;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.impl.IntegerValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class IntegerTimedState extends AbstractStrolchTimedState<IntegerValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "IntegerState";
public IntegerTimedState() {
super();
}
public IntegerTimedState(String id, String name) {
super(id, name);
}
public IntegerTimedState(Element element) {
super.fromDom(element);
this.state = new TimedState<>();
NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE);
for (int i = 0; i < timeValueElems.getLength(); i++) {
Element timeValueElem = (Element) timeValueElems.item(i);
Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME));
Integer value = Integer.valueOf(timeValueElem.getAttribute(Tags.VALUE));
IntegerValue integerValue = new IntegerValue(value);
this.state.getTimeEvolution().setValueAt(time, integerValue);
}
}
@Override
public Element toDom(Document doc) {
Element stateElement = doc.createElement(Tags.TIMED_STATE);
super.fillElement(stateElement);
SortedSet<ITimeValue<IntegerValue>> values = this.state.getTimeEvolution().getValues();
for (ITimeValue<IntegerValue> timeValue : values) {
Long time = timeValue.getTime();
IntegerValue value = timeValue.getValue();
Element valueElem = doc.createElement(Tags.VALUE);
valueElem.setAttribute(Tags.TIME, time.toString());
valueElem.setAttribute(Tags.VALUE, value.getValue().toString());
stateElement.appendChild(valueElem);
}
return stateElement;
}
@Override
public String getType() {
return TYPE;
}
@Override
public StrolchElement getClone() {
IntegerTimedState clone = new IntegerTimedState();
fillClone(clone);
return clone;
}
}

View File

@ -0,0 +1,111 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.impl.AString;
import li.strolch.model.timevalue.impl.StringSetValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class StringSetTimedState extends AbstractStrolchTimedState<StringSetValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "StringSetState";
public StringSetTimedState() {
super();
}
public StringSetTimedState(String id, String name) {
super(id, name);
}
public StringSetTimedState(Element element) {
super.fromDom(element);
this.state = new TimedState<>();
NodeList timeValueElems = element.getElementsByTagName(Tags.VALUE);
for (int i = 0; i < timeValueElems.getLength(); i++) {
Element timeValueElem = (Element) timeValueElems.item(i);
Long time = Long.valueOf(timeValueElem.getAttribute(Tags.TIME));
String valueAsString = timeValueElem.getAttribute(Tags.VALUE);
Set<AString> value = new HashSet<>();
String[] values = valueAsString.split(",");
for (String s : values) {
value.add(new AString(s.trim()));
}
StringSetValue integerValue = new StringSetValue(value);
this.state.getTimeEvolution().setValueAt(time, integerValue);
}
}
@Override
public Element toDom(Document doc) {
Element stateElement = doc.createElement(Tags.TIMED_STATE);
super.fillElement(stateElement);
SortedSet<ITimeValue<StringSetValue>> values = this.state.getTimeEvolution().getValues();
for (ITimeValue<StringSetValue> timeValue : values) {
Long time = timeValue.getTime();
StringSetValue stringSetValue = timeValue.getValue();
Set<AString> value = stringSetValue.getValue();
StringBuilder sb = new StringBuilder();
Iterator<AString> iter = value.iterator();
while (iter.hasNext()) {
sb.append(iter.next().getString());
if (iter.hasNext()) {
sb.append(", ");
}
}
String valueAsString = sb.toString();
Element valueElem = doc.createElement(Tags.VALUE);
valueElem.setAttribute(Tags.TIME, time.toString());
valueElem.setAttribute(Tags.VALUE, valueAsString);
stateElement.appendChild(valueElem);
}
return stateElement;
}
@Override
public String getType() {
return TYPE;
}
@Override
public StrolchElement getClone() {
StringSetTimedState clone = new StringSetTimedState();
fillClone(clone);
return clone;
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import li.strolch.model.Resource;
import li.strolch.model.StrolchElement;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.ITimeVariable;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@SuppressWarnings("rawtypes")
public interface StrolchTimedState<T extends IValue> extends StrolchElement {
public ITimeValue<T> getNextMatch(Long time, T value);
public ITimeValue<T> getPreviousMatch(Long time, T value);
public <U extends IValueChange<T>> void applyChange(U change);
public ITimeValue<T> getStateAt(Long time);
public ITimeVariable<T> getTimeEvolution();
public void setParent(Resource aThis);
}

View File

@ -34,7 +34,7 @@ import li.strolch.model.timevalue.impl.TimeVariable;
public class TimedState<T extends IValue> implements ITimedState<T>, Serializable {
private static final long serialVersionUID = 1L;
private ITimeVariable<T> timeVariable = new TimeVariable<T>();
@Override
@ -64,7 +64,7 @@ public class TimedState<T extends IValue> implements ITimedState<T>, Serializabl
}
@Override
public void applyChange(final IValueChange<T> change) {
public <U extends IValueChange<T>> void applyChange(U change) {
this.timeVariable.applyChange(change);
}
@ -78,4 +78,10 @@ public class TimedState<T extends IValue> implements ITimedState<T>, Serializabl
return this.timeVariable;
}
@Override
public ITimedState<T> getCopy() {
TimedState<T> copy = new TimedState<>();
copy.timeVariable = this.timeVariable.getCopy();
return copy;
}
}

View File

@ -36,4 +36,5 @@ public interface ITimeValue<T extends IValue> extends Comparable<ITimeValue<T>>
ITimeValue<T> add(final T change);
ITimeValue<T> getCopy();
}

View File

@ -19,8 +19,7 @@ import java.util.Collection;
import java.util.SortedSet;
/**
* A timed variable storing a ordered sequence of {@link ITimeValue} objects
* modeling a time evolution of a quantity.
* A timed variable storing a ordered sequence of {@link ITimeValue} objects modeling a time evolution of a quantity.
*
* @author Martin Smock <smock.martin@gmail.com>
*
@ -41,14 +40,12 @@ public interface ITimeVariable<T extends IValue> {
void setValueAt(final Long time, final T value);
/**
* get the latest {@link ITimeValue} whose time field is less or equal to
* the time given
* get the latest {@link ITimeValue} whose time field is less or equal to the time given
*/
ITimeValue<T> getValueAt(final Long time);
/**
* Applies a {@link IValueChange} propagating the change to all future
* values starting from the time of the change.
* Applies a {@link IValueChange} propagating the change to all future values starting from the time of the change.
*
* @param change
* the {@link IValueChange} to be applied
@ -56,8 +53,7 @@ public interface ITimeVariable<T extends IValue> {
void applyChange(final IValueChange<T> change);
/**
* Get all {@link ITimeValue} objects whose time field is greater or equal
* to the given time
* Get all {@link ITimeValue} objects whose time field is greater or equal to the given time
*
* @param time
* the time the sequence starts with
@ -66,8 +62,7 @@ public interface ITimeVariable<T extends IValue> {
Collection<ITimeValue<T>> getFutureValues(final Long time);
/**
* Get all {@link ITimeValue} objects whose time field is strictly smaller
* than the given time
* Get all {@link ITimeValue} objects whose time field is strictly smaller than the given time
*
* @param time
* the time the sequence starts with
@ -81,13 +76,16 @@ public interface ITimeVariable<T extends IValue> {
* @return a defensive copy of the {@link ITimeValue}s
*/
SortedSet<ITimeValue<T>> getValues();
/**
* removes {@link ITimeValue} objects from the sequence, where the successor
* matches value. I.e considering a pair of adjacent {@link ITimeValue}
* objects in the sequence which have the same {@link IValue}, the later one
* is removed, since it contains no additional information.
* removes {@link ITimeValue} objects from the sequence, where the successor matches value. I.e considering a pair
* of adjacent {@link ITimeValue} objects in the sequence which have the same {@link IValue}, the later one is
* removed, since it contains no additional information.
*/
void compact();
/**
* @return a copy of this time variable
*/
ITimeVariable<T> getCopy();
}

View File

@ -23,7 +23,6 @@ package li.strolch.model.timevalue;
*
* @param <T>
* any object for which a (generalized) add operation can be defined.
*
*/
public interface IValue<T> {
@ -52,5 +51,4 @@ public interface IValue<T> {
* @return a copy of this
*/
IValue<T> getCopy();
}

View File

@ -0,0 +1,108 @@
/*
* Copyright 2013 Martin Smock <smock.martin@gmail.com>
*
* 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.model.timevalue.impl;
import java.io.Serializable;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
/**
* {@link IValue} implementation to work with Boolean valued {@link ITimeValue} objects
*
* @author Martin Smock <smock.martin@gmail.com>
*/
public class BooleanValue implements IValue<Boolean>, Serializable {
private static final long serialVersionUID = 1L;
public static final BooleanValue NEUTRAL = new BooleanValue(false);
private Boolean value;
public BooleanValue(Boolean value) {
this.value = value;
}
public BooleanValue(boolean value) {
this.value = Boolean.valueOf(value);
}
public BooleanValue(String valueAsString) throws NumberFormatException {
this.value = Boolean.parseBoolean(valueAsString);
}
@Override
public BooleanValue add(Boolean o) {
this.value = o;
return this;
}
@Override
public boolean matches(IValue<Boolean> other) {
return this.value.equals(other.getValue());
}
@Override
public Boolean getValue() {
return this.value;
}
@Override
public BooleanValue getInverse() {
return new BooleanValue(!getValue());
}
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("BooleanValue [value=");
sb.append(this.value);
sb.append("]");
return sb.toString();
}
@Override
public BooleanValue getCopy() {
return new BooleanValue(this.value);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.value == null) ? 0 : this.value.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BooleanValue other = (BooleanValue) obj;
if (this.value == null) {
if (other.value != null)
return false;
} else if (!this.value.equals(other.value))
return false;
return true;
}
}

View File

@ -25,36 +25,36 @@ import li.strolch.model.timevalue.IValue;
*
* @author Martin Smock <smock.martin@gmail.com>
*/
public class DoubleValue implements IValue<Double>, Serializable {
public class FloatValue implements IValue<Double>, Serializable {
private static final long serialVersionUID = 1L;
public static final DoubleValue NEUTRAL = new DoubleValue(0.0d);
public static final FloatValue NEUTRAL = new FloatValue(0.0d);
private Double value;
public DoubleValue(Double value) {
public FloatValue(Double value) {
this.value = value;
}
public DoubleValue(double value) {
public FloatValue(double value) {
this.value = Double.valueOf(value);
}
public DoubleValue(Integer value) {
public FloatValue(Integer value) {
this.value = this.value.doubleValue();
}
public DoubleValue(int value) {
public FloatValue(int value) {
this.value = Integer.valueOf(value).doubleValue();
}
public DoubleValue(String valueAsString) throws NumberFormatException {
public FloatValue(String valueAsString) throws NumberFormatException {
this.value = Double.parseDouble(valueAsString);
}
@Override
public DoubleValue add(Double o) {
public FloatValue add(Double o) {
this.value += o;
return this;
}
@ -80,13 +80,13 @@ public class DoubleValue implements IValue<Double>, Serializable {
}
@Override
public DoubleValue getInverse() {
return new DoubleValue(-getValue());
public FloatValue getInverse() {
return new FloatValue(-getValue());
}
@Override
public DoubleValue getCopy(){
return new DoubleValue(this.value);
public FloatValue getCopy(){
return new FloatValue(this.value);
}
@Override
@ -105,7 +105,7 @@ public class DoubleValue implements IValue<Double>, Serializable {
return false;
if (getClass() != obj.getClass())
return false;
DoubleValue other = (DoubleValue) obj;
FloatValue other = (FloatValue) obj;
if (this.value == null) {
if (other.value != null)
return false;

View File

@ -15,93 +15,103 @@
*/
package li.strolch.model.timevalue.impl;
import ch.eitchnet.utils.helper.StringHelper;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import li.strolch.exception.StrolchException;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
/**
* {@link IValue} implementation to work with String valued {@link ITimeValue} objects. Since a java.util.String object
* does not define a inverse, a algebraic {@link AString} wrapper is used.
*
*
* @author Martin Smock <smock.martin@gmail.com>
*/
public class StringSetValue implements IValue<Set<AString>>, Serializable {
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 1L;
private static Set<AString> neu = Collections.emptySet();
public static final IValue<Set<AString>> NEUTRAL = new StringSetValue(neu);
private static Set<AString> neu = Collections.emptySet();
public static final IValue<Set<AString>> NEUTRAL = new StringSetValue(neu);
private Set<AString> aStrings = new HashSet<AString>();
private Set<AString> aStrings = new HashSet<>();
public StringSetValue() {
}
public StringSetValue() {
}
public StringSetValue(final Set<AString> aStrings) {
this.aStrings = aStrings;
}
public StringSetValue(final Set<AString> aStrings) {
// assert no null values in set
for (AString aString : aStrings) {
if (StringHelper.isEmpty(aString.getString())) {
throw new StrolchException("StringSetValue may not contain null values in set!");
}
}
this.aStrings = aStrings;
}
@Override
public Set<AString> getValue() {
return this.aStrings;
}
@Override
public Set<AString> getValue() {
return this.aStrings;
}
@Override
public IValue<Set<AString>> add(Set<AString> o) {
@Override
public IValue<Set<AString>> add(Set<AString> o) {
Set<AString> toBeAdded = new HashSet<AString>(o);
Set<AString> toBeAdded = new HashSet<>(o);
for (Iterator<AString> iter1 = toBeAdded.iterator(); iter1.hasNext();) {
AString toAdd = iter1.next();
for (Iterator<AString> iter = this.aStrings.iterator(); iter.hasNext();) {
AString aString = iter.next();
boolean valueMatch = aString.getString().equals(toAdd.getString());
boolean compensate = (toAdd.isInverse() && !aString.isInverse())
|| (!toAdd.isInverse() && aString.isInverse());
if (valueMatch && compensate) {
iter.remove();
iter1.remove();
}
}
}
this.aStrings.addAll(toBeAdded);
return this;
}
for (Iterator<AString> iter1 = toBeAdded.iterator(); iter1.hasNext();) {
AString toAdd = iter1.next();
if (StringHelper.isEmpty(toAdd.getString())) {
throw new StrolchException("StringSetValue may not contain null values in set!");
}
@Override
public boolean matches(IValue<Set<AString>> other) {
return getValue().equals(other.getValue());
}
for (Iterator<AString> iter = this.aStrings.iterator(); iter.hasNext();) {
AString aString = iter.next();
boolean valueMatch = aString.getString().equals(toAdd.getString());
boolean compensate = (toAdd.isInverse() && !aString.isInverse())
|| (!toAdd.isInverse() && aString.isInverse());
if (valueMatch && compensate) {
iter.remove();
iter1.remove();
}
}
}
this.aStrings.addAll(toBeAdded);
return this;
}
@Override
public IValue<Set<AString>> getInverse() {
Set<AString> inverseSet = new HashSet<AString>();
for (AString as : this.aStrings) {
inverseSet.add(as.getInverse());
}
StringSetValue inverse = new StringSetValue();
inverse.aStrings = inverseSet;
return inverse;
}
@Override
public boolean matches(IValue<Set<AString>> other) {
return getValue().equals(other.getValue());
}
@Override
public StringSetValue getCopy() {
return new StringSetValue(this.aStrings);
}
@Override
public StringSetValue getInverse() {
Set<AString> inverseSet = new HashSet<>();
for (AString as : this.aStrings) {
inverseSet.add(as.getInverse());
}
StringSetValue inverse = new StringSetValue();
inverse.aStrings = inverseSet;
return inverse;
}
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StringSetValue [aStrings=");
sb.append(this.aStrings);
sb.append("]");
return sb.toString();
}
@Override
public StringSetValue getCopy() {
return new StringSetValue(new HashSet<>(this.aStrings));
}
@SuppressWarnings("nls")
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StringSetValue [aStrings=");
sb.append(this.aStrings);
sb.append("]");
return sb.toString();
}
}

View File

@ -69,6 +69,12 @@ public class TimeValue<T extends IValue> implements ITimeValue<T>, Serializable
return this.getTime().compareTo(arg0.getTime());
}
@SuppressWarnings("unchecked")
@Override
public ITimeValue<T> getCopy() {
return new TimeValue<T>(time, (T) value.getCopy());
}
@SuppressWarnings("nls")
@Override
public String toString() {
@ -112,5 +118,4 @@ public class TimeValue<T extends IValue> implements ITimeValue<T>, Serializable
return false;
return true;
}
}

View File

@ -26,7 +26,6 @@ import li.strolch.model.timevalue.ITimeVariable;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
/**
* @author Martin Smock <smock.martin@gmail.com>
*/
@ -34,7 +33,7 @@ import li.strolch.model.timevalue.IValueChange;
public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Serializable {
private static final long serialVersionUID = 1L;
public SortedSet<ITimeValue<T>> container = new TreeSet<ITimeValue<T>>();
@Override
@ -71,7 +70,7 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
TimeValue<T> picker = new TimeValue<T>(time, null);
return new TreeSet<ITimeValue<T>>(this.container.headSet(picker));
}
@Override
public SortedSet<ITimeValue<T>> getValues() {
return new TreeSet<ITimeValue<T>>(this.container);
@ -91,9 +90,10 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
this.container.add(newValue);
} else if (initialValue.getTime().longValue() < change.getTime().longValue()) {
ITimeValue<T> newValue = new TimeValue<T>(change.getTime(), initialValue.getValue());
newValue.add(change.getValue());
newValue.add(change.getValue());
this.container.add(newValue);
}
compact();
}
@ -115,7 +115,15 @@ public class TimeVariable<T extends IValue> implements ITimeVariable<T>, Seriali
predecessor = successor;
}
}
}
@Override
public ITimeVariable<T> getCopy() {
TimeVariable<T> clone = new TimeVariable<>();
for (ITimeValue<T> timeValue : this.container) {
clone.container.add(timeValue.getCopy());
}
return clone;
}
}

View File

@ -18,7 +18,6 @@ package li.strolch.model.visitor;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Locator;
import li.strolch.model.Order;
@ -26,123 +25,165 @@ import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.StrolchElement;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.ITimeVariable;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*
*/
public class StrolchElementDeepEqualsVisitor {
private List<Locator> mismatchedLocators;
private List<Locator> mismatchedLocators;
public StrolchElementDeepEqualsVisitor() {
this.mismatchedLocators = new ArrayList<>();
}
public StrolchElementDeepEqualsVisitor() {
this.mismatchedLocators = new ArrayList<>();
}
/**
* @return the mismatchedLocators
*/
public List<Locator> getMismatchedLocators() {
return this.mismatchedLocators;
}
/**
* @return the mismatchedLocators
*/
public List<Locator> getMismatchedLocators() {
return this.mismatchedLocators;
}
public boolean isEqual() {
return this.mismatchedLocators.isEmpty();
}
public boolean isEqual() {
return this.mismatchedLocators.isEmpty();
}
protected void deepEquals(StrolchElement srcElement, StrolchElement dstElement) {
if (!srcElement.getName().equals(dstElement.getName()))
this.mismatchedLocators.add(dstElement.getLocator());
if (!srcElement.getType().equals(dstElement.getType()))
this.mismatchedLocators.add(dstElement.getLocator());
}
protected void deepEquals(StrolchElement srcElement, StrolchElement dstElement) {
if (!srcElement.getName().equals(dstElement.getName())) {
this.mismatchedLocators.add(dstElement.getLocator());
}
if (!srcElement.getType().equals(dstElement.getType())) {
this.mismatchedLocators.add(dstElement.getLocator());
}
}
protected void deepEquals(Order srcOrder, Order dstOrder) {
deepEquals((StrolchElement) srcOrder, (StrolchElement) dstOrder);
if (!srcOrder.getState().equals(dstOrder.getState()))
this.mismatchedLocators.add(dstOrder.getLocator());
if (!srcOrder.getDate().equals(dstOrder.getDate()))
this.mismatchedLocators.add(dstOrder.getLocator());
protected void deepEquals(Order srcOrder, Order dstOrder) {
deepEquals((StrolchElement) srcOrder, (StrolchElement) dstOrder);
if (!srcOrder.getState().equals(dstOrder.getState())) {
this.mismatchedLocators.add(dstOrder.getLocator());
}
if (!srcOrder.getDate().equals(dstOrder.getDate())) {
this.mismatchedLocators.add(dstOrder.getLocator());
}
deepEquals((GroupedParameterizedElement) srcOrder, (GroupedParameterizedElement) dstOrder);
}
deepEquals((GroupedParameterizedElement) srcOrder, (GroupedParameterizedElement) dstOrder);
}
protected void deepEquals(Resource srcRes, Resource dstRes) {
deepEquals((StrolchElement) srcRes, (StrolchElement) dstRes);
deepEquals((GroupedParameterizedElement) srcRes, (GroupedParameterizedElement) dstRes);
}
protected void deepEquals(Resource srcRes, Resource dstRes) {
deepEquals((StrolchElement) srcRes, (StrolchElement) dstRes);
deepEquals((GroupedParameterizedElement) srcRes, (GroupedParameterizedElement) dstRes);
Set<String> srcTimedStateKeySet = srcRes.getTimedStateKeySet();
for (String timedStateKey : srcTimedStateKeySet) {
StrolchTimedState srcTimedState = srcRes.getTimedState(timedStateKey);
protected void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) {
Set<String> srcBagKeySet = srcElement.getParameterBagKeySet();
for (String bagKey : srcBagKeySet) {
ParameterBag srcBag = srcElement.getParameterBag(bagKey);
if (!dstRes.hasTimedState(timedStateKey)) {
this.mismatchedLocators.add(srcTimedState.getLocator());
continue;
}
if (!dstElement.hasParameterBag(bagKey)) {
this.mismatchedLocators.add(srcBag.getLocator());
continue;
}
StrolchTimedState dstTimedState = dstRes.getTimedState(timedStateKey);
deepEquals(srcTimedState, dstTimedState);
}
ParameterBag dstBag = dstElement.getParameterBag(bagKey);
deepEquals(srcBag, dstBag);
}
Set<String> dstTimedStateKeySet = dstRes.getTimedStateKeySet();
for (String timedStateKey : dstTimedStateKeySet) {
if (!srcRes.hasTimedState(timedStateKey)) {
StrolchTimedState dstTimedState = dstRes.getTimedState(timedStateKey);
this.mismatchedLocators.add(dstTimedState.getLocator());
}
}
}
Set<String> dstBagKeySet = dstElement.getParameterBagKeySet();
for (String bagKey : dstBagKeySet) {
if (!srcElement.hasParameterBag(bagKey)) {
ParameterBag dstBag = dstElement.getParameterBag(bagKey);
this.mismatchedLocators.add(dstBag.getLocator());
}
}
}
protected void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) {
Set<String> srcBagKeySet = srcElement.getParameterBagKeySet();
for (String bagKey : srcBagKeySet) {
ParameterBag srcBag = srcElement.getParameterBag(bagKey);
protected void deepEquals(ParameterBag srcBag, ParameterBag dstBag) {
deepEquals((StrolchElement) srcBag, (StrolchElement) dstBag);
if (!dstElement.hasParameterBag(bagKey)) {
this.mismatchedLocators.add(srcBag.getLocator());
continue;
}
Set<String> srcParamKeySet = srcBag.getParameterKeySet();
for (String paramKey : srcParamKeySet) {
Parameter<?> srcParam = srcBag.getParameter(paramKey);
if (!dstBag.hasParameter(paramKey)) {
this.mismatchedLocators.add(srcParam.getLocator());
continue;
}
ParameterBag dstBag = dstElement.getParameterBag(bagKey);
deepEquals(srcBag, dstBag);
}
Parameter<?> dstParam = dstBag.getParameter(paramKey);
deepEquals(srcParam, dstParam);
}
Set<String> dstBagKeySet = dstElement.getParameterBagKeySet();
for (String bagKey : dstBagKeySet) {
if (!srcElement.hasParameterBag(bagKey)) {
ParameterBag dstBag = dstElement.getParameterBag(bagKey);
this.mismatchedLocators.add(dstBag.getLocator());
}
}
}
Set<String> dstParamKeySet = dstBag.getParameterKeySet();
for (String paramKey : dstParamKeySet) {
if (!srcBag.hasParameter(paramKey)) {
Parameter<?> dstParam = dstBag.getParameter(paramKey);
this.mismatchedLocators.add(dstParam.getLocator());
}
}
}
protected void deepEquals(ParameterBag srcBag, ParameterBag dstBag) {
deepEquals((StrolchElement) srcBag, (StrolchElement) dstBag);
protected void deepEquals(Parameter<?> srcParam, Parameter<?> dstParam) {
deepEquals((StrolchElement) srcParam, (StrolchElement) dstParam);
if (!srcParam.getUom().equals(dstParam.getUom()))
this.mismatchedLocators.add(dstParam.getLocator());
if (!srcParam.getInterpretation().equals(dstParam.getInterpretation()))
this.mismatchedLocators.add(dstParam.getLocator());
if (srcParam.isHidden() != dstParam.isHidden())
this.mismatchedLocators.add(dstParam.getLocator());
if (srcParam.getIndex() != dstParam.getIndex())
this.mismatchedLocators.add(dstParam.getLocator());
Set<String> srcParamKeySet = srcBag.getParameterKeySet();
for (String paramKey : srcParamKeySet) {
Parameter<?> srcParam = srcBag.getParameter(paramKey);
if (!dstBag.hasParameter(paramKey)) {
this.mismatchedLocators.add(srcParam.getLocator());
continue;
}
if (!srcParam.getValue().equals(dstParam.getValue()))
this.mismatchedLocators.add(dstParam.getLocator());
}
Parameter<?> dstParam = dstBag.getParameter(paramKey);
deepEquals(srcParam, dstParam);
}
public static boolean isEqual(Order srcOrder, Order dstOrder) {
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(srcOrder);
return visitor.isEqual();
}
Set<String> dstParamKeySet = dstBag.getParameterKeySet();
for (String paramKey : dstParamKeySet) {
if (!srcBag.hasParameter(paramKey)) {
Parameter<?> dstParam = dstBag.getParameter(paramKey);
this.mismatchedLocators.add(dstParam.getLocator());
}
}
}
public static boolean isEqual(Resource srcRes, Resource dstRes) {
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(srcRes);
return visitor.isEqual();
}
protected void deepEquals(Parameter<?> srcParam, Parameter<?> dstParam) {
deepEquals((StrolchElement) srcParam, (StrolchElement) dstParam);
if (!srcParam.getUom().equals(dstParam.getUom())) {
this.mismatchedLocators.add(dstParam.getLocator());
}
if (!srcParam.getInterpretation().equals(dstParam.getInterpretation())) {
this.mismatchedLocators.add(dstParam.getLocator());
}
if (srcParam.isHidden() != dstParam.isHidden()) {
this.mismatchedLocators.add(dstParam.getLocator());
}
if (srcParam.getIndex() != dstParam.getIndex()) {
this.mismatchedLocators.add(dstParam.getLocator());
}
if (!srcParam.getValue().equals(dstParam.getValue())) {
this.mismatchedLocators.add(dstParam.getLocator());
}
}
protected void deepEquals(StrolchTimedState<?> srcState, StrolchTimedState<?> dstState) {
deepEquals((StrolchElement) srcState, (StrolchElement) dstState);
final ITimeVariable<?> srcTimeEvolution = srcState.getTimeEvolution();
final ITimeVariable<?> dstTimeEvolution = dstState.getTimeEvolution();
if (!srcTimeEvolution.getValues().equals(dstTimeEvolution.getValues())) {
this.mismatchedLocators.add(dstState.getLocator());
}
}
public static boolean isEqual(Order srcOrder, Order dstOrder) {
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(dstOrder);
return visitor.isEqual();
}
public static boolean isEqual(Resource srcRes, Resource dstRes) {
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(dstRes);
return visitor.isEqual();
}
}

View File

@ -30,6 +30,9 @@ import li.strolch.model.parameter.IntegerParameter;
import li.strolch.model.parameter.LongParameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.timedstate.BooleanTimedState;
import li.strolch.model.timevalue.impl.BooleanValue;
import li.strolch.model.timevalue.impl.ValueChange;
import li.strolch.model.visitor.OrderDeepEqualsVisitor;
import li.strolch.model.visitor.ResourceDeepEqualsVisitor;
@ -38,99 +41,112 @@ import org.junit.Test;
@SuppressWarnings("nls")
public class ModelTest {
@Test
public void shouldCreateResource() {
@Test
public void shouldCreateResource() {
Resource resource = ModelGenerator.createResource("@res01", "Test resource", "MyType");
ParameterBag bag = resource.getParameterBag(ModelGenerator.BAG_ID);
validateBag(bag);
}
Resource resource = ModelGenerator.createResource("@res01", "Test resource", "MyType");
ParameterBag bag = resource.getParameterBag(ModelGenerator.BAG_ID);
validateBag(bag);
}
@Test
public void shouldCreateOrder() {
@Test
public void shouldCreateOrder() {
Order order = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", new Date(), State.OPEN);
ParameterBag bag = order.getParameterBag(ModelGenerator.BAG_ID);
validateBag(bag);
}
Order order = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", new Date(), State.OPEN);
ParameterBag bag = order.getParameterBag(ModelGenerator.BAG_ID);
validateBag(bag);
}
@Test
public void shouldPerformDeepResourceEquals() {
Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(dstRes);
assertTrue("Same Resource should be deep equal!", visitor.isEqual());
}
@Test
public void shouldPerformDeepResourceEquals() {
Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(dstRes);
assertTrue("Same Resource should be deep equal!", visitor.isEqual());
}
@Test
public void shouldFailDeepResourceEquals1() {
Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
ParameterBag bag = dstRes.getParameterBag(ModelGenerator.BAG_ID);
bag.setName("Bla bla");
FloatParameter fParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID);
fParam.setValue(23434234.234);
fParam.setName("Ohla");
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(dstRes);
assertFalse("Resource should not be same if param is changed!", visitor.isEqual());
assertEquals("Three changes should be registered", 3, visitor.getMismatchedLocators().size());
}
@Test
public void shouldFailDeepResourceEquals1() {
Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
ParameterBag bag = dstRes.getParameterBag(ModelGenerator.BAG_ID);
bag.setName("Bla bla");
FloatParameter fParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID);
fParam.setValue(23434234.234);
fParam.setName("Ohla");
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(dstRes);
assertFalse("Resource should not be same if param is changed!", visitor.isEqual());
assertEquals("Three changes should be registered", 3, visitor.getMismatchedLocators().size());
}
@Test
public void shouldPerformDeepOrderEquals() {
Date date = new Date();
Order srcOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
Order dstOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(dstOrder);
assertTrue("Same Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFailDeepResourceEquals2() {
Resource srcRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
Resource dstRes = ModelGenerator.createResource("@res01", "Test resource", "MyType");
BooleanTimedState timedState = dstRes.getTimedState(ModelGenerator.STATE_BOOLEAN_ID);
timedState.applyChange(new ValueChange<>(System.currentTimeMillis(), new BooleanValue(Boolean.TRUE)));
timedState.setName("Ohla");
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(srcRes);
visitor.visit(dstRes);
assertFalse("Resource should not be same if param is changed!", visitor.isEqual());
assertEquals("One change should be registered!", 1, visitor.getMismatchedLocators().size());
}
public static void validateBag(ParameterBag bag) {
@Test
public void shouldPerformDeepOrderEquals() {
Date date = new Date();
Order srcOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
Order dstOrder = ModelGenerator.createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(dstOrder);
assertTrue("Same Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual());
}
assertNotNull(bag);
public static void validateBag(ParameterBag bag) {
assertEquals(ModelGenerator.BAG_ID, bag.getId());
assertEquals(ModelGenerator.BAG_NAME, bag.getName());
assertEquals(ModelGenerator.BAG_TYPE, bag.getType());
assertNotNull(bag);
validateParams(bag);
}
assertEquals(ModelGenerator.BAG_ID, bag.getId());
assertEquals(ModelGenerator.BAG_NAME, bag.getName());
assertEquals(ModelGenerator.BAG_TYPE, bag.getType());
public static void validateParams(ParameterBag bag) {
validateParams(bag);
}
BooleanParameter boolParam = bag.getParameter(ModelGenerator.PARAM_BOOLEAN_ID);
assertNotNull("Boolean Param missing with id " + ModelGenerator.PARAM_BOOLEAN_ID, boolParam);
assertEquals(true, boolParam.getValue().booleanValue());
public static void validateParams(ParameterBag bag) {
FloatParameter floatParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID);
assertNotNull("Float Param missing with id " + ModelGenerator.PARAM_FLOAT_ID, floatParam);
assertEquals(44.3, floatParam.getValue().doubleValue(), 0.0001);
BooleanParameter boolParam = bag.getParameter(ModelGenerator.PARAM_BOOLEAN_ID);
assertNotNull("Boolean Param missing with id " + ModelGenerator.PARAM_BOOLEAN_ID, boolParam);
assertEquals(true, boolParam.getValue().booleanValue());
IntegerParameter integerParam = bag.getParameter(ModelGenerator.PARAM_INTEGER_ID);
assertNotNull("Integer Param missing with id " + ModelGenerator.PARAM_INTEGER_ID, integerParam);
assertEquals(77, integerParam.getValue().intValue());
FloatParameter floatParam = bag.getParameter(ModelGenerator.PARAM_FLOAT_ID);
assertNotNull("Float Param missing with id " + ModelGenerator.PARAM_FLOAT_ID, floatParam);
assertEquals(44.3, floatParam.getValue().doubleValue(), 0.0001);
LongParameter longParam = bag.getParameter(ModelGenerator.PARAM_LONG_ID);
assertNotNull("Long Param missing with id " + ModelGenerator.PARAM_LONG_ID, longParam);
assertEquals(4453234566L, longParam.getValue().longValue());
IntegerParameter integerParam = bag.getParameter(ModelGenerator.PARAM_INTEGER_ID);
assertNotNull("Integer Param missing with id " + ModelGenerator.PARAM_INTEGER_ID, integerParam);
assertEquals(77, integerParam.getValue().intValue());
StringParameter stringParam = bag.getParameter(ModelGenerator.PARAM_STRING_ID);
assertNotNull("String Param missing with id " + ModelGenerator.PARAM_STRING_ID, stringParam);
assertEquals("Strolch", stringParam.getValue());
LongParameter longParam = bag.getParameter(ModelGenerator.PARAM_LONG_ID);
assertNotNull("Long Param missing with id " + ModelGenerator.PARAM_LONG_ID, longParam);
assertEquals(4453234566L, longParam.getValue().longValue());
DateParameter dateParam = bag.getParameter(ModelGenerator.PARAM_DATE_ID);
assertNotNull("Date Param missing with id " + ModelGenerator.PARAM_DATE_ID, dateParam);
assertEquals(1354295525628L, dateParam.getValue().getTime());
StringParameter stringParam = bag.getParameter(ModelGenerator.PARAM_STRING_ID);
assertNotNull("String Param missing with id " + ModelGenerator.PARAM_STRING_ID, stringParam);
assertEquals("Strolch", stringParam.getValue());
StringListParameter stringListP = bag.getParameter(ModelGenerator.PARAM_LIST_STRING_ID);
assertNotNull("StringList Param missing with id " + ModelGenerator.PARAM_LIST_STRING_ID, stringListP);
DateParameter dateParam = bag.getParameter(ModelGenerator.PARAM_DATE_ID);
assertNotNull("Date Param missing with id " + ModelGenerator.PARAM_DATE_ID, dateParam);
assertEquals(1354295525628L, dateParam.getValue().getTime());
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Hello");
stringList.add("World");
assertEquals(stringList, stringListP.getValue());
}
StringListParameter stringListP = bag.getParameter(ModelGenerator.PARAM_LIST_STRING_ID);
assertNotNull("StringList Param missing with id " + ModelGenerator.PARAM_LIST_STRING_ID, stringListP);
ArrayList<String> stringList = new ArrayList<>();
stringList.add("Hello");
stringList.add("World");
assertEquals(stringList, stringListP.getValue());
}
}

View File

@ -15,51 +15,55 @@
*/
package li.strolch.model;
import static org.junit.Assert.assertTrue;
import li.strolch.model.visitor.StrolchElementDeepEqualsVisitor;
import li.strolch.model.visitor.OrderDeepEqualsVisitor;
import li.strolch.model.visitor.ResourceDeepEqualsVisitor;
import li.strolch.model.xml.OrderToDomVisitor;
import li.strolch.model.xml.ResourceToDomVisitor;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*
*/
@SuppressWarnings("nls")
public class XmlToDomTest extends ModelTest {
@Test
public void shouldFormatAndParseOrder() {
@Test
public void shouldFormatAndParseOrder() {
Order order = ModelGenerator.createOrder("@1", "My Order 1", "MyOrder");
Order order = ModelGenerator.createOrder("@1", "My Order 1", "MyOrder");
OrderToDomVisitor domVisitor = new OrderToDomVisitor();
domVisitor.visit(order);
Document document = domVisitor.getDocument();
OrderToDomVisitor domVisitor = new OrderToDomVisitor();
domVisitor.visit(order);
Document document = domVisitor.getDocument();
Element rootElement = document.getDocumentElement();
Order parsedOrder = new Order(rootElement);
Element rootElement = document.getDocumentElement();
Order parsedOrder = new Order(rootElement);
assertTrue("To DOM and back should equal same Order!",
StrolchElementDeepEqualsVisitor.isEqual(order, parsedOrder));
}
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(order);
visitor.visit(parsedOrder);
assertTrue("To DOM and back should equal same Order:\n" + visitor.getMismatchedLocators(),
visitor.isEqual());
}
@Test
public void shouldFormatAndParseResource() {
@Test
public void shouldFormatAndParseResource() {
Resource resource = ModelGenerator.createResource("@1", "My Resource 1", "MyResource");
Resource resource = ModelGenerator.createResource("@1", "My Resource 1", "MyResource");
ResourceToDomVisitor domVisitor = new ResourceToDomVisitor();
domVisitor.visit(resource);
Document document = domVisitor.getDocument();
ResourceToDomVisitor domVisitor = new ResourceToDomVisitor();
domVisitor.visit(resource);
Document document = domVisitor.getDocument();
Element rootElement = document.getDocumentElement();
Resource parsedResource = new Resource(rootElement);
Element rootElement = document.getDocumentElement();
Resource parsedResource = new Resource(rootElement);
assertTrue("To DOM and back should equal same Resource!",
StrolchElementDeepEqualsVisitor.isEqual(resource, parsedResource));
}
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(resource);
visitor.visit(parsedResource);
assertTrue("To DOM and back should equal same Resource:\n" + visitor.getMismatchedLocators(),
visitor.isEqual());
}
}

View File

@ -0,0 +1,117 @@
/*
* Copyright 2013 Robert von Burg <eitch@eitchnet.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.model.timedstate;
import java.util.HashSet;
import java.util.Set;
import li.strolch.model.ModelGenerator;
import li.strolch.model.Resource;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.impl.AString;
import li.strolch.model.timevalue.impl.BooleanValue;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.IntegerValue;
import li.strolch.model.timevalue.impl.StringSetValue;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class StrolchTimedStateTest {
@Test
public void testFloatState() {
Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated");
FloatTimedState floatState = myRes.getTimedState(ModelGenerator.STATE_FLOAT_ID);
ITimeValue<FloatValue> valueAt0 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0);
assertEquals(ModelGenerator.STATE_FLOAT_TIME_0, valueAt0.getValue().getValue());
ITimeValue<FloatValue> valueAt10 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10);
assertEquals(ModelGenerator.STATE_FLOAT_TIME_10, valueAt10.getValue().getValue());
ITimeValue<FloatValue> valueAt20 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20);
assertEquals(ModelGenerator.STATE_FLOAT_TIME_20, valueAt20.getValue().getValue());
ITimeValue<FloatValue> valueAt30 = floatState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30);
assertEquals(ModelGenerator.STATE_FLOAT_TIME_30, valueAt30.getValue().getValue());
}
@Test
public void testIntegerState() {
Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated");
IntegerTimedState integerState = myRes.getTimedState(ModelGenerator.STATE_INTEGER_ID);
ITimeValue<IntegerValue> valueAt0 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0);
assertEquals(ModelGenerator.STATE_INTEGER_TIME_0, valueAt0.getValue().getValue());
ITimeValue<IntegerValue> valueAt10 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10);
assertEquals(ModelGenerator.STATE_INTEGER_TIME_10, valueAt10.getValue().getValue());
ITimeValue<IntegerValue> valueAt20 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20);
assertEquals(ModelGenerator.STATE_INTEGER_TIME_20, valueAt20.getValue().getValue());
ITimeValue<IntegerValue> valueAt30 = integerState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30);
assertEquals(ModelGenerator.STATE_INTEGER_TIME_30, valueAt30.getValue().getValue());
}
@Test
public void testBooleanState() {
Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated");
BooleanTimedState booleanState = myRes.getTimedState(ModelGenerator.STATE_BOOLEAN_ID);
ITimeValue<BooleanValue> valueAt0 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0);
assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_0, valueAt0.getValue().getValue());
ITimeValue<BooleanValue> valueAt10 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10);
assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_10, valueAt10.getValue().getValue());
ITimeValue<BooleanValue> valueAt20 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20);
assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_20, valueAt20.getValue().getValue());
ITimeValue<BooleanValue> valueAt30 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30);
assertEquals(ModelGenerator.STATE_BOOLEAN_TIME_30, valueAt30.getValue().getValue());
}
@Test
public void testStringSetState() {
Resource myRes = ModelGenerator.createResource("@1", "Test With States", "Stated");
StringSetTimedState booleanState = myRes.getTimedState(ModelGenerator.STATE_STRING_ID);
ITimeValue<StringSetValue> valueAt0 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_0);
assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_0), valueAt0.getValue().getValue());
ITimeValue<StringSetValue> valueAt10 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_10);
assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_10), valueAt10.getValue().getValue());
ITimeValue<StringSetValue> valueAt20 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_20);
assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_20), valueAt20.getValue().getValue());
ITimeValue<StringSetValue> valueAt30 = booleanState.getTimeEvolution().getValueAt(ModelGenerator.STATE_TIME_30);
assertEquals(asSet(ModelGenerator.STATE_STRING_TIME_30), valueAt30.getValue().getValue());
}
private static Set<AString> asSet(String value) {
HashSet<AString> hashSet = new HashSet<>();
hashSet.add(new AString(value));
return hashSet;
}
}

View File

@ -20,7 +20,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValueChange;
import li.strolch.model.timevalue.impl.DoubleValue;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.ValueChange;
import org.junit.Before;
@ -28,10 +28,10 @@ import org.junit.Test;
public class TimeStateTest {
private ITimedState<DoubleValue> state = new TimedState<DoubleValue>();
private ITimedState<FloatValue> state = new TimedState<FloatValue>();
final DoubleValue expectedValue1 = new DoubleValue(Double.valueOf(100D));
final DoubleValue expectedValue2 = new DoubleValue(Double.valueOf(200D));
final FloatValue expectedValue1 = new FloatValue(Double.valueOf(100D));
final FloatValue expectedValue2 = new FloatValue(Double.valueOf(200D));
final Long t0 = Long.valueOf(0);
final Long t10 = Long.valueOf(10);
@ -42,20 +42,20 @@ public class TimeStateTest {
@Before
public void before() {
final IValueChange<DoubleValue> change1 = new ValueChange<DoubleValue>(this.t10, this.expectedValue1);
final IValueChange<FloatValue> change1 = new ValueChange<FloatValue>(this.t10, this.expectedValue1);
this.state.applyChange(change1);
final ITimeValue<DoubleValue> stateAt9 = this.state.getStateAt(9L);
final ITimeValue<FloatValue> stateAt9 = this.state.getStateAt(9L);
assertNull(stateAt9);
final ITimeValue<DoubleValue> stateAt11 = this.state.getStateAt(11L);
final ITimeValue<FloatValue> stateAt11 = this.state.getStateAt(11L);
assertNotNull(stateAt11);
assertEquals(true, stateAt11.getValue().matches(this.expectedValue1));
final IValueChange<DoubleValue> change2 = new ValueChange<DoubleValue>(this.t30, this.expectedValue1);
final IValueChange<FloatValue> change2 = new ValueChange<FloatValue>(this.t30, this.expectedValue1);
this.state.applyChange(change2);
final ITimeValue<DoubleValue> stateAt31 = this.state.getStateAt(31L);
final ITimeValue<FloatValue> stateAt31 = this.state.getStateAt(31L);
assertNotNull(stateAt31);
assertEquals(true, stateAt31.getValue().matches(this.expectedValue2));
}
@ -63,7 +63,7 @@ public class TimeStateTest {
@Test
public void testGetNextMatch() {
ITimeValue<DoubleValue> nextMatch = this.state.getNextMatch(this.t0, this.expectedValue1);
ITimeValue<FloatValue> nextMatch = this.state.getNextMatch(this.t0, this.expectedValue1);
assertNotNull(nextMatch);
assertEquals(this.t10, nextMatch.getTime());
@ -89,7 +89,7 @@ public class TimeStateTest {
@Test
public void testGetPreviousMatch() {
ITimeValue<DoubleValue> previousMatch = this.state.getPreviousMatch(this.t100, this.expectedValue2);
ITimeValue<FloatValue> previousMatch = this.state.getPreviousMatch(this.t100, this.expectedValue2);
assertNotNull(previousMatch);
assertEquals(this.t30, previousMatch.getTime());

View File

@ -22,7 +22,7 @@ import static org.junit.Assert.assertTrue;
import java.util.Collection;
import java.util.SortedSet;
import li.strolch.model.timevalue.impl.DoubleValue;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.TimeVariable;
import li.strolch.model.timevalue.impl.ValueChange;
@ -35,22 +35,22 @@ public class FloatTimeVariableTest {
private static final Long STEP = 10L;
private static final Long PICK = 50L;
private TimeVariable<DoubleValue> timeVariable;
private TimeVariable<FloatValue> timeVariable;
/**
* set the values ascending with a difference of STEP
*/
@Before
public void init() {
this.timeVariable = new TimeVariable<DoubleValue>();
this.timeVariable = new TimeVariable<FloatValue>();
for (long i = 0; i < MAX; i += STEP) {
this.timeVariable.setValueAt(Long.valueOf(i), new DoubleValue(i));
this.timeVariable.setValueAt(Long.valueOf(i), new FloatValue(i));
}
}
@Test
public void testGetValueAt() {
ITimeValue<DoubleValue> valueAt = this.timeVariable.getValueAt(PICK);
ITimeValue<FloatValue> valueAt = this.timeVariable.getValueAt(PICK);
assertEquals(PICK.doubleValue(), valueAt.getValue().getValue(), 0.0001);
}
@ -59,12 +59,12 @@ public class FloatTimeVariableTest {
*/
@Test
public void testGetFutureValues() {
Collection<ITimeValue<DoubleValue>> futureValues = this.timeVariable.getFutureValues(PICK);
Collection<ITimeValue<FloatValue>> futureValues = this.timeVariable.getFutureValues(PICK);
Long expectedTime = PICK;
Double expectedValue = PICK.doubleValue();
for (ITimeValue<DoubleValue> value : futureValues) {
for (ITimeValue<FloatValue> value : futureValues) {
assertEquals(expectedTime, value.getTime());
assertTrue(value.getValue().matches(new DoubleValue(expectedValue)));
assertTrue(value.getValue().matches(new FloatValue(expectedValue)));
expectedTime += STEP;
expectedValue += STEP.doubleValue();
}
@ -76,12 +76,12 @@ public class FloatTimeVariableTest {
*/
@Test
public void testGetPastValues() {
Collection<ITimeValue<DoubleValue>> pastValues = this.timeVariable.getPastValues(MAX);
Collection<ITimeValue<FloatValue>> pastValues = this.timeVariable.getPastValues(MAX);
Long expectedTime = 0L;
Double expectedValue = expectedTime.doubleValue();
for (ITimeValue<DoubleValue> value : pastValues) {
for (ITimeValue<FloatValue> value : pastValues) {
assertEquals(expectedTime, value.getTime());
assertTrue(value.getValue().matches(new DoubleValue(expectedValue)));
assertTrue(value.getValue().matches(new FloatValue(expectedValue)));
expectedTime += STEP;
expectedValue += STEP.doubleValue();
}
@ -93,17 +93,17 @@ public class FloatTimeVariableTest {
@Test
public void testApplyChange() {
DoubleValue doubleValue = new DoubleValue(STEP.doubleValue());
FloatValue doubleValue = new FloatValue(STEP.doubleValue());
IValueChange<DoubleValue> change = new ValueChange<DoubleValue>(PICK, doubleValue);
IValueChange<FloatValue> change = new ValueChange<FloatValue>(PICK, doubleValue);
this.timeVariable.applyChange(change);
Collection<ITimeValue<DoubleValue>> futureValues = this.timeVariable.getFutureValues(PICK);
Collection<ITimeValue<FloatValue>> futureValues = this.timeVariable.getFutureValues(PICK);
Long expectedTime = PICK;
IValue<Double> expectedValue = new DoubleValue(PICK.doubleValue() + change.getValue().getValue());
IValue<Double> expectedValue = new FloatValue(PICK.doubleValue() + change.getValue().getValue());
for (ITimeValue<DoubleValue> value : futureValues) {
for (ITimeValue<FloatValue> value : futureValues) {
assertEquals(expectedTime, value.getTime());
assertTrue(expectedValue.matches(value.getValue()));
expectedTime += STEP;
@ -117,17 +117,17 @@ public class FloatTimeVariableTest {
@Test
public void testApply2Change() {
this.timeVariable = new TimeVariable<DoubleValue>();
this.timeVariable = new TimeVariable<FloatValue>();
DoubleValue doubleValue = new DoubleValue(STEP.doubleValue());
FloatValue doubleValue = new FloatValue(STEP.doubleValue());
IValueChange<DoubleValue> change = new ValueChange<DoubleValue>(PICK, doubleValue);
IValueChange<FloatValue> change = new ValueChange<FloatValue>(PICK, doubleValue);
this.timeVariable.applyChange(change);
ITimeValue<DoubleValue> actual = this.timeVariable.getValueAt(PICK);
ITimeValue<FloatValue> actual = this.timeVariable.getValueAt(PICK);
assertNotNull(actual);
IValue<Double> expectedValue = new DoubleValue(STEP.doubleValue());
IValue<Double> expectedValue = new FloatValue(STEP.doubleValue());
assertEquals(true, actual.getValue().matches(expectedValue));
}
@ -138,14 +138,14 @@ public class FloatTimeVariableTest {
@Test
public void testCompact() {
this.timeVariable = new TimeVariable<DoubleValue>();
this.timeVariable = new TimeVariable<FloatValue>();
for (Long i = 0L; i < MAX; i += STEP) {
this.timeVariable.setValueAt(i, new DoubleValue(STEP.doubleValue()));
this.timeVariable.setValueAt(i, new FloatValue(STEP.doubleValue()));
}
// call
this.timeVariable.compact();
// check
SortedSet<ITimeValue<DoubleValue>> futureValues = this.timeVariable.getFutureValues(0L);
SortedSet<ITimeValue<FloatValue>> futureValues = this.timeVariable.getFutureValues(0L);
assertEquals(1, futureValues.size());
}

View File

@ -21,7 +21,7 @@ import java.util.HashSet;
import java.util.Set;
import li.strolch.model.timevalue.impl.AString;
import li.strolch.model.timevalue.impl.DoubleValue;
import li.strolch.model.timevalue.impl.FloatValue;
import li.strolch.model.timevalue.impl.IntegerValue;
import li.strolch.model.timevalue.impl.StringSetValue;
@ -34,8 +34,8 @@ public class ValueTests {
*/
@Test
public void testDoubleInverse() {
DoubleValue value = new DoubleValue(10.0d);
DoubleValue inverse = value.getInverse();
FloatValue value = new FloatValue(10.0d);
FloatValue inverse = value.getInverse();
assertEquals(Double.valueOf(-10.0d), inverse.getValue());
assertEquals(Double.valueOf(0), value.add(inverse.getValue()).getValue());
}