Commit 52c10219 authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Added new caching strategies and analyzer

parent f4af1a74
/*
* 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.api.analyzer;
import de.tudarmstadt.maki.simonstrator.api.component.core.MonitorComponent.Analyzer;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
/**
* Basic vehicle decision analyzer
*
* @author Tobias Meuser
*/
public interface VehicleDecisionAnalyzer extends Analyzer {
void edgeChanged(RoadNetworkEdge pEdge, RoadNetworkEdge pNextEdge, boolean pRerouted);
void setJam(boolean pIsJam);
}
......@@ -53,10 +53,15 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
@Override
public <T extends PointInformation> List<T> getCacheEntries(Class<T> pCacheEntryClass) {
if (_cache.containsKey(pCacheEntryClass)) {
List<? extends Object> cacheEntries = _cache.get(pCacheEntryClass);
List<? extends PointInformation> cacheEntries = _cache.get(pCacheEntryClass);
List<T> results = new ArrayList<>();
for (Object object : cacheEntries) {
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);
}
......@@ -79,14 +84,23 @@ public class DefaultCachingComponent implements CachingComponent, ConnectivityLi
if (!_cache.containsKey(pCacheEntry.getClass())) {
_cache.put(pCacheEntry.getClass(), new ArrayList<>());
}
_cache.get(pCacheEntry.getClass()).add(pCacheEntry);
List<PointInformation> entries = _cache.get(pCacheEntry.getClass());
int index;
if ((index = entries.indexOf(pCacheEntry)) != -1) {
if (_replacementStrategy.replaceInformation(entries.get(index), pCacheEntry)) {
entries.remove(index);
entries.add(pCacheEntry);
} else {
// Nothing to do
}
} else {
entries.add(pCacheEntry);
}
}
@Override
public void initialize() {
_cache.clear();
}
@Override
......
......@@ -23,7 +23,7 @@ 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);
DEFAULT(DefaultCacheInvalidationStrategy.class), TTL(TTLBasedCacheInvalidationStrategy.class);
private final Class<? extends CacheInvalidationStrategy> replacementStrategy;
......
/*
* 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();
}
}
......@@ -23,7 +23,7 @@ package de.tud.kom.p2psim.impl.vehicular.caching.replacement;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.caching.replacement.CacheReplacementStrategy;
public enum CacheReplacementStrategyType {
DEFAULT(DefaultCacheReplacementStrategy.class);
DEFAULT(DefaultCacheReplacementStrategy.class), TTL(TTLbasedCacheReplacementStrategy.class);
private final Class<? extends CacheReplacementStrategy> replacementStrategy;
......
......@@ -28,8 +28,7 @@ public class DefaultCacheReplacementStrategy implements CacheReplacementStrategy
@Override
public boolean replaceInformation(PointInformation pCachedInformation,
PointInformation pSensedInformation) {
return true;
return false;//pSensedInformation.getDetectionDate() > pCachedInformation.getDetectionDate();
}
}
/*
* 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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.tudarmstadt.maki.simonstrator.api.Time;
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.PointInformation;
public class TTLbasedCacheReplacementStrategy implements CacheReplacementStrategy {
private Map<Object, Map<Object, List<Long>>> detectionTimeStorage = new HashMap<>();
@Override
public boolean replaceInformation(PointInformation pCachedInformation,
PointInformation pSensedInformation) {
if (!pCachedInformation.hasAttribute(AvailableInformationAttributes.TTL)
|| !pSensedInformation.hasAttribute(AvailableInformationAttributes.TTL)) {
throw new AssertionError("Cannot use TTL caching without TTL set!");
}
keepDetectionTimes(pCachedInformation);
keepDetectionTimes(pSensedInformation);
if (pCachedInformation.getValue().equals(pSensedInformation.getValue())) {
return pSensedInformation.getDetectionDate() > pCachedInformation.getDetectionDate();
} else {
Object cachedPosition = getEdgeOrPosition(pCachedInformation);
Object sensedPosition = getEdgeOrPosition(pSensedInformation);
if (cachedPosition.equals(sensedPosition)) {
Map<Object, List<Long>> detectedStates = detectionTimeStorage.get(cachedPosition);
long currentTime = Time.getCurrentTime();
long weightCached = 0;
List<Long> detectionTimes = detectedStates.get(pCachedInformation.getValue());
for (int i = 0; i < detectionTimes.size(); i++) {
if (detectionTimes.get(i) < currentTime) {
detectionTimes.remove(i);
} else {
weightCached += detectionTimes.get(i) - currentTime;
}
}
long weightSensed = 0;
detectionTimes = detectedStates.get(pSensedInformation.getValue());
for (int i = 0; i < detectionTimes.size(); i++) {
if (detectionTimes.get(i) < currentTime) {
detectionTimes.remove(i);
} else {
weightSensed += detectionTimes.get(i) - currentTime;
}
}
return weightSensed > weightCached;
}
}
return true;
}
public void keepDetectionTimes(PointInformation information) {
Object key = getEdgeOrPosition(information);
if (!detectionTimeStorage.containsKey(key)) {
detectionTimeStorage.put(key, new HashMap<>());
}
Map<Object, List<Long>> detectedStates = detectionTimeStorage.get(key);
Object value = information.getValue();
if (!detectedStates.containsKey(value)) {
detectedStates.put(value, new ArrayList<>());
}
List<Long> detectionDates = detectedStates.get(value);
if (!detectionDates.contains(information.getDetectionDate()
+ (long) information.getAttribute(AvailableInformationAttributes.TTL))) {
detectionDates.add(information.getDetectionDate() + (long)information.getAttribute(AvailableInformationAttributes.TTL));
}
}
public Object getEdgeOrPosition(PointInformation information) {
if (information.hasAttribute(AvailableInformationAttributes.EDGE)) {
return information.getAttribute(AvailableInformationAttributes.EDGE);
} else {
return information.getLocation();
}
}
}
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