Skip to content

Commit

Permalink
Merge pull request #40 from VU-Thesis-2019-2020-Wesley-Shann/36/simpl…
Browse files Browse the repository at this point in the history
…ify-strategy-instantiation

Simplify and Organize instantiation of prefetching strategies
  • Loading branch information
iivanoo authored Jun 9, 2020
2 parents ca21090 + e0f8b82 commit 5064af7
Show file tree
Hide file tree
Showing 14 changed files with 255 additions and 84 deletions.
3 changes: 2 additions & 1 deletion Prefetching-Library/android_prefetching_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ android {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0.0+1"
versionName "1.0.0+2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -27,6 +27,7 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
api group: 'org.jetbrains', name: 'annotations', version: jetbrainsAnnotation

implementation "org.bitbucket.cowwoc:diff-match-patch:$diffMatchPatch"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package nl.vu.cs.s2group.nappa;

import android.app.Activity;
import androidx.lifecycle.LiveData;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.NonNull;
import android.util.Log;
import android.util.LongSparseArray;
import android.util.LruCache;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;

import org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch;

import java.io.File;
Expand All @@ -32,12 +33,8 @@

import nl.vu.cs.s2group.nappa.graph.ActivityGraph;
import nl.vu.cs.s2group.nappa.graph.ActivityNode;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategy;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl5;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl6;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl7;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl8;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl9;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchingStrategy;
import nl.vu.cs.s2group.nappa.prefetch.PPMPrefetchingStrategy;
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.ActivityData;
import nl.vu.cs.s2group.nappa.room.PrefetchingDatabase;
Expand All @@ -47,9 +44,6 @@
import nl.vu.cs.s2group.nappa.room.data.SessionData;
import nl.vu.cs.s2group.nappa.room.data.UrlCandidate;
import nl.vu.cs.s2group.nappa.room.data.UrlCandidateParts;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl3;
import nl.vu.cs.s2group.nappa.prefetch.PrefetchStrategyImpl4;
import okhttp3.Cache;
import okhttp3.Headers;
import okhttp3.Interceptor;
Expand All @@ -72,8 +66,7 @@ public class PrefetchingLib {
private static LiveData<List<ActivityData>> listLiveData;
public static HashMap<String, Long> activityMap = new HashMap<>(); // Map of ActivityNodes containing Key: ActivityName Value: ID,
private static Session session;
private static PrefetchStrategy strategyHistory = new PrefetchStrategyImpl();
private static PrefetchStrategy strategyIntent;
private static PrefetchingStrategy strategyIntent;
//private static PrefetchStrategy strategyIntent = new PrefetchStrategyImpl3(0.6f);
private static OkHttpClient okHttpClient;
private static ConcurrentHashMap<String, Long> prefetchRequest = new ConcurrentHashMap<>();
Expand All @@ -94,31 +87,7 @@ public static LongSparseArray<Map<String, String>> getExtrasMap() {

private PrefetchingLib(int prefetchStrategyNum) {
this.prefetchStrategyNum = prefetchStrategyNum;
switch(prefetchStrategyNum){
case 3:
strategyIntent = new PrefetchStrategyImpl3(0.6f);
break;
case 4:
strategyIntent = new PrefetchStrategyImpl4(0.6f);
break;
case 5:
strategyIntent = new PrefetchStrategyImpl5(0.6f);
break;
case 6:
strategyIntent = new PrefetchStrategyImpl6(0.6f);
break;
case 7:
strategyIntent = new PrefetchStrategyImpl7(0.6f);
break;
case 8:
strategyIntent = new PrefetchStrategyImpl8(0.6f);
break;
case 9:
strategyIntent = new PrefetchStrategyImpl9(0.6f);
break;
default:
strategyIntent = new PrefetchStrategyImpl3(0.6f);
}
strategyIntent = PrefetchingStrategy.getStrategy(prefetchStrategyNum);
}


Expand Down Expand Up @@ -184,7 +153,7 @@ public static void init(Context context,int prefetchStrategyNum) {
)
);

byName.setLastNListSessionAggregateLiveData(PrefetchingDatabase.getInstance().sessionDao().getCountForActivitySource(actId,PrefetchStrategyImpl4.lastN));
byName.setLastNListSessionAggregateLiveData(PrefetchingDatabase.getInstance().sessionDao().getCountForActivitySource(actId, PPMPrefetchingStrategy.lastN));

}

Expand Down Expand Up @@ -328,7 +297,7 @@ public static void setCurrentActivity(@NonNull Activity activity) {
activityMap.get(currentActivityName)
)
);
activityGraph.getCurrent().setLastNListSessionAggregateLiveData(PrefetchingDatabase.getInstance().sessionDao().getCountForActivitySource(activityMap.get(currentActivityName),PrefetchStrategyImpl4.lastN));
activityGraph.getCurrent().setLastNListSessionAggregateLiveData(PrefetchingDatabase.getInstance().sessionDao().getCountForActivitySource(activityMap.get(currentActivityName), PPMPrefetchingStrategy.lastN));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,32 @@
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl3 implements PrefetchStrategy {
private final static String LOG_TAG = PrefetchStrategyImpl3.class.getSimpleName();
/**
* This strategy greedily determine which activity successors can benefit the most from
* prefetching. This is not limited to immediate successors but can consider successors
* at a higher depth in the ENG, provided that the "added value" of prefetching these
* high-depth successors is sufficient.
* <br/><br/>
*
* <p> The node selection process introduces the notion of a "Weight" factor.
* The algorithm recursively traverses the set of all successors in a depth first way
* up to the point where the base case does not hold. With every recursive
* iteration of the algorithm, the weight value is defined as the score that has been
* calculated in the previous iteration. With every iteration of the recursive step,
* the weight value decays further.
* <br/><br/>
*
* <p> Considering the recursive step, it can be seen that as the graph depth increases,
* then the scores corresponding to all subsequent nodes decreases. To limit the number
* of node candidates generated, a threshold value is inserted.
*/
public class GreedyPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = GreedyPrefetchingStrategy.class.getSimpleName();

private float threshold;
private HashMap<Long, String> reversedHashMap = new HashMap<>();

public PrefetchStrategyImpl3(float threshold) {
public GreedyPrefetchingStrategy(float threshold) {
this.threshold = threshold;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl8 implements PrefetchStrategy {
private final static String LOG_TAG = PrefetchStrategyImpl8.class.getSimpleName();
/**
* This strategy is based in the {@link GreedyPrefetchingStrategy}, where the score
* calculation is changed to use the PageRank score used in {@link PageRankPrefetchingStrategy}.
* This strategy only considers the direct successors of the current node.
*/
@Deprecated
public class GreedyWithPageRankScoresPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = GreedyWithPageRankScoresPrefetchingStrategy.class.getSimpleName();
private float threshold;
private HashMap<Long, String> reversedHashMap = new HashMap<>();

public PrefetchStrategyImpl8(float threshold) {
public GreedyWithPageRankScoresPrefetchingStrategy(float threshold) {
this.threshold = threshold;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,23 @@
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl6 implements PrefetchStrategy{
private final static String LOG_TAG = PrefetchStrategyImpl6.class.getSimpleName();
/**
* This strategy utilizes the Hyperlink-Induced Topic Search (HITS) link analysis
* algorithm to determine which nodes to select. This strategy only considers the
* direct successors of the current node.
* <br/><br/>
*
* <p> The HITS algorithm is defined in the paper
* <a href="https://dl.acm.org/doi/abs/10.1145/324133.324140">
* Authoritative sources in a hyperlinked environment</a>
*/
@Deprecated
public class HITSPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = HITSPrefetchingStrategy.class.getSimpleName();
private HashMap<Long, String> reversedHashMap = new HashMap<>();
float threshold;

public PrefetchStrategyImpl6(float threshold) {this.threshold=threshold;}
public HITSPrefetchingStrategy(float threshold) {this.threshold=threshold;}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,44 @@
import nl.vu.cs.s2group.nappa.room.PrefetchingDatabase;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl implements PrefetchStrategy {
/**
* This strategy selects the most visited successor of the current node. This strategy
* only considers the direct successors of the current node.
*/
@Deprecated
public class MostVisitedSuccessorPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = MostVisitedSuccessorPrefetchingStrategy.class.getSimpleName();
@NonNull
@Override
public List<String> getTopNUrlToPrefetchForNode(ActivityNode node, Integer maxNumber) {
Log.d("PrefStratImpl", "Started for node: "+node.activityName);
Log.d(LOG_TAG, "Started for node: "+node.activityName);
SessionDao.SessionAggregate best = null;
List<SessionDao.SessionAggregate> sessionAggregateList = node.getSessionAggregateList();
if (sessionAggregateList!=null) {
for (SessionDao.SessionAggregate aggregate : sessionAggregateList) {
Log.d("PrefStratImpl", "Evaluating successor: " + aggregate.actName);
Log.d(LOG_TAG, "Evaluating successor: " + aggregate.actName);
if (best == null) {
best = aggregate;
} else if (aggregate.countSource2Dest > best.countSource2Dest) {
Log.d("PrefStratImpl",
Log.d(LOG_TAG,
"choosing "+aggregate.actName+": "+aggregate.countSource2Dest+" against " +
best.actName+": "+best.countSource2Dest);
best = aggregate;
}
}
if (best != null) {
Log.d("PrefStratImpl", "Chosen successor: " + best.actName);
Log.d(LOG_TAG, "Chosen successor: " + best.actName);
List<AggregateUrlDao.AggregateURL> list = PrefetchingDatabase.getInstance().urlDao().getAggregateForIdActivity(best.idActDest, maxNumber);
LinkedList<String> toBeReturned = new LinkedList<String>();
for (AggregateUrlDao.AggregateURL elem : list) {
toBeReturned.add(elem.getUrl());
}
return toBeReturned;
} else {
Log.d("PrefStratImpl", "Null successor");
Log.d(LOG_TAG, "Null successor");
}
} else {
Log.d("PrefStratImpl", "SessionAggregateList is null");
Log.d(LOG_TAG, "SessionAggregateList is null");
}
return Arrays.asList(new String[]{});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,24 @@
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl4 implements PrefetchStrategy {
private final static String LOG_TAG = PrefetchStrategyImpl4.class.getSimpleName();
/**
* Utilizes the Prediction by Partial Match (PPM) based on a 0-order markov-model.
* This strategy only considers the direct successors of the current node.
* <br/><br/>
*
* <p> The PPM algorithm is defined in the paper
* <a href="https://dl.acm.org/doi/10.1145/301453.301557">
* Web prefetching between low-bandwidth clients and proxies: potential and
* performance</a>
*/
@Deprecated
public class PPMPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = PPMPrefetchingStrategy.class.getSimpleName();

private float threshold;
private HashMap<Long, String> reversedHashMap = new HashMap<>();
private static ScheduledThreadPoolExecutor poolExecutor = new ScheduledThreadPoolExecutor(1);
public PrefetchStrategyImpl4(float threshold) {
public PPMPrefetchingStrategy(float threshold) {
this.threshold = threshold;
}
public static int lastN = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,19 @@
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl9 implements PrefetchStrategy {
private final static String LOG_TAG = PrefetchStrategyImpl9.class.getSimpleName();
/**
* This strategy is based in the {@link PPMPrefetchingStrategy}, where the score
* calculation is changed to use the HITS score used in {@link HITSPrefetchingStrategy}.
* This strategy only considers the direct successors of the current node.
*/
@Deprecated
public class PPMWithHITSScoresPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = PPMWithHITSScoresPrefetchingStrategy.class.getSimpleName();

private float threshold;
private HashMap<Long, String> reversedHashMap = new HashMap<>();
private static ScheduledThreadPoolExecutor poolExecutor = new ScheduledThreadPoolExecutor(1);
public PrefetchStrategyImpl9(float threshold) {
public PPMWithHITSScoresPrefetchingStrategy(float threshold) {
this.threshold = threshold;
}
public static int lastN = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
import nl.vu.cs.s2group.nappa.prefetchurl.ParameteredUrl;
import nl.vu.cs.s2group.nappa.room.dao.SessionDao;

public class PrefetchStrategyImpl5 implements PrefetchStrategy {
private final static String LOG_TAG = PrefetchStrategyImpl5.class.getSimpleName();
/**
* This strategy utilizes the PageRank link analysis algorithm to determine which
* nodes to select. This strategy only considers the direct successors of the
* current node.
*/
@Deprecated
public class PageRankPrefetchingStrategy implements PrefetchingStrategy {
private final static String LOG_TAG = PageRankPrefetchingStrategy.class.getSimpleName();

private HashMap<Long, String> reversedHashMap = new HashMap<>();
float threshold;
public PrefetchStrategyImpl5(float threshold) {this.threshold=threshold;}
public PageRankPrefetchingStrategy(float threshold) {this.threshold=threshold;}



Expand Down

This file was deleted.

Loading

0 comments on commit 5064af7

Please sign in to comment.