blob: 750f4c2017fd1e9d41083d7d5c1f2002061e6f2b [file] [log] [blame]
package org.eclipse.stem.populationmodels.standard.impl;
/*******************************************************************************
* Copyright (c) 2009 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.osgi.util.NLS;
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
import org.eclipse.stem.core.STEMURI;
import org.eclipse.stem.core.Utility;
import org.eclipse.stem.core.graph.DynamicLabel;
import org.eclipse.stem.core.graph.Edge;
import org.eclipse.stem.core.graph.Exchange;
import org.eclipse.stem.core.graph.ExchangeType;
import org.eclipse.stem.core.graph.Graph;
import org.eclipse.stem.core.graph.GraphFactory;
import org.eclipse.stem.core.graph.IntegrationLabel;
import org.eclipse.stem.core.graph.IntegrationLabelValue;
import org.eclipse.stem.core.graph.Label;
import org.eclipse.stem.core.graph.Node;
import org.eclipse.stem.core.graph.NodeLabel;
import org.eclipse.stem.core.model.Decorator;
import org.eclipse.stem.core.model.STEMTime;
import org.eclipse.stem.core.model.impl.IntegrationDecoratorImpl;
import org.eclipse.stem.definitions.adapters.relativevalue.RelativeValueProvider;
import org.eclipse.stem.definitions.adapters.relativevalue.RelativeValueProviderAdapter;
import org.eclipse.stem.definitions.adapters.relativevalue.RelativeValueProviderAdapterFactory;
import org.eclipse.stem.core.scenario.ScenarioInitializationException;
import org.eclipse.stem.definitions.edges.MigrationEdge;
import org.eclipse.stem.definitions.labels.PopulationLabel;
import org.eclipse.stem.definitions.labels.RelativePhysicalRelationshipLabel;
import org.eclipse.stem.definitions.transport.PipeTransportEdgeLabel;
import org.eclipse.stem.populationmodels.Activator;
import org.eclipse.stem.populationmodels.standard.PopulationModel;
import org.eclipse.stem.populationmodels.standard.PopulationModelLabel;
import org.eclipse.stem.populationmodels.standard.PopulationModelLabelValue;
import org.eclipse.stem.populationmodels.standard.StandardPackage;
import org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabel;
import org.eclipse.stem.populationmodels.standard.StandardPopulationModelLabelValue;
/**
* <!-- begin-user-doc -->
* An implementation of the model object '<em><b>Population Model</b></em>'.
* <!-- end-user-doc -->
* <p>
* The following features are implemented:
* <ul>
* <li>{@link org.eclipse.stem.populationmodels.standard.impl.PopulationModelImpl#getPopulationIdentifier <em>Population Identifier</em>}</li>
* <li>{@link org.eclipse.stem.populationmodels.standard.impl.PopulationModelImpl#getName <em>Name</em>}</li>
* <li>{@link org.eclipse.stem.populationmodels.standard.impl.PopulationModelImpl#getTargetISOKey <em>Target ISO Key</em>}</li>
* </ul>
* </p>
*
* @generated
*/
public abstract class PopulationModelImpl extends IntegrationDecoratorImpl implements PopulationModel {
/**
* The default value of the '{@link #getPopulationIdentifier() <em>Population Identifier</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getPopulationIdentifier()
* @generated
* @ordered
*/
protected static final String POPULATION_IDENTIFIER_EDEFAULT = "human"; //$NON-NLS-1$
/**
* The cached value of the '{@link #getPopulationIdentifier() <em>Population Identifier</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getPopulationIdentifier()
* @generated
* @ordered
*/
protected String populationIdentifier = POPULATION_IDENTIFIER_EDEFAULT;
/**
* The default value of the '{@link #getName() <em>Name</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getName()
* @generated
* @ordered
*/
protected static final String NAME_EDEFAULT = "HumanPopulationModel"; //$NON-NLS-1$
/**
* The cached value of the '{@link #getName() <em>Name</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getName()
* @generated
* @ordered
*/
protected String name = NAME_EDEFAULT;
/**
* The default value of the '{@link #getTargetISOKey() <em>Target ISO Key</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getTargetISOKey()
* @generated
* @ordered
*/
protected static final String TARGET_ISO_KEY_EDEFAULT = ""; //$NON-NLS-1$
/**
* The cached value of the '{@link #getTargetISOKey() <em>Target ISO Key</em>}' attribute.
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @see #getTargetISOKey()
* @generated
* @ordered
*/
protected String targetISOKey = TARGET_ISO_KEY_EDEFAULT;
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
public PopulationModelImpl() {
super();
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
protected EClass eStaticClass() {
return StandardPackage.Literals.POPULATION_MODEL;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public String getPopulationIdentifier() {
return populationIdentifier;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setPopulationIdentifier(String newPopulationIdentifier) {
String oldPopulationIdentifier = populationIdentifier;
populationIdentifier = newPopulationIdentifier;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, StandardPackage.POPULATION_MODEL__POPULATION_IDENTIFIER, oldPopulationIdentifier, populationIdentifier));
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public String getName() {
return name;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setName(String newName) {
String oldName = name;
name = newName;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, StandardPackage.POPULATION_MODEL__NAME, oldName, name));
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public String getTargetISOKey() {
return targetISOKey;
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
public void setTargetISOKey(String newTargetISOKey) {
String oldTargetISOKey = targetISOKey;
targetISOKey = newTargetISOKey;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, StandardPackage.POPULATION_MODEL__TARGET_ISO_KEY, oldTargetISOKey, targetISOKey));
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
public EList<String> getAllLabelIdentifiers() {
EList<String> identifiers = new BasicEList<String>();
identifiers.add(getPopulationIdentifier());
return identifiers;
}
/**
* Decorate the graph for a standard population model
*
*/
@Override
public void decorateGraph(STEMTime time) throws ScenarioInitializationException {
if(this.isGraphDecorated()) return;
boolean success = false;
for (final Iterator<PopulationLabel> populationLabelIter = getPopulationLabels(
getPopulationIdentifier(), getGraph()).iterator(); populationLabelIter
.hasNext();) {
success = true;
final PopulationLabel populationLabel = populationLabelIter.next();
if(this.getPopulationIdentifier().equals(populationLabel.getPopulationIdentifier())) {
// Make sure the node does not already have a population model label for this population
// identifier.
boolean found = false;
for(NodeLabel l:populationLabel.getNode().getLabels()) {
if(l instanceof PopulationModelLabel &&
((PopulationModelLabel)l).getPopulationIdentifier().equals(this.getPopulationIdentifier()))
{found = true;break;}
}
if(found)continue;
// Okay, another population model has not yet added population model labels
// for the same population identifier, but it might do so in the future depending
// upon the order decorateGraph() is called on the decorators. Check if there
// is another population model with a higher iso level target node URI that the
// node is contained within.
found = false;
for(Decorator d:this.getGraph().getDecorators()) {
if(!d.equals(this) &&
d instanceof PopulationModel &&
((PopulationModel)d).getPopulationIdentifier().equals(this.getPopulationIdentifier()) &&
Utility.keyLevel(((PopulationModel)d).getTargetISOKey()) > Utility.keyLevel(this.getTargetISOKey()) &&
isContained(populationLabel.getNode(), (((PopulationModel)d).getTargetISOKey())))
{found = true;break;}
}
if(found) continue;
// Final check. If this is not a leaf node, we don't add the population model label since there's no reason
// to do the calculations here since they will be done in the children
if(!isLeaf(populationLabel.getNode())) continue;
// Ok, let's create a population model label to keep the dynamic state of the population
final PopulationModelLabel pl = createPopulationLabel();
pl.setPopulationLabel(populationLabel); // Remember the static population label
pl.setPopulationIdentifier(populationLabel.getPopulationIdentifier());
// Make a unique URI for the label that's the same across running instances
URI unique = STEMURI.createURI(Label.URI_TYPE_LABEL_SEGMENT + "/"+populationLabel.getNode().getURI().lastSegment()+"/"+this.getName()+"/"+getPopulationIdentifier()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
pl.setURI(unique);
pl.getDublinCore().setIdentifier(unique.toString());
getLabelsToUpdate().add(pl);
populationLabel.getNode().getLabels().add(pl);
pl.setNode(populationLabel.getNode());
getGraph().putNodeLabel(pl);
}
} // for each population label
if(!success)
throw new ScenarioInitializationException(NLS.bind(Messages.POP_MODEL_MISSING_POP_LABELS, new Object[] {this.getURI().toString(), getPopulationIdentifier()}), this, new Exception());
resetLabels();
} // decorateGraph
/**
* isLeaf. Return true if the passed in node is a leaf
* @param n
* @return
*/
public boolean isLeaf(Node n) {
for(Edge e:n.getEdges())
if(e.getLabel() instanceof RelativePhysicalRelationshipLabel &&
e.getA().equals(n))
return false;
return true;
}
/**
* Search through the graph and find all of the population labels that have
* the same identifier.
*
* @param populationIdentifier
* the population being labeled
* @param graph
* the graph to search
* @return the PopulationLabel instances of the graph that match the
* identifier.
*/
protected Collection<PopulationLabel> getPopulationLabels(
final String populationIdentifier, final Graph graph) {
final List<PopulationLabel> retValue = new ArrayList<PopulationLabel>();
// Iterate through all of the population labels in the graph
EList<NodeLabel> labels = graph.getNodeLabelsByTypeURI(
PopulationLabel.URI_TYPE_POPULATION_LABEL);
for (NodeLabel pl:labels) {
if(getTargetISOKey() != null && !getTargetISOKey().trim().equals("") && pl.getNode() != null && //$NON-NLS-1$
!isContained(pl.getNode(), getTargetISOKey()) &&
!pl.getNode().getURI().lastSegment().equals("ZZZ")) //$NON-NLS-1$
continue;
final PopulationLabel populationLabel = (PopulationLabel) pl;
// Is this label for the population we're looking for?
if (populationLabel.getPopulationIdentifier().equals(
populationIdentifier)) {
// Yes
// If there is a problem with the "node uri" of the population
// label then it would not have been associated with a node
// instance in the graph at this point. This is a problem for
// disease models that are trying to label the node (there isn't
// one!). So filter out those mistakes here.
// Does the population label have an associated node?
if (populationLabel.getNode() != null) {
// Yes
retValue.add(populationLabel);
} // if the population label has a node
} // if the population we're looking for
} // for each population label
return retValue;
} // getPopulationLabels
protected boolean isContained(Node node, String targetISOKey) {
if(node.getURI().lastSegment().equals(targetISOKey))
return true;
// Check parents
EList<Edge>edges = node.getEdges();
for(Edge e:edges) {
if(e.getLabel() instanceof RelativePhysicalRelationshipLabel ||
e.getLabel() instanceof PipeTransportEdgeLabel)
if(e.getA().equals(node)) continue;
else if(Utility.keyLevel(e.getA().getURI().lastSegment())
> Utility.keyLevel(node.getURI().lastSegment())) continue; // skip air transport edges that goes "down"
else return isContained(e.getA(), targetISOKey);
}
return false;
}
/**
* Search through the graph and find all of the population model labels (i.e. dynamic ones, not static) that have
* the same identifier.
*
* @param populationIdentifier
* the population being labeled
* @param graph
* the graph to search
* @return the PopulationLabel instances of the graph that match the
* identifier.
*/
protected Collection<PopulationModelLabel> getPopulationModelLabels(
final String populationIdentifier, final Graph graph) {
final List<PopulationModelLabel> retValue = new ArrayList<PopulationModelLabel>();
// Iterate through all of the population labels in the graph
EList<NodeLabel> labels = graph.getNodeLabelsByTypeURI(
PopulationModelLabel.URI_TYPE_DYNAMIC_POPULATION_LABEL);
for (NodeLabel pl:labels) {
final PopulationModelLabel populationModelLabel = (PopulationModelLabel) pl;
// Is this label for the population we're looking for?
if (populationModelLabel.getPopulationIdentifier().equals(
populationIdentifier)) {
if (populationModelLabel.getNode() != null) {
// Yes
retValue.add(populationModelLabel);
} // if the population label has a node
} // if the population we're looking for
} // for each population label
return retValue;
} // getPopulationLabels
public void applyExternalDeltas(STEMTime time, long timeDelta,
EList<DynamicLabel> labels) {
for (final Iterator<DynamicLabel> currentStateLabelIter = labels
.iterator(); currentStateLabelIter.hasNext();) {
final StandardPopulationModelLabel plabel = (StandardPopulationModelLabel) currentStateLabelIter
.next();
StandardPopulationModelLabelValue myDelta = (StandardPopulationModelLabelValue)plabel.getDeltaValue();
Node n = plabel.getNode();
// Find other labels on the node that wants to exchange data
EList<NodeLabel> labs = n.getLabels();
for(NodeLabel l:labs) {
if(l instanceof IntegrationLabel && !l.equals(plabel) &&
((IntegrationLabel)l).getIdentifier().equals(plabel.getIdentifier())) {
IntegrationLabelValue sdeLabelValue = (IntegrationLabelValue)((IntegrationLabel)l).getDeltaValue();
EList<Exchange>arrivals = sdeLabelValue.getArrivals();
EList<Exchange>departures = sdeLabelValue.getDepartures();
// Arrivals are births. Observe that arrivals should be 0 since
// other decorators are disease models that don't cause an "increase"
// in births.
if(arrivals != null) {
for (Exchange entry : arrivals) {
if(entry.getType() == ExchangeType.BIRTHS_AND_DEATHS) {
// Only the local node makes sense for disease models
myDelta.setCount(myDelta.getCount()+entry.getCount());
myDelta.setBirths(myDelta.getBirths()+entry.getCount());
}
}
}
// Departures are deaths
if(departures != null) {
//for(Node n2:departures.keySet()) {
for (Exchange entry : departures) {
if(entry.getType() == ExchangeType.BIRTHS_AND_DEATHS) {// Only the local node makes sense for disease models
myDelta.setCount(myDelta.getCount() - entry.getCount());
myDelta.setDeaths(myDelta.getDeaths()+entry.getCount());
}
}
}
}
}
}
}
protected void handleMigration(StandardPopulationModelLabelImpl label, EList<Exchange>arrivals,EList<Exchange>departures, long timeperiod, long timeDelta, StandardPopulationModelLabelValueImpl delta) {
Node n = (Node)label.getIdentifiable();
for(Edge e:n.getEdges()) {
if(e instanceof MigrationEdge) {
MigrationEdge me = (MigrationEdge)e;
if(!me.getPopulationIdentifier().equals(this.getPopulationIdentifier())) continue;
// Migration is FROM A TO B
Node source = me.getA();
Node dest = me.getB();
boolean leaving = source.equals(n);
double rate = me.getLabel().getCurrentValue().getMigrationRate();
if(leaving) {
StandardPopulationModelLabelValue val = ((StandardPopulationModelLabelValue) label.getTempValue()); // Should be probe value
double count = val.getCount();
double goodbye = count*rate*(double)timeDelta/(double)timeperiod; // rescale and adjust
delta.setCount(delta.getCount()-goodbye);
Exchange migrationExchange = GraphFactory.eINSTANCE.createExchange();
Label otherLabel = null;
for (Label lab : dest.getLabels()) {
if (lab instanceof StandardPopulationModelLabel && ((StandardPopulationModelLabel)lab).getPopulationIdentifier().equals(label.getPopulationIdentifier())) {
otherLabel = lab;
break;
}
}
if(otherLabel == null) {
Activator.logError(NLS.bind(Messages.EDGE_POP_MODEL_MISSING, new Object[] {dest.getURI().toString(), label.getPopulationIdentifier()}), new Exception());
return;
}
migrationExchange.setType(ExchangeType.MIGRATION);
migrationExchange.setOtherLabel(otherLabel);
migrationExchange.setCount(goodbye);
delta.getDepartures().add(migrationExchange);
} else {
// Find the population model label on the dest node
StandardPopulationModelLabelValue otherVal = null;
Label otherLabel = null;
for(NodeLabel lab:source.getLabels()) {
if(lab instanceof StandardPopulationModelLabel && ((StandardPopulationModelLabel)lab).getPopulationIdentifier().equals(label.getPopulationIdentifier())) {
otherVal = (StandardPopulationModelLabelValue)((StandardPopulationModelLabel)lab).getTempValue();
otherLabel = lab;
break;
}
}
if(otherVal == null) {
Activator.logError(NLS.bind(Messages.EDGE_POP_MODEL_MISSING, new Object[] {dest.getURI().toString(), label.getPopulationIdentifier()}), new Exception());
return;
}
double count = otherVal.getCount();
double welcome = count*rate*(double)timeDelta/(double)timeperiod; // rescale and adjust
delta.setCount(delta.getCount()+welcome);
Exchange migrationExchange = GraphFactory.eINSTANCE.createExchange();
migrationExchange.setType(ExchangeType.MIGRATION);
migrationExchange.setOtherLabel(otherLabel);
migrationExchange.setCount(welcome);
delta.getArrivals().add(migrationExchange);
}
}
}
}
public List<IItemPropertyDescriptor> getCompartments(String populationIdentifier) {
PopulationModelLabel label = createPopulationLabel();
final RelativeValueProviderAdapter rvp = (RelativeValueProviderAdapter)
RelativeValueProviderAdapterFactory.INSTANCE.adapt(label, RelativeValueProvider.class);
// Does the label have relative values?
if (rvp != null) {
// Yes
rvp.setTarget(label);
return rvp.getProperties();
}
return null;
}
/**
* <!-- begin-user-doc -->
*
* @return
*
* <!-- end-user-doc -->
* @generated NOT
*/
public abstract PopulationModelLabel createPopulationLabel();
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated NOT
*/
public abstract PopulationModelLabelValue createPopulationLabelValue();
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public Object eGet(int featureID, boolean resolve, boolean coreType) {
switch (featureID) {
case StandardPackage.POPULATION_MODEL__POPULATION_IDENTIFIER:
return getPopulationIdentifier();
case StandardPackage.POPULATION_MODEL__NAME:
return getName();
case StandardPackage.POPULATION_MODEL__TARGET_ISO_KEY:
return getTargetISOKey();
}
return super.eGet(featureID, resolve, coreType);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public void eSet(int featureID, Object newValue) {
switch (featureID) {
case StandardPackage.POPULATION_MODEL__POPULATION_IDENTIFIER:
setPopulationIdentifier((String)newValue);
return;
case StandardPackage.POPULATION_MODEL__NAME:
setName((String)newValue);
return;
case StandardPackage.POPULATION_MODEL__TARGET_ISO_KEY:
setTargetISOKey((String)newValue);
return;
}
super.eSet(featureID, newValue);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public void eUnset(int featureID) {
switch (featureID) {
case StandardPackage.POPULATION_MODEL__POPULATION_IDENTIFIER:
setPopulationIdentifier(POPULATION_IDENTIFIER_EDEFAULT);
return;
case StandardPackage.POPULATION_MODEL__NAME:
setName(NAME_EDEFAULT);
return;
case StandardPackage.POPULATION_MODEL__TARGET_ISO_KEY:
setTargetISOKey(TARGET_ISO_KEY_EDEFAULT);
return;
}
super.eUnset(featureID);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public boolean eIsSet(int featureID) {
switch (featureID) {
case StandardPackage.POPULATION_MODEL__POPULATION_IDENTIFIER:
return POPULATION_IDENTIFIER_EDEFAULT == null ? populationIdentifier != null : !POPULATION_IDENTIFIER_EDEFAULT.equals(populationIdentifier);
case StandardPackage.POPULATION_MODEL__NAME:
return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
case StandardPackage.POPULATION_MODEL__TARGET_ISO_KEY:
return TARGET_ISO_KEY_EDEFAULT == null ? targetISOKey != null : !TARGET_ISO_KEY_EDEFAULT.equals(targetISOKey);
}
return super.eIsSet(featureID);
}
/**
* <!-- begin-user-doc -->
* <!-- end-user-doc -->
* @generated
*/
@Override
public String toString() {
if (eIsProxy()) return super.toString();
StringBuffer result = new StringBuffer(super.toString());
result.append(" (populationIdentifier: "); //$NON-NLS-1$
result.append(populationIdentifier);
result.append(", name: "); //$NON-NLS-1$
result.append(name);
result.append(", targetISOKey: "); //$NON-NLS-1$
result.append(targetISOKey);
result.append(')');
return result.toString();
}
} //PopulationModelImpl