Merge remote-tracking branch 'refs/remotes/origin/activity-agent-map' into develop

This commit is contained in:
Robert von Burg 2015-07-08 08:50:02 +02:00
commit 3ea59da454
140 changed files with 4406 additions and 1519 deletions

@ -1 +1 @@
Subproject commit 1689ff69a93f7fcf491400e36b941c905d368018
Subproject commit 2e58db83fd35fea958431d7a2e9edae14ff6a20b

@ -1 +1 @@
Subproject commit f72ff76b406f2675cf194174aa35872d0c969ab6
Subproject commit 6659b90b83aa253e79e9a705c42e9c0e3b3fd63c

View File

@ -0,0 +1,31 @@
/*
* 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.agent.api;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.StrolchTransaction;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityMap extends ElementMap<Activity> {
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor);
}

View File

@ -0,0 +1,57 @@
/*
* 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.agent.impl;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ElementMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.StrolchTransaction;
/**
* This is the {@link AuditTrail} for {@link Activity Activities}
*
* @author Robert von Burg <eitch@eitchnet.ch>
*
* @see AuditingElementMapFacade
*/
public class AuditingActivityMap extends AuditingElementMapFacade<Activity> implements ActivityMap {
/**
* @param elementMap
*/
public AuditingActivityMap(ElementMap<Activity> elementMap, boolean observeAccessReads) {
super(elementMap, observeAccessReads);
}
@Override
protected ActivityMap getElementMap() {
return (ActivityMap) super.getElementMap();
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
List<U> result = getElementMap().doQuery(tx, query, activity -> {
this.read.add(activity);
return activityVisitor.visit(activity);
});
return result;
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ACTIVITY_REF;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.persistence.inmemory.InMemoryActivityDao;
public class CachedActivityMap extends CachedElementMap<Activity> implements ActivityMap {
private ActivityDao cachedDao;
public CachedActivityMap() {
super();
this.cachedDao = new InMemoryActivityDao();
}
@Override
protected void assertIsRefParam(Parameter<?> refP) {
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ACTIVITY_REF, refP);
}
@Override
protected ActivityDao getDbDao(StrolchTransaction tx) {
return tx.getPersistenceHandler().getActivityDao(tx);
}
@Override
public ActivityDao getCachedDao() {
return this.cachedDao;
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
return getCachedDao().doQuery(query, activityVisitor);
}
}

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ORDER_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.parameter.Parameter;
@ -42,18 +39,7 @@ public class CachedOrderMap extends CachedElementMap<Order> implements OrderMap
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_ORDER_REF)) {
String msg = "{0} is not an Order reference as its interpretation is not {1} it is {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), INTERPRETATION_ORDER_REF,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an Order reference as its UOM is not set to a type!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator()));
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ORDER_REF, refP);
}
@Override

View File

@ -19,12 +19,15 @@ import java.text.MessageFormat;
import java.util.List;
import java.util.Set;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
import li.strolch.persistence.api.ResourceDao;
@ -43,6 +46,7 @@ public class CachedRealm extends InternalStrolchRealm {
private PersistenceHandler persistenceHandler;
private CachedResourceMap resourceMap;
private CachedOrderMap orderMap;
private CachedActivityMap activityMap;
private AuditTrail auditTrail;
public CachedRealm(String realm) {
@ -76,6 +80,11 @@ public class CachedRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;
@ -88,6 +97,7 @@ public class CachedRealm extends InternalStrolchRealm {
this.persistenceHandler = container.getComponent(PersistenceHandler.class);
this.resourceMap = new CachedResourceMap();
this.orderMap = new CachedOrderMap();
this.activityMap = new CachedActivityMap();
if (isAuditTrailEnabled()) {
this.auditTrail = new CachedAuditTrail();
@ -104,6 +114,7 @@ public class CachedRealm extends InternalStrolchRealm {
long start = System.nanoTime();
int nrOfOrders = 0;
int nrOfResources = 0;
int nrOfActivities = 0;
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
ResourceDao resourceDao = tx.getPersistenceHandler().getResourceDao(tx);
@ -133,11 +144,26 @@ public class CachedRealm extends InternalStrolchRealm {
tx.commitOnClose();
}
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
ActivityDao activityDao = tx.getPersistenceHandler().getActivityDao(tx);
Set<String> activityTypes = activityDao.queryTypes();
for (String type : activityTypes) {
List<Activity> activities = activityDao.queryAll(type);
for (Activity activity : activities) {
this.activityMap.insert(activity);
nrOfActivities++;
}
}
tx.commitOnClose();
}
long duration = System.nanoTime() - start;
String durationS = StringHelper.formatNanoDuration(duration);
logger.info(MessageFormat.format("Loading Model from Database for realm {0} took {1}.", getRealm(), durationS)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Orders", nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Resources", nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Activities", nrOfActivities)); //$NON-NLS-1$
}
@Override

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_RESOURCE_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.parameter.Parameter;
@ -42,20 +39,7 @@ public class CachedResourceMap extends CachedElementMap<Resource> implements Res
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_RESOURCE_REF)) {
String msg = MessageFormat.format(
"{0} is not an Resource reference as its interpretation is not {1} it is {2}", //$NON-NLS-1$
refP.getLocator(), INTERPRETATION_RESOURCE_REF, interpretation);
throw new StrolchException(msg);
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = MessageFormat.format("{0} is not an Resource reference as its UOM is not set to a type!", //$NON-NLS-1$
refP.getLocator());
throw new StrolchException(msg);
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_RESOURCE_REF, refP);
}
@Override

View File

@ -0,0 +1,44 @@
/*
* 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.agent.impl;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.parameter.Parameter;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
class ElementMapHelpers {
public static void assertIsRefParam(String expectedInterpretation, Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(expectedInterpretation)) {
String msg = "{0} is not an expected element reference as its interpretation is {1} instead of {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), expectedInterpretation,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an expected element reference as its UOM is not set to a type it is set to {1}!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), UOM_NONE));
}
}
}

View File

@ -17,6 +17,7 @@ package li.strolch.agent.impl;
import java.text.MessageFormat;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
@ -37,6 +38,7 @@ public class EmptyRealm extends InternalStrolchRealm {
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
private AuditTrail auditTrail;
private PersistenceHandler persistenceHandler;
@ -71,6 +73,11 @@ public class EmptyRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;

View File

@ -18,10 +18,12 @@ package li.strolch.agent.impl;
import java.util.Collections;
import java.util.Set;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.persistence.api.StrolchTransaction;
@ -32,26 +34,34 @@ public class InMemoryElementListener implements StrolchElementListener {
private boolean addOrders;
private boolean addResources;
private boolean addActivities;
private boolean updateOrders;
private boolean updateResources;
private boolean updateActivities;
private Set<String> orderTypes;
private Set<String> resourceTypes;
private Set<String> activityTypes;
private StrolchTransaction tx;
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
public InMemoryElementListener(StrolchTransaction tx) {
this.tx = tx;
this.resourceMap = tx.getResourceMap();
this.orderMap = tx.getOrderMap();
this.activityMap = tx.getActivityMap();
this.addResources = true;
this.addOrders = true;
this.addActivities = true;
this.updateResources = true;
this.updateOrders = true;
this.updateActivities = true;
this.orderTypes = Collections.emptySet();
this.resourceTypes = Collections.emptySet();
this.activityTypes = Collections.emptySet();
}
/**
@ -70,6 +80,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.addOrders = addOrders;
}
/**
* @param addActivities
* the addActivities to set
*/
public void setAddActivities(boolean addActivities) {
this.addActivities = addActivities;
}
/**
* @param updateResources
* the updateResources to set
@ -86,6 +104,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.updateOrders = updateOrders;
}
/**
* @param updateActivities
* the updateActivities to set
*/
public void setUpdateActivities(boolean updateActivities) {
this.updateActivities = updateActivities;
}
/**
* @param orderTypes
* the orderTypes to set
@ -102,6 +128,14 @@ public class InMemoryElementListener implements StrolchElementListener {
this.resourceTypes = resourceTypes;
}
/**
* @param activityTypes
* the activityTypes to set
*/
public void setActivityTypes(Set<String> activityTypes) {
this.activityTypes = activityTypes;
}
@Override
public void notifyResource(Resource resource) {
if (!this.resourceTypes.isEmpty() && !this.resourceTypes.contains(resource.getType()))
@ -129,4 +163,18 @@ public class InMemoryElementListener implements StrolchElementListener {
this.orderMap.add(this.tx, order);
}
}
@Override
public void notifyActivity(Activity activity) {
if (!this.activityTypes.isEmpty() && !this.activityTypes.contains(activity.getType()))
return;
if (this.activityMap.hasElement(this.tx, activity.getType(), activity.getId())) {
if (this.updateActivities) {
this.activityMap.update(this.tx, activity);
}
} else if (this.addActivities) {
this.activityMap.add(this.tx, activity);
}
}
}

View File

@ -18,6 +18,7 @@ package li.strolch.agent.impl;
import java.text.MessageFormat;
import java.util.concurrent.TimeUnit;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.LockHandler;
@ -132,6 +133,8 @@ public abstract class InternalStrolchRealm implements StrolchRealm {
public abstract OrderMap getOrderMap();
public abstract ActivityMap getActivityMap();
public abstract AuditTrail getAuditTrail();
}

View File

@ -17,7 +17,9 @@ package li.strolch.agent.impl;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.ResourceDao;
import li.strolch.persistence.api.StrolchTransaction;
@ -27,11 +29,13 @@ public class StoreToDaoElementListener implements StrolchElementListener {
private StrolchTransaction tx;
private ResourceDao resourceDao;
private OrderDao orderDao;
private ActivityDao activityDao;
public StoreToDaoElementListener(StrolchTransaction tx) {
this.tx = tx;
this.resourceDao = tx.getPersistenceHandler().getResourceDao(this.tx);
this.orderDao = tx.getPersistenceHandler().getOrderDao(this.tx);
this.activityDao = tx.getPersistenceHandler().getActivityDao(this.tx);
}
@Override
@ -43,4 +47,9 @@ public class StoreToDaoElementListener implements StrolchElementListener {
public void notifyOrder(Order order) {
this.orderDao.save(order);
}
@Override
public void notifyActivity(Activity activity) {
this.activityDao.save(activity);
}
}

View File

@ -0,0 +1,46 @@
/*
* 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.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ACTIVITY_REF;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.StrolchTransaction;
public class TransactionalActivityMap extends TransactionalElementMap<Activity> implements ActivityMap {
@Override
protected void assertIsRefParam(Parameter<?> refP) {
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ACTIVITY_REF, refP);
}
@Override
protected ActivityDao getDao(StrolchTransaction tx) {
return tx.getPersistenceHandler().getActivityDao(tx);
}
@Override
public <U> List<U> doQuery(StrolchTransaction tx, ActivityQuery query, ActivityVisitor<U> activityVisitor) {
return getDao(tx).doQuery(query, activityVisitor);
}
}

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_ORDER_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.OrderMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.parameter.Parameter;
@ -34,18 +31,7 @@ public class TransactionalOrderMap extends TransactionalElementMap<Order> implem
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_ORDER_REF)) {
String msg = "{0} is not an Order reference as its interpretation is not {1} it is {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), INTERPRETATION_ORDER_REF,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an Order reference as its UOM is not set to a type!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator()));
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_ORDER_REF, refP);
}
@Override

View File

@ -17,6 +17,7 @@ package li.strolch.agent.impl;
import java.text.MessageFormat;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
@ -36,6 +37,7 @@ public class TransactionalRealm extends InternalStrolchRealm {
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
private AuditTrail auditTrail;
private PersistenceHandler persistenceHandler;
@ -70,6 +72,11 @@ public class TransactionalRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;
@ -80,6 +87,7 @@ public class TransactionalRealm extends InternalStrolchRealm {
super.initialize(container, configuration);
this.resourceMap = new TransactionalResourceMap();
this.orderMap = new TransactionalOrderMap();
this.activityMap = new TransactionalActivityMap();
if (isAuditTrailEnabled()) {
this.auditTrail = new TransactionalAuditTrail();
@ -98,6 +106,7 @@ public class TransactionalRealm extends InternalStrolchRealm {
long start = System.nanoTime();
int nrOfOrders = 0;
int nrOfResources = 0;
int nrOfActivities = 0;
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
nrOfOrders = this.orderMap.getAllKeys(tx).size();
@ -107,12 +116,17 @@ public class TransactionalRealm extends InternalStrolchRealm {
nrOfResources = this.resourceMap.getAllKeys(tx).size();
}
try (StrolchTransaction tx = openTx(privilegeContext.getCertificate(), DefaultRealmHandler.AGENT_BOOT)) {
nrOfActivities = this.activityMap.getAllKeys(tx).size();
}
long duration = System.nanoTime() - start;
String durationS = StringHelper.formatNanoDuration(duration);
logger.info(MessageFormat.format(
"Initialized Transactional Maps for realm {0} took {1}.", getRealm(), durationS)); //$NON-NLS-1$
logger.info(MessageFormat.format("There are {0} Orders", nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("There are {0} Resources", nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("There are {0} Activities", nrOfActivities)); //$NON-NLS-1$
}
@Override

View File

@ -16,13 +16,10 @@
package li.strolch.agent.impl;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_RESOURCE_REF;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import java.util.List;
import li.strolch.agent.api.ResourceMap;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.parameter.Parameter;
@ -34,18 +31,7 @@ public class TransactionalResourceMap extends TransactionalElementMap<Resource>
@Override
protected void assertIsRefParam(Parameter<?> refP) {
String interpretation = refP.getInterpretation();
if (!interpretation.equals(INTERPRETATION_RESOURCE_REF)) {
String msg = "{0} is not an Resource reference as its interpretation is not {1} it is {2}"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator(), INTERPRETATION_RESOURCE_REF,
interpretation));
}
if (refP.getUom().equals(UOM_NONE)) {
String msg = "{0} is not an Resource reference as its UOM is not set to a type!"; //$NON-NLS-1$
throw new StrolchException(MessageFormat.format(msg, refP.getLocator()));
}
ElementMapHelpers.assertIsRefParam(INTERPRETATION_RESOURCE_REF, refP);
}
@Override

View File

@ -18,6 +18,7 @@ package li.strolch.agent.impl;
import java.io.File;
import java.text.MessageFormat;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.OrderMap;
@ -42,6 +43,7 @@ public class TransientRealm extends InternalStrolchRealm {
private ResourceMap resourceMap;
private OrderMap orderMap;
private ActivityMap activityMap;
private AuditTrail auditTrail;
private PersistenceHandler persistenceHandler;
@ -78,6 +80,11 @@ public class TransientRealm extends InternalStrolchRealm {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
return this.auditTrail;
@ -99,6 +106,7 @@ public class TransientRealm extends InternalStrolchRealm {
this.persistenceHandler = new InMemoryPersistence(container.getPrivilegeHandler());
this.resourceMap = new TransactionalResourceMap();
this.orderMap = new TransactionalOrderMap();
this.activityMap = new TransactionalActivityMap();
if (isAuditTrailEnabled()) {
this.auditTrail = new TransactionalAuditTrail();
@ -126,6 +134,7 @@ public class TransientRealm extends InternalStrolchRealm {
"Loading XML Model file {0} for realm {1} took {2}.", this.modelFile.getName(), getRealm(), durationS)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Orders", statistics.nrOfOrders)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Resources", statistics.nrOfResources)); //$NON-NLS-1$
logger.info(MessageFormat.format("Loaded {0} Activities", statistics.nrOfActivities)); //$NON-NLS-1$
}
@Override

View File

@ -23,6 +23,7 @@ import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.ObserverHandler;
import li.strolch.agent.api.OrderMap;
@ -30,12 +31,14 @@ import li.strolch.agent.api.ResourceMap;
import li.strolch.agent.api.StrolchAgent;
import li.strolch.agent.api.StrolchLockException;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.agent.impl.AuditingActivityMap;
import li.strolch.agent.impl.AuditingAuditMapFacade;
import li.strolch.agent.impl.AuditingOrderMap;
import li.strolch.agent.impl.AuditingResourceMap;
import li.strolch.agent.impl.InternalStrolchRealm;
import li.strolch.exception.StrolchAccessDeniedException;
import li.strolch.exception.StrolchException;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.GroupedParameterizedElement;
import li.strolch.model.Locator;
import li.strolch.model.Order;
@ -46,6 +49,7 @@ import li.strolch.model.ResourceVisitor;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
@ -54,12 +58,14 @@ import li.strolch.model.audit.NoStrategyAuditVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ResourceQuery;
import li.strolch.model.query.StrolchQuery;
import li.strolch.model.timedstate.StrolchTimedState;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.visitor.ElementTypeVisitor;
import li.strolch.model.visitor.NoStrategyActivityVisitor;
import li.strolch.model.visitor.NoStrategyOrderVisitor;
import li.strolch.model.visitor.NoStrategyResourceVisitor;
import li.strolch.runtime.StrolchConstants;
@ -95,6 +101,7 @@ public abstract class AbstractTransaction implements StrolchTransaction {
private AuditingOrderMap orderMap;
private AuditingResourceMap resourceMap;
private AuditingActivityMap activityMap;
private AuditingAuditMapFacade auditTrail;
private String action;
@ -257,6 +264,15 @@ public abstract class AbstractTransaction implements StrolchTransaction {
return this.orderMap;
}
@Override
public ActivityMap getActivityMap() {
if (this.activityMap == null) {
this.activityMap = new AuditingActivityMap(this.realm.getActivityMap(),
this.realm.isAuditTrailEnabledForRead());
}
return this.activityMap;
}
@Override
public AuditTrail getAuditTrail() {
if (this.auditTrail == null) {
@ -298,6 +314,18 @@ public abstract class AbstractTransaction implements StrolchTransaction {
assertQueryAllowed(query);
return getResourceMap().doQuery(this, query, resourceVisitor);
}
@Override
public List<Activity> doQuery(ActivityQuery query) {
assertQueryAllowed(query);
return getActivityMap().doQuery(this, query, new NoStrategyActivityVisitor());
}
@Override
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor) {
assertQueryAllowed(query);
return getActivityMap().doQuery(this, query, activityVisitor);
}
@Override
public List<Audit> doQuery(AuditQuery query) {
@ -486,7 +514,78 @@ public abstract class AbstractTransaction implements StrolchTransaction {
DBC.PRE.assertNotNull("refP", refP);
return getResourceMap().getBy(this, refP, assertExists);
}
@Override
public Activity getActivityBy(String type, String id) {
return getActivityBy(type, id, false);
}
@Override
public Activity getActivityBy(String type, String id, boolean assertExists) throws StrolchException {
Activity activity = getActivityMap().getBy(this, type, id);
if (assertExists && activity == null) {
String msg = "No Activity exists with the id {0} with type {1}";
throw new StrolchException(MessageFormat.format(msg, id, type));
}
return activity;
}
@Override
public Activity getActivityBy(StringParameter refP) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityBy(refP, false);
}
@Override
public Activity getActivityBy(StringParameter refP, boolean assertExists) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityMap().getBy(this, refP, assertExists);
}
@Override
public List<Activity> getActivitiesBy(StringListParameter refP) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityMap().getBy(this, refP, false);
}
@Override
public List<Activity> getActivitiesBy(StringListParameter refP, boolean assertExists) throws StrolchException {
DBC.PRE.assertNotNull("refP", refP);
return getActivityMap().getBy(this, refP, assertExists);
}
@Override
public void flush() {
try {

View File

@ -0,0 +1,30 @@
/*
* 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.persistence.api;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityDao extends StrolchDao<Activity> {
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor);
}

View File

@ -15,12 +15,14 @@
*/
package li.strolch.persistence.api;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.Audit;
import ch.eitchnet.privilege.model.Certificate;
@ -68,6 +70,18 @@ public interface PersistenceHandler {
*/
public ResourceDao getResourceDao(StrolchTransaction tx);
/**
* Returns the {@link ActivityDao} for the given transaction. Use this only if you want to bypass certain
* transaction features. Accessing {@link Activity Activities} should be done through the {@link ActivityMap}
* accessed from the transaction
*
* @param tx
* the transaction for which the {@link ActivityDao} is to be returned
*
* @return the {@link ActivityDao}
*/
public ActivityDao getActivityDao(StrolchTransaction tx);
/**
* Returns the {@link AuditDao} for the given transaction. Use this only if you want to bypass certain transaction
* features. Accessing {@link Audit Audits} should be done through the {@link AuditTrail} accessed from the

View File

@ -17,6 +17,7 @@ package li.strolch.persistence.api;
import java.util.List;
import li.strolch.agent.api.ActivityMap;
import li.strolch.agent.api.AuditTrail;
import li.strolch.agent.api.OrderMap;
import li.strolch.agent.api.ResourceMap;
@ -25,6 +26,7 @@ import li.strolch.agent.api.StrolchLockException;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.agent.impl.DataStoreMode;
import li.strolch.exception.StrolchException;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.Locator;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
@ -34,6 +36,7 @@ import li.strolch.model.ResourceVisitor;
import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.activity.Activity;
import li.strolch.model.audit.AccessType;
import li.strolch.model.audit.Audit;
import li.strolch.model.audit.AuditQuery;
@ -41,6 +44,7 @@ import li.strolch.model.audit.AuditVisitor;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.query.ActivityQuery;
import li.strolch.model.query.OrderQuery;
import li.strolch.model.query.ResourceQuery;
import li.strolch.runtime.StrolchConstants;
@ -123,6 +127,13 @@ public interface StrolchTransaction extends AutoCloseable {
*/
public OrderMap getOrderMap();
/**
* Returns a reference to the {@link ActivityMap} for the {@link StrolchRealm} for which this transaction was opened
*
* @return the {@link ActivityMap}
*/
public ActivityMap getActivityMap();
/**
* Returns the {@link PersistenceHandler}. If the {@link StrolchRealm} is not running in
* {@link DataStoreMode#TRANSIENT} mode, then the {@link PersistenceHandler} will be a {@link StrolchComponent},
@ -424,6 +435,42 @@ public interface StrolchTransaction extends AutoCloseable {
*/
public <U> List<U> doQuery(ResourceQuery query, ResourceVisitor<U> resourceVisitor);
/**
* <p>
* Performs the given {@link ActivityQuery} returning the resulting list of {@link Activity Activities}.
* </p>
*
* <p>
* <b>Note:</b> Should the result be mapped to different objects, then use
* {@link #doQuery(ActivityQuery, ActivityVisitor)}
* </p>
*
* @param query
* the query to perform
*
* @return the result list, never null
*/
public List<Activity> doQuery(ActivityQuery query);
/**
* <p>
* Performs the given {@link ActivityQuery} and each returned {@link Activity} is passed through the
* {@link ActivityVisitor} and the return value of the visitor is added to the return list
* </p>
*
* <p>
* This method is intended for situations where the query result should not be {@link Activity} but some other
* object type. For instance in a restful API, the result might have to be mapped to a POJO, thus using this method
* can perform the mapping step for you
* </p>
*
* @param query
* the query to perform
*
* @return the result list of elements as returned by the {@link ActivityVisitor}, never null
*/
public <U> List<U> doQuery(ActivityQuery query, ActivityVisitor<U> activityVisitor);
/**
* <p>
* Performs the given {@link AuditQuery} returning the resulting list of {@link Audit Audits}.
@ -670,13 +717,111 @@ public interface StrolchTransaction extends AutoCloseable {
* if true, and resource does not exist, then a {@link StrolchException} is thrown
*
* @return the resources referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing resources are not returned!
* missing resources are not returned unless <code>assertExists</code> is true
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Resource> getResourcesBy(StringListParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Activity} with the given type and id, or null if it does not exist
*
* @param type
* the type of the {@link Activity}
* @param id
* the id of the {@link Activity}
*
* @return the {@link Activity} with the given type and id, or null if it does not exist
*/
public Activity getActivityBy(String type, String id);
/**
* Returns the {@link Activity} with the given type and id, or null if it does not exist
*
* @param type
* the type of the {@link Activity}
* @param id
* the id of the {@link Activity}
* @param assertExists
* if true, and activity does not exist, then a {@link StrolchException} is thrown
*
* @return the {@link Activity} with the given type and id, or null if it does not exist
*
* @throws StrolchException
* if the activity does not exist, and assertExists is true
*/
public Activity getActivityBy(String type, String id, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Activity} which is referenced by the given {@link StringParameter}. A reference
* {@link Parameter} must have its interpretation set to {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and
* the UOM must be set to the activity's type and the value is the id of the activity
*
* @param refP
* the {@link StringParameter} which references an {@link Activity}
*
* @return the activity referenced by the parameter, or null if it does not exist
*
* @throws StrolchException
* if the {@link StringParameter} is not a properly configured as a reference parameter
*/
public Activity getActivityBy(StringParameter refP) throws StrolchException;
/**
* Returns the {@link Activity} which is referenced by the given {@link StringParameter}. A reference
* {@link Parameter} must have its interpretation set to {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and
* the UOM must be set to the activity's type and the value is the id of the activity
*
* @param refP
* the {@link StringParameter} which references an {@link Activity}
* @param assertExists
* if true, and activity does not exist, then a {@link StrolchException} is thrown
*
* @return the activity referenced by the parameter, or null if it does not exist
*
* @throws StrolchException
* if the {@link StringParameter} is not a properly configured as a reference parameter, or if the
* activity does not exist, and assertExists is true
*/
public Activity getActivityBy(StringParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns all {@link Activity Activities} which are referenced by the given {@link StringListParameter}. A
* reference {@link Parameter} must have its interpretation set to
* {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and the UOM must be set to the activity's type and the value
* is the id of the activity
*
* @param refP
* the {@link StringListParameter} which references a list of {@link Activity Activities}
*
* @return the activities referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing activities are not returned!
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Activity> getActivitiesBy(StringListParameter refP) throws StrolchException;
/**
* Returns all {@link Activity Activities} which are referenced by the given {@link StringListParameter}. A
* reference {@link Parameter} must have its interpretation set to
* {@link StrolchConstants#INTERPRETATION_ACTIVITY_REF} and the UOM must be set to the activity's type and the value
* is the id of the activity
*
* @param refP
* the {@link StringListParameter} which references a list of {@link Activity Activities}
* @param assertExists
* if true, and activity does not exist, then a {@link StrolchException} is thrown
*
* @return the activities referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any
* missing activities are not returned unless <code>assertExists</code> is true
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter
*/
public List<Activity> getActivitiesBy(StringListParameter refP, boolean assertExists) throws StrolchException;
/**
* Returns the {@link Order} with the given type and id, or null if it does not exist
*
@ -766,7 +911,7 @@ public interface StrolchTransaction extends AutoCloseable {
* if true, and order does not exist, then a {@link StrolchException} is thrown
*
* @return the orders referenced by the parameter, or the empty list if they do not exist. <b>Note:</b> Any missing
* orders are not returned!
* orders are not returned unless <code>assertExists</code> is true
*
* @throws StrolchException
* if the {@link StringListParameter} is not a properly configured as a reference parameter

View File

@ -0,0 +1,35 @@
/*
* 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.persistence.inmemory;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.runtime.query.inmemory.InMemoryActivityQueryVisitor;
import li.strolch.runtime.query.inmemory.InMemoryQuery;
public class InMemoryActivityDao extends InMemoryDao<Activity> implements ActivityDao {
@Override
public <U> List<U> doQuery(ActivityQuery activityQuery, ActivityVisitor<U> activityVisitor) {
InMemoryActivityQueryVisitor visitor = new InMemoryActivityQueryVisitor();
InMemoryQuery<Activity, U> query = visitor.visit(activityQuery, activityVisitor);
return query.doQuery(this);
}
}

View File

@ -19,6 +19,7 @@ import java.util.HashMap;
import java.util.Map;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
@ -54,6 +55,12 @@ public class InMemoryPersistence implements PersistenceHandler {
return daoCache.getResourceDao();
}
@Override
public ActivityDao getActivityDao(StrolchTransaction tx) {
DaoCache daoCache = getDaoCache(tx);
return daoCache.getActivityDao();
}
@Override
public AuditDao getAuditDao(StrolchTransaction tx) {
DaoCache daoCache = getDaoCache(tx);
@ -68,7 +75,8 @@ public class InMemoryPersistence implements PersistenceHandler {
private synchronized DaoCache getDaoCache(StrolchTransaction tx) {
DaoCache daoCache = this.daoCache.get(tx.getRealmName());
if (daoCache == null) {
daoCache = new DaoCache(new InMemoryOrderDao(), new InMemoryResourceDao(), new InMemoryAuditDao());
daoCache = new DaoCache(new InMemoryOrderDao(), new InMemoryResourceDao(), new InMemoryActivityDao(),
new InMemoryAuditDao());
this.daoCache.put(tx.getRealmName(), daoCache);
}
return daoCache;
@ -77,11 +85,13 @@ public class InMemoryPersistence implements PersistenceHandler {
private class DaoCache {
private OrderDao orderDao;
private ResourceDao resourceDao;
private ActivityDao activityDao;
private AuditDao auditDao;
public DaoCache(OrderDao orderDao, ResourceDao resourceDao, AuditDao auditDao) {
public DaoCache(OrderDao orderDao, ResourceDao resourceDao, ActivityDao activityDao, AuditDao auditDao) {
this.orderDao = orderDao;
this.resourceDao = resourceDao;
this.activityDao = activityDao;
this.auditDao = auditDao;
}
@ -96,5 +106,9 @@ public class InMemoryPersistence implements PersistenceHandler {
public AuditDao getAuditDao() {
return this.auditDao;
}
public ActivityDao getActivityDao() {
return this.activityDao;
}
}
}

View File

@ -18,6 +18,7 @@ package li.strolch.persistence.inmemory;
import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.persistence.api.ActivityDao;
import li.strolch.persistence.api.AuditDao;
import li.strolch.persistence.api.OrderDao;
import li.strolch.persistence.api.PersistenceHandler;
@ -62,6 +63,11 @@ public class InMemoryPersistenceHandler extends StrolchComponent implements Pers
return this.persistence.getResourceDao(tx);
}
@Override
public ActivityDao getActivityDao(StrolchTransaction tx) {
return this.persistence.getActivityDao(tx);
}
@Override
public AuditDao getAuditDao(StrolchTransaction tx) {
return this.persistence.getAuditDao(tx);

View File

@ -53,6 +53,11 @@ public class StrolchConstants {
*/
public static final String INTERPRETATION_ORDER_REF = StrolchModelConstants.INTERPRETATION_ORDER_REF;
/**
* @see StrolchModelConstants#INTERPRETATION_ACTIVITY_REF
*/
public static final String INTERPRETATION_ACTIVITY_REF = StrolchModelConstants.INTERPRETATION_ACTIVITY_REF;
public static String makeRealmKey(String realmName, String key) {
String realmKey = key;
if (!realmName.equals(DEFAULT_REALM))

View File

@ -0,0 +1,31 @@
/*
* 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.runtime.query.inmemory;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityTypeNavigator extends StrolchTypeNavigator<Activity> {
/**
* @param type
*/
public ActivityTypeNavigator(String type) {
super(type);
}
}

View File

@ -0,0 +1,65 @@
/*
* 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.runtime.query.inmemory;
import java.util.List;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import li.strolch.model.query.ActivityQuery;
import li.strolch.model.query.ActivityQueryVisitor;
import li.strolch.model.query.StrolchTypeNavigation;
import li.strolch.persistence.api.ActivityDao;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class InMemoryActivityQueryVisitor extends InMemoryQueryVisitor<Activity, ActivityDao> implements
ActivityQueryVisitor {
public InMemoryActivityQueryVisitor() {
super();
}
@Override
protected InMemoryQueryVisitor<Activity, ActivityDao> newInstance() {
return new InMemoryActivityQueryVisitor();
}
public <U> InMemoryQuery<Activity, U> visit(ActivityQuery activityQuery, ActivityVisitor<U> activityVisitor) {
DBC.PRE.assertNotNull("ActivityVisitor may not be null!", activityVisitor); //$NON-NLS-1$
activityQuery.accept(this);
Navigator<Activity> navigator = getNavigator();
if (navigator == null) {
String msg = "Query is missing a navigation!"; //$NON-NLS-1$
throw new QueryException(msg);
}
List<Selector<Activity>> selectors = getSelectors();
if (selectors.isEmpty())
return new InMemoryQuery<>(navigator, null, activityVisitor);
DBC.PRE.assertTrue("Invalid query as it may only contain one selector!", selectors.size() == 1); //$NON-NLS-1$
return new InMemoryQuery<>(navigator, selectors.get(0), activityVisitor);
}
@Override
public void visit(StrolchTypeNavigation navigation) {
setNavigator(new ActivityTypeNavigator(navigation.getType()));
}
}

View File

@ -10,12 +10,12 @@
<Parameter Id="@param2" Name="Float Param" Type="Float" Value="44.3" />
<Parameter Id="@param1" Name="Boolean Param" Type="Boolean" Value="true" />
</ParameterBag>
<TimedState Id="@booleanState" Name="Boolean State" Type="BooleanState">
<TimedState Id="@booleanState" Name="Boolean State" Type="Boolean">
<Value Time="1970-01-01T00:00:00.000+01:00" Value="false" />
<Value Time="1970-01-01T00:01:00.000+01:00" Value="true" />
<Value Time="1970-01-01T00:02:00.000+01:00" Value="false" />
</TimedState>
<TimedState Id="@integerState" Name="Integer State" Type="IntegerState">
<TimedState Id="@integerState" Name="Integer State" Type="Integer">
<Value Time="1970-01-01T00:00:00.000+01:00" Value="1" />
<Value Time="1970-01-01T00:01:00.000+01:00" Value="2" />
<Value Time="1970-01-01T00:02:00.000+01:00" Value="3" />
@ -23,7 +23,7 @@
<Value Time="1970-01-01T00:04:00.000+01:00" Value="1" />
<Value Time="1970-01-01T00:05:00.000+01:00" Value="0" />
</TimedState>
<TimedState Id="@floatState" Name="Float State" Type="FloatState">
<TimedState Id="@floatState" Name="Float State" Type="Float">
<Value Time="1970-01-01T00:00:00.000+01:00" Value="1.1" />
<Value Time="1970-01-01T00:01:00.000+01:00" Value="2.2" />
<Value Time="1970-01-01T00:02:00.000+01:00" Value="3.3" />
@ -31,7 +31,7 @@
<Value Time="1970-01-01T00:04:00.000+01:00" Value="1.1" />
<Value Time="1970-01-01T00:05:00.000+01:00" Value="0.0" />
</TimedState>
<TimedState Id="@stringSetState" Name="StringSet State" Type="StringSetState">
<TimedState Id="@stringSetState" Name="StringSet State" Type="StringSet">
<Value Time="1970-01-01T00:00:00.000+01:00" Value="foo" />
<Value Time="1970-01-01T00:01:00.000+01:00" Value="foo, bar" />
<Value Time="1970-01-01T00:02:00.000+01:00" Value="bar" />

View File

@ -19,9 +19,6 @@ import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Locator.LocatorBuilder;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -36,10 +33,10 @@ public abstract class AbstractStrolchElement implements StrolchElement {
protected String name;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
public AbstractStrolchElement() {
//
super();
}
/**
@ -114,31 +111,6 @@ public abstract class AbstractStrolchElement implements StrolchElement {
clone.setName(getName());
}
protected void fillElement(Element element) {
element.setAttribute(Tags.ID, getId());
element.setAttribute(Tags.NAME, getName());
element.setAttribute(Tags.TYPE, getType());
}
/**
* Builds the fields of this {@link StrolchElement} from a {@link Element}
*
* @param element
*/
protected void fromDom(Element element) {
String id = element.getAttribute(Tags.ID);
String name = element.getAttribute(Tags.NAME);
if (id != null && name != null) {
setId(id);
setName(name);
} else {
String msg = "Check the values of the element: {0} either id or name attribute is null!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, element.getNodeName());
throw new StrolchException(msg);
}
}
@Override
public int hashCode() {
final int prime = 31;

View File

@ -0,0 +1,29 @@
/*
* 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;
import li.strolch.model.activity.Activity;
import li.strolch.model.visitor.StrolchElementVisitor;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
* @param <U>
*/
public interface ActivityVisitor<U> extends StrolchElementVisitor<Activity, U> {
@Override
public U visit(Activity element);
}

View File

@ -25,10 +25,6 @@ import java.util.Set;
import li.strolch.exception.StrolchException;
import li.strolch.exception.StrolchModelException;
import li.strolch.model.parameter.Parameter;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -42,10 +38,10 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
protected String type;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
protected GroupedParameterizedElement() {
//
super();
}
/**
@ -291,32 +287,6 @@ public abstract class GroupedParameterizedElement extends AbstractStrolchElement
return new HashSet<String>(this.parameterBagMap.keySet());
}
@Override
public void fromDom(Element element) {
super.fromDom(element);
String type = element.getAttribute(Tags.TYPE);
setType(type);
NodeList bags = element.getElementsByTagName(Tags.PARAMETER_BAG);
for (int i = 0; i < bags.getLength(); i++) {
Element bagElement = (Element) bags.item(i);
ParameterBag bag = new ParameterBag(bagElement);
addParameterBag(bag);
}
}
@Override
protected void fillElement(Element element) {
super.fillElement(element);
if (this.parameterBagMap != null) {
for (ParameterBag bag : this.parameterBagMap.values()) {
element.appendChild(bag.toDom(element.getOwnerDocument()));
}
}
}
/**
* Fills {@link GroupedParameterizedElement} properties of this clone
*

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

@ -49,6 +49,9 @@ public class ModelStatistics {
@XmlAttribute(name = "nrOfOrders")
public long nrOfOrders;
@XmlAttribute(name = "nrOfActivities")
public long nrOfActivities;
/**
* @return the nrOfOrders
*/
@ -70,6 +73,24 @@ public class ModelStatistics {
return this.nrOfOrders + this.nrOfResources;
}
/**
* @return the nrOfActivities
*/
public long getNrOfActivities() {
return this.nrOfActivities;
}
/**
* Adds the statistics of the other statistics to this statistics instance
*
* @param statistics
*/
public void add(ModelStatistics statistics) {
this.nrOfOrders += statistics.nrOfOrders;
this.nrOfResources += statistics.nrOfResources;
this.nrOfActivities += statistics.nrOfActivities;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
@ -81,6 +102,8 @@ public class ModelStatistics {
builder.append(this.nrOfResources);
builder.append(", nrOfOrders=");
builder.append(this.nrOfOrders);
builder.append(", nrOfActivities=");
builder.append(this.nrOfActivities);
builder.append("]");
return builder.toString();
}

View File

@ -19,11 +19,6 @@ import java.util.Date;
import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
@ -44,10 +39,10 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
private State state;
/**
* Empty constructor
* Empty constructor - for marshalling only!
*/
public Order() {
//
super();
}
/**
@ -80,30 +75,6 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
setDate(date);
}
/**
* DOM Constructor
*
* @param element
*/
public Order(Element element) {
super.fromDom(element);
String date = element.getAttribute(Tags.DATE);
String state = element.getAttribute(Tags.STATE);
if (StringHelper.isEmpty(date)) {
setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse("-")); //$NON-NLS-1$
} else {
setDate(ISO8601FormatFactory.getInstance().getDateFormat().parse(date));
}
if (state == null || state.isEmpty()) {
setState(State.CREATED);
} else {
setState(State.valueOf(state));
}
}
/**
* @return the date
*/
@ -134,18 +105,6 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
this.state = state;
}
@Override
public Element toDom(Document doc) {
Element orderElement = doc.createElement(Tags.ORDER);
fillElement(orderElement);
orderElement.setAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(this.date));
orderElement.setAttribute(Tags.STATE, this.state.toString());
return orderElement;
}
@Override
public Order getClone() {
Order clone = new Order();
@ -179,7 +138,7 @@ public class Order extends GroupedParameterizedElement implements StrolchRootEle
public Order getRootElement() {
return this;
}
@Override
public boolean isRootElement() {
return true;

View File

@ -17,9 +17,6 @@ package li.strolch.model;
import li.strolch.model.Locator.LocatorBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -45,15 +42,6 @@ public class ParameterBag extends ParameterizedElement {
super(id, name, type);
}
/**
* DOM Constructor
*
* @param bagElement
*/
public ParameterBag(Element bagElement) {
super.fromDom(bagElement);
}
@Override
public ParameterBag getClone() {
ParameterBag clone = new ParameterBag();
@ -68,16 +56,6 @@ public class ParameterBag extends ParameterizedElement {
lb.append(this.id);
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.PARAMETER_BAG);
fillElement(element);
return element;
}
@Override
public boolean isRootElement() {
return false;

View File

@ -26,23 +26,7 @@ import java.util.Set;
import li.strolch.exception.StrolchException;
import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.DurationParameter;
import li.strolch.model.parameter.FloatListParameter;
import li.strolch.model.parameter.FloatParameter;
import li.strolch.model.parameter.IntegerListParameter;
import li.strolch.model.parameter.IntegerParameter;
import li.strolch.model.parameter.LongListParameter;
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 org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.dbc.DBC;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -206,75 +190,6 @@ public abstract class ParameterizedElement extends AbstractStrolchElement {
return lb.build();
}
// TODO remove the whole fromDom methods from strolch model - we want to use the visitor pattern only!
@Override
protected void fromDom(Element element) {
super.fromDom(element);
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);
DBC.PRE.assertNotEmpty("Type must be set on Parameter for bag with id " + this.id, paramtype);
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(DurationParameter.TYPE)) {
DurationParameter param = new DurationParameter(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 if (paramtype.equals(IntegerListParameter.TYPE)) {
IntegerListParameter param = new IntegerListParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(FloatListParameter.TYPE)) {
FloatListParameter param = new FloatListParameter(paramElement);
addParameter(param);
} else if (paramtype.equals(LongListParameter.TYPE)) {
LongListParameter param = new LongListParameter(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);
}
}
}
@Override
protected void fillElement(Element element) {
super.fillElement(element);
if (this.parameterMap != null) {
for (Parameter<?> parameter : this.parameterMap.values()) {
element.appendChild(parameter.toDom(element.getOwnerDocument()));
}
}
}
@Override
protected void fillClone(StrolchElement clone) {
super.fillClone(clone);

View File

@ -15,7 +15,6 @@
*/
package li.strolch.model;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@ -24,22 +23,11 @@ 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.timevalue.IValue;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.dbc.DBC;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -50,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();
}
/**
@ -67,41 +55,6 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
super(id, name, type);
}
/**
* DOM Constructor
*
* @param element
*/
public Resource(Element element) {
super.fromDom(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);
DBC.PRE.assertNotEmpty("Type must be set on TimedState for resource with id " + this.id, typeS);
if (typeS.equals(FloatTimedState.TYPE)) {
FloatTimedState timedState = new FloatTimedState(timedStateElem);
addTimedState(timedState);
} else if (typeS.equals(IntegerTimedState.TYPE)) {
IntegerTimedState timedState = new IntegerTimedState(timedStateElem);
addTimedState(timedState);
} else if (typeS.equals(BooleanTimedState.TYPE)) {
BooleanTimedState timedState = new BooleanTimedState(timedStateElem);
addTimedState(timedState);
} else if (typeS.equals(StringSetTimedState.TYPE)) {
StringSetTimedState 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);
}
}
}
@SuppressWarnings("unchecked")
public void addTimedState(StrolchTimedState<?> strolchTimedState) {
if (this.timedStateMap == null) {
@ -150,26 +103,9 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
return this.timedStateMap != null && this.timedStateMap.containsKey(id);
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.RESOURCE);
fillElement(element);
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);
if (this.timedStateMap != null) {
@ -202,7 +138,7 @@ public class Resource extends GroupedParameterizedElement implements StrolchRoot
public Resource getRootElement() {
return this;
}
@Override
public boolean isRootElement() {
return true;

View File

@ -17,9 +17,6 @@ package li.strolch.model;
import java.io.Serializable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -76,17 +73,6 @@ public interface StrolchElement extends Serializable, Comparable<StrolchElement>
*/
public long getDbid();
/**
* Returns an {@link Element} object which is an XML representation of this object
*
* @param doc
* the document to which this element is being written. The client must not append to the document, the
* caller will perform this as needed
*
* @return
*/
public Element toDom(Document doc);
/**
* Returns the type of this {@link StrolchElement}
*

View File

@ -40,6 +40,12 @@ public class StrolchModelConstants {
*/
public static final String INTERPRETATION_ORDER_REF = "Order-Ref"; //$NON-NLS-1$
/**
* This interpretation value indicates that the value of the {@link Parameter} should be understood as a reference
* to an {@link Activity}
*/
public static final String INTERPRETATION_ACTIVITY_REF = "Activity-Ref"; //$NON-NLS-1$
/**
* This interpretation value indicates that the {@link Parameter} has no defined interpretation
*/

View File

@ -0,0 +1,378 @@
/*
* 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;
import java.text.MessageFormat;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.DurationParameter;
import li.strolch.model.parameter.FloatListParameter;
import li.strolch.model.parameter.FloatParameter;
import li.strolch.model.parameter.IntegerListParameter;
import li.strolch.model.parameter.IntegerParameter;
import li.strolch.model.parameter.LongListParameter;
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 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.timedstate.TimedState;
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.FloatValue;
import li.strolch.model.timevalue.impl.IntegerValue;
import li.strolch.model.timevalue.impl.StringSetValue;
public enum StrolchValueType {
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* <li>{@link TimedState}</li>
* <li>{@link IValue}</li>
* <li>{@link IValueChange}</li>
* </ul>
*/
BOOLEAN("Boolean") {
@Override
public Parameter<?> parameterInstance() {
return new BooleanParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
return new BooleanTimedState();
}
@Override
public IValue<?> valueInstance(String valueAsString) {
return new BooleanValue(valueAsString);
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* <li>{@link TimedState}</li>
* <li>{@link IValue}</li>
* <li>{@link IValueChange}</li>
* </ul>
*/
INTEGER("Integer") {
@Override
public Parameter<?> parameterInstance() {
return new IntegerParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
return new IntegerTimedState();
}
@Override
public IValue<?> valueInstance(String valueAsString) {
return new IntegerValue(valueAsString);
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* <li>{@link TimedState}</li>
* <li>{@link IValue}</li>
* <li>{@link IValueChange}</li>
* </ul>
*/
FLOAT("Float") {
@Override
public Parameter<?> parameterInstance() {
return new FloatParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
return new FloatTimedState();
}
@Override
public IValue<?> valueInstance(String valueAsString) {
return new FloatValue(valueAsString);
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
LONG("Long") {
@Override
public Parameter<?> parameterInstance() {
return new LongParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
STRING("String") {
@Override
public Parameter<?> parameterInstance() {
return new StringParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
DATE("Date") {
@Override
public Parameter<?> parameterInstance() {
return new DateParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
DURATION("Duration") {
@Override
public Parameter<?> parameterInstance() {
return new DurationParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
FLOAT_LIST("FloatList") {
@Override
public Parameter<?> parameterInstance() {
return new FloatListParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
INTEGER_LIST("IntegerList") {
@Override
public Parameter<?> parameterInstance() {
return new IntegerListParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
LONG_LIST("LongList") {
@Override
public Parameter<?> parameterInstance() {
return new LongListParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link Parameter}</li>
* </ul>
*/
STRING_LIST("StringList") {
@Override
public Parameter<?> parameterInstance() {
return new StringListParameter();
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"TimeStates of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public IValue<?> valueInstance(String valueAsString) {
throw new UnsupportedOperationException(MessageFormat.format(
"Values of type {0} are not supported!", getType())); //$NON-NLS-1$
}
},
/**
* Can be used for:<br />
* <ul>
* <li>{@link TimedState}</li>
* <li>{@link IValue}</li>
* <li>{@link IValueChange}</li>
* </ul>
*/
STRING_SET("StringSet") {
@Override
public Parameter<?> parameterInstance() {
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", getType())); //$NON-NLS-1$
}
@Override
public StrolchTimedState<? extends IValue<?>> timedStateInstance() {
return new StringSetTimedState();
}
@Override
public IValue<?> valueInstance(String valueAsString) {
return new StringSetValue(valueAsString);
}
};
private String type;
private StrolchValueType(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
public static StrolchValueType parse(String value) {
// TODO this is for backwards compatibility where we still had States of type BooleanState instead of Boolean
String strippedValue = value.replace("State", "");
for (StrolchValueType type : StrolchValueType.values()) {
if (type.type.equals(strippedValue))
return type;
}
throw new IllegalArgumentException("Type " + value + " does not exist!");
}
public abstract Parameter<?> parameterInstance();
public abstract StrolchTimedState<? extends IValue<?>> timedStateInstance();
public abstract IValue<?> valueInstance(String valueAsString);
}

View File

@ -41,19 +41,17 @@ public class Tags {
public static final String FILE = "file";
public static final String BAG = "Bag";
public static final String AUDIT = "Audit";
public static final String ACTIVITY = "Activity";
public static final String ACTION = "Action";
public static final String START = "Start";
public static final String END = "End";
public static final String VALUE_CHANGE = "ValueChange";
public static final String VALUE_CLASS = "Class";
public static final String RESOURCE_ID = "ResourceId";
public static final String RESOURCE_TYPE = "ResourceType";
public static final String STATE_ID = "StateId";
public class Audit {
public static class Audit {
public static final String ID = Tags.ID;
public static final String USERNAME = "Username";

View File

@ -18,6 +18,8 @@ package li.strolch.model.activity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import li.strolch.model.GroupedParameterizedElement;
@ -25,19 +27,14 @@ 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.Tags;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* An {@link Action} represents a single step within an {@link Activity}, that
* is, one that is not further decomposed within the {@link Activity}. A
* {@link Activity} applies {@link IValueChange} objects at the start and end
* time of the {@link Activity}.
* An {@link Action} represents a single step within an {@link Activity}, that is, one that is not further decomposed
* within the {@link Activity}. A {@link Activity} applies {@link IValueChange} objects at the start and end time of the
* {@link Activity}.
*
* @author Martin Smock <martin.smock@bluewin.ch>
*/
@ -46,20 +43,41 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
protected static final long serialVersionUID = 1L;
protected Activity parent;
protected String resourceId, resourceType;
protected State state = State.CREATED;
protected String resourceId;
protected String resourceType;
protected State state;
protected final List<IValueChange<?>> changes = new ArrayList<>();
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<>();
}
/**
* @return the id of the {@link Resource} the {@link Action} acts on
*/
public String getResourceId() {
return resourceId;
return this.resourceId;
}
/**
@ -73,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;
}
/**
@ -86,8 +105,7 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
}
/**
* @return the type of the <code>Resource</code> this <code>Action</code>
* acts on
* @return the type of the <code>Resource</code> this <code>Action</code> acts on
*/
public String getResourceType() {
return this.resourceType;
@ -101,32 +119,48 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
}
/**
* @param add
* <code>IValueChange</code> to be applied to the
* <code>Resource</code>
* Returns true if this {@link Action} contains any {@link IValueChange changes}, false if not
*
* @return <tt>true</tt> (as specified by {@link Collection#add})
* @return true if this {@link Action} contains any {@link IValueChange changes}, false if not
*/
public boolean addChange(IValueChange<?> change) {
return changes.add(change);
public boolean hasChanges() {
return this.changes != null && !this.changes.isEmpty();
}
/**
* @return the list of <code>IValueChange</code> attached to the
* <code>Action</code> start
* @param add
* <code>IValueChange</code> to be applied to the <code>Resource</code>
*
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public List<IValueChange<?>> getChanges() {
return changes;
public boolean addChange(IValueChange<? extends IValue<?>> change) {
initChanges();
return this.changes.add(change);
}
/**
* @return the list of <code>IValueChange</code> attached to the <code>Action</code> start
*/
public List<IValueChange<? extends IValue<?>>> getChanges() {
if (this.changes == null)
return Collections.emptyList();
return this.changes;
}
public Iterator<IValueChange<? extends IValue<?>>> changesIterator() {
if (this.changes == null)
return Collections.<IValueChange<? extends IValue<?>>> emptyList().iterator();
return this.changes.iterator();
}
@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
@ -135,15 +169,20 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
}
@Override
public StrolchElement getClone() {
Action clone = new Action(getId(), getName(), getType());
clone.setDbid(getDbid());
clone.setResourceId(resourceId);
clone.setResourceType(resourceType);
clone.setState(state);
for (IValueChange<?> change : getChanges()) {
clone.changes.add(change.getClone());
public Action getClone() {
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;
}
@ -185,7 +224,9 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
@Override
public Long getStart() {
Long start = Long.MAX_VALUE;
for (IValueChange<?> change : changes) {
if (this.changes == null)
return start;
for (IValueChange<?> change : this.changes) {
start = Math.min(start, change.getTime());
}
return start;
@ -194,23 +235,11 @@ public class Action extends GroupedParameterizedElement implements IActivityElem
@Override
public Long getEnd() {
Long end = 0L;
for (IValueChange<?> change : changes) {
if (this.changes == null)
return end;
for (IValueChange<?> change : this.changes) {
end = Math.max(end, change.getTime());
}
return end;
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.ACTION);
fillElement(element);
element.setAttribute(Tags.STATE, this.state.toString());
element.setAttribute(Tags.RESOURCE_ID, this.resourceId);
element.setAttribute(Tags.RESOURCE_TYPE, this.resourceType);
for (IValueChange<?> change : changes) {
element.appendChild(change.toDom(doc));
}
return element;
}
}

View File

@ -15,6 +15,7 @@
*/
package li.strolch.model.activity;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
@ -29,13 +30,11 @@ import li.strolch.model.StrolchElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.visitor.StrolchRootElementVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.dbc.DBC;
/**
* Parameterized object grouping a collection of {@link Activity} and
* {@link Action} objects defining the process to be scheduled
* Parameterized object grouping a collection of {@link Activity} and {@link Action} objects defining the process to be
* scheduled
*
* @author Martin Smock <martin.smock@bluewin.ch>
*/
@ -45,27 +44,68 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
protected Activity parent;
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);
}
// use a LinkedHashMap since we will iterate elements in the order added and
// lookup elements by ID
protected Map<String, IActivityElement> elements = new LinkedHashMap<String, IActivityElement>();
private void initElements() {
if (this.elements == null) {
// use a LinkedHashMap since we will iterate elements in the order added and lookup elements by ID
elements = new LinkedHashMap<String, IActivityElement>();
}
}
/**
* add an activity element to the <code>LinkedHashMap</code> of
* <code>IActivityElements</code>
* 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>
*
* @param activityElement
* @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)
throw new StrolchException("Cannot add IActivityElement without id.");
else if (elements.containsKey(id))
throw new StrolchException("Activiy " + getLocator() + " already contains an activity element with id = " + id);
throw new StrolchException("Activiy " + getLocator() + " already contains an activity element with id = "
+ id);
else {
activityElement.setParent(this);
return elements.put(activityElement.getId(), activityElement);
@ -79,28 +119,35 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
* the id of the <code>IActivityElement</code>
* @return IActivityElement
*/
public IActivityElement getElement(String id) {
return elements.get(id);
@SuppressWarnings("unchecked")
public <T extends IActivityElement> T getElement(String id) {
if (this.elements == null)
return null;
return (T) elements.get(id);
}
/**
* @return get the <code>LinkedHashMap</code> of
* <code>IActivityElements</code>
* @return get the <code>LinkedHashMap</code> of <code>IActivityElements</code>
*/
public Map<String, IActivityElement> getElements() {
if (this.elements == null)
return Collections.emptyMap();
return elements;
}
/**
* @return the iterator for entries, which include the id as key and the
* {@link IActivityElement} as value
* @return the iterator for entries, which include the id as key and the {@link IActivityElement} as value
*/
public Iterator<Entry<String, IActivityElement>> elementIterator() {
if (this.elements == null)
return Collections.<String, IActivityElement> emptyMap().entrySet().iterator();
return elements.entrySet().iterator();
}
public Long getStart() {
Long start = Long.MAX_VALUE;
if (this.elements == null)
return start;
Iterator<Entry<String, IActivityElement>> elementIterator = elementIterator();
while (elementIterator.hasNext()) {
IActivityElement action = elementIterator.next().getValue();
@ -111,6 +158,8 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
public Long getEnd() {
Long end = 0L;
if (this.elements == null)
return end;
Iterator<Entry<String, IActivityElement>> elementIterator = elementIterator();
while (elementIterator.hasNext()) {
IActivityElement action = elementIterator.next().getValue();
@ -121,6 +170,8 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
public State getState() {
State state = State.PLANNED;
if (this.elements == null)
return state;
Iterator<Entry<String, IActivityElement>> elementIterator = elementIterator();
while (elementIterator.hasNext()) {
IActivityElement child = elementIterator.next().getValue();
@ -144,18 +195,6 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
locatorBuilder.append(Tags.ACTIVITY).append(getType()).append(getId());
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.ACTIVITY);
fillElement(element);
Iterator<Entry<String, IActivityElement>> elementIterator = elementIterator();
while (elementIterator.hasNext()) {
IActivityElement activityElement = elementIterator.next().getValue();
element.appendChild(activityElement.toDom(doc));
}
return element;
}
@Override
public StrolchElement getParent() {
return parent;
@ -172,12 +211,15 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
}
@Override
public StrolchElement getClone() {
Activity clone = new Activity(id, name, type);
Iterator<Entry<String, IActivityElement>> elementIterator = elementIterator();
while (elementIterator.hasNext()) {
Entry<String, IActivityElement> next = elementIterator.next();
clone.elements.put(next.getKey(), (IActivityElement) next.getValue().getClone());
public Activity getClone() {
Activity clone = new Activity();
super.fillClone(clone);
if (this.elements == null)
return clone;
for (IActivityElement element : this.elements.values()) {
clone.addElement(element.getClone());
}
return clone;
}
@ -210,5 +252,4 @@ public class Activity extends GroupedParameterizedElement implements IActivityEl
public void setParent(Activity activity) {
this.parent = activity;
}
}

View File

@ -22,16 +22,16 @@ import li.strolch.model.StrolchElement;
* Marker for all child elements of {@link Activity} objects
*
* @author Martin Smock <martin.smock@bluewin.ch>
*
*/
public interface IActivityElement extends StrolchElement {
Long getStart();
public Long getStart();
Long getEnd();
public Long getEnd();
State getState();
void setParent(Activity activity);
public State getState();
public void setParent(Activity activity);
public IActivityElement getClone();
}

View File

@ -26,12 +26,7 @@ import li.strolch.model.Locator;
import li.strolch.model.Locator.LocatorBuilder;
import li.strolch.model.ParameterizedElement;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.Tags;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -135,74 +130,6 @@ public abstract class AbstractParameter<T> extends AbstractStrolchElement implem
return false;
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.PARAMETER);
fillElement(element);
element.setAttribute(Tags.VALUE, getValueAsString());
if (!this.interpretation.equals(INTERPRETATION_NONE)) {
element.setAttribute(Tags.INTERPRETATION, this.interpretation);
}
if (!this.uom.equals(UOM_NONE)) {
element.setAttribute(Tags.UOM, this.uom);
}
if (this.hidden) {
element.setAttribute(Tags.HIDDEN, Boolean.toString(this.hidden));
}
if (this.index != 0) {
element.setAttribute(Tags.INDEX, Integer.toString(this.index));
}
return element;
}
@Override
public void fromDom(Element element) {
super.fromDom(element);
String typeS = element.getAttribute(Tags.TYPE);
if (StringHelper.isEmpty(typeS)) {
String msg = "Type must be set on element with id {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, this.id);
throw new StrolchException(msg);
} else if (!typeS.equals(getType())) {
String msg = "{0} must have type {1}, not: {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, getClass().getSimpleName(), getType(), typeS);
throw new StrolchException(msg);
}
String interpretation = element.getAttribute(Tags.INTERPRETATION);
String hidden = element.getAttribute(Tags.HIDDEN);
String uom = element.getAttribute(Tags.UOM);
String index = element.getAttribute(Tags.INDEX);
setInterpretation(interpretation);
setUom(uom);
if (StringHelper.isEmpty(index)) {
this.index = 0;
} else {
this.index = Integer.valueOf(index);
}
if (StringHelper.isEmpty(hidden)) {
setHidden(false);
} else {
if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) {
setHidden(true);
} else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) {
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);
}
}
}
@Override
protected void fillLocator(LocatorBuilder lb) {
lb.append(this.id);

View File

@ -15,14 +15,8 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -30,7 +24,6 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class BooleanParameter extends AbstractParameter<Boolean> {
public static final String TYPE = "Boolean"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
private Boolean value = Boolean.FALSE;
@ -54,23 +47,6 @@ public class BooleanParameter extends AbstractParameter<Boolean> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public BooleanParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
return this.value.toString();
@ -87,9 +63,14 @@ public class BooleanParameter extends AbstractParameter<Boolean> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public String getType() {
return BooleanParameter.TYPE;
return StrolchValueType.BOOLEAN.getType();
}
@Override

View File

@ -15,16 +15,10 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import java.util.Date;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
@ -32,7 +26,6 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
*/
public class DateParameter extends AbstractParameter<Date> {
public static final String TYPE = "Date"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
private Date value;
@ -56,23 +49,6 @@ public class DateParameter extends AbstractParameter<Date> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public DateParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
return ISO8601FormatFactory.getInstance().formatDate(this.value);
@ -89,9 +65,14 @@ public class DateParameter extends AbstractParameter<Date> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public String getType() {
return DateParameter.TYPE;
return StrolchValueType.DATE.getType();
}
@Override

View File

@ -15,15 +15,8 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
@ -31,7 +24,6 @@ import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
*/
public class DurationParameter extends AbstractParameter<Long> {
public static final String TYPE = "Duration"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
private Long value;
@ -55,23 +47,6 @@ public class DurationParameter extends AbstractParameter<Long> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public DurationParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
return ISO8601FormatFactory.getInstance().formatDuration(this.value);
@ -88,9 +63,14 @@ public class DurationParameter extends AbstractParameter<Long> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public String getType() {
return DurationParameter.TYPE;
return StrolchValueType.DURATION.getType();
}
@Override

View File

@ -15,18 +15,13 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class FloatListParameter extends AbstractParameter<List<Double>> implements ListParameter<Double> {
public static final String TYPE = "FloatList"; //$NON-NLS-1$
private static final long serialVersionUID = 1L;
protected List<Double> value;
@ -59,23 +53,6 @@ public class FloatListParameter extends AbstractParameter<List<Double>> implemen
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public FloatListParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
@ -111,6 +88,11 @@ public class FloatListParameter extends AbstractParameter<List<Double>> implemen
this.value.addAll(value);
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public void addValue(Double value) {
this.value.add(value);
@ -133,7 +115,7 @@ public class FloatListParameter extends AbstractParameter<List<Double>> implemen
@Override
public String getType() {
return TYPE;
return StrolchValueType.FLOAT_LIST.getType();
}
@Override

View File

@ -15,23 +15,15 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class FloatParameter extends AbstractParameter<Double> {
public static final String TYPE = "Float"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
private Double value = Double.MAX_VALUE;
@ -56,23 +48,6 @@ public class FloatParameter extends AbstractParameter<Double> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public FloatParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
return Double.toString(this.value);
@ -89,9 +64,14 @@ public class FloatParameter extends AbstractParameter<Double> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public String getType() {
return FloatParameter.TYPE;
return StrolchValueType.FLOAT.getType();
}
@Override

View File

@ -15,18 +15,13 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class IntegerListParameter extends AbstractParameter<List<Integer>> implements ListParameter<Integer> {
public static final String TYPE = "IntegerList"; //$NON-NLS-1$
private static final long serialVersionUID = 1L;
protected List<Integer> value;
@ -59,23 +53,6 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public IntegerListParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
@ -111,6 +88,11 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
this.value.addAll(value);
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public void addValue(Integer value) {
this.value.add(value);
@ -133,7 +115,7 @@ public class IntegerListParameter extends AbstractParameter<List<Integer>> imple
@Override
public String getType() {
return TYPE;
return StrolchValueType.INTEGER_LIST.getType();
}
@Override

View File

@ -15,23 +15,15 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class IntegerParameter extends AbstractParameter<Integer> {
public static final String TYPE = "Integer"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
private Integer value = Integer.MAX_VALUE;
@ -55,26 +47,9 @@ public class IntegerParameter extends AbstractParameter<Integer> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public IntegerParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getType() {
return IntegerParameter.TYPE;
return StrolchValueType.INTEGER.getType();
}
@Override
@ -93,6 +68,11 @@ public class IntegerParameter extends AbstractParameter<Integer> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public IntegerParameter getClone() {
IntegerParameter clone = new IntegerParameter();

View File

@ -15,18 +15,13 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class LongListParameter extends AbstractParameter<List<Long>> implements ListParameter<Long> {
public static final String TYPE = "LongList"; //$NON-NLS-1$
private static final long serialVersionUID = 1L;
protected List<Long> value;
@ -59,23 +53,6 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public LongListParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
@ -111,6 +88,11 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
this.value.addAll(value);
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public void addValue(Long value) {
this.value.add(value);
@ -133,7 +115,7 @@ public class LongListParameter extends AbstractParameter<List<Long>> implements
@Override
public String getType() {
return TYPE;
return StrolchValueType.LONG_LIST.getType();
}
@Override

View File

@ -15,23 +15,15 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
public class LongParameter extends AbstractParameter<Long> {
public static final String TYPE = "Long"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
protected Long value;
@ -55,23 +47,6 @@ public class LongParameter extends AbstractParameter<Long> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public LongParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
return this.value.toString();
@ -88,9 +63,14 @@ public class LongParameter extends AbstractParameter<Long> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public String getType() {
return LongParameter.TYPE;
return StrolchValueType.LONG.getType();
}
@Override

View File

@ -26,12 +26,20 @@ import li.strolch.model.visitor.ParameterVisitor;
public interface Parameter<T> extends StrolchElement {
/**
* the value of the parameter as string
* Returns the value of the parameter as string
*
* @return String
* @return the value as string
*/
public String getValueAsString();
/**
* Set the value of the parameter from a string
*
* @param valueAsString
* the string from which to set the value
*/
public void setValueFromString(String valueAsString);
/**
* the value of the parameter
*

View File

@ -15,18 +15,13 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -34,7 +29,6 @@ import ch.eitchnet.utils.helper.StringHelper;
*/
public class StringListParameter extends AbstractParameter<List<String>> implements ListParameter<String> {
public static final String TYPE = "StringList"; //$NON-NLS-1$
private static final long serialVersionUID = 1L;
protected List<String> value;
@ -59,23 +53,6 @@ public class StringListParameter extends AbstractParameter<List<String>> impleme
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public StringListParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(parseFromString(valueS));
}
@Override
public String getValueAsString() {
if (this.value.isEmpty()) {
@ -111,6 +88,11 @@ public class StringListParameter extends AbstractParameter<List<String>> impleme
this.value.addAll(value);
}
@Override
public void setValueFromString(String valueAsString) {
setValue(parseFromString(valueAsString));
}
@Override
public void addValue(String value) {
this.value.add(value);
@ -133,7 +115,7 @@ public class StringListParameter extends AbstractParameter<List<String>> impleme
@Override
public String getType() {
return TYPE;
return StrolchValueType.STRING_LIST.getType();
}
@Override

View File

@ -15,16 +15,9 @@
*/
package li.strolch.model.parameter;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.Tags;
import li.strolch.model.StrolchValueType;
import li.strolch.model.visitor.ParameterVisitor;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
@ -32,7 +25,6 @@ import ch.eitchnet.utils.helper.StringHelper;
public class StringParameter extends AbstractParameter<String> {
public static final String UNDEFINED_VALUE = "-"; //$NON-NLS-1$
public static final String TYPE = "String"; //$NON-NLS-1$
private static final long serialVersionUID = 0L;
private String value = UNDEFINED_VALUE;
@ -57,26 +49,9 @@ public class StringParameter extends AbstractParameter<String> {
setValue(value);
}
/**
* DOM Constructor
*
* @param element
*/
public StringParameter(Element element) {
super.fromDom(element);
String valueS = element.getAttribute(Tags.VALUE);
if (StringHelper.isEmpty(valueS)) {
String msg = MessageFormat.format("No value defined for {0}", this.id); //$NON-NLS-1$
throw new StrolchException(msg);
}
setValue(valueS);
}
@Override
public String getType() {
return StringParameter.TYPE;
return StrolchValueType.STRING.getType();
}
@Override
@ -95,6 +70,11 @@ public class StringParameter extends AbstractParameter<String> {
this.value = value;
}
@Override
public void setValueFromString(String valueAsString) {
setValue(valueAsString);
}
@Override
public StringParameter getClone() {
StringParameter clone = new StringParameter();

View File

@ -0,0 +1,66 @@
/*
* 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.query;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
/**
* {@link ActivityQuery} is the user API to query {@link Activity Activities} in Strolch. The {@link Navigation} is used
* to navigate to a type of activity on which any further {@link Selection Selections} will be performed. The
* {@link ActivityVisitor} is used to transform the returned object into a domain specific object (if required). This
* mechanism allows you to query e.g. a specific {@link Action} instead of having to return all the elements and then
* performing this transformation.
*
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityQuery extends StrolchElementQuery<ActivityQueryVisitor> {
/**
* @param navigation
* @param elementVisitor
*/
public ActivityQuery(Navigation navigation) {
super(navigation);
}
@Override
public ActivityQuery with(Selection selection) {
super.with(selection);
return this;
}
@Override
public ActivityQuery not(Selection selection) {
super.not(selection);
return this;
}
@Override
public ActivityQuery withAny() {
super.withAny();
return this;
}
public static ActivityQuery query(Navigation navigation) {
return new ActivityQuery(navigation);
}
public static ActivityQuery query(String type) {
return new ActivityQuery(new StrolchTypeNavigation(type));
}
}

View File

@ -0,0 +1,24 @@
/*
* 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.query;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public interface ActivityQueryVisitor extends StrolchRootElementSelectionVisitor, ParameterSelectionVisitor {
// marker interface
}

View File

@ -17,10 +17,6 @@ package li.strolch.model.timedstate;
import static li.strolch.model.StrolchModelConstants.INTERPRETATION_NONE;
import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.text.MessageFormat;
import li.strolch.exception.StrolchException;
import li.strolch.model.AbstractStrolchElement;
import li.strolch.model.Locator;
import li.strolch.model.Locator.LocatorBuilder;
@ -33,10 +29,6 @@ import li.strolch.model.timevalue.ITimeVariable;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import li.strolch.model.visitor.TimedStateVisitor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.StringHelper;
/**
@ -160,72 +152,6 @@ public abstract class AbstractStrolchTimedState<T extends IValue> extends Abstra
return false;
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.PARAMETER);
fillElement(element);
if (!this.interpretation.equals(INTERPRETATION_NONE)) {
element.setAttribute(Tags.INTERPRETATION, this.interpretation);
}
if (!this.uom.equals(UOM_NONE)) {
element.setAttribute(Tags.UOM, this.uom);
}
if (this.hidden) {
element.setAttribute(Tags.HIDDEN, Boolean.toString(this.hidden));
}
if (this.index != 0) {
element.setAttribute(Tags.INDEX, Integer.toString(this.index));
}
return element;
}
@Override
public void fromDom(Element element) {
super.fromDom(element);
String typeS = element.getAttribute(Tags.TYPE);
if (StringHelper.isEmpty(typeS)) {
String msg = "Type must be set on element with id {0}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, this.id);
throw new StrolchException(msg);
} else if (!typeS.equals(getType())) {
String msg = "{0} must have type {1}, not: {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, getClass().getSimpleName(), getType(), typeS);
throw new StrolchException(msg);
}
String interpretation = element.getAttribute(Tags.INTERPRETATION);
String hidden = element.getAttribute(Tags.HIDDEN);
String uom = element.getAttribute(Tags.UOM);
String index = element.getAttribute(Tags.INDEX);
setInterpretation(interpretation);
setUom(uom);
if (StringHelper.isEmpty(index)) {
this.index = 0;
} else {
this.index = Integer.valueOf(index);
}
if (StringHelper.isEmpty(hidden)) {
setHidden(false);
} else {
if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) {
setHidden(true);
} else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) {
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);
}
}
}
@Override
protected void fillLocator(LocatorBuilder lb) {
lb.append(Tags.STATE);

View File

@ -15,19 +15,9 @@
*/
package li.strolch.model.timedstate;
import java.util.Date;
import java.util.SortedSet;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.impl.BooleanValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -35,8 +25,6 @@ public class BooleanTimedState extends AbstractStrolchTimedState<BooleanValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "BooleanState";
public BooleanTimedState() {
super();
}
@ -45,45 +33,14 @@ public class BooleanTimedState extends AbstractStrolchTimedState<BooleanValue> {
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);
String timeS = timeValueElem.getAttribute(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(timeS);
long time = date.getTime();
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, ISO8601FormatFactory.getInstance().formatDate(time));
valueElem.setAttribute(Tags.VALUE, value.getValue().toString());
stateElement.appendChild(valueElem);
}
return stateElement;
public void setStateFromStringAt(Long time, String value) {
getTimeEvolution().setValueAt(time, new BooleanValue(value));
}
@Override
public String getType() {
return TYPE;
return StrolchValueType.BOOLEAN.getType();
}
@Override

View File

@ -15,19 +15,9 @@
*/
package li.strolch.model.timedstate;
import java.util.Date;
import java.util.SortedSet;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.impl.FloatValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -35,8 +25,6 @@ public class FloatTimedState extends AbstractStrolchTimedState<FloatValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "FloatState";
public FloatTimedState() {
super();
}
@ -45,45 +33,14 @@ public class FloatTimedState extends AbstractStrolchTimedState<FloatValue> {
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);
String timeS = timeValueElem.getAttribute(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(timeS);
long time = date.getTime();
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, ISO8601FormatFactory.getInstance().formatDate(time));
valueElem.setAttribute(Tags.VALUE, value.getValue().toString());
stateElement.appendChild(valueElem);
}
return stateElement;
public void setStateFromStringAt(Long time, String value) {
getTimeEvolution().setValueAt(time, new FloatValue(value));
}
@Override
public String getType() {
return TYPE;
return StrolchValueType.FLOAT.getType();
}
@Override

View File

@ -15,19 +15,9 @@
*/
package li.strolch.model.timedstate;
import java.util.Date;
import java.util.SortedSet;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.impl.IntegerValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -35,8 +25,6 @@ public class IntegerTimedState extends AbstractStrolchTimedState<IntegerValue> {
private static final long serialVersionUID = 1L;
public static final String TYPE = "IntegerState";
public IntegerTimedState() {
super();
}
@ -45,45 +33,14 @@ public class IntegerTimedState extends AbstractStrolchTimedState<IntegerValue> {
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);
String timeS = timeValueElem.getAttribute(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(timeS);
long time = date.getTime();
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, ISO8601FormatFactory.getInstance().formatDate(time));
valueElem.setAttribute(Tags.VALUE, value.getValue().toString());
stateElement.appendChild(valueElem);
}
return stateElement;
public void setStateFromStringAt(Long time, String value) {
getTimeEvolution().setValueAt(time, new IntegerValue(value));
}
@Override
public String getType() {
return TYPE;
return StrolchValueType.INTEGER.getType();
}
@Override

View File

@ -15,23 +15,9 @@
*/
package li.strolch.model.timedstate;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.SortedSet;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.impl.AString;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.impl.StringSetValue;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
@ -39,8 +25,6 @@ public class StringSetTimedState extends AbstractStrolchTimedState<StringSetValu
private static final long serialVersionUID = 1L;
public static final String TYPE = "StringSetState";
public StringSetTimedState() {
super();
}
@ -49,63 +33,14 @@ public class StringSetTimedState extends AbstractStrolchTimedState<StringSetValu
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);
String timeS = timeValueElem.getAttribute(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(timeS);
long time = date.getTime();
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, ISO8601FormatFactory.getInstance().formatDate(time));
valueElem.setAttribute(Tags.VALUE, valueAsString);
stateElement.appendChild(valueElem);
}
return stateElement;
public void setStateFromStringAt(Long time, String value) {
getTimeEvolution().setValueAt(time, new StringSetValue(value));
}
@Override
public String getType() {
return TYPE;
return StrolchValueType.STRING_SET.getType();
}
@Override

View File

@ -108,6 +108,16 @@ public interface StrolchTimedState<T extends IValue> extends StrolchElement {
public ITimeValue<T> getStateAt(Long time);
/**
* set the value at a point in time to a given time value object from a string value
*
* @param time
* the time to set the {@link IValue}
* @param value
* the string to parse to an {@link IValue}
*/
void setStateFromStringAt(final Long time, final String value);
public ITimeVariable<T> getTimeEvolution();
public void setParent(Resource aThis);

View File

@ -26,6 +26,11 @@ package li.strolch.model.timevalue;
*/
public interface IValue<T> {
/**
* @return the type of this {@link IValue}
*/
String getType();
/**
* @return the backing value
*/

View File

@ -18,12 +18,9 @@ package li.strolch.model.timevalue;
import li.strolch.model.timedstate.AbstractStrolchTimedState;
import li.strolch.model.timevalue.impl.TimeVariable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* Interface for operators to be used to change the values of {@link ITimeValue}
* in a {@link ITimeVariable} or {@link AbstractStrolchTimedState}.
* Interface for operators to be used to change the values of {@link ITimeValue} in a {@link ITimeVariable} or
* {@link AbstractStrolchTimedState}.
*
* @author Martin Smock <smock.martin@gmail.com>
*/
@ -31,15 +28,13 @@ import org.w3c.dom.Element;
public interface IValueChange<T extends IValue> {
/**
* @return the id of the {@link AbstractStrolchTimedState} the change
* applies to
* @return the id of the {@link AbstractStrolchTimedState} the change applies to
*/
String getStateId();
/**
* @param id
* the id of the {@link AbstractStrolchTimedState} the change
* applies to
* the id of the {@link AbstractStrolchTimedState} the change applies to
*/
void setStateId(String id);
@ -47,31 +42,23 @@ public interface IValueChange<T extends IValue> {
* @return the time this change has to be applied
*/
Long getTime();
void setTime(Long time);
void setTime(Long time);
/**
* @return the value of the change
*/
T getValue();
void setValue(T value);
void setValue(T value);
/**
* @return the inverse neutralizing a change. Very useful to undo changes
* made to a {@link TimeVariable}.
* @return the inverse neutralizing a change. Very useful to undo changes made to a {@link TimeVariable}.
*/
IValueChange<T> getInverse();
/**
* @return a copy of this
*/
IValueChange<T> getClone();
/**
* @param doc
* @return a xml serialisation of this
*/
Element toDom(Document doc);
IValueChange<T> getClone();
}

View File

@ -17,6 +17,7 @@ package li.strolch.model.timevalue.impl;
import java.io.Serializable;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
@ -45,6 +46,11 @@ public class BooleanValue implements IValue<Boolean>, Serializable {
this.value = Boolean.parseBoolean(valueAsString);
}
@Override
public String getType() {
return StrolchValueType.BOOLEAN.getType();
}
@Override
public BooleanValue add(Boolean o) {
this.value = o;

View File

@ -17,6 +17,7 @@ package li.strolch.model.timevalue.impl;
import java.io.Serializable;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
@ -53,6 +54,11 @@ public class FloatValue implements IValue<Double>, Serializable {
this.value = Double.parseDouble(valueAsString);
}
@Override
public String getType() {
return StrolchValueType.FLOAT.getType();
}
@Override
public FloatValue add(Double o) {
this.value += o;

View File

@ -17,6 +17,7 @@ package li.strolch.model.timevalue.impl;
import java.io.Serializable;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
@ -45,6 +46,11 @@ public class IntegerValue implements IValue<Integer>, Serializable {
this.value = Integer.parseInt(valueAsString);
}
@Override
public String getType() {
return StrolchValueType.INTEGER.getType();
}
@Override
public IntegerValue add(Integer o) {
this.value += o;

View File

@ -22,6 +22,7 @@ import java.util.Iterator;
import java.util.Set;
import li.strolch.exception.StrolchException;
import li.strolch.model.StrolchValueType;
import li.strolch.model.timevalue.ITimeValue;
import li.strolch.model.timevalue.IValue;
import ch.eitchnet.utils.dbc.DBC;
@ -42,7 +43,7 @@ public class StringSetValue implements IValue<Set<AString>>, Serializable {
private Set<AString> aStrings = new HashSet<>();
public StringSetValue() {
private StringSetValue() {
}
public StringSetValue(final Set<AString> aStrings) {
@ -50,6 +51,20 @@ public class StringSetValue implements IValue<Set<AString>>, Serializable {
this.aStrings = aStrings;
}
public StringSetValue(String valueAsString) {
Set<AString> value = new HashSet<>();
String[] values = valueAsString.split(",");
for (String s : values) {
value.add(new AString(s.trim()));
}
this.aStrings = value;
}
@Override
public String getType() {
return StrolchValueType.STRING_SET.getType();
}
@Override
public Set<AString> getValue() {
return this.aStrings;

View File

@ -17,15 +17,9 @@ package li.strolch.model.timevalue.impl;
import java.io.Serializable;
import li.strolch.model.Tags;
import li.strolch.model.timevalue.IValue;
import li.strolch.model.timevalue.IValueChange;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Martin Smock <smock.martin@gmail.com>
*/
@ -67,7 +61,8 @@ public class ValueChange<T extends IValue> implements IValueChange<T>, Serializa
public Long getTime() {
return this.time;
}
@Override
public void setTime(Long time) {
this.time = time;
}
@ -77,7 +72,8 @@ public class ValueChange<T extends IValue> implements IValueChange<T>, Serializa
public T getValue() {
return (T) this.value.getCopy();
}
@Override
public void setValue(T value) {
this.value = value;
}
@ -134,13 +130,15 @@ public class ValueChange<T extends IValue> implements IValueChange<T>, Serializa
sb.append(this.time);
sb.append(", value=");
sb.append(this.value);
sb.append(", stateId=");
sb.append(this.stateId);
sb.append("]");
return sb.toString();
}
@Override
public String getStateId() {
return stateId;
return this.stateId;
}
@Override
@ -151,17 +149,6 @@ public class ValueChange<T extends IValue> implements IValueChange<T>, Serializa
@SuppressWarnings("unchecked")
@Override
public IValueChange<T> getClone() {
return new ValueChange(time, value);
return new ValueChange(this.time, this.value, this.stateId);
}
@Override
public Element toDom(Document doc) {
Element element = doc.createElement(Tags.VALUE_CHANGE);
element.setAttribute(Tags.STATE_ID, this.stateId);
element.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time));
element.setAttribute(Tags.VALUE, this.value.getValueAsString());
element.setAttribute(Tags.VALUE_CLASS, this.value.getClass().getName());
return element;
}
}

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

@ -0,0 +1,30 @@
/*
* 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.visitor;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class NoStrategyActivityVisitor implements ActivityVisitor<Activity> {
@Override
public Activity visit(Activity element) {
return element;
}
}

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

@ -20,8 +20,9 @@ import static li.strolch.model.StrolchModelConstants.UOM_NONE;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@ -36,12 +37,15 @@ import li.strolch.model.ParameterizedElement;
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.ITimeVariable;
import li.strolch.model.timevalue.IValue;
import ch.eitchnet.utils.helper.StringHelper;
import li.strolch.model.timevalue.IValueChange;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
@ -55,16 +59,81 @@ public abstract class AbstractToSaxWriterVisitor {
this.writer = writer;
}
protected void writeElement(String tag, Order order) throws XMLStreamException {
protected void writeElement(Order order) throws XMLStreamException {
boolean empty = !order.hasParameterBags();
writeElement(tag, empty, order);
writeStartStrolchElement(Tags.ORDER, empty, order);
this.writer.writeAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(order.getDate()));
this.writer.writeAttribute(Tags.STATE, order.getState().name());
if (order.hasParameterBags()) {
writeParameterBags(order);
}
if (!empty)
this.writer.writeEndElement();
}
protected void writeElement(String tag, Resource resource) throws XMLStreamException {
protected void writeElement(Activity activity) throws XMLStreamException {
boolean empty = !activity.hasParameterBags() && !activity.hasElements();
writeStartStrolchElement(Tags.ACTIVITY, empty, activity);
if (activity.hasParameterBags()) {
writeParameterBags(activity);
}
if (activity.hasElements()) {
Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator();
while (iter.hasNext()) {
IActivityElement element = iter.next().getValue();
if (element instanceof Activity)
writeElement((Activity) element);
else if (element instanceof Action)
writeElement((Action) element);
else
throw new IllegalArgumentException("Unhandled Element class " + element.getClass());
}
}
if (!empty)
this.writer.writeEndElement();
}
private <T> void writeElement(Action action) throws XMLStreamException {
boolean empty = !action.hasParameterBags() && !action.hasChanges();
writeStartStrolchElement(Tags.ACTION, empty, action);
this.writer.writeAttribute(Tags.STATE, action.getState().name());
this.writer.writeAttribute(Tags.RESOURCE_ID, action.getResourceId());
this.writer.writeAttribute(Tags.RESOURCE_TYPE, action.getResourceType());
if (action.hasParameterBags()) {
writeParameterBags(action);
}
if (action.hasChanges()) {
for (IValueChange<? extends IValue<?>> change : action.getChanges()) {
this.writer.writeEmptyElement(Tags.VALUE_CHANGE);
this.writer.writeAttribute(Tags.STATE_ID, change.getStateId());
this.writer.writeAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(change.getTime()));
this.writer.writeAttribute(Tags.VALUE, change.getValue().getValueAsString());
this.writer.writeAttribute(Tags.TYPE, change.getValue().getClass().getName());
}
}
if (!empty)
this.writer.writeEndElement();
}
protected void writeElement(Resource resource) throws XMLStreamException {
boolean empty = !resource.hasParameterBags() && !resource.hasTimedStates();
writeElement(tag, empty, resource);
writeStartStrolchElement(Tags.RESOURCE, empty, resource);
if (resource.hasParameterBags()) {
writeParameterBags(resource);
}
if (resource.hasTimedStates())
writeTimedStates(resource);
@ -97,14 +166,6 @@ public abstract class AbstractToSaxWriterVisitor {
}
}
protected void writeElement(String tag, boolean empty, GroupedParameterizedElement element)
throws XMLStreamException {
writeStartStrolchElement(tag, empty, element);
if (!empty) {
writeParameterBags(element);
}
}
protected void writeStartStrolchElement(String tag, boolean empty, StrolchElement element)
throws XMLStreamException {
if (empty) {
@ -114,9 +175,7 @@ public abstract class AbstractToSaxWriterVisitor {
}
this.writer.writeAttribute(Tags.ID, element.getId());
if (StringHelper.isNotEmpty(element.getName())) {
this.writer.writeAttribute(Tags.NAME, element.getName());
}
this.writer.writeAttribute(Tags.NAME, element.getName());
this.writer.writeAttribute(Tags.TYPE, element.getType());
}
@ -136,12 +195,7 @@ public abstract class AbstractToSaxWriterVisitor {
protected void writeParameters(ParameterizedElement element) throws XMLStreamException {
List<Parameter<?>> parameters = new ArrayList<>(element.getParameters());
Collections.sort(parameters, new Comparator<Parameter<?>>() {
@Override
public int compare(Parameter<?> o1, Parameter<?> o2) {
return Integer.valueOf(o1.getIndex()).compareTo(o2.getIndex());
}
});
Collections.sort(parameters, (o1, o2) -> Integer.valueOf(o1.getIndex()).compareTo(o2.getIndex()));
for (Parameter<?> parameter : parameters) {
writeStartStrolchElement(Tags.PARAMETER, true, parameter);

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

@ -0,0 +1,51 @@
/*
* 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.xml;
import javax.xml.parsers.DocumentBuilder;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.helper.DomUtil;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityToDomVisitor extends StrolchElementToDomVisitor implements ActivityVisitor<Document> {
@Override
public Document visit(Activity activity) {
DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder();
this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null);
Element asDom = toDom(activity);
document.appendChild(asDom);
return this.document;
}
public Element toDom(Action action) {
return super.toDom(action);
}
public Element toDom(Activity activity) {
return super.toDom(activity);
}
}

View File

@ -0,0 +1,48 @@
/*
* 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 java.text.MessageFormat;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
import org.xml.sax.ContentHandler;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityToSaxVisitor extends StrolchElementToSaxVisitor implements ActivityVisitor<Void> {
public ActivityToSaxVisitor(ContentHandler contentHandler) {
super(contentHandler);
}
@Override
public Void visit(Activity activity) {
try {
toSax(activity);
} catch (Exception e) {
String msg = "Failed to transform Activity {0} to XML due to {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage());
throw new RuntimeException(msg, e);
}
return null;
}
}

View File

@ -0,0 +1,49 @@
/*
* 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 java.text.MessageFormat;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import li.strolch.exception.StrolchException;
import li.strolch.model.ActivityVisitor;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ActivityToSaxWriterVisitor extends AbstractToSaxWriterVisitor implements ActivityVisitor<Void> {
public ActivityToSaxWriterVisitor(XMLStreamWriter writer) {
super(writer);
}
@Override
public Void visit(Activity activity) {
try {
writeElement(activity);
this.writer.flush();
} catch (XMLStreamException e) {
String msg = "Failed to write Activity {0} due to {1}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, activity.getLocator(), e.getMessage());
throw new StrolchException(msg, e);
}
return null;
}
}

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.Order;
import org.w3c.dom.Document;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderFromDomVisitor extends StrolchElementFromDomVisitor {
public Order visit(Document doc) {
Order order = new Order();
fillElement(doc.getDocumentElement(), order);
return order;
}
}

View File

@ -28,25 +28,15 @@ import ch.eitchnet.utils.helper.DomUtil;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class OrderToDomVisitor implements OrderVisitor<Document> {
private Document document;
/**
* @return the document
*/
public Document getDocument() {
return this.document;
}
public class OrderToDomVisitor extends StrolchElementToDomVisitor implements OrderVisitor<Document> {
@Override
public Document visit(Order order) {
DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder();
Document document = documentBuilder.getDOMImplementation().createDocument(null, null, null);
this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null);
Element orderDom = order.toDom(document);
document.appendChild(orderDom);
this.document = document;
Element asDom = toDom(order);
document.appendChild(asDom);
return this.document;
}
}

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

@ -23,7 +23,6 @@ import javax.xml.stream.XMLStreamWriter;
import li.strolch.exception.StrolchException;
import li.strolch.model.Order;
import li.strolch.model.OrderVisitor;
import li.strolch.model.Tags;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -37,7 +36,7 @@ public class OrderToSaxWriterVisitor extends AbstractToSaxWriterVisitor implemen
@Override
public Void visit(Order order) {
try {
writeElement(Tags.ORDER, order);
writeElement(order);
this.writer.flush();
} catch (XMLStreamException e) {
String msg = "Failed to write Order {0} due to {1}"; //$NON-NLS-1$

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.Resource;
import org.w3c.dom.Document;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ResourceFromDomVisitor extends StrolchElementFromDomVisitor {
public Resource visit(Document doc) {
Resource resource = new Resource();
fillElement(doc.getDocumentElement(), resource);
return resource;
}
}

View File

@ -28,25 +28,15 @@ import ch.eitchnet.utils.helper.DomUtil;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class ResourceToDomVisitor implements ResourceVisitor<Document> {
private Document document;
/**
* @return the document
*/
public Document getDocument() {
return this.document;
}
public class ResourceToDomVisitor extends StrolchElementToDomVisitor implements ResourceVisitor<Document> {
@Override
public Document visit(Resource resource) {
DocumentBuilder documentBuilder = DomUtil.createDocumentBuilder();
Document document = documentBuilder.getDOMImplementation().createDocument(null, null, null);
this.document = documentBuilder.getDOMImplementation().createDocument(null, null, null);
Element resourceDom = resource.toDom(document);
document.appendChild(resourceDom);
this.document = document;
Element asDom = toDom(resource);
document.appendChild(asDom);
return this.document;
}
}

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

@ -23,7 +23,6 @@ import javax.xml.stream.XMLStreamWriter;
import li.strolch.exception.StrolchException;
import li.strolch.model.Resource;
import li.strolch.model.ResourceVisitor;
import li.strolch.model.Tags;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -37,7 +36,7 @@ public class ResourceToSaxWriterVisitor extends AbstractToSaxWriterVisitor imple
@Override
public Void visit(Resource resource) {
try {
writeElement(Tags.RESOURCE, resource);
writeElement(resource);
this.writer.flush();
} catch (XMLStreamException e) {
String msg = "Failed to write Resource {0} due to {1}"; //$NON-NLS-1$

View File

@ -21,6 +21,7 @@ import java.util.List;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -29,6 +30,7 @@ public class SimpleStrolchElementListener implements StrolchElementListener {
private List<Resource> resources;
private List<Order> orders;
private List<Activity> activities;
@Override
public void notifyResource(Resource resource) {
@ -46,6 +48,14 @@ public class SimpleStrolchElementListener implements StrolchElementListener {
this.orders.add(order);
}
@Override
public void notifyActivity(Activity activity) {
if (this.activities == null) {
this.activities = new ArrayList<>();
}
this.activities.add(activity);
}
/**
* @return the resources
*/
@ -63,4 +73,13 @@ public class SimpleStrolchElementListener implements StrolchElementListener {
return Collections.emptyList();
return this.orders;
}
/**
* @return the activities
*/
public List<Activity> getActivities() {
if (this.activities == null)
return Collections.emptyList();
return this.activities;
}
}

View File

@ -0,0 +1,305 @@
/*
* 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 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.Node;
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 {
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++) {
Node item = childNodes.item(i);
if (!(item instanceof Element))
continue;
Element timedStateElem = (Element) item;
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++) {
Node timeValueItem = timeValueElems.item(j);
if (!(timeValueItem instanceof Element))
continue;
Element timeValueElem = (Element) timeValueItem;
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);
if (id != null && name != null) {
strolchElement.setId(id);
strolchElement.setName(name);
} else {
String msg = "Check the values of the element: {0} either id or name attribute is null!"; //$NON-NLS-1$
msg = MessageFormat.format(msg, element.getNodeName());
throw new StrolchException(msg);
}
}
protected void fillElement(Element element, GroupedParameterizedElement groupedParameterizedElement) {
fillElement(element, (AbstractStrolchElement) groupedParameterizedElement);
String type = element.getAttribute(Tags.TYPE);
groupedParameterizedElement.setType(type);
NodeList bags = element.getChildNodes();
for (int i = 0; i < bags.getLength(); i++) {
Node item = bags.item(i);
if (!(item instanceof Element))
continue;
Element bagElement = (Element) item;
if (!bagElement.getNodeName().equals(Tags.PARAMETER_BAG))
continue;
ParameterBag bag = new ParameterBag();
fillElement(bagElement, bag);
groupedParameterizedElement.addParameterBag(bag);
}
}
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.getChildNodes();
for (int i = 0; i < parameterElements.getLength(); i++) {
Node item = parameterElements.item(i);
if (!(item instanceof Element))
continue;
Element paramElement = (Element) item;
if (!paramElement.getNodeName().equals(Tags.PARAMETER))
continue;
String paramtype = paramElement.getAttribute(Tags.TYPE);
StrolchValueType paramValueType = StrolchValueType.parse(paramtype);
Parameter<?> parameter = paramValueType.parameterInstance();
fillElement(paramElement, parameter);
parameterizedElement.addParameter(parameter);
}
}
protected void fillElement(Element element, Parameter<?> param) {
fillElement(element, (AbstractStrolchElement) param);
String value = element.getAttribute(Tags.VALUE);
param.setValueFromString(value);
String interpretation = element.getAttribute(Tags.INTERPRETATION);
String hidden = element.getAttribute(Tags.HIDDEN);
String uom = element.getAttribute(Tags.UOM);
String index = element.getAttribute(Tags.INDEX);
param.setInterpretation(interpretation);
param.setUom(uom);
if (StringHelper.isEmpty(index)) {
param.setIndex(0);
} else {
param.setIndex(Integer.valueOf(index));
}
if (StringHelper.isEmpty(hidden)) {
param.setHidden(false);
} else {
if (hidden.equalsIgnoreCase(Boolean.TRUE.toString())) {
param.setHidden(true);
} else if (hidden.equalsIgnoreCase(Boolean.FALSE.toString())) {
param.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);
}
}
}
protected void fillElement(Element activityElement, Activity activity) {
fillElement(activityElement, (GroupedParameterizedElement) activity);
NodeList childNodes = activityElement.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (!(item instanceof Element))
continue;
Element childElem = (Element) item;
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++) {
Node item = valueChangeNodes.item(i);
if (!(item instanceof Element))
continue;
Element valueChangeElem = (Element) item;
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

@ -17,6 +17,7 @@ package li.strolch.model.xml;
import li.strolch.model.Order;
import li.strolch.model.Resource;
import li.strolch.model.activity.Activity;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -27,4 +28,6 @@ public interface StrolchElementListener {
public void notifyResource(Resource resource);
public void notifyOrder(Order order);
public void notifyActivity(Activity activity);
}

View File

@ -0,0 +1,224 @@
/*
* 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 java.util.Iterator;
import java.util.Map.Entry;
import java.util.SortedSet;
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.StrolchModelConstants;
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.w3c.dom.Document;
import org.w3c.dom.Element;
import ch.eitchnet.utils.iso8601.ISO8601FormatFactory;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*/
public class StrolchElementToDomVisitor {
protected Document document;
public Document getDocument() {
return this.document;
}
protected Element toDom(Order order) {
Element asDom = document.createElement(Tags.ORDER);
fillElement(asDom, order);
asDom.setAttribute(Tags.DATE, ISO8601FormatFactory.getInstance().formatDate(order.getDate()));
asDom.setAttribute(Tags.STATE, order.getState().name());
return asDom;
}
protected Element toDom(Resource resource) {
Element asDom = document.createElement(Tags.RESOURCE);
fillElement(asDom, resource);
if (resource.hasTimedStates()) {
for (String stateKey : resource.getTimedStateKeySet()) {
StrolchTimedState<IValue<?>> timedState = resource.getTimedState(stateKey);
Element stateElement = toDom(timedState);
asDom.appendChild(stateElement);
}
}
return asDom;
}
protected Element toDom(Activity activity) {
Element element = document.createElement(Tags.ACTIVITY);
fillElement(element, activity);
if (activity.hasElements()) {
Iterator<Entry<String, IActivityElement>> iter = activity.elementIterator();
while (iter.hasNext()) {
IActivityElement activityElement = iter.next().getValue();
if (activityElement instanceof Activity) {
element.appendChild(toDom((Activity) activityElement));
} else if (activityElement instanceof Action) {
element.appendChild(toDom((Action) activityElement));
} else {
throw new IllegalArgumentException("Unhandled element " + activityElement.getClass());
}
}
}
return element;
}
protected Element toDom(Action action) {
Element element = document.createElement(Tags.ACTION);
fillElement(element, action);
element.setAttribute(Tags.RESOURCE_ID, action.getResourceId());
element.setAttribute(Tags.RESOURCE_TYPE, action.getResourceType());
element.setAttribute(Tags.STATE, action.getState().name());
if (action.hasChanges()) {
Iterator<IValueChange<? extends IValue<?>>> iter = action.changesIterator();
while (iter.hasNext()) {
IValueChange<? extends IValue<?>> value = iter.next();
Element valueChangeElement = toDom(value);
element.appendChild(valueChangeElement);
}
}
return element;
}
protected Element toDom(IValueChange<? extends IValue<?>> value) {
Element element = document.createElement(Tags.VALUE_CHANGE);
element.setAttribute(Tags.STATE_ID, value.getStateId());
element.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(value.getTime()));
element.setAttribute(Tags.VALUE, value.getValue().getValueAsString());
element.setAttribute(Tags.TYPE, value.getValue().getType());
return element;
}
protected Element toDom(StrolchTimedState<IValue<?>> timedState) {
Element element = document.createElement(Tags.TIMED_STATE);
fillElement(element, (AbstractStrolchElement) timedState);
if (!timedState.getInterpretation().equals(StrolchModelConstants.INTERPRETATION_NONE)) {
element.setAttribute(Tags.INTERPRETATION, timedState.getInterpretation());
}
if (!timedState.getUom().equals(StrolchModelConstants.UOM_NONE)) {
element.setAttribute(Tags.UOM, timedState.getUom());
}
if (timedState.isHidden()) {
element.setAttribute(Tags.HIDDEN, Boolean.toString(timedState.isHidden()));
}
if (timedState.getIndex() != 0) {
element.setAttribute(Tags.INDEX, Integer.toString(timedState.getIndex()));
}
SortedSet<ITimeValue<IValue<?>>> values = timedState.getTimeEvolution().getValues();
for (ITimeValue<IValue<?>> iTimeValue : values) {
Long time = iTimeValue.getTime();
String valueS = iTimeValue.getValue().getValueAsString();
Element valueElement = document.createElement(Tags.VALUE);
valueElement.setAttribute(Tags.TIME, ISO8601FormatFactory.getInstance().formatDate(time));
valueElement.setAttribute(Tags.VALUE, valueS);
element.appendChild(valueElement);
}
return element;
}
protected Element toDom(ParameterBag bag) {
Element bagElement = document.createElement(Tags.PARAMETER_BAG);
fillElement(bagElement, (ParameterizedElement) bag);
return bagElement;
}
protected Element toDom(Parameter<?> param) {
Element element = document.createElement(Tags.PARAMETER);
fillElement(element, (AbstractStrolchElement) param);
element.setAttribute(Tags.VALUE, param.getValueAsString());
if (!param.getInterpretation().equals(StrolchModelConstants.INTERPRETATION_NONE)) {
element.setAttribute(Tags.INTERPRETATION, param.getInterpretation());
}
if (!param.getUom().equals(StrolchModelConstants.UOM_NONE)) {
element.setAttribute(Tags.UOM, param.getUom());
}
if (param.isHidden()) {
element.setAttribute(Tags.HIDDEN, Boolean.toString(param.isHidden()));
}
if (param.getIndex() != 0) {
element.setAttribute(Tags.INDEX, Integer.toString(param.getIndex()));
}
return element;
}
protected void fillElement(Element element, AbstractStrolchElement strolchElement) {
element.setAttribute(Tags.ID, strolchElement.getId());
element.setAttribute(Tags.NAME, strolchElement.getName());
element.setAttribute(Tags.TYPE, strolchElement.getType());
}
protected void fillElement(Element element, GroupedParameterizedElement groupedParameterizedElement) {
fillElement(element, (AbstractStrolchElement) groupedParameterizedElement);
if (groupedParameterizedElement.hasParameterBags()) {
for (String bagKey : groupedParameterizedElement.getParameterBagKeySet()) {
ParameterBag bag = groupedParameterizedElement.getParameterBag(bagKey);
Element bagElement = toDom(bag);
element.appendChild(bagElement);
}
}
}
protected void fillElement(Element element, ParameterizedElement parameterizedElement) {
fillElement(element, (AbstractStrolchElement) parameterizedElement);
if (parameterizedElement.hasParameters()) {
for (String paramKey : parameterizedElement.getParameterKeySet()) {
Parameter<?> parameter = parameterizedElement.getParameter(paramKey);
Element paramElement = toDom(parameter);
element.appendChild(paramElement);
}
}
}
}

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

@ -76,8 +76,7 @@ public class XmlModelSaxFileReader extends XmlModelSaxReader {
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(this.listener, includeFile, this.allowInclude);
handler.parseFile();
this.statistics.nrOfOrders += handler.statistics.nrOfOrders;
this.statistics.nrOfResources += handler.statistics.nrOfResources;
this.statistics.add(handler.statistics);
break;
default:

View File

@ -16,9 +16,9 @@
package li.strolch.model.xml;
import java.text.MessageFormat;
import java.util.ArrayDeque;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.Deque;
import li.strolch.exception.StrolchException;
import li.strolch.model.GroupedParameterizedElement;
@ -27,30 +27,14 @@ import li.strolch.model.Order;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.State;
import li.strolch.model.StrolchValueType;
import li.strolch.model.Tags;
import li.strolch.model.parameter.BooleanParameter;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.DurationParameter;
import li.strolch.model.parameter.FloatListParameter;
import li.strolch.model.parameter.FloatParameter;
import li.strolch.model.parameter.IntegerListParameter;
import li.strolch.model.parameter.IntegerParameter;
import li.strolch.model.parameter.LongListParameter;
import li.strolch.model.parameter.LongParameter;
import li.strolch.model.activity.Action;
import li.strolch.model.activity.Activity;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringListParameter;
import li.strolch.model.parameter.StringParameter;
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.IValue;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -72,13 +56,15 @@ public class XmlModelSaxReader extends DefaultHandler {
protected ModelStatistics statistics;
private GroupedParameterizedElement parameterizedElement;
private Deque<Activity> activityStack;
private ParameterBag pBag;
private StrolchTimedState<? extends IValue<?>> state;
private String stateType;
private StrolchValueType stateType;
public XmlModelSaxReader(StrolchElementListener listener) {
this.listener = listener;
this.statistics = new ModelStatistics();
this.activityStack = new ArrayDeque<>();
}
/**
@ -104,6 +90,50 @@ public class XmlModelSaxReader extends DefaultHandler {
String resType = attributes.getValue(Tags.TYPE);
Resource resource = new Resource(resId, resName, resType);
this.parameterizedElement = resource;
break;
case Tags.ACTIVITY:
String activityId = attributes.getValue(Tags.ID);
String activityName = attributes.getValue(Tags.NAME);
String activityType = attributes.getValue(Tags.TYPE);
Activity activity = new Activity(activityId, activityName, activityType);
this.parameterizedElement = activity;
this.activityStack.push(activity);
break;
case Tags.ACTION:
String actionId = attributes.getValue(Tags.ID);
String actionName = attributes.getValue(Tags.NAME);
String actionType = attributes.getValue(Tags.TYPE);
String actionResourceId = attributes.getValue(Tags.RESOURCE_ID);
String actionResourceType = attributes.getValue(Tags.RESOURCE_TYPE);
String actionState = attributes.getValue(Tags.STATE);
Action action = new Action(actionId, actionName, actionType);
action.setResourceId(actionResourceId);
action.setResourceType(actionResourceType);
action.setState(State.valueOf(actionState));
this.parameterizedElement = action;
break;
case Tags.VALUE_CHANGE:
String valueChangeStateId = attributes.getValue(Tags.STATE_ID);
String valueChangeTimeS = attributes.getValue(Tags.TIME);
String valueChangeValue = attributes.getValue(Tags.VALUE);
String valueChangeType = attributes.getValue(Tags.TYPE);
IValue<?> value = StrolchValueType.parse(valueChangeType).valueInstance(valueChangeValue);
long valueChangeTime = ISO8601FormatFactory.getInstance().getDateFormat().parse(valueChangeTimeS).getTime();
ValueChange<IValue<?>> valueChange = new ValueChange<IValue<?>>(valueChangeTime, value, valueChangeStateId);
((Action) this.parameterizedElement).addChange(valueChange);
break;
case Tags.ORDER:
@ -122,6 +152,7 @@ public class XmlModelSaxReader extends DefaultHandler {
order.setState(orderState);
}
this.parameterizedElement = order;
break;
case Tags.PARAMETER_BAG:
@ -130,13 +161,11 @@ 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;
case Tags.PARAMETER:
// TODO refactor this code into using visitors
String paramId = attributes.getValue(Tags.ID);
String paramName = attributes.getValue(Tags.NAME);
String paramType = attributes.getValue(Tags.TYPE);
@ -149,110 +178,50 @@ public class XmlModelSaxReader extends DefaultHandler {
.parseBoolean(paramHiddenS);
String paramUom = attributes.getValue(Tags.UOM);
String paramInterpretation = attributes.getValue(Tags.INTERPRETATION);
Parameter<?> param;
switch (paramType) {
case StringParameter.TYPE:
param = new StringParameter(paramId, paramName, paramValue);
break;
case IntegerParameter.TYPE:
param = new IntegerParameter(paramId, paramName, IntegerParameter.parseFromString(paramValue));
break;
case BooleanParameter.TYPE:
param = new BooleanParameter(paramId, paramName, BooleanParameter.parseFromString(paramValue));
break;
case LongParameter.TYPE:
param = new LongParameter(paramId, paramName, LongParameter.parseFromString(paramValue));
break;
case DateParameter.TYPE:
param = new DateParameter(paramId, paramName, DateParameter.parseFromString(paramValue));
break;
case DurationParameter.TYPE:
param = new DurationParameter(paramId, paramName, DurationParameter.parseFromString(paramValue));
break;
case StringListParameter.TYPE:
param = new StringListParameter(paramId, paramName, StringListParameter.parseFromString(paramValue));
break;
case IntegerListParameter.TYPE:
param = new IntegerListParameter(paramId, paramName,
IntegerListParameter.parseFromString(paramValue));
break;
case FloatListParameter.TYPE:
param = new FloatListParameter(paramId, paramName, FloatListParameter.parseFromString(paramValue));
break;
case LongListParameter.TYPE:
param = new LongListParameter(paramId, paramName, LongListParameter.parseFromString(paramValue));
break;
case FloatParameter.TYPE:
param = new FloatParameter(paramId, paramName, FloatParameter.parseFromString(paramValue));
break;
default:
throw new UnsupportedOperationException(MessageFormat.format(
"Parameters of type {0} are not supported!", paramType)); //$NON-NLS-1$
}
StrolchValueType type = StrolchValueType.parse(paramType);
Parameter<?> param = type.parameterInstance();
param.setId(paramId);
param.setName(paramName);
param.setValueFromString(paramValue);
param.setHidden(paramHidden);
param.setUom(paramUom);
param.setInterpretation(paramInterpretation);
param.setIndex(index);
this.pBag.addParameter(param);
} catch (Exception e) {
throw new StrolchException("Failed to instantiate parameter " + paramId + " for bag "
+ this.pBag.getLocator() + " due to " + e.getMessage(), e);
}
break;
case Tags.TIMED_STATE:
String stateId = attributes.getValue(Tags.ID);
String stateName = attributes.getValue(Tags.NAME);
this.stateType = attributes.getValue(Tags.TYPE);
String stateType = attributes.getValue(Tags.TYPE);
switch (this.stateType) {
case FloatTimedState.TYPE:
this.state = new FloatTimedState(stateId, stateName);
break;
case IntegerTimedState.TYPE:
this.state = new IntegerTimedState(stateId, stateName);
break;
case BooleanTimedState.TYPE:
this.state = new BooleanTimedState(stateId, stateName);
break;
case StringSetTimedState.TYPE:
this.state = new StringSetTimedState(stateId, stateName);
break;
default:
break;
}
this.stateType = StrolchValueType.parse(stateType);
this.state = this.stateType.timedStateInstance();
this.state.setId(stateId);
this.state.setName(stateName);
break;
case Tags.VALUE:
String valueTime = attributes.getValue(Tags.TIME);
Date date = ISO8601FormatFactory.getInstance().parseDate(valueTime);
long time = date.getTime();
String valueValue = attributes.getValue(Tags.VALUE);
switch (this.stateType) {
case FloatTimedState.TYPE:
((FloatTimedState) this.state).getTimeEvolution().setValueAt(time, new FloatValue(valueValue));
break;
case IntegerTimedState.TYPE:
((IntegerTimedState) this.state).getTimeEvolution().setValueAt(time, new IntegerValue(valueValue));
break;
case BooleanTimedState.TYPE:
((BooleanTimedState) this.state).getTimeEvolution().setValueAt(time, new BooleanValue(valueValue));
break;
case StringSetTimedState.TYPE:
Set<AString> value = new HashSet<>();
String[] values = valueValue.split(",");
for (String s : values) {
value.add(new AString(s.trim()));
}
this.state.setStateFromStringAt(time, valueValue);
StringSetValue stringSetValue = new StringSetValue(value);
((StringSetTimedState) this.state).getTimeEvolution().setValueAt(time, stringSetValue);
break;
default:
break;
}
break;
default:
@ -264,30 +233,62 @@ public class XmlModelSaxReader extends DefaultHandler {
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (qName) {
case Tags.STROLCH_MODEL:
break;
case Tags.RESOURCE:
this.listener.notifyResource((Resource) this.parameterizedElement);
this.statistics.nrOfResources++;
this.parameterizedElement = null;
break;
case Tags.ACTIVITY:
Activity activity = this.activityStack.pop();
if (this.activityStack.isEmpty()) {
this.listener.notifyActivity(activity);
this.statistics.nrOfActivities++;
} else {
this.activityStack.peek().addElement(activity);
}
this.parameterizedElement = null;
break;
case Tags.ORDER:
this.listener.notifyOrder((Order) this.parameterizedElement);
this.statistics.nrOfOrders++;
this.parameterizedElement = null;
break;
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;
case Tags.PARAMETER:
break;
case Tags.INCLUDE_FILE:
break;
case Tags.TIMED_STATE:
((Resource) this.parameterizedElement).addTimedState(this.state);
break;
case Tags.PARAMETER:
case Tags.INCLUDE_FILE:
case Tags.VALUE:
case Tags.VALUE_CHANGE:
case Tags.STROLCH_MODEL:
break;
default:
throw new IllegalArgumentException(MessageFormat.format("The element ''{0}'' is unhandled!", qName)); //$NON-NLS-1$
}

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,33 +86,116 @@ 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;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SuppressWarnings("nls")
public class ModelTest {
protected static final Logger logger = LoggerFactory.getLogger(ModelTest.class);
@Test
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() {
@ -127,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");
@ -145,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");
@ -167,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
@ -175,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
@ -193,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

@ -21,6 +21,7 @@ import java.io.File;
import java.util.HashMap;
import java.util.Map;
import li.strolch.model.activity.Activity;
import li.strolch.model.xml.StrolchElementListener;
import li.strolch.model.xml.XmlModelSaxFileReader;
@ -32,7 +33,6 @@ import ch.eitchnet.utils.helper.StringHelper;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
*
*/
@SuppressWarnings("nls")
public class XmlModelDefaultHandlerTest {
@ -42,8 +42,9 @@ public class XmlModelDefaultHandlerTest {
@Test
public void shouldParseXmlModelFile() {
final Map<String, Resource> resourceMap = new HashMap<>();
final Map<String, Order> orderMap = new HashMap<>();
Map<String, Resource> resourceMap = new HashMap<>();
Map<String, Order> orderMap = new HashMap<>();
Map<String, Activity> activityMap = new HashMap<>();
File file = new File("src/test/resources/data/StrolchModel.xml");
StrolchElementListener listener = new StrolchElementListener() {
@ -56,16 +57,24 @@ public class XmlModelDefaultHandlerTest {
public void notifyOrder(Order order) {
orderMap.put(order.getId(), order);
}
@Override
public void notifyActivity(Activity activity) {
activityMap.put(activity.getId(), activity);
}
};
XmlModelSaxFileReader handler = new XmlModelSaxFileReader(listener, file, true);
handler.parseFile();
assertEquals(3, resourceMap.size());
assertEquals(3, orderMap.size());
assertEquals(3, activityMap.size());
ModelStatistics statistics = handler.getStatistics();
logger.info("Parsing took " + StringHelper.formatNanoDuration(statistics.durationNanos));
assertEquals(3, statistics.nrOfOrders);
assertEquals(3, statistics.nrOfResources);
assertEquals(3, statistics.nrOfActivities);
}
}

View File

@ -16,14 +16,19 @@
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;
import li.strolch.model.xml.ResourceToDomVisitor;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* @author Robert von Burg <eitch@eitchnet.ch>
@ -40,8 +45,7 @@ public class XmlToDomTest extends ModelTest {
domVisitor.visit(order);
Document document = domVisitor.getDocument();
Element rootElement = document.getDocumentElement();
Order parsedOrder = new Order(rootElement);
Order parsedOrder = new OrderFromDomVisitor().visit(document);
OrderDeepEqualsVisitor visitor = new OrderDeepEqualsVisitor(order);
visitor.visit(parsedOrder);
@ -57,11 +61,27 @@ public class XmlToDomTest extends ModelTest {
domVisitor.visit(resource);
Document document = domVisitor.getDocument();
Element rootElement = document.getDocumentElement();
Resource parsedResource = new Resource(rootElement);
Resource parsedResource = new ResourceFromDomVisitor().visit(document);
ResourceDeepEqualsVisitor visitor = new ResourceDeepEqualsVisitor(resource);
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();
}
}

Some files were not shown because too many files have changed in this diff Show More