📄 commonmodel.java
字号:
}
/**Adds details for a new station. For each type of station proper parameter
* values are set.*/
protected void addStationDetailsForClass(Object classKey){
Vector stationKeys = getStationKeys();
for(int i=0; i<stationKeys.size(); i++){
StationData sd = (StationData)stationDataHM.get(stationKeys.get(i));
StationClassData defaultDetails = getDefaultDetailsForStationType(sd);
stationDetailsBDM.put(classKey, stationKeys.get(i), defaultDetails);
}
}
/**deletes station details from repository, given station search key.*/
protected void deleteStationDetails(Object stationKey){
stationDetailsBDM.remove(stationKey, BDMap.Y);
}
/**Deletes station details about a certain class, given its search key*/
protected void deleteStationDetailsForClass(Object classKey){
stationDetailsBDM.remove(classKey, BDMap.X);
}
/*------------------------------------------------------------------------------
*------------- methods for inter-station connections definition --------------
*-------------------------------------------------------------------------------*/
/**Adds a connection between two stations in this model, given search keys of
* source and target stations. If connection could not be created (if, for example,
* target station's type is "Source") false value is returned.
* @param sourceKey: search key for source station
* @param targetKey: search key for target station
* @param areConnected: true if stations must be connected, false otherwise.
* @return : true if connection was created, false otherwise.
* */
public boolean setConnected(Object sourceKey, Object targetKey, boolean areConnected){
Connection toSet = (Connection)connectionsBDM.get(targetKey, sourceKey);
//element does not exist, no further operation possible, return.
if(toSet == null) return false;
//exists but is not connectable, return.
if(!toSet.isConnectable) return false;
//is already been connected: can't be connected twice, but can be disconnected
if(toSet.isConnected){
save = true;
//must disconnect
if(!areConnected) {
toSet.isConnected = false;
}
}else{
toSet.isConnected = areConnected;
return toSet.isConnected;
}
return false;
}
/**Tells wether two stations are connected
* @param sourceKey: search key for source station
* @param targetKey: search key for target station
* @return : true if stations are connected, false otherwise.
*/
public boolean areConnected(Object sourceKey, Object targetKey){
Connection conn = (Connection)connectionsBDM.get(targetKey, sourceKey);
if(conn == null) return false;
else return conn.isConnected;
}
/**Tells wether two stations can be connected
* @param sourceKey: search key for source station
* @param targetKey: search key for target station
* @return : true if stations are connectable, false otherwise.
*/
public boolean areConnectable(Object sourceKey, Object targetKey){
Connection conn = (Connection)connectionsBDM.get(targetKey, sourceKey);
if(conn == null) {
return false;
}
else return conn.isConnectable;
}
/**Returns a set of station keys specified station is connected to as a source.
* @param stationKey: source station for which (target)connected stations must be
* returned.
* @return Vector containing keys for connected stations.
*/
public Vector getForwardConnections(Object stationKey){
//must find entry for row index (e.g. connection sources set)
Map conns = connectionsBDM.get(stationKey, BDMap.Y);
return scanForConnections(conns);
}
/**Returns a set of station keys specified station is connected to as a target.
* @param stationKey: source station for which (source)connected stations must be
* returned.
* @return Vector containing keys for connected stations.
*/
public Vector getBackwardConnections(Object stationKey){
//must find entry for row index (e.g. connection targets set)
Map conns = connectionsBDM.get(stationKey, BDMap.X);
return scanForConnections(conns);
}
//finds out connections, given a connection set
private Vector scanForConnections(Map conns){
Vector retval = new Vector(0);
//check returned map not to be null
if(conns != null){
//scan all of the station entries to find out which selected one is connected to
for(int i=0; i<stationsKeyset.size(); i++){
Connection c = (Connection)conns.get(stationsKeyset.get(i));
//assure connection is not null, preventing NullPointerException
if(c!=null){
//finally, if connection exists and is connected, add key to returned vector
if(c.isConnected)retval.add(stationsKeyset.get(i));
}
}
}
return retval;
}
/**Tells wether two stations can be connected. Subclasses can override this method to
* change behaviour in connection creation.*/
protected boolean canBeConnected(Object sourceKey, Object targetKey){
StationData source = (StationData)stationDataHM.get(sourceKey),
target = (StationData)stationDataHM.get(targetKey);
/*if source and target are both servers or delays or LDServers, at first
instance, declare them connectables*/
if((source.type.equals(STATION_TYPE_SERVER)||
source.type.equals(STATION_TYPE_DELAY) )&&
(target.type.equals(STATION_TYPE_SERVER)||
target.type.equals(STATION_TYPE_DELAY) )) return true;
//no outgoing connections from sink
if(source.type.equals(STATION_TYPE_SINK)) return false;
//no incoming connection to source
if(target.type.equals(STATION_TYPE_SOURCE)) return false;
// No connection between a router or a fork or join and itself
if (sourceKey == targetKey && (source.type.equals(STATION_TYPE_ROUTER) ||
source.type.equals(STATION_TYPE_FORK) ||
source.type.equals(STATION_TYPE_JOIN)))
return false;
//no direct connections from source to sink
//no loops on terminal
//no direct connections between terminal and source or sink
if((source.type.equals(STATION_TYPE_SOURCE)||
source.type.equals(STATION_TYPE_TERMINAL)||
source.type.equals(STATION_TYPE_SINK))&&
(target.type.equals(STATION_TYPE_SOURCE)||
target.type.equals(STATION_TYPE_SINK)||
target.type.equals(STATION_TYPE_TERMINAL))) return false;
return true;
}
/*Creates all of the entries inside connection data repository for a new station.*/
private void addNewStationConnections(Object stationKey){
//first add all of the forward connections
//picking all possible targets
Vector keys = new Vector(connectionsBDM.keySet(BDMap.X));
int size = keys.size();
Map conns = new HashMap();
//building set of forward connections
for(int i=0; i<size; i++){
//current station key
Object current = keys.get(i);
conns.put(current, new Connection(canBeConnected(stationKey, current), false));
}
connectionsBDM.put(stationKey, BDMap.Y, conns);
//reset all data structures for bw connections addition
//picking all possible sources
keys = new Vector(connectionsBDM.keySet(BDMap.Y));
size = keys.size();
conns = new HashMap();
//building set of backward connections
for(int i=0; i<size; i++){
//current station key
Object current = keys.get(i);
conns.put(current, new Connection(canBeConnected(current, stationKey), false));
}
connectionsBDM.put(stationKey, BDMap.X, conns);
}
//deletes connections for deleted station
private void deleteStationConnections(Object stationKey){
connectionsBDM.remove(stationKey, BDMap.X);
connectionsBDM.remove(stationKey, BDMap.Y);
}
/*------------------------------------------------------------------------------
*--------------- methods for simulation parameters definition ----------------
*-------------------------------------------------------------------------------*/
public Object addMeasure(String type, Object stationKey, Object classKey) {
return addMeasure(type, stationKey, classKey,
Defaults.getAsDouble("measureAlpha"), Defaults.getAsDouble("measurePrecision"));
}
public Object addMeasure(String type, Object stationKey, Object classKey, Double alpha, Double precision) {
Object key = new Long(++incrementalKey);
MeasureData md = new MeasureData(stationKey, classKey, type, alpha, precision);
measureDataHM.put(key, md);
measuresKeyset.add(key);
save = true;
return key;
}
public void removeMeasure(Object measureKey) {
if(measuresKeyset.contains(measureKey)){
measuresKeyset.remove(measureKey);
measureDataHM.remove(measureKey);
save = true;
}
}
public Vector getMeasureKeys() {
return measuresKeyset;
}
public String getMeasureType(Object measureKey) {
if(!measuresKeyset.contains(measureKey)){
return null;
}else{
return ((MeasureData)measureDataHM.get(measureKey)).type;
}
}
public void setMeasureType(String newType, Object measureKey) {
if(measuresKeyset.contains(measureKey)){
String oldType = ((MeasureData)measureDataHM.get(measureKey)).type;
((MeasureData)measureDataHM.get(measureKey)).type = newType;
if (!oldType.equals(newType))
save = true;
}
}
public Object getMeasureClass(Object measureKey) {
if(!measuresKeyset.contains(measureKey)){
return null;
}else{
return ((MeasureData)measureDataHM.get(measureKey)).classKey;
}
}
public void setMeasureClass(Object classKey, Object measureKey) {
if(measuresKeyset.contains(measureKey)){
Object oldKey = ((MeasureData)measureDataHM.get(measureKey)).classKey;
((MeasureData)measureDataHM.get(measureKey)).classKey = classKey;
if (oldKey != classKey)
save = true;
}
}
public Object getMeasureStation(Object measureKey) {
if(!measuresKeyset.contains(measureKey)){
return null;
}else{
return ((MeasureData)measureDataHM.get(measureKey)).stationKey;
}
}
public void setMeasureStation(Object stationKey, Object measureKey) {
if(measuresKeyset.contains(measureKey)){
Object oldKey = ((MeasureData)measureDataHM.get(measureKey)).stationKey;
((MeasureData)measureDataHM.get(measureKey)).stationKey = stationKey;
if (oldKey != stationKey)
save = true;
}
}
public Double getMeasureAlpha(Object measureKey) {
if(!measuresKeyset.contains(measureKey)){
return null;
}else{
return ((MeasureData)measureDataHM.get(measureKey)).alpha;
}
}
public void setMeasureAlpha(Double alpha, Object measureKey) {
if (alpha.doubleValue() > 0 && alpha.doubleValue() < 1)
if(measuresKeyset.contains(measureKey)){
Double oldAlpha = ((MeasureData)measureDataHM.get(measureKey)).alpha;
((MeasureData)measureDataHM.get(measureKey)).alpha = alpha;
if (!oldAlpha.equals(alpha))
save = true;
}
}
public Double getMeasurePrecision(Object measureKey) {
if(!measuresKeyset.contains(measureKey)){
return null;
}else{
return ((MeasureData)measureDataHM.get(measureKey)).precision;
}
}
public void setMeasurePrecision(Double precision, Object measureKey) {
if (precision.doubleValue() > 0 && precision.doubleValue() < 1)
if(measuresKeyset.contains(measureKey)){
Double oldPrecision = ((MeasureData)measureDataHM.get(measureKey)).precision;
((MeasureData)measureDataHM.get(measureKey)).precision = precision;
if (!oldPrecision.equals(precision))
save = true;
}
}
/**
* Tells if a given measure is global or local (i.e. if it's station independent
* or station dependent)
* <br>Author: Bertoli Marco
* @param key search's key for given measure
* @return true if measure is global
*/
public boolean isGlobalMeasure(Object key) {
String type = getMeasureType(key);
return type.equals(SimulationDefinition.MEASURE_S_X) ||
type.equals(SimulationDefinition.MEASURE_S_RP) ||
type.equals(SimulationDefinition.MEASURE_S_CN);
}
/**
* Sets preloaded jobs on a given station for a given class
* @param jobs number of jobs to be put in queue
* @param stationKey search's key for station
* @param classKey search's key for class
* Author: Bertoli Marco
*/
public void setPreloadedJobs(Integer jobs, Object stationKey, Object classKey) {
StationClassData current =
(StationClassData)stationDetailsBDM.get(classKey, stationKey);
if(current!=null){
if (!current.preload.equals(jobs))
save = true;
current.preload = jobs;
}
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -