Commit b1395ed4 authored by cabrerac's avatar cabrerac
Browse files

maaco version running

parent 1fb6c66c
eclipse.preferences.version=1
encoding/<project>=UTF-8
encoding/pom.xml=UTF-8
encoding/src=UTF-8
eclipse.preferences.version=1
encoding/<project>=UTF-8
encoding/pom.xml=UTF-8
encoding/src=UTF-8
This diff is collapsed.
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.analyzer.metric.spatial;
import java.util.ArrayList;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.common.metric.AbstractMetric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.ActiveSpatialMetric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.Metric;
public abstract class AbstractSpatialMetric<T extends AbstractSpatialMetricValue<?>> extends AbstractMetric<T> implements Metric<T>, ActiveSpatialMetric<T> {
protected boolean aggregate = false;
public AbstractSpatialMetric(String pDescription, MetricUnit pUnit) {
super(pDescription, pUnit);
}
public AbstractSpatialMetric(String pName, String pDescription, MetricUnit pUnit) {
super(pName, pDescription, pUnit);
}
protected void setOverallMetric(T aggregate) {
super.setOverallMetric(aggregate);
}
@Override
public boolean isAggregated() {
return aggregate;
}
private List<ActiveSpatialMetricListener> _listeners = new ArrayList<>();
@Override
public void addActiveSpatialMetricListener(ActiveSpatialMetricListener pListener) {
_listeners.add(pListener);
}
@Override
public void notifyListeners() {
for (ActiveSpatialMetricListener activeSpatialMetricListener : _listeners) {
activeSpatialMetricListener.onMetricUpdate(this);
}
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.analyzer.metric.spatial;
import java.util.ArrayList;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.common.metric.AbstractMetric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.ActiveSpatialMetric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.Metric;
public abstract class AbstractSpatialMetric<T extends AbstractSpatialMetricValue<?>> extends AbstractMetric<T> implements Metric<T>, ActiveSpatialMetric<T> {
protected boolean aggregate = false;
public AbstractSpatialMetric(String pDescription, MetricUnit pUnit) {
super(pDescription, pUnit);
}
public AbstractSpatialMetric(String pName, String pDescription, MetricUnit pUnit) {
super(pName, pDescription, pUnit);
}
protected void setOverallMetric(T aggregate) {
super.setOverallMetric(aggregate);
}
@Override
public boolean isAggregated() {
return aggregate;
}
private List<ActiveSpatialMetricListener> _listeners = new ArrayList<>();
@Override
public void addActiveSpatialMetricListener(ActiveSpatialMetricListener pListener) {
_listeners.add(pListener);
}
@Override
public void notifyListeners() {
for (ActiveSpatialMetricListener activeSpatialMetricListener : _listeners) {
activeSpatialMetricListener.onMetricUpdate(this);
}
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.analyzer.metric.spatial;
import de.tudarmstadt.maki.simonstrator.api.common.metric.ActiveSpatialMetric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.ActiveSpatialMetric.SpatialMetricValue;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
public abstract class AbstractSpatialMetricValue<S extends Object> implements SpatialMetricValue<S> {
private S value;
private Location location;
private ActiveSpatialMetric<?> metric;
public AbstractSpatialMetricValue(ActiveSpatialMetric<?> pMetric) {
metric = pMetric;
}
public AbstractSpatialMetricValue(ActiveSpatialMetric<?> pMetric, S pValue) {
metric = pMetric;
value = pValue;
}
public void setValue(S pValue, Location pLocation) {
value = pValue;
location = pLocation;
metric.notifyListeners();
}
@Override
public S getValue() {
return value;
}
@Override
public Location getLocation() {
return location;
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.analyzer.metric.spatial;
import de.tudarmstadt.maki.simonstrator.api.common.metric.ActiveSpatialMetric;
import de.tudarmstadt.maki.simonstrator.api.common.metric.ActiveSpatialMetric.SpatialMetricValue;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
public abstract class AbstractSpatialMetricValue<S extends Object> implements SpatialMetricValue<S> {
private S value;
private Location location;
private ActiveSpatialMetric<?> metric;
public AbstractSpatialMetricValue(ActiveSpatialMetric<?> pMetric) {
metric = pMetric;
}
public AbstractSpatialMetricValue(ActiveSpatialMetric<?> pMetric, S pValue) {
metric = pMetric;
value = pValue;
}
public void setValue(S pValue, Location pLocation) {
value = pValue;
location = pLocation;
metric.notifyListeners();
}
@Override
public S getValue() {
return value;
}
@Override
public Location getLocation() {
return location;
}
}
\ No newline at end of file
/*
* Copyright (c) 2005-2011 KOM - Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.common;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import de.tud.kom.p2psim.api.simengine.SimulatorObserver;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.util.db.dao.DAO;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent;
/**
* New Monitor-Component to work with the simonstrator-API (provides
* overlay-access to analyzers)
*
* @author Bjoern Richerzhagen
* @version 1.0, Jul 8, 2013
*/
public class DefaultMonitor implements MonitorComponent, EventHandler,
SimulatorObserver {
private final static List<Analyzer> analyzers = new LinkedList<Analyzer>();
private final static Map<Class<?>, Logger> loggers = new LinkedHashMap<Class<?>, Logger>();
private static DefaultMonitor singletonInstance = null;
private DefaultMonitor() {
this.isMonitoring = false;
}
public static DefaultMonitor getInstance() {
if (singletonInstance == null) {
singletonInstance = new DefaultMonitor();
}
return singletonInstance;
}
@Override
public <A extends Analyzer> List<A> getAnalyzers(Class<A> analyzerType)
throws AnalyzerNotAvailableException {
List<A> found = new LinkedList<A>();
for (Analyzer a : analyzers) {
if (analyzerType.isInstance(a)) {
found.add(analyzerType.cast(a));
}
}
if (found.isEmpty()) {
throw new AnalyzerNotAvailableException();
}
return found;
}
@Override
public <A extends Analyzer> void registerAnalyzer(A analyzer) {
if (!analyzers.contains(analyzer)) {
analyzers.add(analyzer);
}
}
@Override
public void log(Class<?> subject, Level level, String msg, Object... data) {
Logger log = loggers.get(subject);
if (log == null) {
log = Logger.getLogger(subject);
loggers.put(subject, log);
}
switch (level) {
case DEBUG:
if (log.isDebugEnabled()) {
log.debug(String.format(msg, data));
}
break;
case ERROR:
log.error(String.format(msg, data));
break;
case WARN:
log.warn(String.format(msg, data));
break;
case INFO:
if (log.isInfoEnabled()) {
log.info(String.format(msg, data));
}
break;
}
}
/**
* Called by the Configurator
*
* @param analyzer
*/
public void setAnalyzer(Analyzer analyzer) {
Monitor.registerAnalyzer(analyzer);
}
/**
* Specifies where to write the monitoring results to.
*
* @param output
* writer (e.g. FileWriter, StringWriter, ...)
*/
public void setResultWriter(Writer output) {
this.output = new BufferedWriter(output);
}
/**
* @deprecated use the MetricOutputDAO instead!
* @param tableName
*/
public void setTableName(String tableName) {
System.out.println("Table Name is set to: " + tableName);
if (tableName != null && !tableName.equals("")) {
DAO.setDatabase(tableName);
}
}
/*
* FROM HERE ON: deprecated!
*/
private BufferedWriter output = new BufferedWriter(new OutputStreamWriter(
System.out));
private MonitorState state = MonitorState.INIT;
private boolean isMonitoring = false;
protected String experimentDescription = "IS NOT SET";
public final int MONITOR_START = 1;
public final int MONITOR_STOP = 2;
public final int MONITOR_TEST = 3;
public void setStart(long time) {
if (state.equals(MonitorState.STOP_SET)) {
state = MonitorState.READY;
} else if (state.equals(MonitorState.INIT)) {
state = MonitorState.START_SET;
} else {
return;
}
Event.scheduleWithDelay(Time.getCurrentTime() + time, this, null,
MONITOR_START);
Event.scheduleWithDelay(0, this, null, MONITOR_TEST);
Simulator.getInstance().addObserver(this);
}
public void setStop(long time) {
if (state.equals(MonitorState.START_SET)) {
state = MonitorState.READY;
} else if (state.equals(MonitorState.INIT)) {
state = MonitorState.STOP_SET;
} else {
return;
}
Event.scheduleWithDelay(time, this, null, MONITOR_STOP);
}
@Override
public void simulationFinished() {
close();
}
public void setExperimentDescription(String description) {
this.experimentDescription = description;
}
public String getExperimentDescription() {
return experimentDescription;
}
public void close() {
if (this.isMonitoring && analyzers.size() != 0) {
try {
output.write("*******************************************************\n");
output.write("# Monitoring results \n");
output.newLine();
for (Analyzer analyzer : analyzers) {
analyzer.stop(output);
}
output.write("*******************************************************\n");
output.close();
} catch (IOException e) {
throw new AssertionError();
}
}
this.isMonitoring = false;
}
@Override
public void eventOccurred(Object se, int type) {
if (type == MONITOR_START) {
this.isMonitoring = true;
for (Analyzer analyzer : analyzers) {
analyzer.start();
}
} else if (type == MONITOR_TEST) {
//
} else if (type == MONITOR_STOP) {
this.close();
} else {
throw new AssertionError("Unknown event type.");
}
}
private enum MonitorState {
INIT, START_SET, STOP_SET, READY;
}
}
/*
* Copyright (c) 2005-2011 KOM - Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.common;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import de.tud.kom.p2psim.api.simengine.SimulatorObserver;
import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.util.db.dao.DAO;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Monitor;
import de.tudarmstadt.maki.simonstrator.api.Monitor.Level;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent;
/**
* New Monitor-Component to work with the simonstrator-API (provides
* overlay-access to analyzers)
*
* @author Bjoern Richerzhagen
* @version 1.0, Jul 8, 2013
*/
public class DefaultMonitor implements MonitorComponent, EventHandler,
SimulatorObserver {
private final static List<Analyzer> analyzers = new LinkedList<Analyzer>();
private final static Map<Class<?>, Logger> loggers = new LinkedHashMap<Class<?>, Logger>();
private static DefaultMonitor singletonInstance = null;
private DefaultMonitor() {
this.isMonitoring = false;
}
public static DefaultMonitor getInstance() {
if (singletonInstance == null) {
singletonInstance = new DefaultMonitor();
}
return singletonInstance;
}
@Override
public <A extends Analyzer> List<A> getAnalyzers(Class<A> analyzerType)
throws AnalyzerNotAvailableException {
List<A> found = new LinkedList<A>();
for (Analyzer a : analyzers) {
if (analyzerType.isInstance(a)) {
found.add(analyzerType.cast(a));
}
}
if (found.isEmpty()) {
throw new AnalyzerNotAvailableException();
}
return found;
}
@Override
public <A extends Analyzer> void registerAnalyzer(A analyzer) {
if (!analyzers.contains(analyzer)) {
analyzers.add(analyzer);
}
}
@Override
public void log(Class<?> subject, Level level, String msg, Object... data) {
Logger log = loggers.get(subject);
if (log == null) {
log = Logger.getLogger(subject);
loggers.put(subject, log);
}
switch (level) {
case DEBUG:
if (log.isDebugEnabled()) {
log.debug(String.format(msg, data));
}
break;
case ERROR:
log.error(String.format(msg, data));
break;
case WARN:
log.warn(String.format(msg, data));
break;
case INFO:
if (log.isInfoEnabled()) {
log.info(String.format(msg, data));
}
break;
}
}
/**
* Called by the Configurator
*
* @param analyzer
*/
public void setAnalyzer(Analyzer analyzer) {
Monitor.registerAnalyzer(analyzer);
}
/**
* Specifies where to write the monitoring results to.
*
* @param output
* writer (e.g. FileWriter, StringWriter, ...)
*/
public void setResultWriter(Writer output) {
this.output = new BufferedWriter(output);
}
/**
* @deprecated use the MetricOutputDAO instead!
* @param tableName
*/
public void setTableName(String tableName) {
System.out.println("Table Name is set to: " + tableName);
if (tableName != null && !tableName.equals("")) {
DAO.setDatabase(tableName);
}
}
/*
* FROM HERE ON: deprecated!
*/
private BufferedWriter output = new BufferedWriter(new OutputStreamWriter(
System.out));
private MonitorState state = MonitorState.INIT;
private boolean isMonitoring = false;
protected String experimentDescription = "IS NOT SET";
public final int MONITOR_START = 1;
public final int MONITOR_STOP = 2;
public final int MONITOR_TEST = 3;
public void setStart(long time) {
if (state.equals(MonitorState.STOP_SET)) {
state = MonitorState.READY;
} else if (state.equals(MonitorState.INIT)) {
state = MonitorState.START_SET;
} else {
return;
}
Event.scheduleWithDelay(Time.getCurrentTime() + time, this, null,
MONITOR_START);
Event.scheduleWithDelay(0, this, null, MONITOR_TEST);
Simulator.getInstance().addObserver(this);
}
public void setStop(long time) {
if (state.equals(MonitorState.START_SET)) {
state = MonitorState.READY;
} else if (state.equals(MonitorState.INIT)) {
state = MonitorState.STOP_SET;
} else {
return;
}
Event.scheduleWithDelay(time, this, null, MONITOR_STOP);
}
@Override
public void simulationFinished() {
close();
}
public void setExperimentDescription(String description) {
this.experimentDescription = description;
}
public String getExperimentDescription() {
return experimentDescription;
}
public void close() {
if (this.isMonitoring && analyzers.size() != 0) {
try {
output.write("*******************************************************\n");
output.write("# Monitoring results \n");
output.newLine();
for (Analyzer analyzer : analyzers) {
analyzer.stop(output);
}
output.write("*******************************************************\n");
output.close();
} catch (IOException e) {
throw new AssertionError();
}
}
this.isMonitoring = false;
}
@Override
public void eventOccurred(Object se, int type) {
if (type == MONITOR_START) {
this.isMonitoring = true;
for (Analyzer analyzer : analyzers) {
analyzer.start();
}
} else if (type == MONITOR_TEST) {
//
} else if (type == MONITOR_STOP) {
this.close();
} else {
throw new AssertionError("Unknown event type.");
}
}
private enum MonitorState {
INIT, START_SET, STOP_SET, READY;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.energy;
/**
* This interface implements basic methods used by the EnergyModel to simulate a
* battery.
*
* @author Fabio Zöllner
* @version 1.0, 20.04.2012
*/
public interface Battery extends Cloneable {
/**
* Returns the current status of the Battery as a Percentage-Value
*
* @return double between 0 and 100
*/
public double getCurrentPercentage();
/**
* Returns the current energy level in uJ.
*
* @return the current capacity of the battery in uJ
*/
public double getCurrentEnergyLevel();
/**
* Returns the consumed energy in uJoule
*
* @return The consumed Energy in uJoule
*/
public double getConsumedEnergy();
/**
* Resets the Battery to the initial Energy-Level
*/
public void reset();
/**
* Sets the battery to a given percentage.
*
* @param double between 0 and 100
*/
public void setToPercentage(double percentage);
/**
* Consumes the given amount of energy in uJoule (10^-6)
*
* @param energy
* Energy in uJoule
*/
public void consumeEnergy(double energy);
/**
* Returns true, if the battery is empty. In such a case, there should be no
* further calls to consumeEnergy.
*
* @return
*/
public boolean isEmpty();
/**
* Clone a battery, used during configuration
*
* @param battery
* @return
*/
public Battery clone();
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.energy;
/**
* This interface implements basic methods used by the EnergyModel to simulate a
* battery.
*
* @author Fabio Zöllner
* @version 1.0, 20.04.2012
*/
public interface Battery extends Cloneable {
/**
* Returns the current status of the Battery as a Percentage-Value
*
* @return double between 0 and 100
*/
public double getCurrentPercentage();
/**
* Returns the current energy level in uJ.
*
* @return the current capacity of the battery in uJ
*/
public double getCurrentEnergyLevel();
/**
* Returns the consumed energy in uJoule
*
* @return The consumed Energy in uJoule
*/
public double getConsumedEnergy();
/**
* Resets the Battery to the initial Energy-Level
*/
public void reset();
/**
* Sets the battery to a given percentage.
*
* @param double between 0 and 100
*/
public void setToPercentage(double percentage);
/**
* Consumes the given amount of energy in uJoule (10^-6)
*
* @param energy
* Energy in uJoule
*/
public void consumeEnergy(double energy);
/**
* Returns true, if the battery is empty. In such a case, there should be no
* further calls to consumeEnergy.
*
* @return
*/
public boolean isEmpty();
/**
* Clone a battery, used during configuration
*
* @param battery
* @return
*/
public Battery clone();
}
/*
* Copyright (c) 2005-2013 KOM - Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tud.kom.p2psim.impl.scenario.simcfg2.utils;
import de.tud.kom.p2psim.impl.scenario.simcfg2.configuration.SimCfgConfiguration;
import org.apache.log4j.Logger;
import javax.script.*;
import java.util.Map;
/**
* @author Fabio Zöllner
* @version 1.0, 12.07.13
*/
public class SimCfgExpressionExecutor {
private static final Logger log = Logger.getLogger(SimCfgExpressionExecutor.class);
private ScriptEngine javascriptEngine = null;
public SimCfgExpressionExecutor() {
log.debug("Initializing JavaScript engine.");
javascriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
log.debug("JavaScript engine initialized.");
}
public boolean evaluate(SimCfgConfiguration configuration, String expression) {
Map<String, Boolean> flags = configuration.getFlags();
for (Map.Entry<String, Boolean> entry : flags.entrySet()) {
javascriptEngine.put(entry.getKey().replace(".", "_"), entry.getValue());
}
boolean result = evaluate(expression);
log.debug("Evaluated '" + expression + "' => " + result);
javascriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
return result;
}
public boolean evaluate(String expression) {
if (expression == null) return true;
expression = expression.replace(".", "_");
try {
Object value = javascriptEngine.eval(expression);
if (value instanceof Boolean) {
return (Boolean) value;
} else {
return false;
}
} catch (ScriptException e) {
String missingReference = findMissingReference(e);
if (missingReference == null) {
e.printStackTrace();
} else {
javascriptEngine.put(missingReference, false);
return evaluate(expression);
}
}
return false;
}
private String findMissingReference(Throwable e) {
Throwable cause = e.getCause();
if (cause == null) {
return null;
}
if (cause.getMessage().contains("is not defined.")) {
String message = cause.getMessage();
String[] parts = message.split("\\\"");
return parts[1];
} else {
if (cause.equals(e)) {
return null;
} else {
return findMissingReference(cause);
}
}
}
}
/*
* Copyright (c) 2005-2013 KOM - Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*/
package de.tud.kom.p2psim.impl.scenario.simcfg2.utils;
import de.tud.kom.p2psim.impl.scenario.simcfg2.configuration.SimCfgConfiguration;
import org.apache.log4j.Logger;
import javax.script.*;
import java.util.Map;
/**
* @author Fabio Zöllner
* @version 1.0, 12.07.13
*/
public class SimCfgExpressionExecutor {
private static final Logger log = Logger.getLogger(SimCfgExpressionExecutor.class);
private ScriptEngine javascriptEngine = null;
public SimCfgExpressionExecutor() {
log.debug("Initializing JavaScript engine.");
javascriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
log.debug("JavaScript engine initialized.");
}
public boolean evaluate(SimCfgConfiguration configuration, String expression) {
Map<String, Boolean> flags = configuration.getFlags();
for (Map.Entry<String, Boolean> entry : flags.entrySet()) {
javascriptEngine.put(entry.getKey().replace(".", "_"), entry.getValue());
}
boolean result = evaluate(expression);
log.debug("Evaluated '" + expression + "' => " + result);
javascriptEngine.getBindings(ScriptContext.ENGINE_SCOPE).clear();
return result;
}
public boolean evaluate(String expression) {
if (expression == null) return true;
expression = expression.replace(".", "_");
try {
Object value = javascriptEngine.eval(expression);
if (value instanceof Boolean) {
return (Boolean) value;
} else {
return false;
}
} catch (ScriptException e) {
String missingReference = findMissingReference(e);
if (missingReference == null) {
e.printStackTrace();
} else {
javascriptEngine.put(missingReference, false);
return evaluate(expression);
}
}
return false;
}
private String findMissingReference(Throwable e) {
Throwable cause = e.getCause();
if (cause == null) {
return null;
}
if (cause.getMessage().contains("is not defined.")) {
String message = cause.getMessage();
String[] parts = message.split("\\\"");
return parts[1];
} else {
if (cause.equals(e)) {
return null;
} else {
return findMissingReference(cause);
}
}
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
/**
* In the current implementation, {@link AttractionPoint}s cannot move anymore.
* We hold a static list of attraction points to ensure that there exists only
* one instance of each name at a time.
*
* @author Christoph Muenker, Bjoern Richerzhagen
* @version 1.0, 02.07.2013
*/
public class AttractionPointImpl extends PositionVector
implements IAttractionPoint {
protected static Random rnd = Randoms.getRandom(AttractionPoint.class);
private static Map<String, AttractionPointImpl> instances = new LinkedHashMap<>();
private String name;
private double weight = 0;
private double radius = 0;
public AttractionPointImpl(String name, PositionVector posVec) {
super(posVec);
this.name = name;
if (instances.containsKey(name)) {
throw new AssertionError("Name "+name+" already in use for an attraction point.");
}
instances.put(name, this);
}
@Override
public String getName() {
return name;
}
@Override
public double getWeight() {
return weight;
}
@Override
public double getRadius() {
return radius;
}
@Override
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public IAttractionPoint clone(String newName) {
return new AttractionPointImpl(name, this);
}
@Override
public int getTransmissionSize() {
return super.getTransmissionSize() + name.length() + Double.BYTES * 2;
}
@Override
public int hashCode() {
final int prime = 31;
int result = prime * ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (getClass() != obj.getClass())
return false;
AttractionPointImpl other = (AttractionPointImpl) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static Map<String, AttractionPointImpl> getInstances() {
return instances;
}
public static void setInstances(Map<String, AttractionPointImpl> instances) {
AttractionPointImpl.instances = instances;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.topology.movement.modularosm.attraction;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
import de.tud.kom.p2psim.impl.topology.util.PositionVector;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.IAttractionPoint;
/**
* In the current implementation, {@link AttractionPoint}s cannot move anymore.
* We hold a static list of attraction points to ensure that there exists only
* one instance of each name at a time.
*
* @author Christoph Muenker, Bjoern Richerzhagen
* @version 1.0, 02.07.2013
*/
public class AttractionPointImpl extends PositionVector
implements IAttractionPoint {
protected static Random rnd = Randoms.getRandom(AttractionPoint.class);
private static Map<String, AttractionPointImpl> instances = new LinkedHashMap<>();
private String name;
private double weight = 0;
private double radius = 0;
public AttractionPointImpl(String name, PositionVector posVec) {
super(posVec);
this.name = name;
if (instances.containsKey(name)) {
throw new AssertionError("Name "+name+" already in use for an attraction point.");
}
instances.put(name, this);
}
@Override
public String getName() {
return name;
}
@Override
public double getWeight() {
return weight;
}
@Override
public double getRadius() {
return radius;
}
@Override
public void setWeight(double weight) {
this.weight = weight;
}
@Override
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public IAttractionPoint clone(String newName) {
return new AttractionPointImpl(name, this);
}
@Override
public int getTransmissionSize() {
return super.getTransmissionSize() + name.length() + Double.BYTES * 2;
}
@Override
public int hashCode() {
final int prime = 31;
int result = prime * ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (getClass() != obj.getClass())
return false;
AttractionPointImpl other = (AttractionPointImpl) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static Map<String, AttractionPointImpl> getInstances() {
return instances;
}
public static void setInstances(Map<String, AttractionPointImpl> instances) {
AttractionPointImpl.instances = instances;
}
}
......@@ -86,9 +86,12 @@ public class ShowMapQuestMapViz extends JComponent
try {
//Based on the meters per pixel, the needed height and width in pixels can be determined.
int pxx = (int) (worldDimensions.getX() / GPSCalculation.getMetersPerPixel());
if (pxx > 1920)
pxx = 1920;
int pxy = (int) (worldDimensions.getY() / GPSCalculation.getMetersPerPixel());
String imageUrl = "http://www.mapquestapi.com/staticmap/v4/getmap?key="
if (pxy > 1920)
pxy = 1920;
String imageUrl = "http://www.mapquestapi.com/staticmap/v5/map?key="
+ mapQuestKey + "&type=" + mapType
+ "&imagetype=jpeg&center="
+ GPSCalculation.getLatCenter() + ","
......
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.vehicular.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.properties.RoadProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
public class DefaultVehicleDecisionComponent extends AbstractVehicleDecisionComponent {
public DefaultVehicleDecisionComponent(Host pHost) {
super(pHost);
}
@Override
public BenefitBasedRoute getOptimalRoute(RoadNetworkRoute pInvestigatedRoute, List<RoadInformation> knownInformation) {
if (getVehicleInformation().isValid()) {
return new BenefitBasedRoute(0, pInvestigatedRoute);
}
return null;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of PeerfactSim.KOM.
*
* PeerfactSim.KOM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* PeerfactSim.KOM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PeerfactSim.KOM. If not, see <http://www.gnu.org/licenses/>.
*
*/
package de.tud.kom.p2psim.impl.vehicular.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.properties.RoadProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
public class DefaultVehicleDecisionComponent extends AbstractVehicleDecisionComponent {
public DefaultVehicleDecisionComponent(Host pHost) {
super(pHost);
}
@Override
public BenefitBasedRoute getOptimalRoute(RoadNetworkRoute pInvestigatedRoute, List<RoadInformation> knownInformation) {
if (getVehicleInformation().isValid()) {
return new BenefitBasedRoute(0, pInvestigatedRoute);
}
return null;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment