Commit a9baf33b authored by Björn Richerzhagen's avatar Björn Richerzhagen
Browse files

Merge remote-tracking branch 'simonstrator/tm/vehicular-services'

parents 705984e5 f476dae3
......@@ -19,94 +19,94 @@
*/
package de.tud.kom.p2psim.impl.util.stat.distributions;
package de.tud.kom.p2psim.impl.util.stat.distributions;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.util.Distribution;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
import umontreal.iro.lecuyer.probdist.LognormalDist;
public class MixedLogNormalDistribution implements Distribution {
@Override
public String toString() {
return "MixedLogNormalDistribution [weight1=" + weight1 + ", mu1="
+ mu1 + ", sigma1=" + sigma1 + ", weight2=" + weight2
+ ", mu2=" + mu2 + ", sigma2=" + sigma2 + "]";
}
private double weight1;
private double mu1;
private double sigma1;
private double weight2;
private double mu2;
private double sigma2;
private LognormalDist distr1;
private LognormalDist distr2;
@XMLConfigurableConstructor({"mu1", "mu2", "sigma1", "sigma2", "weight1", "weight2"})
public MixedLogNormalDistribution(double mu1, double mu2, double sigma1, double sigma2, double weight1, double weight2) {
this.mu1 = mu1;
this.mu2 = mu2;
this.sigma1 = sigma1;
this.sigma2 = sigma2;
this.weight1 = weight1;
this.weight2 = weight2;
distr1 = new LognormalDist(mu1, sigma1);
distr2 = new LognormalDist(mu2, sigma2);
}
public double returnValue() {
public class MixedLogNormalDistribution implements Distribution {
@Override
public String toString() {
return "MixedLogNormalDistribution [weight1=" + weight1 + ", mu1="
+ mu1 + ", sigma1=" + sigma1 + ", weight2=" + weight2
+ ", mu2=" + mu2 + ", sigma2=" + sigma2 + "]";
}
private double weight1;
private double mu1;
private double sigma1;
private double weight2;
private double mu2;
private double sigma2;
private LognormalDist distr1;
private LognormalDist distr2;
@XMLConfigurableConstructor({"mu1", "mu2", "sigma1", "sigma2", "weight1", "weight2"})
public MixedLogNormalDistribution(double mu1, double mu2, double sigma1, double sigma2, double weight1, double weight2) {
this.mu1 = mu1;
this.mu2 = mu2;
this.sigma1 = sigma1;
this.sigma2 = sigma2;
this.weight1 = weight1;
this.weight2 = weight2;
distr1 = new LognormalDist(mu1, sigma1);
distr2 = new LognormalDist(mu2, sigma2);
}
public double returnValue() {
double random = Randoms.getRandom(MixedLogNormalDistribution.class)
.nextDouble();
double result = 0;
try {
result = weight1 * distr1.inverseF(random) + weight2
* distr2.inverseF(random);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* returns a random value distributed after a mixed lognormal distribution:
* _weight1 * lognormal1 + _weight2 * lognormal2 with lorgnormal1(_mu1,
* _sigma1) and with lorgnormal1(_mu2, _sigma2).
*
* @param _mu1
* @param _sigma1
* @param _weight1
* @param _mu2
* @param _sigma2
* @param _weight2
* @return
*/
public static double returnValue(double _mu1, double _sigma1,
double _weight1, double _mu2, double _sigma2, double _weight2) {
try {
LognormalDist d1 = new LognormalDist(_mu1, _sigma1);
LognormalDist d2 = new LognormalDist(_mu2, _sigma2);
.nextDouble();
double result = 0;
try {
result = weight1 * distr1.inverseF(random) + weight2
* distr2.inverseF(random);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* returns a random value distributed after a mixed lognormal distribution:
* _weight1 * lognormal1 + _weight2 * lognormal2 with lorgnormal1(_mu1,
* _sigma1) and with lorgnormal1(_mu2, _sigma2).
*
* @param _mu1
* @param _sigma1
* @param _weight1
* @param _mu2
* @param _sigma2
* @param _weight2
* @return
*/
public static double returnValue(double _mu1, double _sigma1,
double _weight1, double _mu2, double _sigma2, double _weight2) {
try {
LognormalDist d1 = new LognormalDist(_mu1, _sigma1);
LognormalDist d2 = new LognormalDist(_mu2, _sigma2);
double random = Randoms.getRandom(MixedLogNormalDistribution.class)
.nextDouble();
return _weight1 * d1.inverseF(random) + _weight2
* d2.inverseF(random);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}
.nextDouble();
return _weight1 * d1.inverseF(random) + _weight2
* d2.inverseF(random);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}
/*
* 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.util.stat.distributions;
import java.util.Random;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistributionImpl;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.util.Distribution;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class NormalDistribution implements Distribution {
private NormalDistributionImpl normal;
private Random randomGen = Randoms.getRandom(NormalDistribution.class);
private double mu;
private double sigma;
@XMLConfigurableConstructor({"mu", "sigma"})
public NormalDistribution(double mu, double sigma) {
this.mu = mu;
this.sigma = sigma;
normal = new NormalDistributionImpl(mu, sigma);
}
@Override
public double returnValue() {
double random = randomGen.nextDouble();
double result;
try {
result = normal.inverseCumulativeProbability(random);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = 0;
}
return result;
}
@Override
public String toString() {
return "NormalDistribution [mu=" + mu + ", sigma=" + sigma + "]";
}
/**
* returns a random value normally distributed with mu = _mu and sigma =
* _sigma.
*
* @param _mu
* @param _sigma
* @return as double
*/
public static double returnValue(double _mu, double _sigma) {
try {
NormalDistributionImpl d = new NormalDistributionImpl(_mu, _sigma);
return d.inverseCumulativeProbability(Randoms.getRandom(
NormalDistribution.class)
.nextDouble());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}
/*
* 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.util.stat.distributions;
import java.util.Random;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.NormalDistributionImpl;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.util.Distribution;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class NormalDistribution implements Distribution {
private NormalDistributionImpl normal;
private Random randomGen = Randoms.getRandom(NormalDistribution.class);
private double mu;
private double sigma;
@XMLConfigurableConstructor({"mu", "sigma"})
public NormalDistribution(double mu, double sigma) {
this.mu = mu;
this.sigma = sigma;
normal = new NormalDistributionImpl(mu, sigma);
}
@Override
public double returnValue() {
double random = randomGen.nextDouble();
double result;
try {
result = normal.inverseCumulativeProbability(random);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = 0;
}
return result;
}
@Override
public String toString() {
return "NormalDistribution [mu=" + mu + ", sigma=" + sigma + "]";
}
/**
* returns a random value normally distributed with mu = _mu and sigma =
* _sigma.
*
* @param _mu
* @param _sigma
* @return as double
*/
public static double returnValue(double _mu, double _sigma) {
try {
NormalDistributionImpl d = new NormalDistributionImpl(_mu, _sigma);
return d.inverseCumulativeProbability(Randoms.getRandom(
NormalDistribution.class)
.nextDouble());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}
......@@ -19,64 +19,64 @@
*/
package de.tud.kom.p2psim.impl.util.stat.distributions;
package de.tud.kom.p2psim.impl.util.stat.distributions;
import org.apache.commons.math.MathException;
import org.apache.commons.math.distribution.PoissonDistributionImpl;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.util.Distribution;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class PoissonDistribution implements Distribution {
private double lambda;
private PoissonDistributionImpl poisson;
@XMLConfigurableConstructor({"lambda"})
public PoissonDistribution(double lambda){
this.lambda = lambda;
this.poisson = new PoissonDistributionImpl(lambda);
}
// returns the x-value for a random value in the cdf
public double returnValue() {
public class PoissonDistribution implements Distribution {
private double lambda;
private PoissonDistributionImpl poisson;
@XMLConfigurableConstructor({"lambda"})
public PoissonDistribution(double lambda){
this.lambda = lambda;
this.poisson = new PoissonDistributionImpl(lambda);
}
// returns the x-value for a random value in the cdf
public double returnValue() {
double random = Randoms.getRandom(PoissonDistribution.class)
.nextDouble();
int result;
try {
result = poisson.inverseCumulativeProbability(random);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = 0;
}
return result;
}
/**
* returns a random value Poisson distributed with lamda = _lamda.
* @param _lamda
* @return as double
*/
public static double returnValue(double _lamda) {
try {
PoissonDistributionImpl d = new PoissonDistributionImpl(_lamda);
.nextDouble();
int result;
try {
result = poisson.inverseCumulativeProbability(random);
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
result = 0;
}
return result;
}
/**
* returns a random value Poisson distributed with lamda = _lamda.
* @param _lamda
* @return as double
*/
public static double returnValue(double _lamda) {
try {
PoissonDistributionImpl d = new PoissonDistributionImpl(_lamda);
return d.inverseCumulativeProbability(Randoms.getRandom(
PoissonDistribution.class).nextDouble());
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
@Override
public String toString() {
return "PoissonDistribution [lambda=" + lambda + "]";
}
}
PoissonDistribution.class).nextDouble());
} catch (MathException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
@Override
public String toString() {
return "PoissonDistribution [lambda=" + lambda + "]";
}
}
......@@ -19,62 +19,62 @@
*/
package de.tud.kom.p2psim.impl.util.stat.distributions;
package de.tud.kom.p2psim.impl.util.stat.distributions;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.util.Distribution;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class UniformDistribution implements Distribution {
@Override
public String toString() {
return "UniformDistribution [min=" + min + ", max=" + max + "]";
}
private double min;
private double max;
private double factor;
@XMLConfigurableConstructor({"min", "max"})
public UniformDistribution(double min, double max) {
this.min = Math.min(min, max);
this.max = Math.max(min, max);
factor = Math.abs(max - min);
}
/**
* Delivers a random value distributed as the configured distribution.
*/
public double returnValue() {
public class UniformDistribution implements Distribution {
@Override
public String toString() {
return "UniformDistribution [min=" + min + ", max=" + max + "]";
}
private double min;
private double max;
private double factor;
@XMLConfigurableConstructor({"min", "max"})
public UniformDistribution(double min, double max) {
this.min = Math.min(min, max);
this.max = Math.max(min, max);
factor = Math.abs(max - min);
}
/**
* Delivers a random value distributed as the configured distribution.
*/
public double returnValue() {
return min + factor
* Randoms.getRandom(UniformDistribution.class).nextDouble();
}
/**
* delivers a random value that is uniformly distributed between the _min
* and the _max value.
*
* @param _min
* @param _max
* @return random value as double
*/
public static double returnValue(double _min, double _max) {
double lmin, lmax, lfactor;
if (_min < _max) {
lmin = _min;
lmax = _max;
} else {
lmin = _max;
lmax = _min;
}
lfactor = Math.abs(lmax - lmin);
* Randoms.getRandom(UniformDistribution.class).nextDouble();
}
/**
* delivers a random value that is uniformly distributed between the _min
* and the _max value.
*
* @param _min
* @param _max
* @return random value as double
*/
public static double returnValue(double _min, double _max) {
double lmin, lmax, lfactor;
if (_min < _max) {
lmin = _min;
lmax = _max;
} else {
lmin = _max;
lmax = _min;
}
lfactor = Math.abs(lmax - lmin);
return lmin + lfactor
* Randoms.getRandom(UniformDistribution.class).nextDouble();
}
}
* Randoms.getRandom(UniformDistribution.class).nextDouble();
}
}
/*
* 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;
import java.util.List;
import java.util.Set;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.route.Route;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSComponent;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSDataCallback;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInfoProperties;
import de.tudarmstadt.maki.simonstrator.api.component.sis.SiSInformationProvider.SiSProviderHandle;
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.VehicleInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.VehicleController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException;
public class DefaultVehicleInformationComponent implements VehicleInformationComponent {
private Host host;
private VehicleController controller;
private SimulationSetupExtractor extractor;
private String vehicleID;
public DefaultVehicleInformationComponent(Host host, VehicleController controller, SimulationSetupExtractor extractor, boolean pRouteKnown) {
this.host = host;
this.controller = controller;
this.extractor = extractor;
try {
SiSComponent siSComponent = getHost().getComponent(SiSComponent.class);
siSComponent.provide().nodeState(SiSTypes.ROAD_EDGE, new SiSDataCallback<RoadNetworkEdge>() {
@Override
public RoadNetworkEdge getValue(INodeID pNodeID,
SiSProviderHandle pProviderHandle)
throws InformationNotAvailableException {
if (pNodeID == getHost().getId()) {
if (isValid()) {
RoadNetworkRoute route = getCurrentRoute();
if (route != null) {
return route.getStart();
}
}
}
return null;
}
@Override
public Set<INodeID> getObservedNodes() {
return INodeID.getSingleIDSet(getHost().getId());
}
@Override
public SiSInfoProperties getInfoProperties() {
return new SiSInfoProperties();
}
});
siSComponent.provide().nodeState(SiSTypes.HEADING, new SiSDataCallback<Double>() {
@Override
public Double getValue(INodeID pNodeID,
SiSProviderHandle pProviderHandle)
throws InformationNotAvailableException {
if (pNodeID == getHost().getId()) {
if (isValid()) {
return controller.getVehicleHeading(vehicleID);
}
}
return null;
}
@Override
public Set<INodeID> getObservedNodes() {
return INodeID.getSingleIDSet(getHost().getId());
}
@Override
public SiSInfoProperties getInfoProperties() {
return new SiSInfoProperties();
}
});
if (pRouteKnown) {
siSComponent.provide().nodeState(SiSTypes.ROUTE, new SiSDataCallback<RoadNetworkRoute>() {
@Override
public RoadNetworkRoute getValue(INodeID pNodeID,
SiSProviderHandle pProviderHandle)
throws InformationNotAvailableException {
if (pNodeID == getHost().getId()) {
if (isValid()) {
return getCurrentRoute();
}
}
return null;
}
@Override
public Set<INodeID> getObservedNodes() {
return INodeID.getSingleIDSet(getHost().getId());
}
@Override
public SiSInfoProperties getInfoProperties() {
return new SiSInfoProperties();
}
});
}
} catch (ComponentNotAvailableException e) {
// Nothing to do!
}
}
@Override
public void setVehicleID(String pVehicleID) {
vehicleID = pVehicleID;
}
@Override
public void resetVehicleID() {
vehicleID = null;
}
@Override
public void initialize() {
// TODO Auto-generated method stub
}
@Override
public void shutdown() {
// TODO Auto-generated method stub
}
@Override
public Host getHost() {
return host;
}
@Override
public RoadNetworkRoute getCurrentRoute() {
return controller.getCurrentRoute(vehicleID);
}
@Override
public boolean changeCurrentRoute(RoadNetworkRoute pRoute) {
controller.rerouteVehicle(vehicleID, pRoute);
return true;
}
@Override
public RoadNetworkRoute findNewRoute() {
try {
return controller.findNewRoute(vehicleID);
} catch (NoAdditionalRouteAvailableException e) {
return null;
}
}
@Override
public RoadNetworkRoute findNewRoute(List<RoadNetworkEdge> pEdgesToAvoid,
boolean pKeepDestination) {
try {
return controller.findNewRoute(vehicleID, pEdgesToAvoid, pKeepDestination);
} catch (NoAdditionalRouteAvailableException
| NoExitAvailableException e) {
return null;
}
}
@Override
public boolean isValid() {
return vehicleID != null;
}
@Override
public void stopVehicle() {
controller.stopVehicle(vehicleID);
}
@Override
public double getCurrentSpeed() {
return controller.getVehicleSpeed(vehicleID);
}
@Override
public void addRouteListener(RouteListener listener) {
throw new UnsupportedOperationException();
}
@Override
public void removeRouteListener(RouteListener listener) {
throw new UnsupportedOperationException();
}
@Override
public void addRouteSegmentListener(RouteSegmentListener listener) {
throw new UnsupportedOperationException();
}
@Override
public void removeRouteSegmentListener(RouteSegmentListener listener) {
throw new UnsupportedOperationException();
}
@Override
public Route getRoute() {
throw new UnsupportedOperationException();
}
}
/*
* 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.tud.kom.p2psim.impl.vehicular.caching;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetInterface;
import de.tudarmstadt.maki.simonstrator.api.component.network.NetworkComponent.NetInterfaceName;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.transport.ConnectivityListener;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.VehicleInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.CachingComponent;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.JamInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
public class DefaultCachingComponent
implements CachingComponent, ConnectivityListener {
private Map<Class<? extends Object>, List<PointInformation>> _cache = new HashMap<>();
private Map<Integer, Integer> _lastColorValues = new HashMap<>();
private Host _host;
private CacheInvalidationStrategy _invalidationStrategy;
private CacheReplacementStrategy _replacementStrategy;
private CacheDecisionStrategy _decisionStrategy;
private double[] informationRatios = new double[] {1, 0.75, 0.5, 0.25, 0};
public DefaultCachingComponent(Host pHost,
CacheInvalidationStrategy pInvalidationStrategy,
CacheReplacementStrategy pReplacementStrategy,
CacheDecisionStrategy pDecisionStrategy) {
_host = pHost;
if (_host != null) {
_host.getNetworkComponent().getByName(NetInterfaceName.WIFI)
.addConnectivityListener(this);
}
_invalidationStrategy = pInvalidationStrategy;
_replacementStrategy = pReplacementStrategy;
_decisionStrategy = pDecisionStrategy;
}
@Override
public <T extends PointInformation> List<T> getDecidedCacheEntries(
Class<T> pCacheEntryClass) {
List<T> cacheEntries = getCacheEntries(pCacheEntryClass);
if (cacheEntries == null) {
return null;
}
Map<Object, List<PointInformation>> similarCacheEntries = new HashMap<>();
for (T t : cacheEntries) {
Object position = getEdgeOrPosition(t);
if (!similarCacheEntries.containsKey(position)) {
similarCacheEntries.put(position, new ArrayList<>());
}
similarCacheEntries.get(position).add(t);
}
List<T> decidedInformation = new ArrayList<>();
for (List<PointInformation> similarEntries : similarCacheEntries
.values()) {
PointInformation correctInformation = _decisionStrategy
.decideOnCorrectInformation(similarEntries);
decidedInformation.add((T) correctInformation);
}
return decidedInformation;
}
@Override
public <T extends PointInformation> List<T> getCacheEntries(
Class<T> pCacheEntryClass) {
if (_cache.containsKey(pCacheEntryClass)) {
List<? extends PointInformation> cacheEntries = _cache
.get(pCacheEntryClass);
List<T> results = new ArrayList<>();
for (int i = 0; i < cacheEntries.size(); i++) {
PointInformation object = cacheEntries.get(i);
if (_invalidationStrategy.checkInformation(object)) {
cacheEntries.remove(i--);
continue;
}
results.add((T) object);
}
return results;
}
return null;
}
@Override
public <T extends PointInformation> boolean containsEntry(T pCacheEntry) {
if (_cache.containsKey(pCacheEntry)) {
List<? extends Object> cacheEntries = _cache.get(pCacheEntry);
return cacheEntries.contains(pCacheEntry);
}
return false;
}
@Override
public <T extends PointInformation> void storeCacheEntry(T pCacheEntry) {
if (!_cache.containsKey(pCacheEntry.getClass())) {
_cache.put(pCacheEntry.getClass(), new ArrayList<>());
}
List<PointInformation> entries = _cache.get(pCacheEntry.getClass());
entries.add(pCacheEntry);
}
@Override
public void initialize() {
_cache.clear();
}
@Override
public void shutdown() {
}
@Override
public Host getHost() {
return _host;
}
@Override
public void wentOnline(Host pHost, NetInterface pNetInterface) {
_cache.clear();
}
@Override
public void wentOffline(Host pHost, NetInterface pNetInterface) {
}
@Override
public void clear() {
_cache.clear();
}
public Object getEdgeOrPosition(PointInformation information) {
if (information.hasAttribute(AvailableInformationAttributes.EDGE)) {
return information
.getAttribute(AvailableInformationAttributes.EDGE);
} else if (information instanceof RoadInformation) {
return ((RoadInformation) information).getEdge();
} else {
return information.getLocation();
}
}
public CacheDecisionStrategy getDecisionStrategy() {
return _decisionStrategy;
}
@Override
public String getNodeDescription() {
return " " + getHost().getId();
}
@Override
public int getNodeColorDimensions() {
return 2;
}
@Override
public String[] getNodeColorDimensionDescriptions() {
return new String[] {"Route Information", "Number of jam information"};
}
@Override
public String[] getNodeColorDescriptions(int pDimension) {
if (pDimension == 0) {
String[] labels = new String[informationRatios.length];
for (int i = 0; i < informationRatios.length; i++) {
labels[i] = informationRatios[i] * 100 + "%";
}
return labels;
} else if (pDimension == 1) {
String[] numbers = new String[10];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = String.valueOf(i);
}
return numbers;
}
return new String[0];
}
@Override
public int getNodeColor(int pDimension) {
if (pDimension == 0) {
List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
try {
VehicleInformationComponent vehicleInformationComponent = getHost().getComponent(VehicleInformationComponent.class);
if (vehicleInformationComponent.isValid()) {
RoadNetworkRoute currentRoute = vehicleInformationComponent.getCurrentRoute();
if (currentRoute != null) {
int count = 0;
int active = 0;
for (RoadNetworkEdge edge : currentRoute.getRoute()) {
if (edge.isActive()) {
if (decidedCacheEntries != null) {
for (JamInformation jamInformation : decidedCacheEntries) {
if (jamInformation.getEdge().equals(edge) && jamInformation.getValue()) {
count++;
break;
}
}
}
active++;
}
}
if (active != 0) {
double ratio = count / ((double) active);
for (int i = 0; i < informationRatios.length; i++) {
if (informationRatios[i] <= ratio) {
_lastColorValues.put(pDimension, i);
break;
}
}
} else {
_lastColorValues.put(pDimension, 0);
}
}
}
} catch (ComponentNotAvailableException e) {
}
if (_lastColorValues.containsKey(pDimension)) {
return _lastColorValues.get(pDimension);
}
return 0;
}
if (pDimension == 1) {
List<JamInformation> decidedCacheEntries = getDecidedCacheEntries(JamInformation.class);
if (decidedCacheEntries != null) {
_lastColorValues.put(pDimension, decidedCacheEntries.size());
}
if (_lastColorValues.containsKey(pDimension)) {
return Math.min(9, _lastColorValues.get(pDimension));
}
return 0;
}
return 0;
}
@Override
public boolean isActive() {
return getHost().getNetworkComponent().getByName(NetInterfaceName.MOBILE).isUp()
|| getHost().getNetworkComponent().getByName(NetInterfaceName.WIFI).isUp();
}
}
/*
* 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.caching.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.NumericVectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.VectoralJamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
public class AveragingCacheDecisionStrategy implements CacheDecisionStrategy {
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
double sum = 0;
double count = 0;
NumericVectoralProperty cloned = null;
for (T t : pSimilarPointInformation) {
RoadInformation roadInformation = ((RoadInformation) t);
NumericVectoralProperty property = (NumericVectoralProperty) roadInformation.getValue();
if (cloned == null) {
cloned = property.clone();
}
sum += property.getMostProbableValue();
count++;
}
double value = sum / count;
if (cloned instanceof VectoralJamProperty) {
((VectoralJamProperty) cloned).setSpeed(((int)(value / VectoralJamProperty.SCALING)) * VectoralJamProperty.SCALING);
} else {
throw new AssertionError("Unknown data type " + cloned.getClass().getSimpleName());
}
return (T) new RoadInformation(cloned);
}
}
/*
* 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.caching.decision;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
public enum CacheDecisionStrategyType {
DEFAULT(NewestCacheDecisionStrategy.class), NEWEST(NewestCacheDecisionStrategy.class), MAJORITY(MajorityVotingCacheDecisionStrategy.class), AVERAGING(AveragingCacheDecisionStrategy.class), TTL(TTLbasedCacheDecisionStrategy.class), OPTIMAL(OptimalCacheDecisionStrategy.class), RANDOM(RandomCacheDecisionStrategy.class), TTL_VECTOR(TTLbasedVectoralCacheDecisionStrategy.class), MAJORITY_VECTOR(MajorityVotingVectoralCacheDecisionStrategy.class);
private final Class<? extends CacheDecisionStrategy> decisionStrategy;
private final Map<String, String> params = new HashMap<>();
private CacheDecisionStrategyType(final Class<? extends CacheDecisionStrategy> decisionStrategy) {
this.decisionStrategy = decisionStrategy;
}
public CacheDecisionStrategy getDecisionStrategy() {
try {
try {
Constructor<? extends CacheDecisionStrategy> constructor = decisionStrategy.getConstructor(Map.class);
return constructor.newInstance(params);
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException | InvocationTargetException e) {
return decisionStrategy.newInstance();
}
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
public void addAttribute(String pKey, String pValue) {
params.put(pKey, pValue);
}
}
/*
* 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.caching.decision;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class MajorityVotingCacheDecisionStrategy implements CacheDecisionStrategy {
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
Map<Object, Integer> voting = new HashMap<>();
for (T t : pSimilarPointInformation) {
if (!voting.containsKey(t.getValue())) {
voting.put(t.getValue(), 0);
}
voting.put(t.getValue(), voting.get(t.getValue()) + 1);
}
Entry<Object, Integer> maxEntry = null;
for (Entry<Object, Integer> entry : voting.entrySet()) {
if (maxEntry == null) {
maxEntry = entry;
}
if (maxEntry.getValue() < entry.getValue()) {
maxEntry = entry;
}
}
long maxTimestamp = 0;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (t.getValue().equals(maxEntry.getKey()) && timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
return maxFitting;
}
}
/*
* 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.caching.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.TemporalDependencyMatrix;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
public class MajorityVotingVectoralCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND;
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
VectoralProperty currentProperty = null;
long minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
}
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
}
}
for (T t : pSimilarPointInformation) {
RoadInformation roadInformation = ((RoadInformation) t);
VectoralProperty property = (VectoralProperty) roadInformation.getValue();
TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
VectoralProperty agedProperty = property.age((maxTimestamp - property.getDetectionDate()) / SCALING, dependencyMatrix);
if (currentProperty != null) {
currentProperty = currentProperty.combine(agedProperty);
} else {
currentProperty = agedProperty;
}
}
TemporalDependencyMatrix dependencyMatrix = currentProperty.getDependencyMatrix();
VectoralProperty agedProperty = currentProperty.age((Time.getCurrentTime() - maxTimestamp) / SCALING, dependencyMatrix);
RoadInformation roadInformation = new RoadInformation(agedProperty);
copyAttributes((RoadInformation) pSimilarPointInformation.get(0), roadInformation);
return (T) roadInformation;
}
/**
* @param pT
* @param pRoadInformation
*/
private void copyAttributes(RoadInformation pSrc, RoadInformation pDest) {
for (AvailableInformationAttributes attribute : pSrc.getAvailableAttributes()) {
pDest.setAttribute(attribute, pSrc.getAttribute(attribute));
}
}
}
/*
* 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.caching.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class NewestCacheDecisionStrategy implements CacheDecisionStrategy {
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
long newestDetectionTime = 0;
T chosenInformation = null;
for (T t : pSimilarPointInformation) {
if (t.getDetectionDate() > newestDetectionTime) {
newestDetectionTime = t.getDetectionDate();
chosenInformation = t;
}
}
return chosenInformation;
}
}
/*
* 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.caching.decision;
import java.util.List;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.JamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
public class OptimalCacheDecisionStrategy implements CacheDecisionStrategy {
public OptimalCacheDecisionStrategy() {
}
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
RoadNetworkEdge edge = (RoadNetworkEdge) pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.EDGE);
double actualSpeed = edge.getCurrentSpeed();
JamProperty jamProperty = edge.getJamProperty();
long maxTimestamp;
if (jamProperty != null) {
maxTimestamp = jamProperty.getDetectionDate();
} else {
maxTimestamp = -1;
}
double difference = Double.MAX_VALUE;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
Object currentValue = t.getValue();
if (currentValue instanceof VectoralProperty) {
currentValue = ((VectoralProperty) currentValue).getMostProbableValue();
}
if (timestamp >= maxTimestamp) {
if (currentValue.equals(actualSpeed)) {
maxFitting = t;
difference = 0;
} else if (currentValue instanceof Number && Math.abs(((Number)currentValue).doubleValue() - actualSpeed) < difference) {
maxFitting = t;
difference = Math.abs(((Number)currentValue).doubleValue() - actualSpeed);
}
}
}
if (maxFitting == null) {
maxTimestamp = -1;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
if (timestamp >= maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
}
return maxFitting;
}
}
/*
* 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.caching.decision;
import java.util.List;
import java.util.Random;
import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class RandomCacheDecisionStrategy implements CacheDecisionStrategy {
private Random _random = Randoms.getRandom(getClass());
public RandomCacheDecisionStrategy() {
}
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
return pSimilarPointInformation.get(_random.nextInt(pSimilarPointInformation.size()));
}
}
/*
* 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.caching.decision;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.RoadProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.jam.VectoralJamProperty;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import edu.emory.mathcs.backport.java.util.Arrays;
public class TTLbasedCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND;
private static final double ACCURACY_FACTOR = 100000;
private long ttl = 300 * Time.SECOND / SCALING;
private double accuracy = 1;
private double costWrongKeep = 1;
private double costWrongChange = 1;
private Object _lastDecision = false;
public TTLbasedCacheDecisionStrategy(Map<String, String> pParams) {
for (Entry<String, String> param : pParams.entrySet()) {
switch (param.getKey()) {
case "ACCURACY":
accuracy = Double.valueOf(param.getValue());
break;
case "COST_RATIO":
double ratio = Double.valueOf(param.getValue());
costWrongChange = 2 / (ratio + 1);
costWrongKeep = 2 - costWrongChange;
break;
default:
break;
}
}
}
public double getCostWrongChange() {
return costWrongChange;
}
public double getCostWrongKeep() {
return costWrongKeep;
}
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
_lastDecision = decision.getValue();
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
Collections.sort(pSimilarPointInformation, new Comparator<T>() {
@Override
public int compare(T pArg0, T pArg1) {
return Long.compare(pArg0.getDetectionDate(), pArg1.getDetectionDate());
}
});
long minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0;
Object value = pSimilarPointInformation.get(0).getValue();
boolean differentValue = false;
for (T t : pSimilarPointInformation) {
if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Unable to perform TTL-based majority voting witout TTL");
}
long timestamp = t.getDetectionDate();
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
}
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
}
if (!value.equals(t.getValue())) {
differentValue = true;
}
}
if (differentValue) {
long difference = maxTimestamp - minTimestamp;
if (difference == 0) {
return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
}
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
long ttl = (long)pSimilarPointInformation.get(0).getAttribute(AvailableInformationAttributes.TTL) / SCALING;
rate = Math.min(rate, ttl / 10.0);
double b;
if (Boolean.FALSE.equals(_lastDecision)) {
b = determineB(rate, 1 - accuracy, ttl, costWrongKeep, costWrongChange);
} else {
b = determineB(rate, 1 - accuracy, ttl, costWrongChange, costWrongKeep);
}
Map<Object, Double> weight = new HashMap<>();
for (T t : pSimilarPointInformation) {
double impact = calculateImpact(1 - accuracy, ttl, t.getDetectionDate() / SCALING, b, maxTimestamp / SCALING);
double sumImpact = 0;
Object currentValue = t.getValue();
if (currentValue instanceof VectoralJamProperty) {
currentValue = ((VectoralJamProperty) currentValue).getMostProbableValue();
}
if (weight.containsKey(currentValue)) {
sumImpact = weight.get(currentValue);
}
sumImpact += impact;
weight.put(currentValue, sumImpact);
}
double maxWeight = -1;
Object maxValue = null;
for (Object key : weight.keySet()) {
if (weight.get(key) > maxWeight) {
maxWeight = weight.get(key);
maxValue = key;
}
}
maxTimestamp = -1;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = t.getDetectionDate();
Object currentValue = t.getValue();
if (currentValue instanceof VectoralProperty) {
currentValue = ((VectoralProperty)currentValue).getMostProbableValue();
}
if (currentValue.equals(maxValue) && timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
if (maxFitting.getValue() instanceof VectoralProperty) {
VectoralProperty vectoralProperty = ((VectoralProperty)maxFitting.getValue()).clone();
double[] valueProbabilities = vectoralProperty.getValueProbabilities();
Arrays.fill(valueProbabilities, 0);
double sum = 0;
for (Object key : weight.keySet()) {
valueProbabilities[vectoralProperty.getIndexForValue(key)] = weight.get(key);
sum += weight.get(key);
}
for (int i = 0; i < valueProbabilities.length; i++) {
valueProbabilities[i] /= sum;
}
RoadInformation roadInformation = new RoadInformation(vectoralProperty);
roadInformation.copyAttributes((RoadInformation)maxFitting);
maxFitting = (T) roadInformation;
}
_lastDecision = maxFitting.getValue();
return maxFitting;
} else {
maxTimestamp = -1;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL);
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
_lastDecision = maxFitting.getValue();
return maxFitting;
}
}
public double calculateImpact(double errorProbability, long ttl, long time, double b, long maxTimestamp) {
long age = maxTimestamp - time;
if (errorProbability == 0) {
if (time == maxTimestamp) {
return 1;
} else {
return 0;
}
} else if (errorProbability == 1) {
return 1;
} else if (errorProbability == 0.5) {
return (errorProbability - 1) / ttl * age + errorProbability;
} else if (b == Double.NEGATIVE_INFINITY) {
if (time == maxTimestamp) {
return 1;
} else {
return 0;
}
}
return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * ttl)) / (1 - Math.exp(b * ttl));
}
public double getChangeProbability(long ttl) {
return 1 - Math.pow(0.5, 1 / (double) ttl);
}
public int getOptimalMessageAmountForSwitch(double changeProbability, double errorProbability, double costSlow, double costFast) {
return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability));
}
public double determineB(double rate, double errorProbability, long ttl, double costSlow, double costFast) {
return determineB(rate, errorProbability, ttl, costSlow, costFast, 1);
}
public double determineB(double rate, double errorProbability, long ttl, double costSlow, double costFast, int reversed) {
if (errorProbability == 0 || errorProbability == 1 || errorProbability == 0.5) {
return Double.NaN;
}
double b;
double p_c = getChangeProbability((long) (ttl / rate));
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY;
}
boolean first = true;
double leftSide;
double rightSide;
double step = 5;
if (errorProbability < 0.5) {
b = -2 * step * reversed;
} else {
b = 2 * step * reversed;
}
int similar = 0;
double lastDifference = -1;
do {
leftSide = calculateWeightingForOldState(optimalAmount, rate, errorProbability, ttl, b);
rightSide = calculateWeightingForNewState(optimalAmount, rate, errorProbability, ttl, b);
if (Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR)) == lastDifference) {
similar++;
} else {
lastDifference = Math.abs(Math.round((rightSide - leftSide) * ACCURACY_FACTOR));
similar = 0;
}
if (Double.isNaN(leftSide) || Double.isNaN(rightSide) || similar > 100) {
if (reversed != -1) {
double determineB = determineB(rate, errorProbability, ttl, costSlow, costFast, -1);
if (!Double.isNaN(determineB)) {
return determineB;
} else {
return b;
}
} else {
return Double.NaN;
}
}
leftSide = Math.round(leftSide * ACCURACY_FACTOR);
rightSide = Math.round(rightSide * ACCURACY_FACTOR);
if (leftSide > rightSide) {
if (b < 0) {
b -= step;
if (!first) {
step *= 0.5;
}
} else {
b -= step;
step *= 0.5;
first = false;
}
} else if (leftSide < rightSide) {
if (b > 0) {
b += step;
if (!first) {
step *= 0.5;
}
} else {
b += step;
step *= 0.5;
first = false;
}
} else {
break;
}
} while (true);
return b;
}
public double calculateWeightingForOldState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
double impact = 0;
for (int a = optimalMessageAmount; a < Math.max(Math.floor(ttl / rate), optimalMessageAmount + 2); a++) {
impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
}
return impact;
}
public double calculateWeightingForNewState(int optimalMessageAmount, double rate, double errorProbability, long ttl, double b) {
double impact = 0;
for (int a = 0; a < optimalMessageAmount; a++) {
impact += calculateImpact(errorProbability, ttl, Time.getCurrentTime() / SCALING - (long)Math.floor(a * rate), b, Time.getCurrentTime() / SCALING);
}
return impact;
}
}
/*
* 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.caching.decision;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.TemporalDependencyMatrix;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.VectoralProperty;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.environment.data.measurement.MeasurementDistributionTypeContainer;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.decision.CacheDecisionStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.RoadInformation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import edu.emory.mathcs.backport.java.util.Collections;
public class TTLbasedVectoralCacheDecisionStrategy implements CacheDecisionStrategy {
private static final long SCALING = Time.SECOND;
private static final double ACCURACY_FACTOR = 100000;
private static final double MIN_STEP = 0.00001;
private double accuracy = 1;
private double costWrongKeep = 1;
private double costWrongChange = 1;
private Object _lastDecision = null;
public TTLbasedVectoralCacheDecisionStrategy(Map<String, String> pParams) {
for (Entry<String, String> param : pParams.entrySet()) {
switch (param.getKey()) {
case "ACCURACY":
accuracy = Double.valueOf(param.getValue());
break;
case "COST_RATIO":
double ratio = Double.valueOf(param.getValue());
costWrongChange = 2 / (ratio + 1);
costWrongKeep = 2 - costWrongChange;
break;
default:
break;
}
}
}
public double getCostWrongChange() {
return costWrongChange;
}
public double getCostWrongKeep() {
return costWrongKeep;
}
@Override
public <T extends PointInformation> T decideOnCorrectInformation(
List<T> pSimilarPointInformation) {
if (pSimilarPointInformation.size() == 1) {
T decision = pSimilarPointInformation.get(0);
_lastDecision = decision.getValue();
return decision;
} else if (pSimilarPointInformation.size() == 0) {
return null;
}
Collections.sort(pSimilarPointInformation, new Comparator<T>() {
@Override
public int compare(T pArg0, T pArg1) {
return Long.compare(pArg0.getDetectionDate(), pArg1.getDetectionDate());
}
});
long minTimestamp = Long.MAX_VALUE;
long maxTimestamp = 0;
Object value = pSimilarPointInformation.get(0).getValue();
if (value instanceof VectoralProperty) {
value = ((VectoralProperty) value).getMostProbableIndex();
}
boolean differentValue = false;
List<Integer> possibleValues = new ArrayList<>();
for (T t : pSimilarPointInformation) {
if (!t.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Unable to perform TTL-based majority voting witout TTL");
}
long timestamp = t.getDetectionDate();
if (timestamp < minTimestamp) {
minTimestamp = timestamp;
}
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
}
Object currentValue = t.getValue();
if (currentValue instanceof VectoralProperty) {
VectoralProperty currentProperty = (VectoralProperty) currentValue;
currentValue = currentProperty.getMostProbableIndex();
if (!value.equals(currentValue)) {
differentValue = true;
}
for (int i = 0; i < currentProperty.getValueProbabilities().length; i++) {
if (!possibleValues.contains(i)) {
possibleValues.add(i);
}
}
}
}
if (differentValue) {
long difference = maxTimestamp - minTimestamp;
if (difference == 0) {
return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
}
double rate = difference / ((double) (pSimilarPointInformation.size() - 1) * SCALING);
long ttl = getTTL(pSimilarPointInformation.get(0));
double numberOfMessages = ttl / rate + 1;
VectoralProperty currentProperty = null;
List<Double> bValues = new ArrayList<>();
double b;
for (Integer possibleValue : possibleValues) {
double temp = determineB((VectoralProperty) pSimilarPointInformation.get(0).getValue(), ((RoadInformation)pSimilarPointInformation.get(0)).getEdge(), possibleValue, getChangeRate(pSimilarPointInformation.get(0), rate), rate, 1 - accuracy, numberOfMessages, costWrongKeep, costWrongChange);
if (!Double.isNaN(temp)) {
bValues.add(temp);
}
}
Collections.sort(bValues);
if (bValues.size() > 0) {
if (bValues.size() % 2 == 0) {
b = (bValues.get(bValues.size() / 2) + bValues.get(bValues.size() / 2 - 1)) / 2.0;
} else {
b = bValues.get(bValues.size() / 2);
}
} else {
b = Double.NEGATIVE_INFINITY;
}
int count = 0;
for (T t : pSimilarPointInformation) {
RoadInformation roadInformation = ((RoadInformation) t);
VectoralProperty property = (VectoralProperty) roadInformation.getValue();
double impact = calculateImpact(1 - accuracy, numberOfMessages, (((t.getDetectionDate() - maxTimestamp) / SCALING + ttl) / (double)ttl) * numberOfMessages, b) / (accuracy);
TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
dependencyMatrix = modifyDependencyMatrix(dependencyMatrix.age((maxTimestamp - property.getDetectionDate()) / SCALING), impact);
VectoralProperty agedProperty = property.age(1, dependencyMatrix);
if (currentProperty != null) {
currentProperty = currentProperty.combine(agedProperty);
} else {
currentProperty = agedProperty;
}
}
if (Double.isNaN(currentProperty.getValueProbabilities()[0])) {
return pSimilarPointInformation.get(pSimilarPointInformation.size() - 1);
}
RoadInformation roadInformation = new RoadInformation(currentProperty);
copyAttributes((RoadInformation) pSimilarPointInformation.get(0), roadInformation);
_lastDecision = roadInformation.getValue();
return (T) roadInformation;
} else {
maxTimestamp = -1;
T maxFitting = null;
for (T t : pSimilarPointInformation) {
long timestamp = (long) t.getAttribute(AvailableInformationAttributes.TTL);
if (timestamp > maxTimestamp) {
maxTimestamp = timestamp;
maxFitting = t;
}
}
_lastDecision = maxFitting.getValue();
return maxFitting;
}
}
/**
* @param pT
* @return
*/
private <T extends PointInformation> double getAccuracy(T pT) {
// if (pT instanceof RoadInformation) {
// RoadInformation roadInformation = ((RoadInformation) pT);
// VectoralProperty property = (VectoralProperty) roadInformation.getValue();
// double accuracy = property.getProbabilityForIndex(property.getMostProbableIndex());
// return accuracy;
// }
return this.accuracy;
}
private <T extends PointInformation> double getChangeRate(T pT, double pRate) {
// if (pT instanceof RoadInformation) {
// RoadInformation roadInformation = ((RoadInformation) pT);
// VectoralProperty property = (VectoralProperty) roadInformation.getValue();
//
// TemporalDependencyMatrix dependencyMatrix = property.getDependencyMatrix();
// return 1 - Math.pow(1 - dependencyMatrix.getChangeProbability(0), pRate * (SCALING / Time.SECOND));
// }
return getChangeProbability((long) (getTTL(pT) / pRate));
}
public <T extends PointInformation> long getTTL(
T pT) {
return (long)pT.getAttribute(AvailableInformationAttributes.TTL) / SCALING;
}
/**
* @param pT
* @param pRoadInformation
*/
private void copyAttributes(RoadInformation pSrc, RoadInformation pDest) {
for (AvailableInformationAttributes attribute : pSrc.getAvailableAttributes()) {
pDest.setAttribute(attribute, pSrc.getAttribute(attribute));
}
}
private TemporalDependencyMatrix modifyDependencyMatrix(
TemporalDependencyMatrix pDependencyMatrix, double pImpact) {
TemporalDependencyMatrix result = pDependencyMatrix.clone();
double[][] dependencies = result.getDependencies();
for (int i = 0; i < dependencies.length; i++) {
double finalPercentages = 1.0 / dependencies[i].length;
for (int j = 0; j < dependencies[i].length; j++) {
dependencies[i][j] = finalPercentages + (pDependencyMatrix.getDependencies()[i][j] - finalPercentages) * pImpact;
}
}
return result;
}
public double calculateImpact(double errorProbability, double pNumberOfMessages, double pMessageNumber, double b) {
double age = pNumberOfMessages - pMessageNumber;
if (errorProbability == 0) {
if (pMessageNumber == pNumberOfMessages) {
return 1;
} else {
return 0;
}
} else if (errorProbability == 1) {
return 1;
} else if (errorProbability == 0.5 || b == 0) {
return (1 - errorProbability) / pNumberOfMessages * age + errorProbability;
} else if (b == Double.NEGATIVE_INFINITY) {
if (pMessageNumber == pNumberOfMessages) {
return 1;
} else {
return 0;
}
}
return (1 - errorProbability) * (Math.exp(b * age) - Math.exp(b * pNumberOfMessages)) / (1 - Math.exp(b * pNumberOfMessages));
}
public double getChangeProbability(long ttl) {
return 1 - Math.pow(0.5, 1 / (double) ttl);
}
public int getOptimalMessageAmountForSwitch(double changeProbability, double errorProbability, double costSlow, double costFast) {
return (int) Math.round(Math.log(-changeProbability / Math.log(errorProbability) * costSlow / costFast) / Math.log(errorProbability));
}
public double determineB(VectoralProperty pTemplate, RoadNetworkEdge pRoadNetworkEdge, int pPossibleValue, double change, double rate, double errorProbability, double pNumberOfMessages, double costSlow, double costFast) {
if (errorProbability == 0 || errorProbability == 1 || errorProbability == 0.5) {
return Double.NaN;
}
if (_lastDecision != null) {
if (pPossibleValue == ((VectoralProperty)_lastDecision).getMostProbableIndex()) {
return Double.NaN;
}
} else {
if (pPossibleValue == pTemplate.getDefaultIndex()) {
return Double.NaN;
}
}
double b;
double p_c = change;
int optimalAmount = getOptimalMessageAmountForSwitch(p_c, errorProbability, costSlow, costFast);
if ((int)pNumberOfMessages <= optimalAmount) {
return Double.POSITIVE_INFINITY;
}
if (optimalAmount == 1) {
return Double.NEGATIVE_INFINITY;
}
boolean first = true;
double step = 10;
if (errorProbability < 0.5) {
b = -1 * step;
} else {
b = 1 * step;
}
do {
VectoralProperty valueAfterN = calculateMostProbable(pTemplate, pRoadNetworkEdge, pPossibleValue, optimalAmount, rate, errorProbability, pNumberOfMessages, b);
double probableValueAfterN = Double.NaN;
if (valueAfterN != null) {
probableValueAfterN = valueAfterN.getMostProbableIndex();
}
VectoralProperty valueBeforeN = calculateMostProbable(pTemplate, pRoadNetworkEdge, pPossibleValue, optimalAmount - 1, rate, errorProbability, pNumberOfMessages, b);
double probableValueBeforeN = Double.NaN;
if (valueBeforeN != null) {
probableValueBeforeN = valueBeforeN.getMostProbableIndex();
}
if (probableValueAfterN == pPossibleValue && probableValueAfterN != probableValueBeforeN) {
return b;
}
if (!Double.isNaN(probableValueBeforeN) && !Double.isNaN(probableValueBeforeN) && step >= MIN_STEP) {
if (probableValueAfterN != pPossibleValue) {
if (b < 0) {
b -= step;
if (!first) {
step *= 0.5;
}
} else {
b -= step;
step *= 0.5;
first = false;
}
} else if (probableValueAfterN == pPossibleValue && probableValueAfterN == probableValueBeforeN) {
if (b > 0) {
b += step;
if (!first) {
step *= 0.5;
}
} else {
b += step;
step *= 0.5;
first = false;
}
} else {
return Double.NaN;
}
} else {
return Double.NaN;
}
} while (true);
}
public VectoralProperty calculateMostProbable(VectoralProperty pTemplate, RoadNetworkEdge pRoadNetworkEdge, int pNewValue, int optimalMessageAmount, double rate, double errorProbability, double pNumberOfMessages, double b) {
VectoralProperty currentProperty = null;
for (int a = optimalMessageAmount + 1; a <= pNumberOfMessages; a++) {
VectoralProperty jamProperty = pTemplate.clone();
if (_lastDecision != null) {
jamProperty.set(((VectoralProperty)_lastDecision).getMostProbableIndex(), accuracy, MeasurementDistributionTypeContainer.getDistribution(pTemplate.getClass()));
} else {
jamProperty.set(pTemplate.getDefaultIndex(), accuracy, MeasurementDistributionTypeContainer.getDistribution(pTemplate.getClass()));
}
TemporalDependencyMatrix temporalDependencyMatrix = jamProperty.getDependencyMatrix();
temporalDependencyMatrix = temporalDependencyMatrix.age((long) (a * rate));
double impact = calculateImpact(errorProbability, pNumberOfMessages, pNumberOfMessages - a, b);
if (Double.isNaN(impact)) {
return null;
}
temporalDependencyMatrix = modifyDependencyMatrix(temporalDependencyMatrix, impact);
jamProperty = (VectoralProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
}
for (int a = 0; a <= optimalMessageAmount; a++) {
VectoralProperty jamProperty = pTemplate.clone();
jamProperty.setGaussianWithAccuracy(pNewValue, accuracy);
TemporalDependencyMatrix temporalDependencyMatrix = jamProperty.getDependencyMatrix();
temporalDependencyMatrix = temporalDependencyMatrix.age((long) (a * rate));
double impact = calculateImpact(errorProbability, pNumberOfMessages, pNumberOfMessages - a, b);
if (Double.isNaN(impact)) {
return null;
}
temporalDependencyMatrix = modifyDependencyMatrix(temporalDependencyMatrix, impact);
jamProperty = (VectoralProperty) jamProperty.age(1, temporalDependencyMatrix);
if (currentProperty != null) {
currentProperty = (VectoralProperty) currentProperty.combine(jamProperty);
} else {
currentProperty = jamProperty;
}
}
return currentProperty;
}
}
/*
* 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.tud.kom.p2psim.impl.vehicular.caching.invalidation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
public enum CacheInvalidationStrategyType {
DEFAULT(DefaultCacheInvalidationStrategy.class), NONE(DefaultCacheInvalidationStrategy.class), TTL(TTLBasedCacheInvalidationStrategy.class);
private final Class<? extends CacheInvalidationStrategy> replacementStrategy;
private CacheInvalidationStrategyType(final Class<? extends CacheInvalidationStrategy> replacementStrategy) {
this.replacementStrategy = replacementStrategy;
}
public CacheInvalidationStrategy getInvalidationStrategy() {
try {
return replacementStrategy.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
}
/*
* 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.caching.invalidation;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class DefaultCacheInvalidationStrategy implements CacheInvalidationStrategy {
@Override
public boolean checkInformation(PointInformation pCachedInformation) {
return false;
}
}
/*
* 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.caching.invalidation;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.invalidation.CacheInvalidationStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.AvailableInformationAttributes;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class TTLBasedCacheInvalidationStrategy implements CacheInvalidationStrategy {
@Override
public boolean checkInformation(PointInformation pCachedInformation) {
if (!pCachedInformation.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Cannot use TTL caching without TTL set!");
}
return pCachedInformation.getDetectionDate() + (long)pCachedInformation.getAttribute(AvailableInformationAttributes.TTL) <= Time.getCurrentTime();
}
}
/*
* 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.tud.kom.p2psim.impl.vehicular.caching.replacement;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
public enum CacheReplacementStrategyType {
DEFAULT(NoCacheReplacementStrategy.class), NONE(NoCacheReplacementStrategy.class), IMMEDIATE(ImmediateCacheReplacementStrategy.class), TTL(TTLbasedCacheReplacementStrategy.class);
private final Class<? extends CacheReplacementStrategy> replacementStrategy;
private CacheReplacementStrategyType(final Class<? extends CacheReplacementStrategy> replacementStrategy) {
this.replacementStrategy = replacementStrategy;
}
public CacheReplacementStrategy getReplacementStrategy() {
try {
return replacementStrategy.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new AssertionError(e);
}
}
}
/*
* 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.caching.replacement;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.information.PointInformation;
public class ImmediateCacheReplacementStrategy implements CacheReplacementStrategy {
@Override
public boolean replaceInformation(PointInformation pCachedInformation,
PointInformation pSensedInformation) {
return pSensedInformation.getDetectionDate() > pCachedInformation.getDetectionDate();
}
}
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