[Major] Major refactoring of SAX and DOM implemenetations

- also added SAX and DOM visitors for Acitivities and Actions
This commit is contained in:
Robert von Burg 2015-07-07 17:53:17 +02:00
parent 8f7f6aa920
commit 588952b526
23 changed files with 787 additions and 182 deletions

@ -1 +1 @@
Subproject commit 9e449e56eedc01ef24b9d4a8a5cbf5c52b013a48
Subproject commit 2e58db83fd35fea958431d7a2e9edae14ff6a20b

View File

@ -33,10 +33,10 @@ public abstract class AbstractStrolchElement implements StrolchElement {
protected String name;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
public AbstractStrolchElement() {
//
super();
}
/**

View File

@ -38,10 +38,10 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
protected String type;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
protected GroupedParameterizedElement() {
//
super();
}
/**

View File

@ -23,6 +23,8 @@ import java.util.List;
import java.util.Random;
import java.util.Set;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.parameter.BooleanParameter;
@ -135,6 +137,9 @@ public class ModelGenerator {
public static final String BAG_NAME = "Test Bag";
public static final String BAG_TYPE = "TestBag";
public static final String ACTION_RES_TYPE = "ResType";
public static final String ACTION_RES_ID = "@resId";
/**
* Creates an {@link Resource} with the given values and adds a {@link ParameterBag} by calling
* {@link #createParameterBag(String, String, String)}
@ -322,6 +327,59 @@ public class ModelGenerator {
return orders;
}
public static Activity createActivity(String id, String name, String type) {
Activity rootActivity = new Activity(id, name, type);
ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
rootActivity.addParameterBag(bag);
Action action = createAction("act_" + rootActivity.getId(), "Action " + rootActivity.getName(), "Use");
rootActivity.addElement(action);
Activity subActivity = new Activity("sub_" + id, "sub_" + name, type);
bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
subActivity.addParameterBag(bag);
rootActivity.addElement(subActivity);
action = createAction("act_" + id, "Action " + name, "Use");
subActivity.addElement(action);
Activity subSubActivity = new Activity("subSub_" + id, "subSub_" + name, type);
bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
subSubActivity.addParameterBag(bag);
subActivity.addElement(subSubActivity);
action = createAction("act_" + id, "Action " + name, "Use");
subSubActivity.addElement(action);
return rootActivity;
}
public static Action createAction(String id, String name, String type) {
Action action = new Action(id, name, type);
action.setResourceId(ACTION_RES_ID);
action.setResourceType(ACTION_RES_TYPE);
ParameterBag bag = createParameterBag(BAG_ID, BAG_NAME, BAG_TYPE);
action.addParameterBag(bag);
action.addChange(new ValueChange<>(0L, new IntegerValue(0), STATE_INTEGER_ID));
action.addChange(new ValueChange<>(10L, new IntegerValue(10), STATE_INTEGER_ID));
action.addChange(new ValueChange<>(20L, new IntegerValue(20), STATE_INTEGER_ID));
action.addChange(new ValueChange<>(30L, new IntegerValue(30), STATE_INTEGER_ID));
action.addChange(new ValueChange<>(40L, new IntegerValue(20), STATE_INTEGER_ID));
action.addChange(new ValueChange<>(50L, new IntegerValue(10), STATE_INTEGER_ID));
action.addChange(new ValueChange<>(60L, new IntegerValue(0), STATE_INTEGER_ID));
return action;
}
public static Action createAction(String id, String name, String type, String resourceId, String resourceType) {
Action action = createAction(id, name, type);
action.setResourceId(resourceId);
action.setResourceType(resourceType);
return action;
}
/**
* Creates a {@link ParameterBag} with the given values and calls {@link #addAllParameters(ParameterBag)} to add
* {@link Parameter}s
@ -336,7 +394,6 @@ public class ModelGenerator {
* @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;

View File

@ -39,10 +39,10 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
private State state;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
public Order() {
//
super();
}
/**

View File

@ -38,10 +38,10 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
private Map<String, StrolchTimedState<IValue<?>>> timedStateMap;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
public Resource() {
//
super();
}
/**
@ -106,7 +106,6 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
@Override
public Resource getClone() {
Resource clone = new Resource();
super.fillClone(clone);
if (this.timedStateMap != null) {

View File

@ -27,7 +27,6 @@ import li.strolch.model.Locator;
import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
@ -50,11 +49,25 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
protected List<IValueChange<? extends IValue<?>>> changes;
/**
* Empty constructor - for marshalling only!
*/
public Action() {
super();
}
public Action(String id, String name, String type) {
super(id, name, type);
this.state = State.CREATED;
}
public Action(String id, String name, String type, String resourceId, String resourceType) {
super(id, name, type);
this.resourceId = resourceId;
this.resourceType = resourceType;
this.state = State.CREATED;
}
private void initChanges() {
if (this.changes == null)
this.changes = new ArrayList<>();
@ -64,7 +77,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
* @return the id of the {@link Resource} the {@link Action} acts on
*/
public String getResourceId() {
return resourceId;
return this.resourceId;
}
/**
@ -78,8 +91,9 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
/**
* @return the current <code>State</code> of the a<code>Action</code>
*/
@Override
public State getState() {
return state;
return this.state;
}
/**
@ -121,7 +135,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
*/
public boolean addChange(IValueChange<? extends IValue<?>> change) {
initChanges();
return changes.add(change);
return this.changes.add(change);
}
/**
@ -130,7 +144,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
public List<IValueChange<? extends IValue<?>>> getChanges() {
if (this.changes == null)
return Collections.emptyList();
return changes;
return this.changes;
}
public Iterator<IValueChange<? extends IValue<?>>> changesIterator() {
@ -140,13 +154,13 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
}
@Override
public StrolchElement getParent() {
return parent;
public Activity getParent() {
return this.parent;
}
@Override
public StrolchRootElement getRootElement() {
return (parent == null) ? null : parent.getRootElement();
return (this.parent == null) ? null : this.parent.getRootElement();
}
@Override
@ -156,16 +170,19 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
@Override
public Action getClone() {
Action clone = new Action(getId(), getName(), getType());
clone.setDbid(getDbid());
clone.setResourceId(resourceId);
clone.setResourceType(resourceType);
clone.setState(state);
Action clone = new Action();
super.fillClone(clone);
clone.setResourceId(this.resourceId);
clone.setResourceType(this.resourceType);
clone.setState(this.state);
if (this.changes != null) {
for (IValueChange<? extends IValue<?>> change : getChanges()) {
clone.addChange(change.getClone());
}
}
return clone;
}
@ -209,7 +226,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
Long start = Long.MAX_VALUE;
if (this.changes == null)
return start;
for (IValueChange<?> change : changes) {
for (IValueChange<?> change : this.changes) {
start = Math.min(start, change.getTime());
}
return start;
@ -220,7 +237,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
Long end = 0L;
if (this.changes == null)
return end;
for (IValueChange<?> change : changes) {
for (IValueChange<?> change : this.changes) {
end = Math.max(end, change.getTime());
}
return end;

View File

@ -30,6 +30,7 @@ import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import ch.eitchnet.utils.dbc.DBC;
/**
* Parameterized object grouping a collection of {@link Activity} and {@link Action} objects defining the process to be
@ -45,6 +46,13 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
protected Map<String, IActivityElement> elements;
/**
* Empty constructor - for marshalling only!
*/
public Activity() {
super();
}
public Activity(String id, String name, String type) {
super(id, name, type);
}
@ -56,10 +64,29 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
}
}
/**
* Returns true if this {@link Activity} contains any children i.e. any of {@link Action} or {@link Activity}
*
* @return true if this {@link Activity} contains any children i.e. any of {@link Action} or {@link Activity}
*/
public boolean hasElements() {
return this.elements != null && !this.elements.isEmpty();
}
/**
* Returns true if this {@link Activity} contains a child with the given id. The element instance type is ignored,
* i.e. {@link Action} or {@link Activity}
*
* @param id
* the id of the element to check for
*
* @return true if this {@link Activity} contains a child with the given id. The element instance type is ignored,
* i.e. {@link Action} or {@link Activity}
*/
public boolean hasElement(String id) {
return this.elements != null && this.elements.containsKey(id);
}
/**
* add an activity element to the <code>LinkedHashMap</code> of <code>IActivityElements</code>
*
@ -67,6 +94,11 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
* @return the element added
*/
public IActivityElement addElement(IActivityElement activityElement) {
DBC.PRE.assertNotEquals("Can't add element to itself!", this, activityElement);
DBC.PRE.assertNull("Parent can't already be set!", activityElement.getParent());
// TODO make sure we can't create a circular dependency
initElements();
String id = activityElement.getId();
if (id == null)
@ -87,10 +119,11 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
* the id of the <code>IActivityElement</code>
* @return IActivityElement
*/
public IActivityElement getElement(String id) {
@SuppressWarnings("unchecked")
public <T extends IActivityElement> T getElement(String id) {
if (this.elements == null)
return null;
return elements.get(id);
return (T) elements.get(id);
}
/**
@ -179,9 +212,12 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
@Override
public Activity getClone() {
Activity clone = new Activity(id, name, type);
Activity clone = new Activity();
super.fillClone(clone);
if (this.elements == null)
return clone;
for (IActivityElement element : this.elements.values()) {
clone.addElement(element.getClone());
}

View File

@ -0,0 +1,41 @@
/*
* 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.visitor;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.Locator;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityDeepEqualsVisitor extends StrolchElementDeepEqualsVisitor implements
ActivityVisitor<List<Locator>> {
private Activity sourceActivity;
public ActivityDeepEqualsVisitor(Activity sourceActivity) {
this.sourceActivity = sourceActivity;
}
@Override
public List<Locator> visit(Activity dstActivity) {
deepEquals(this.sourceActivity, dstActivity);
return getMismatchedLocators();
}
}

View File

@ -16,7 +16,9 @@
package li.strolch.model.visitor;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import li.strolch.model.GroupedParameterizedElement;
@ -25,9 +27,13 @@ import li.strolch.model.Order;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.StrolchElement;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.activity.IActivityElement;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.ITimeVariable;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -53,6 +59,7 @@ public class StrolchElementDeepEqualsVisitor {
}
protected void deepEquals(StrolchElement srcElement, StrolchElement dstElement) {
DBC.PRE.assertEquals("Both elements should have the same ID", srcElement.getId(), dstElement.getId());
if (!srcElement.getName().equals(dstElement.getName())) {
this.mismatchedLocators.add(dstElement.getLocator());
}
@ -99,6 +106,74 @@ public class StrolchElementDeepEqualsVisitor {
}
}
protected void deepEquals(Activity srcActivity, Activity dstActivity) {
deepEquals((StrolchElement) srcActivity, (StrolchElement) dstActivity);
deepEquals((GroupedParameterizedElement) srcActivity, (GroupedParameterizedElement) dstActivity);
Iterator<Entry<String, IActivityElement>> iter = srcActivity.elementIterator();
while (iter.hasNext()) {
IActivityElement srcActivityElement = iter.next().getValue();
if (!dstActivity.hasElement(srcActivityElement.getId())) {
this.mismatchedLocators.add(srcActivityElement.getLocator());
continue;
}
IActivityElement dstActivityElement = dstActivity.getElement(srcActivityElement.getId());
if (!srcActivityElement.getClass().equals(dstActivityElement.getClass())) {
this.mismatchedLocators.add(srcActivityElement.getLocator());
continue;
}
if (srcActivityElement instanceof Activity) {
deepEquals((Activity) srcActivityElement, (Activity) dstActivityElement);
} else if (srcActivityElement instanceof Action) {
deepEquals((Action) srcActivityElement, (Action) dstActivityElement);
} else {
throw new UnsupportedOperationException("Unhandled instance type " + srcActivityElement.getClass());
}
}
iter = dstActivity.elementIterator();
while (iter.hasNext()) {
IActivityElement activityElement = iter.next().getValue();
if (!srcActivity.hasElement(activityElement.getId())) {
this.mismatchedLocators.add(activityElement.getLocator());
}
}
}
protected void deepEquals(Action srcAction, Action dstAction) {
deepEquals((StrolchElement) srcAction, (StrolchElement) dstAction);
deepEquals((GroupedParameterizedElement) srcAction, (GroupedParameterizedElement) dstAction);
if (!srcAction.getResourceId().equals(dstAction.getResourceId())) {
this.mismatchedLocators.add(dstAction.getLocator());
}
if (!srcAction.getResourceType().equals(dstAction.getResourceType())) {
this.mismatchedLocators.add(dstAction.getLocator());
}
if (!srcAction.getState().equals(dstAction.getState())) {
this.mismatchedLocators.add(dstAction.getLocator());
}
if ((srcAction.getParent() == null && srcAction.getParent() != null)
|| (srcAction.getParent() != null && srcAction.getParent() == null)) {
this.mismatchedLocators.add(dstAction.getLocator());
} else if (!srcAction.getParent().getId().equals(dstAction.getParent().getId())) {
this.mismatchedLocators.add(dstAction.getLocator());
} else if (!srcAction.getParent().getType().equals(dstAction.getParent().getType())) {
this.mismatchedLocators.add(dstAction.getLocator());
}
if (srcAction.hasChanges() != dstAction.hasChanges()) {
this.mismatchedLocators.add(dstAction.getLocator());
} else if (!srcAction.getChanges().equals(dstAction.getChanges())) {
this.mismatchedLocators.add(dstAction.getLocator());
}
}
protected void deepEquals(GroupedParameterizedElement srcElement, GroupedParameterizedElement dstElement) {
Set<String> srcBagKeySet = srcElement.getParameterBagKeySet();
for (String bagKey : srcBagKeySet) {

View File

@ -0,0 +1,32 @@
/*
* Copyright 2015 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.xml;
import li.strolch.model.activity.Activity;
import org.w3c.dom.Document;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityFromDomVisitor extends StrolchElementFromDomVisitor {
public Activity visit(Document doc) {
Activity activity = new Activity();
fillElement(doc.getDocumentElement(), activity);
return activity;
}
}

View File

@ -18,7 +18,6 @@ package li.strolch.model.xml;
import java.text.MessageFormat;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import org.xml.sax.ContentHandler;
@ -36,9 +35,7 @@ public class ActivityToSaxVisitor extends StrolchElementToSaxVisitor implements
public Void visit(Activity activity) {
try {
this.contentHandler.startElement(null, null, Tags.ACTIVITY, attributesFor(activity));
toSax(activity);
this.contentHandler.endElement(null, null, Tags.ACTIVITY);
} catch (Exception e) {
String msg = "Failed to transform Activity {0} to XML due to {1}"; //$NON-NLS-1$

View File

@ -16,14 +16,8 @@
package li.strolch.model.xml;
import li.strolch.model.Order;
import li.strolch.model.State;
import li.strolch.model.Tags;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -31,28 +25,8 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
public class OrderFromDomVisitor extends StrolchElementFromDomVisitor {
public Order visit(Document doc) {
return fromDom(doc.getDocumentElement());
}
public Order fromDom(Element element) {
Order order = new Order();
fromDom(element, order);
String date = element.getAttribute(Tags.DATE);
String state = element.getAttribute(Tags.STATE);
if (StringHelper.isEmpty(date)) {
order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse("-")); //$NON-NLS-1$
} else {
order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(date));
}
if (state == null || state.isEmpty()) {
order.setState(State.CREATED);
} else {
order.setState(State.valueOf(state));
}
fillElement(doc.getDocumentElement(), order);
return order;
}
}

View File

@ -19,7 +19,6 @@ import java.text.MessageFormat;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.Tags;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
@ -37,9 +36,7 @@ public class OrderToSaxVisitor extends StrolchElementToSaxVisitor implements Ord
public Void visit(Order order) {
try {
this.contentHandler.startElement(null, null, Tags.ORDER, attributesFor(order));
toSax(order);
this.contentHandler.endElement(null, null, Tags.ORDER);
} catch (SAXException e) {
String msg = "Failed to transform Order {0} to XML due to {1}"; //$NON-NLS-1$

View File

@ -15,24 +15,9 @@
*/
package li.strolch.model.xml;
import java.text.MessageFormat;
import java.util.Date;
import li.strolch.exception.StrolchException;
import li.strolch.model.AbstractStrolchElement;
import li.strolch.model.Resource;
import li.strolch.model.StrolchValueType;
import li.strolch.model.Tags;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -40,66 +25,8 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
public class ResourceFromDomVisitor extends StrolchElementFromDomVisitor {
public Resource visit(Document doc) {
return fromDom(doc.getDocumentElement());
}
public Resource fromDom(Element resourceElement) {
Resource resource = new Resource();
fromDom(resourceElement, resource);
NodeList timedStateElems = resourceElement.getElementsByTagName(Tags.TIMED_STATE);
for (int i = 0; i < timedStateElems.getLength(); i++) {
Element timedStateElem = (Element) timedStateElems.item(i);
String typeS = timedStateElem.getAttribute(Tags.TYPE);
DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + resource.getId(), typeS);
StrolchValueType valueType = StrolchValueType.parse(typeS);
StrolchTimedState<? extends IValue<?>> timedState = valueType.timedStateInstance();
fromDom(timedStateElem, (AbstractStrolchElement) timedState);
String interpretation = timedStateElem.getAttribute(Tags.INTERPRETATION);
String hidden = timedStateElem.getAttribute(Tags.HIDDEN);
String uom = timedStateElem.getAttribute(Tags.UOM);
String index = timedStateElem.getAttribute(Tags.INDEX);
timedState.setInterpretation(interpretation);
timedState.setUom(uom);
if (StringHelper.isEmpty(index)) {
timedState.setIndex(0);
} else {
timedState.setIndex(Integer.valueOf(index));
}
if (StringHelper.isEmpty(hidden)) {
timedState.setHidden(false);
} else {
if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) {
timedState.setHidden(true);
} else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) {
timedState.setHidden(false);
} else {
String msg = "Boolean string must be either {0} or {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, Boolean.TRUE.toString(), Boolean.FALSE.toString());
throw new StrolchException(msg);
}
}
NodeList timeValueElems = timedStateElem.getElementsByTagName(Tags.VALUE);
for (int j = 0; j < timeValueElems.getLength(); j++) {
Element timeValueElem = (Element) timeValueElems.item(j);
String timeS = timeValueElem.getAttribute(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(timeS);
long time = date.getTime();
String valueS = timeValueElem.getAttribute(Tags.VALUE);
timedState.setStateFromStringAt(time, valueS);
}
resource.addTimedState(timedState);
}
fillElement(doc.getDocumentElement(), resource);
return resource;
}
}

View File

@ -19,7 +19,6 @@ import java.text.MessageFormat;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.Tags;
import org.xml.sax.ContentHandler;
@ -36,9 +35,7 @@ public class ResourceToSaxVisitor extends StrolchElementToSaxVisitor implements
public Void visit(Resource res) {
try {
this.contentHandler.startElement(null, null, Tags.RESOURCE, attributesFor(res));
toSax(res);
this.contentHandler.endElement(null, null, Tags.RESOURCE);
} catch (Exception e) {
String msg = "Failed to transform Resource {0} to XML due to {1}"; //$NON-NLS-1$

View File

@ -16,27 +16,120 @@
package li.strolch.model.xml;
import java.text.MessageFormat;
import java.util.Date;
import li.strolch.exception.StrolchException;
import li.strolch.model.AbstractStrolchElement;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Order;
import li.strolch.model.ParameterBag;
import li.strolch.model.ParameterizedElement;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.StrolchValueType;
import li.strolch.model.Tags;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.impl.ValueChange;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class StrolchElementFromDomVisitor {
protected void fromDom(Element element, AbstractStrolchElement strolchElement) {
public void fillElement(Element element, Order order) {
fillElement(element, (GroupedParameterizedElement) order);
String date = element.getAttribute(Tags.DATE);
String state = element.getAttribute(Tags.STATE);
if (StringHelper.isEmpty(date)) {
order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse("-")); //$NON-NLS-1$
} else {
order.setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(date));
}
if (state == null || state.isEmpty()) {
order.setState(State.CREATED);
} else {
order.setState(State.valueOf(state));
}
}
public void fillElement(Element resourceElement, Resource resource) {
fillElement(resourceElement, (GroupedParameterizedElement) resource);
NodeList childNodes = resourceElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Element timedStateElem = (Element) childNodes.item(i);
if (!timedStateElem.getNodeName().equals(Tags.TIMED_STATE))
continue;
String typeS = timedStateElem.getAttribute(Tags.TYPE);
DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + resource.getId(), typeS);
StrolchValueType valueType = StrolchValueType.parse(typeS);
StrolchTimedState<? extends IValue<?>> timedState = valueType.timedStateInstance();
fillElement(timedStateElem, (AbstractStrolchElement) timedState);
String interpretation = timedStateElem.getAttribute(Tags.INTERPRETATION);
String hidden = timedStateElem.getAttribute(Tags.HIDDEN);
String uom = timedStateElem.getAttribute(Tags.UOM);
String index = timedStateElem.getAttribute(Tags.INDEX);
timedState.setInterpretation(interpretation);
timedState.setUom(uom);
if (StringHelper.isEmpty(index)) {
timedState.setIndex(0);
} else {
timedState.setIndex(Integer.valueOf(index));
}
if (StringHelper.isEmpty(hidden)) {
timedState.setHidden(false);
} else {
if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) {
timedState.setHidden(true);
} else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) {
timedState.setHidden(false);
} else {
String msg = "Boolean string must be either {0} or {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, Boolean.TRUE.toString(), Boolean.FALSE.toString());
throw new StrolchException(msg);
}
}
NodeList timeValueElems = timedStateElem.getChildNodes();
for (int j = 0; j < timeValueElems.getLength(); j++) {
Element timeValueElem = (Element) timeValueElems.item(j);
if (!timeValueElem.getNodeName().equals(Tags.VALUE))
continue;
String timeS = timeValueElem.getAttribute(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(timeS);
long time = date.getTime();
String valueS = timeValueElem.getAttribute(Tags.VALUE);
timedState.setStateFromStringAt(time, valueS);
}
resource.addTimedState(timedState);
}
}
protected void fillElement(Element element, AbstractStrolchElement strolchElement) {
String id = element.getAttribute(Tags.ID);
String name = element.getAttribute(Tags.NAME);
@ -50,43 +143,49 @@ public class StrolchElementFromDomVisitor {
}
}
protected void fromDom(Element element, GroupedParameterizedElement order) {
fromDom(element, (AbstractStrolchElement) order);
protected void fillElement(Element element, GroupedParameterizedElement groupedParameterizedElement) {
fillElement(element, (AbstractStrolchElement) groupedParameterizedElement);
String type = element.getAttribute(Tags.TYPE);
order.setType(type);
groupedParameterizedElement.setType(type);
NodeList bags = element.getElementsByTagName(Tags.PARAMETER_BAG);
NodeList bags = element.getChildNodes();
for (int i = 0; i < bags.getLength(); i++) {
Element bagElement = (Element) bags.item(i);
if (!bagElement.getNodeName().equals(Tags.PARAMETER_BAG))
continue;
ParameterBag bag = new ParameterBag();
fromDom(bagElement, bag);
order.addParameterBag(bag);
fillElement(bagElement, bag);
groupedParameterizedElement.addParameterBag(bag);
}
}
protected void fromDom(Element element, ParameterizedElement parameterizedElement) {
fromDom(element, (AbstractStrolchElement) parameterizedElement);
protected void fillElement(Element element, ParameterizedElement parameterizedElement) {
fillElement(element, (AbstractStrolchElement) parameterizedElement);
String type = element.getAttribute(Tags.TYPE);
parameterizedElement.setType(type);
// add all the parameters
NodeList parameterElements = element.getElementsByTagName(Tags.PARAMETER);
NodeList parameterElements = element.getChildNodes();
for (int i = 0; i < parameterElements.getLength(); i++) {
Element paramElement = (Element) parameterElements.item(i);
if (!paramElement.getNodeName().equals(Tags.PARAMETER))
continue;
String paramtype = paramElement.getAttribute(Tags.TYPE);
StrolchValueType paramValueType = StrolchValueType.parse(paramtype);
Parameter<?> parameter = paramValueType.parameterInstance();
fromDom(paramElement, parameter);
fillElement(paramElement, parameter);
parameterizedElement.addParameter(parameter);
}
}
protected void fromDom(Element element, Parameter<?> param) {
protected void fillElement(Element element, Parameter<?> param) {
fromDom(element, (AbstractStrolchElement) param);
fillElement(element, (AbstractStrolchElement) param);
String value = element.getAttribute(Tags.VALUE);
param.setValueFromString(value);
@ -119,4 +218,63 @@ public class StrolchElementFromDomVisitor {
}
}
}
protected void fillElement(Element activityElement, Activity activity) {
fillElement(activityElement, (GroupedParameterizedElement) activity);
NodeList childNodes = activityElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Element childElem = (Element) childNodes.item(i);
switch (childElem.getNodeName()) {
case Tags.ACTIVITY:
Activity childActivity = new Activity();
fillElement(childElem, childActivity);
activity.addElement(childActivity);
break;
case Tags.ACTION:
Action childAction = new Action();
fillElement(childElem, childAction);
activity.addElement(childAction);
break;
case Tags.PARAMETER_BAG:
break;
default:
throw new IllegalArgumentException("Unexpected element tag " + childElem.getNodeName());
}
}
}
protected void fillElement(Element element, Action action) {
fillElement(element, (GroupedParameterizedElement) action);
String resourceId = element.getAttribute(Tags.RESOURCE_ID);
String resourceType = element.getAttribute(Tags.RESOURCE_TYPE);
String stateS = element.getAttribute(Tags.STATE);
action.setResourceId(resourceId);
action.setResourceType(resourceType);
action.setState(State.valueOf(stateS));
NodeList valueChangeNodes = element.getChildNodes();
for (int i = 0; i < valueChangeNodes.getLength(); i++) {
Element valueChangeElem = (Element) valueChangeNodes.item(i);
if (!valueChangeElem.getNodeName().equals(Tags.VALUE_CHANGE))
continue;
String stateId = valueChangeElem.getAttribute(Tags.STATE_ID);
String timeS = valueChangeElem.getAttribute(Tags.TIME);
String valueS = valueChangeElem.getAttribute(Tags.VALUE);
String typeS = valueChangeElem.getAttribute(Tags.TYPE);
StrolchValueType type = StrolchValueType.parse(typeS);
IValue<?> value = type.valueInstance(valueS);
long time = ISO8601FormatFactory.getInstance().getDateFormat().parse(timeS).getTime();
ValueChange<IValue<?>> valueChange = new ValueChange<IValue<?>>(time, value, stateId);
action.addChange(valueChange);
}
}
}

View File

@ -91,6 +91,8 @@ public class StrolchElementToDomVisitor {
element.appendChild(toDom((Activity) activityElement));
} else if (activityElement instanceof Action) {
element.appendChild(toDom((Action) activityElement));
} else {
throw new IllegalArgumentException("Unhandled element " + activityElement.getClass());
}
}
}

View File

@ -18,6 +18,8 @@ package li.strolch.model.xml;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
@ -27,10 +29,14 @@ import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.StrolchElement;
import li.strolch.model.Tags;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.activity.IActivityElement;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
@ -58,14 +64,6 @@ public abstract class StrolchElementToSaxVisitor {
return attributes;
}
protected AttributesImpl attributesFor(Order order) {
AttributesImpl attributes = attributesFor((StrolchElement) order);
attributes.addAttribute(null, null, Tags.STATE, Tags.CDATA, order.getState().name());
attributes.addAttribute(null, null, Tags.DATE, Tags.CDATA,
ISO8601FormatFactory.getInstance().formatDate(order.getDate()));
return attributes;
}
protected AttributesImpl attributesFor(Parameter<?> parameter) {
AttributesImpl attributes = attributesFor((StrolchElement) parameter);
attributes.addAttribute(null, null, Tags.VALUE, Tags.CDATA, parameter.getValueAsString());
@ -131,6 +129,8 @@ public abstract class StrolchElementToSaxVisitor {
}
protected void toSax(Resource resource) throws SAXException {
this.contentHandler.startElement(null, null, Tags.RESOURCE, attributesFor(resource));
toSax((GroupedParameterizedElement) resource);
Set<String> stateKeySet = resource.getTimedStateKeySet();
@ -145,5 +145,75 @@ public abstract class StrolchElementToSaxVisitor {
}
this.contentHandler.endElement(null, null, Tags.TIMED_STATE);
}
this.contentHandler.endElement(null, null, Tags.RESOURCE);
}
protected AttributesImpl attributesFor(Order order) {
AttributesImpl attributes = attributesFor((StrolchElement) order);
attributes.addAttribute(null, null, Tags.STATE, Tags.CDATA, order.getState().name());
attributes.addAttribute(null, null, Tags.DATE, Tags.CDATA,
ISO8601FormatFactory.getInstance().formatDate(order.getDate()));
return attributes;
}
protected void toSax(Order order) throws SAXException {
this.contentHandler.startElement(null, null, Tags.ORDER, attributesFor(order));
toSax((GroupedParameterizedElement) order);
this.contentHandler.endElement(null, null, Tags.ORDER);
}
protected void toSax(Activity activity) throws SAXException {
this.contentHandler.startElement(null, null, Tags.ACTIVITY, attributesFor(activity));
toSax((GroupedParameterizedElement) activity);
Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator();
while (iter.hasNext()) {
IActivityElement activityElement = iter.next().getValue();
if (activityElement instanceof Activity) {
toSax((Activity) activityElement);
} else if (activityElement instanceof Action) {
toSax((Action) activityElement);
} else {
throw new IllegalArgumentException("Unhandled element " + activityElement.getClass());
}
}
this.contentHandler.endElement(null, null, Tags.ACTIVITY);
}
protected AttributesImpl attributesFor(Action action) {
AttributesImpl attributes = attributesFor((StrolchElement) action);
attributes.addAttribute(null, null, Tags.RESOURCE_ID, Tags.CDATA, action.getResourceId());
attributes.addAttribute(null, null, Tags.RESOURCE_TYPE, Tags.CDATA, action.getResourceType());
attributes.addAttribute(null, null, Tags.STATE, Tags.CDATA, action.getState().name());
return attributes;
}
protected AttributesImpl attributesFor(IValueChange<? extends IValue<?>> valueChange) {
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute(null, null, Tags.STATE_ID, Tags.CDATA, valueChange.getStateId());
attributes.addAttribute(null, null, Tags.TIME, Tags.CDATA,
ISO8601FormatFactory.getInstance().formatDate(valueChange.getTime()));
attributes.addAttribute(null, null, Tags.VALUE, Tags.CDATA, valueChange.getValue().getValueAsString());
attributes.addAttribute(null, null, Tags.TYPE, Tags.CDATA, valueChange.getValue().getType());
return attributes;
}
protected void toSax(Action action) throws SAXException {
this.contentHandler.startElement(null, null, Tags.ACTION, attributesFor(action));
toSax((GroupedParameterizedElement) action);
Iterator<IValueChange<? extends IValue<?>>> iter = action.changesIterator();
while (iter.hasNext()) {
IValueChange<? extends IValue<?>> valueChange = iter.next();
this.contentHandler.startElement(null, null, Tags.VALUE_CHANGE, attributesFor(valueChange));
this.contentHandler.endElement(null, null, Tags.VALUE_CHANGE);
}
this.contentHandler.endElement(null, null, Tags.ACTION);
}
}

View File

@ -106,8 +106,6 @@ public class XmlModelSaxReader extends DefaultHandler {
case Tags.ACTION:
// Action Id="action_1" Name="Action 1" ResourceId="dummyId" ResourceType="dummyType" State="CREATED" Type="Use"
String actionId = attributes.getValue(Tags.ID);
String actionName = attributes.getValue(Tags.NAME);
String actionType = attributes.getValue(Tags.TYPE);
@ -119,15 +117,12 @@ public class XmlModelSaxReader extends DefaultHandler {
action.setResourceType(actionResourceType);
action.setState(State.valueOf(actionState));
this.activityStack.peek().addElement(action);
this.parameterizedElement = action;
break;
case Tags.VALUE_CHANGE:
// ValueChange StateId="dummyId" Time="2012-11-30T18:12:05.628+01:00" Value="5" ValueClass="Integer"
String valueChangeStateId = attributes.getValue(Tags.STATE_ID);
String valueChangeTimeS = attributes.getValue(Tags.TIME);
String valueChangeValue = attributes.getValue(Tags.VALUE);
@ -135,8 +130,7 @@ public class XmlModelSaxReader extends DefaultHandler {
IValue<?> value = StrolchValueType.parse(valueChangeType).valueInstance(valueChangeValue);
long valueChangeTime = ISO8601FormatFactory.getInstance().getDateFormat().parse(valueChangeTimeS).getTime();
ValueChange<IValue<?>> valueChange = new ValueChange<IValue<?>>(valueChangeTime, value);
valueChange.setStateId(valueChangeStateId);
ValueChange<IValue<?>> valueChange = new ValueChange<IValue<?>>(valueChangeTime, value, valueChangeStateId);
((Action) this.parameterizedElement).addChange(valueChange);
@ -167,7 +161,6 @@ public class XmlModelSaxReader extends DefaultHandler {
String pBagType = attributes.getValue(Tags.TYPE);
ParameterBag pBag = new ParameterBag(pBagId, pBagName, pBagType);
this.pBag = pBag;
this.parameterizedElement.addParameterBag(pBag);
break;
@ -253,6 +246,8 @@ public class XmlModelSaxReader extends DefaultHandler {
if (this.activityStack.isEmpty()) {
this.listener.notifyActivity(activity);
this.statistics.nrOfActivities++;
} else {
this.activityStack.peek().addElement(activity);
}
this.parameterizedElement = null;
@ -268,12 +263,14 @@ public class XmlModelSaxReader extends DefaultHandler {
case Tags.ACTION:
this.activityStack.peek().addElement((Action) parameterizedElement);
this.parameterizedElement = null;
break;
case Tags.PARAMETER_BAG:
this.parameterizedElement.addParameterBag(pBag);
this.pBag = null;
break;

View File

@ -15,6 +15,8 @@
*/
package li.strolch.model;
import static li.strolch.model.ModelGenerator.ACTION_RES_ID;
import static li.strolch.model.ModelGenerator.ACTION_RES_TYPE;
import static li.strolch.model.ModelGenerator.BAG_ID;
import static li.strolch.model.ModelGenerator.BAG_NAME;
import static li.strolch.model.ModelGenerator.BAG_TYPE;
@ -52,6 +54,7 @@ import static li.strolch.model.ModelGenerator.STATE_TIME_0;
import static li.strolch.model.ModelGenerator.STATE_TIME_10;
import static li.strolch.model.ModelGenerator.STATE_TIME_20;
import static li.strolch.model.ModelGenerator.STATE_TIME_30;
import static li.strolch.model.ModelGenerator.createActivity;
import static li.strolch.model.ModelGenerator.createOrder;
import static li.strolch.model.ModelGenerator.createResource;
import static li.strolch.model.Tags.BAG;
@ -65,7 +68,10 @@ import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.FloatListParameter;
@ -80,8 +86,12 @@ 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.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import li.strolch.model.timevalue.impl.BooleanValue;
import li.strolch.model.timevalue.impl.IntegerValue;
import li.strolch.model.timevalue.impl.ValueChange;
import li.strolch.model.visitor.ActivityDeepEqualsVisitor;
import li.strolch.model.visitor.OrderDeepEqualsVisitor;
import li.strolch.model.visitor.ResourceDeepEqualsVisitor;
@ -98,19 +108,94 @@ public class ModelTest {
public void shouldCreateResource() {
Resource resource = createResource("@res01", "Test resource", "MyType");
assertEquals("@res01", resource.getId());
assertEquals("Test resource", resource.getName());
assertEquals("MyType", resource.getType());
ParameterBag bag = resource.getParameterBag(BAG_ID);
validateBag(bag);
validateStates(resource);
}
@Test
public void shouldCreateOrder() {
Order order = createOrder("@ord01", "Test Order", "MyType", new Date(), State.OPEN);
Date date = new Date();
Order order = createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
assertEquals("@ord01", order.getId());
assertEquals("Test Order", order.getName());
assertEquals("MyType", order.getType());
assertEquals(date, order.getDate());
assertEquals(State.OPEN, order.getState());
ParameterBag bag = order.getParameterBag(BAG_ID);
validateBag(bag);
}
@Test
public void shouldCreateActivity() {
String actId = "@act01";
String actName = "Test Activity";
String actType = "MyType";
List<IValueChange<? extends IValue<?>>> changes = new ArrayList<>();
changes.add(new ValueChange<>(0L, new IntegerValue(0), STATE_INTEGER_ID));
changes.add(new ValueChange<>(10L, new IntegerValue(10), STATE_INTEGER_ID));
changes.add(new ValueChange<>(20L, new IntegerValue(20), STATE_INTEGER_ID));
changes.add(new ValueChange<>(30L, new IntegerValue(30), STATE_INTEGER_ID));
changes.add(new ValueChange<>(40L, new IntegerValue(20), STATE_INTEGER_ID));
changes.add(new ValueChange<>(50L, new IntegerValue(10), STATE_INTEGER_ID));
changes.add(new ValueChange<>(60L, new IntegerValue(0), STATE_INTEGER_ID));
Activity activity = createActivity(actId, actName, actType);
assertEquals(actId, activity.getId());
assertEquals(actName, activity.getName());
assertEquals(actType, activity.getType());
ParameterBag bag = activity.getParameterBag(BAG_ID);
validateBag(bag);
Action action = activity.getElement("act_" + actId);
assertEquals("act_" + actId, action.getId());
assertEquals("Action " + actName, action.getName());
assertEquals("Use", action.getType());
assertEquals(ACTION_RES_ID, action.getResourceId());
assertEquals(ACTION_RES_TYPE, action.getResourceType());
assertEquals(changes, action.getChanges());
activity = activity.getElement("sub_" + actId);
assertEquals("sub_" + actId, activity.getId());
assertEquals("sub_" + actName, activity.getName());
assertEquals(actType, activity.getType());
bag = activity.getParameterBag(BAG_ID);
validateBag(bag);
action = activity.getElement("act_" + actId);
assertEquals("act_" + actId, action.getId());
assertEquals("Action " + actName, action.getName());
assertEquals("Use", action.getType());
assertEquals(ACTION_RES_ID, action.getResourceId());
assertEquals(ACTION_RES_TYPE, action.getResourceType());
assertEquals(changes, action.getChanges());
activity = activity.getElement("subSub_" + actId);
assertEquals("subSub_" + actId, activity.getId());
assertEquals("subSub_" + actName, activity.getName());
assertEquals(actType, activity.getType());
bag = activity.getParameterBag(BAG_ID);
validateBag(bag);
action = activity.getElement("act_" + actId);
assertEquals("act_" + actId, action.getId());
assertEquals("Action " + actName, action.getName());
assertEquals("Use", action.getType());
assertEquals(ACTION_RES_ID, action.getResourceId());
assertEquals(ACTION_RES_TYPE, action.getResourceType());
assertEquals(changes, action.getChanges());
}
@Test
public void shouldCreateLocators() {
@ -131,6 +216,71 @@ public class ModelTest {
assertEquals(Locator.valueOf(ORDER, "MyType", "@ord01", BAG, BAG_ID, PARAM_STRING_ID), sP.getLocator());
}
@Test
public void shouldPerformDeepActivityEquals() {
Activity srcActivity = createActivity("@act01", "Test Activity", "MyType");
Activity dstActivity = createActivity("@act01", "Test Activity", "MyType");
ActivityDeepEqualsVisitor visitor = new ActivityDeepEqualsVisitor(srcActivity);
visitor.visit(dstActivity);
assertTrue("Same Activity should be deep equal!", visitor.isEqual());
}
@Test
public void shouldPerformActivityClone() {
Activity srcActivity = createActivity("@act01", "Test Activity", "MyType");
Activity dstActivity = srcActivity.getClone();
ActivityDeepEqualsVisitor visitor = new ActivityDeepEqualsVisitor(srcActivity);
visitor.visit(dstActivity);
assertTrue("Cloned Activity should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFailDeepActivityEquals1() {
Activity srcActivity = createActivity("@act01", "Test Activity", "MyType");
Activity dstActivity = createActivity("@act01", "Test Activity", "MyType");
dstActivity.setName("Bla");
dstActivity.setType("BlaBla");
ParameterBag bag = dstActivity.getParameterBag(BAG_ID);
bag.setName("Bla bla");
FloatParameter fParam = bag.getParameter(PARAM_FLOAT_ID);
fParam.setValue(23434234.234);
fParam.setName("Ohla");
ActivityDeepEqualsVisitor visitor = new ActivityDeepEqualsVisitor(srcActivity);
visitor.visit(dstActivity);
assertFalse("Activity should not be same if something has been changed", visitor.isEqual());
assertEquals("Multiple changes should be registered", 6, visitor.getMismatchedLocators().size());
}
@Test
public void shouldFailDeepActivityEquals2() {
Activity srcActivity = createActivity("@act01", "Test Activity", "MyType");
Activity dstActivity = createActivity("@act01", "Test Activity", "MyType");
Action action = dstActivity.getElement("act_" + "@act01");
action.setResourceId("Bla");
action.setResourceType("Bla");
action.setType("Bla");
action.setState(State.CLOSED);
action.addChange(new ValueChange<>(1234567890L, new IntegerValue(12345), STATE_INTEGER_ID));
Activity activity = dstActivity.getElement("sub_" + "@act01");
activity.addElement(new Action("bla", "Bla", "Bla"));
action = activity.getElement("act_" + "@act01");
action.addChange(new ValueChange<>(1234567890L, new IntegerValue(12345), STATE_INTEGER_ID));
activity = activity.getElement("subSub_" + "@act01");
activity.addElement(new Action("bla", "Bla", "Bla"));
action = activity.getElement("act_" + "@act01");
action.addChange(new ValueChange<>(1234567890L, new IntegerValue(12345), STATE_INTEGER_ID));
ActivityDeepEqualsVisitor visitor = new ActivityDeepEqualsVisitor(srcActivity);
visitor.visit(dstActivity);
assertFalse("Activity should not be same if something has been changed", visitor.isEqual());
assertEquals("Multiple changes should be registered", 9, visitor.getMismatchedLocators().size());
}
@Test
public void shouldPerformDeepResourceEquals() {
Resource srcRes = createResource("@res01", "Test resource", "MyType");
@ -149,16 +299,6 @@ public class ModelTest {
assertTrue("Cloned Resource should be deep equal!", visitor.isEqual());
}
@Test
public void shouldPerformOrderClone() {
Date date = new Date();
Order srcOrder = createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
Order dstOrder = srcOrder.getClone();
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(dstOrder);
assertTrue("Cloned Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFailDeepResourceEquals1() {
Resource srcRes = createResource("@res01", "Test resource", "MyType");
@ -171,7 +311,7 @@ public class ModelTest {
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());
assertEquals("Multiple changes should be registered", 3, visitor.getMismatchedLocators().size());
}
@Test
@ -179,12 +319,12 @@ public class ModelTest {
Resource srcRes = createResource("@res01", "Test resource", "MyType");
Resource dstRes = createResource("@res01", "Test resource", "MyType");
BooleanTimedState timedState = dstRes.getTimedState(STATE_BOOLEAN_ID);
timedState.applyChange(new ValueChange<>(System.currentTimeMillis(), new BooleanValue(Boolean.TRUE)));
timedState.applyChange(new ValueChange<>(System.currentTimeMillis(), new BooleanValue(Boolean.FALSE)));
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());
assertEquals("Multiple change should be registered!", 2, visitor.getMismatchedLocators().size());
}
@Test
@ -197,6 +337,34 @@ public class ModelTest {
assertTrue("Same Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldPerformOrderClone() {
Date date = new Date();
Order srcOrder = createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
Order dstOrder = srcOrder.getClone();
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(dstOrder);
assertTrue("Cloned Order should be deep equal: " + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFailDeepOrderEquals1() {
Date date = new Date();
Order srcOrder = createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
Order dstOrder = createOrder("@ord01", "Test Order", "MyType", date, State.OPEN);
dstOrder.setDate(new Date(1L));
dstOrder.setState(State.CLOSED);
ParameterBag bag = dstOrder.getParameterBag(BAG_ID);
bag.setName("Bla bla");
FloatParameter fParam = bag.getParameter(PARAM_FLOAT_ID);
fParam.setValue(23434234.234);
fParam.setName("Ohla");
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(srcOrder);
visitor.visit(dstOrder);
assertFalse("Order should not be same if something has been changed", visitor.isEqual());
assertEquals("Multiple changes should be registered", 5, visitor.getMismatchedLocators().size());
}
public static void validateBag(ParameterBag bag) {
assertNotNull(bag);

View File

@ -16,8 +16,12 @@
package li.strolch.model;
import static org.junit.Assert.assertTrue;
import li.strolch.model.activity.Activity;
import li.strolch.model.visitor.ActivityDeepEqualsVisitor;
import li.strolch.model.visitor.OrderDeepEqualsVisitor;
import li.strolch.model.visitor.ResourceDeepEqualsVisitor;
import li.strolch.model.xml.ActivityFromDomVisitor;
import li.strolch.model.xml.ActivityToDomVisitor;
import li.strolch.model.xml.OrderFromDomVisitor;
import li.strolch.model.xml.OrderToDomVisitor;
import li.strolch.model.xml.ResourceFromDomVisitor;
@ -63,4 +67,21 @@ public class XmlToDomTest extends ModelTest {
visitor.visit(parsedResource);
assertTrue("To DOM and back should equal same Resource:\n" + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFormatAndParseActivity() {
Activity activity = ModelGenerator.createActivity("@1", "My Activity 1", "Transport");
ActivityToDomVisitor domVisitor = new ActivityToDomVisitor();
domVisitor.visit(activity);
Document document = domVisitor.getDocument();
Activity parsedActivity = new ActivityFromDomVisitor().visit(document);
ActivityDeepEqualsVisitor visitor = new ActivityDeepEqualsVisitor(parsedActivity);
visitor.visit(parsedActivity);
assertTrue("To DOM and back should equal same Activity:\n" + visitor.getMismatchedLocators(), visitor.isEqual());
}
}

View File

@ -25,8 +25,12 @@ import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.model.activity.Activity;
import li.strolch.model.visitor.ActivityDeepEqualsVisitor;
import li.strolch.model.visitor.OrderDeepEqualsVisitor;
import li.strolch.model.visitor.ResourceDeepEqualsVisitor;
import li.strolch.model.xml.ActivityToSaxVisitor;
import li.strolch.model.xml.ActivityToSaxWriterVisitor;
import li.strolch.model.xml.OrderToSaxVisitor;
import li.strolch.model.xml.OrderToSaxWriterVisitor;
import li.strolch.model.xml.ResourceToSaxVisitor;
@ -55,6 +59,7 @@ public class XmlToSaxTest extends ModelTest {
assertEquals(1, listener.getOrders().size());
assertEquals(Collections.emptyList(), listener.getResources());
assertEquals(Collections.emptyList(), listener.getActivities());
Order parsedOrder = listener.getOrders().get(0);
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(order);
@ -74,6 +79,7 @@ public class XmlToSaxTest extends ModelTest {
domVisitor.visit(resource);
assertEquals(1, listener.getResources().size());
assertEquals(Collections.emptyList(), listener.getActivities());
assertEquals(Collections.emptyList(), listener.getOrders());
Resource parsedResource = listener.getResources().get(0);
@ -82,6 +88,27 @@ public class XmlToSaxTest extends ModelTest {
assertTrue("To DOM and back should equal same Resource:\n" + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldFormatAndParseActivity() {
Activity activity = ModelGenerator.createActivity("@1", "My Activity 1", "MyActivity");
SimpleStrolchElementListener listener = new SimpleStrolchElementListener();
XmlModelSaxReader saxReader = new XmlModelSaxReader(listener);
ActivityToSaxVisitor domVisitor = new ActivityToSaxVisitor(saxReader);
domVisitor.visit(activity);
assertEquals(1, listener.getActivities().size());
assertEquals(Collections.emptyList(), listener.getResources());
assertEquals(Collections.emptyList(), listener.getOrders());
Activity parsedActivity = listener.getActivities().get(0);
ActivityDeepEqualsVisitor visitor = new ActivityDeepEqualsVisitor(activity);
visitor.visit(parsedActivity);
assertTrue("To DOM and back should equal same Activity:\n" + visitor.getMismatchedLocators(), visitor.isEqual());
}
@Test
public void shouldToSaxOrder() throws XMLStreamException {
Order order = ModelGenerator.createOrder("@1", "My Order 1", "MyOrder");
@ -107,4 +134,17 @@ public class XmlToSaxTest extends ModelTest {
toSax.visit(resource);
writer.writeEndDocument();
}
@Test
public void shouldToSaxActivity() throws XMLStreamException {
Activity activity = ModelGenerator.createActivity("@1", "My Activity 1", "MyActivity");
XMLOutputFactory factory = XMLOutputFactory.newInstance();
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLStreamWriter writer = factory.createXMLStreamWriter(out, "UTF-8");
writer.writeStartDocument();
writer.writeStartElement(Tags.STROLCH_MODEL);
ActivityToSaxWriterVisitor toSax = new ActivityToSaxWriterVisitor(writer);
toSax.visit(activity);
writer.writeEndDocument();
}
}