From: <de...@us...> - 2008-10-30 17:06:46
|
Revision: 4116 http://fudaa.svn.sourceforge.net/fudaa/?rev=4116&view=rev Author: deniger Date: 2008-10-30 17:06:38 +0000 (Thu, 30 Oct 2008) Log Message: ----------- Added Paths: ----------- branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/ branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheOneTimeDecorator.java branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheWeakDecorator.java branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataOneTimeDecorator.java branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataTimeDecorator.java Added: branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheOneTimeDecorator.java =================================================================== --- branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheOneTimeDecorator.java (rev 0) +++ branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheOneTimeDecorator.java 2008-10-30 17:06:38 UTC (rev 4116) @@ -0,0 +1,122 @@ +/** + * Licence GPL + * Copyright Genesis + */ +package org.fudaa.dodico.ef.decorator; + +import java.io.IOException; +import java.lang.ref.WeakReference; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import org.fudaa.ctulu.CtuluVariable; +import org.fudaa.dodico.ef.EfData; +import org.fudaa.dodico.ef.EfGridData; +import org.fudaa.dodico.ef.EfGridInterface; + +/** + * Un decorateur permettant de cacher les donn\xE9es EfData pour un pas de temps precis. Utilise des WeakReference pour les + * EfData sauf pour les variables definies en tant que persistantes. Ce gridData ne renvoie que le donnees au pas de + * temps passe dans le constructeur. + * + * @author deniger + */ +public class EfGridDataCacheOneTimeDecorator implements EfGridData { + + final EfGridData init; + + Set<CtuluVariable> permanentVariable_; + + Map<CtuluVariable, EfData> permCache_; + final int timeCached_; + Map<CtuluVariable, WeakReference<EfData>> transCache_; + + /** + * @param _init les donnees initiales + * @param _timeCached le pas de temps a utilise + * @param _permVariable + */ + public EfGridDataCacheOneTimeDecorator(EfGridData _init, int _timeCached, CtuluVariable... _permVariable) { + super(); + init = _init; + timeCached_ = _timeCached; + if (_permVariable != null) permanentVariable_ = new HashSet<CtuluVariable>(Arrays.asList(_permVariable)); + else permanentVariable_ = Collections.emptySet(); + } + + protected EfData getCachedData(CtuluVariable _o) { + if (permanentVariable_.contains(_o)) { return permCache_.get(_o); } + WeakReference<EfData> res = transCache_.get(_o); + if (res == null) return null; + return res.get(); + + } + + /** + * {@inheritDoc} + * + * @param _timeIdx ce pas de temps n'est pas pris en compte + */ + public EfData getData(CtuluVariable _o, int _timeIdx) throws IOException { + EfData data = getCachedData(_o); + if (data != null) return data; + data = init.getData(_o, timeCached_); + if (data != null) setCachedData(_o, data); + return data; + } + + /** + * {@inheritDoc} + * + * @param _timeIdx ce pas de temps n'est pas pris en compte + */ + public double getData(CtuluVariable _o, int _timeIdx, int _idxObjet) throws IOException { + EfData data = getCachedData(_o); + if (data != null) return data.getValue(_idxObjet); + return init.getData(_o, timeCached_, _idxObjet); + } + + /** + * @return + * @see org.fudaa.dodico.ef.EfGridData#getGrid() + */ + public EfGridInterface getGrid() { + return init.getGrid(); + } + + /** + * @return the timeCached + */ + public int getTimeCached() { + return timeCached_; + } + + /** + * @param _var + * @return + * @see org.fudaa.dodico.ef.EfGridData#isDefined(org.fudaa.ctulu.CtuluVariable) + */ + public boolean isDefined(CtuluVariable _var) { + return init.isDefined(_var); + } + + /** + * @param _idxVar + * @return + * @see org.fudaa.dodico.ef.EfGridData#isElementVar(org.fudaa.ctulu.CtuluVariable) + */ + public boolean isElementVar(CtuluVariable _idxVar) { + return init.isElementVar(_idxVar); + } + + protected void setCachedData(CtuluVariable _o, EfData _data) { + if (permanentVariable_.contains(_o)) { + permCache_.put(_o, _data); + } else transCache_.put(_o, new WeakReference<EfData>(_data)); + + } + +} Added: branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheWeakDecorator.java =================================================================== --- branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheWeakDecorator.java (rev 0) +++ branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataCacheWeakDecorator.java 2008-10-30 17:06:38 UTC (rev 4116) @@ -0,0 +1,131 @@ +/** + * Licence GPL + * Copyright Genesis + */ +package org.fudaa.dodico.ef.decorator; + +import gnu.trove.TIntObjectHashMap; + +import java.io.IOException; +import java.lang.ref.WeakReference; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.fudaa.ctulu.CtuluVariable; +import org.fudaa.dodico.ef.EfData; +import org.fudaa.dodico.ef.EfGridData; +import org.fudaa.dodico.ef.EfGridInterface; + +import com.memoire.fu.FuLog; + +/** + * Un decorateur permettant de cacher des donnees. Utilise des WeakReference pour les datas pour eviter d'encombrer la + * memoire + * + * @author deniger + */ +public class EfGridDataCacheWeakDecorator implements EfGridData { + + final EfGridData init_; + + int maxSize_ = 10; + + TIntObjectHashMap timeMap_ = new TIntObjectHashMap((int) (maxSize_ / 0.75)); + + public EfGridDataCacheWeakDecorator(EfGridData _init) { + super(); + init_ = _init; + } + + @SuppressWarnings("unchecked") + protected EfData getCachedData(CtuluVariable _o, int _timeIdx) { + Map<CtuluVariable, WeakReference<EfData>> res = (Map<CtuluVariable, WeakReference<EfData>>) timeMap_.get(_timeIdx); + if (res == null) return null; + WeakReference<EfData> reference = res.get(_o); + EfData efData = reference == null ? null : reference.get(); + if (FuLog.isDebug()) { + + FuLog.debug("EfGridDataCacheDecorator getDataFrom cache for " + _o + " " + _timeIdx + " found " + + (efData != null)); + + } + return efData; + + } + + public EfData getData(CtuluVariable _o, int _timeIdx) throws IOException { + EfData data = getCachedData(_o, _timeIdx); + // en cache: on renvoie + if (data != null) return data; + data = init_.getData(_o, _timeIdx); + if (data != null) setCachedData(_o, _timeIdx, data); + return data; + } + + public double getData(CtuluVariable _o, int _timeIdx, int _idxObjet) throws IOException { + EfData data = getCachedData(_o, _timeIdx); + if (data != null) return data.getValue(_idxObjet); + return init_.getData(_o, _timeIdx, _idxObjet); + } + + /** + * @return + * @see org.fudaa.dodico.ef.EfGridData#getGrid() + */ + public EfGridInterface getGrid() { + return init_.getGrid(); + } + + /** + * @return the maxSize + */ + public int getMaxSize() { + return maxSize_; + } + + /** + * @param _var + * @return + * @see org.fudaa.dodico.ef.EfGridData#isDefined(org.fudaa.ctulu.CtuluVariable) + */ + public boolean isDefined(CtuluVariable _var) { + return init_.isDefined(_var); + } + + /** + * @param _idxVar + * @return + * @see org.fudaa.dodico.ef.EfGridData#isElementVar(org.fudaa.ctulu.CtuluVariable) + */ + public boolean isElementVar(CtuluVariable _idxVar) { + return init_.isElementVar(_idxVar); + } + + @SuppressWarnings("unchecked") + protected void setCachedData(CtuluVariable _o, int _timeIdx, EfData _data) { + Map<CtuluVariable, WeakReference<EfData>> res = (Map<CtuluVariable, WeakReference<EfData>>) timeMap_.get(_timeIdx); + if (res == null) { + // on reduit la map + if (timeMap_.size() > maxSize_) { + int[] keys = timeMap_.keys(); + Arrays.sort(keys); + timeMap_.remove(keys[0]); + } + res = new HashMap<CtuluVariable, WeakReference<EfData>>(10); + timeMap_.put(_timeIdx, _data); + } + res.put(_o, new WeakReference<EfData>(_data)); + + } + + /** + * La taille max du nombre de pas de pour lesquels on veut cacher les donnees + * + * @param _maxSize the maxSize to set + */ + public void setMaxSize(int _maxSize) { + maxSize_ = _maxSize; + } + +} Added: branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataOneTimeDecorator.java =================================================================== --- branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataOneTimeDecorator.java (rev 0) +++ branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataOneTimeDecorator.java 2008-10-30 17:06:38 UTC (rev 4116) @@ -0,0 +1,44 @@ +/** + * Licence GPL + * Copyright Genesis + */ +package org.fudaa.dodico.ef.decorator; + +import java.io.IOException; + +import org.fudaa.ctulu.CtuluVariable; +import org.fudaa.dodico.ef.EfData; +import org.fudaa.dodico.ef.EfGridData; +import org.fudaa.dodico.ef.EfGridInterface; + +public class EfGridDataOneTimeDecorator implements EfGridData { + + final EfGridData init_; + + final int tIdx_; + public EfGridDataOneTimeDecorator(EfGridData _init, int _idx) { + super(); + init_ = _init; + tIdx_ = _idx; + } + + public EfData getData(CtuluVariable _o, int _timeIdx) throws IOException { + return init_.getData(_o, tIdx_); + } + + public double getData(CtuluVariable _o, int _timeIdx, int _idxObjet) throws IOException { + return init_.getData(_o, tIdx_, _idxObjet); + } + + public EfGridInterface getGrid() { + return init_.getGrid(); + } + + public boolean isDefined(CtuluVariable _var) { + return init_.isDefined(_var); + } + + public boolean isElementVar(CtuluVariable _idxVar) { + return init_.isElementVar(_idxVar); + } +} \ No newline at end of file Added: branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataTimeDecorator.java =================================================================== --- branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataTimeDecorator.java (rev 0) +++ branches/Prepro-0.92-SNAPSHOT/dodico/src/org/fudaa/dodico/ef/decorator/EfGridDataTimeDecorator.java 2008-10-30 17:06:38 UTC (rev 4116) @@ -0,0 +1,127 @@ +/** + * Licence GPL + * Copyright Genesis + */ +package org.fudaa.dodico.ef.decorator; + +import java.io.IOException; +import java.util.Arrays; + +import org.fudaa.ctulu.CtuluLib; +import org.fudaa.ctulu.CtuluVariable; +import org.fudaa.dodico.ef.EfData; +import org.fudaa.dodico.ef.EfDataElement; +import org.fudaa.dodico.ef.EfDataNode; +import org.fudaa.dodico.ef.EfGridData; +import org.fudaa.dodico.ef.EfGridInterface; + +/** + * un decorateur de grid data qui permet d'interpoler les valeurs sur un pas de temps donn\xE9. + * + * @author deniger + */ +public class EfGridDataTimeDecorator implements EfGridData { + + final EfGridData init_; + final int tMaxIdx_; + final int tMinIdx_; + final double coeff_; + + /** + * @param _init les donn\xE9es initiales + * @param _t le pas de temps sur lequel on veut interpoler + * @param _minIdx le pas de temps inf a t + * @param _min le temps inf + * @param _maxIdx le pas de temps sup a t + * @param _max le temps sup + */ + public EfGridDataTimeDecorator(EfGridData _init, double _t, int _minIdx, double _min, int _maxIdx, double _max) { + super(); + init_ = _init; + tMaxIdx_ = _maxIdx; + tMinIdx_ = _minIdx; + coeff_ = (_t - _min) / (_maxIdx - _min); + } + + /** + * @param _timeIdx non utilise dans ce cas {@inheritDoc} + */ + public EfData getData(CtuluVariable _o, int _timeIdx) throws IOException { + final EfData data1 = init_.getData(_o, tMinIdx_); + final EfData data2 = init_.getData(_o, tMaxIdx_); + final double[] res = new double[data1.getSize()]; + for (int i = res.length - 1; i >= 0; i--) { + final double v1 = data1.getValue(i); + res[i] = coeff_ * (data2.getValue(i) - v1) + v1; + } + return data1.isElementData() ? (EfData) new EfDataElement(res) : new EfDataNode(res); + } + + /** + * @param _timeIdx non utilise dans ce cas {@inheritDoc} + */ + public double getData(CtuluVariable _o, int _timeIdx, int _idxObjet) throws IOException { + double data1 = init_.getData(_o, tMinIdx_, _idxObjet); + double data2 = init_.getData(_o, tMaxIdx_, _idxObjet); + return coeff_ * (data2 - data1) + data1; + } + + public EfGridInterface getGrid() { + return init_.getGrid(); + } + + public boolean isDefined(CtuluVariable _var) { + return init_.isDefined(_var); + } + + public boolean isElementVar(CtuluVariable _idxVar) { + return init_.isElementVar(_idxVar); + } + + /** + * @param srcTimeStep les pas te + * @param time + * @return null si en dehors ou tableau de 2 entier avec idxMin et idxMax des pas de temps englobant + */ + public static int[] getTMinMax(double[] srcTimeStep, double time) { + if (CtuluLib.isEquals(time, srcTimeStep[0], 1E-3)) { return new int[] { 0, 0 }; } + if (CtuluLib.isEquals(time, srcTimeStep[srcTimeStep.length - 1], 1E-3)) { return new int[] { + srcTimeStep.length - 1, srcTimeStep.length - 1 }; } + if (time < srcTimeStep[0] || time > srcTimeStep[srcTimeStep.length - 1]) { + + return null; } + int[] res = new int[2]; + final int idx = Arrays.binarySearch(srcTimeStep, time); + if (idx >= 0) { + res[0] = idx; + res[1] = idx; + } else { + res[0] = -idx - 2; + res[1] = res[0] + 1; + if (CtuluLib.isEquals(time, srcTimeStep[res[0]], 1E-3)) { + res[1] = res[0]; + } + if (CtuluLib.isEquals(time, srcTimeStep[res[1]], 1E-3)) { + res[0] = res[1]; + } + } + return res; + } + + public static EfGridData getTimeInterpolated(EfGridData _init, double[] srcTimeStep, double time) { + + if (time < srcTimeStep[0] || time > srcTimeStep[srcTimeStep.length - 1]) { throw new IllegalArgumentException( + "impossible to interpolate"); } + final int idx = Arrays.binarySearch(srcTimeStep, time); + if (idx >= 0) { + return new EfGridDataOneTimeDecorator(_init, idx); + } else { + int tmin = -idx - 2; + int tmax = tmin + 1; + if (CtuluLib.isEquals(time, srcTimeStep[tmin], 1E-3)) { return new EfGridDataOneTimeDecorator(_init, tmin); } + if (CtuluLib.isEquals(time, srcTimeStep[tmax], 1E-3)) { return new EfGridDataOneTimeDecorator(_init, tmax); } + return new EfGridDataTimeDecorator(_init, time, tmin, srcTimeStep[tmin], tmax, srcTimeStep[tmax]); + } + + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |