Commit 52810173 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Added extendable environment sensor

parent 7e67d8f9
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.SensorComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.Environment;
public interface EnvironmentSensor extends SensorComponent {
Environment getEnvironment();
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment;
import java.util.ArrayList;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.Environment;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.EnvironmentProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.plugin.EnvironmentSensorPlugin;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public class ExtendableEnvironmentSensor implements EnvironmentSensor {
private Host _host;
private List<EnvironmentSensorPlugin> _plugins;
public ExtendableEnvironmentSensor(Host pHost, List<EnvironmentSensorPlugin> pPlugins) {
_host = pHost;
_plugins = pPlugins;
}
@Override
public Host getHost() {
return _host;
}
@Override
public void initialize() {
}
@Override
public void shutdown() {
}
@Override
public Environment getEnvironment() {
List<EnvironmentProperty> properties = new ArrayList<>();
for (EnvironmentSensorPlugin plugin : _plugins) {
properties.addAll(plugin.getEnvironmentProperties());
}
return new Environment(properties);
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment;
import java.util.ArrayList;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponent;
import de.tudarmstadt.maki.simonstrator.api.component.HostComponentFactory;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.plugin.EnvironmentSensorPlugin;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public class ExtendableEnvironmentSensorFactory implements HostComponentFactory {
private List<EnvironmentSensorPlugin> _plugins = new ArrayList<>();
@Override
public HostComponent createComponent(Host host) {
List<EnvironmentSensorPlugin> plugins = new ArrayList<>();
for (EnvironmentSensorPlugin environmentSensorPlugin : _plugins) {
try {
EnvironmentSensorPlugin clone = environmentSensorPlugin.clone();
clone.setHost(host);
plugins.add(clone);
} catch (CloneNotSupportedException e) {
throw new AssertionError("Each " + EnvironmentSensorPlugin.class.getSimpleName() + " must be clonable");
}
}
return new ExtendableEnvironmentSensor(host, plugins);
}
public void setPlugin(EnvironmentSensorPlugin pPlugin) {
_plugins.add(pPlugin);
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public class Environment {
private List<EnvironmentProperty> _properties;
public Environment() {
this(new ArrayList<>());
}
public Environment(List<EnvironmentProperty> pProperties) {
_properties = pProperties;
}
public void addProperty(EnvironmentProperty pProperty) {
_properties.add(pProperty);
}
public List<EnvironmentProperty> getProperties() {
return Collections.unmodifiableList(_properties);
}
public <T extends Object> List<T> getProperties(Class<T> pClass) {
List<T> results = new ArrayList<>();
for (EnvironmentProperty environmentProperty : _properties) {
if (pClass.isAssignableFrom(environmentProperty.getClass())) {
results.add((T) environmentProperty);
}
}
return results;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public interface EnvironmentProperty {
// Enabler Interface
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public class JamProperty implements RoadProperty {
private Location _location;
private RoadNetworkEdge _edge;
private boolean _jammed;
public JamProperty(Location pLocation, RoadNetworkEdge pEdge, boolean pJammed) {
_location = pLocation;
_edge = pEdge;
_jammed = pJammed;
}
@Override
public Location getLocation() {
return _location;
}
@Override
public RoadNetworkEdge getEdge() {
return _edge;
}
public boolean isJammed() {
return _jammed;
}
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public interface LocationBasedEnvironmentProperty extends EnvironmentProperty {
Location getLocation();
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public interface RoadProperty extends LocationBasedEnvironmentProperty {
RoadNetworkEdge getEdge();
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.plugin;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.EnvironmentProperty;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public interface EnvironmentSensorPlugin extends Cloneable {
List<EnvironmentProperty> getEnvironmentProperties();
EnvironmentSensorPlugin clone() throws CloneNotSupportedException;
void setHost(Host pHost);
}
/*
* Copyright (c) 2005-2010 KOM – Multimedia Communications Lab
*
* This file is part of Simonstrator.KOM.
*
* Simonstrator.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.tudarmstadt.maki.simonstrator.api.component.sensor.environment.plugin;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.EnvironmentProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.JamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSRequest;
import de.tudarmstadt.maki.simonstrator.api.component.sis.exception.InformationNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sis.type.SiSTypes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
/**
* @author Tobias Meuser (tobias.meuser@kom.tu-darmstadt.de)
* @version 1.0 at 27.02.2018
*
*/
public class JamEnvironmentSensorPlugin implements EnvironmentSensorPlugin {
private Host _host;
private SiSComponent _sis;
private double _accuracy;
private static Random _random = Randoms.getRandom(JamEnvironmentSensorPlugin.class);
@XMLConfigurableConstructor({ "accuracy" })
public JamEnvironmentSensorPlugin(double pAccuracy) {
setAccuracy(pAccuracy);
}
@Override
public void setHost(Host pHost) {
_host = pHost;
}
@Override
public List<EnvironmentProperty> getEnvironmentProperties() {
if (_sis == null) {
try {
_sis = _host.getComponent(SiSComponent.class);
} catch (ComponentNotAvailableException e) {
throw new AssertionError("SiS requried!", e);
}
}
try {
RoadNetworkEdge edge = _sis.get().localState(SiSTypes.ROAD_EDGE, SiSRequest.NONE);
Location location = _sis.get().localState(SiSTypes.PHY_LOCATION, SiSRequest.NONE);
boolean jammed = edge.isActive();
if (_random.nextDouble() >= _accuracy) {
jammed = !jammed;
}
List<EnvironmentProperty> properties = new ArrayList<>();
EnvironmentProperty property = new JamProperty(location, edge, jammed);
properties.add(property);
return properties;
} catch (InformationNotAvailableException e) {
throw new AssertionError(SiSTypes.ROAD_EDGE + " and " + SiSTypes.PHY_LOCATION + " are required!");
}
}
@Override
public JamEnvironmentSensorPlugin clone() throws CloneNotSupportedException {
return new JamEnvironmentSensorPlugin(_accuracy);
}
public void setAccuracy(double pAccuracy) {
_accuracy = pAccuracy;
}
}
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