package kr.ac.kaist.resl.lilliput.engine.learning;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import kr.ac.kaist.resl.lilliput.model.ClusterRectangle;
import kr.ac.kaist.resl.lilliput.model.GeoPoint;
import kr.ac.kaist.resl.lilliput.model.Period;
import kr.ac.kaist.resl.lilliput.utility.RColors;
import kr.ac.kaist.resl.lilliput.utility.TitanHelper;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.mahout.clustering.kmeans.Cluster;
import org.apache.mahout.clustering.kmeans.KMeansClusterer;
import org.apache.mahout.common.distance.EuclideanDistanceMeasure;
import org.apache.mahout.math.DenseVector;
import org.apache.mahout.math.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
import com.thinkaurelius.titan.core.TitanFactory;
import com.thinkaurelius.titan.core.TitanGraph;
import com.tinkerpop.blueprints.Edge;
import com.tinkerpop.blueprints.Vertex;
/**
* Servlet implementation class LocationClusterer
*/
public class LocationClusterer extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LocationClusterer() {
super();
}
/**
* targets
* from
* until
* cluster
* zoom
*
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Make Query From Request
String url = makeQuery(request);
if( url == null )
return;
System.out.println(" [Location Clusterer] : Get Data From Graphite.............");
// Get Data From Graphite
String locationData = getDataFromGraphite(url);
System.out.println(" [Location Clusterer] : Parse and Fuse obtained data.............");
// Parse and Fuse obtained data
JSONArray jArr = new JSONArray(locationData);
String manipulatedString = fusionGraphiteData(jArr);
System.out.println(" [Location Clusterer] : Manipulate Data.............");
// Manipulate Data ( Separate / Filter )
List<Period> indoorPeriods = getIndoorData(manipulatedString);
manipulatedString = filterIndoorData(manipulatedString);
System.out.println(" [Location Clusterer] : Run K-means Clustering Algorithm.............");
int numOfClusters = 4;
if( request.getParameter("cluster") != null )
numOfClusters = Integer.parseInt(request.getParameter("cluster"));
List<Cluster> clusters = runKMeansClustering(manipulatedString, numOfClusters);
if( clusters.size() == 1 )
{
Vector radi = clusters.get(0).getRadius();
if( radi.size() == 0 )
{
System.out.println(" [Location Clusterer] : Null Cluster Exit");
return;
}
}
System.out.println(" [Location Clusterer] : Filtering Result.............");
String[] centers = getCenters(clusters);
List<String> clusterMembers = getClusterMembers(manipulatedString, centers);
clusterMembers = filterNullClusters(clusterMembers);
System.out.println(" [Location Clusterer] : Make Regions for Clusters.............");
List<ClusterRectangle> clusterRectangles = new ArrayList<ClusterRectangle>();
for( int i = 0 ; i < clusterMembers.size() ; i++ )
{
ClusterRectangle clusterRectangle = getClusterRectangle(clusterMembers.get(i));
clusterRectangles.add(clusterRectangle);
}
// Do Visualization
doVisualization(clusterMembers, manipulatedString, indoorPeriods, request);
// Update Graph
System.out.println(" [Location Clusterer] : Update Graph.............");
String target = (String)request.getParameter("target");
updateGraph(clusterRectangles, indoorPeriods, target);
}
public void updateGraph(List<ClusterRectangle> clusterRectangles, List<Period> indoorPeriods, String android_id)
{
// Graph Update
// Make Graph
Configuration conf = new BaseConfiguration();
conf.setProperty("storage.backend", "cassandra");
conf.setProperty("storage.hostname", "127.0.0.1");
TitanGraph g = TitanFactory.open(conf);
// Get Smartphone Vertex
Vertex v = TitanHelper.getVertex(g, "android_id", android_id);
if( v == null ){
v = g.addVertex(null);
v.setProperty("android_id", android_id);
v.setProperty("ltype", "Object");
}
// Process Outdoor Cluster
for( int i = 0 ; i < clusterRectangles.size() ; i++ )
{
ClusterRectangle clusterRectangle = clusterRectangles.get(i);
double minLon = clusterRectangle.getMinLon();
double minLat = clusterRectangle.getMinLat();
double maxLon = clusterRectangle.getMaxLon();
double maxLat = clusterRectangle.getMaxLat();
for(int j = 0 ; j < clusterRectangle.getPeriods().size() ;j++)
{
Vertex place = g.addVertex(null);
place.setProperty("ltype", "Place.outdoor");
place.setProperty("minLon", minLon);
place.setProperty("minLat", minLat);
place.setProperty("maxLon", maxLon);
place.setProperty("maxLat", maxLat);
Period period = clusterRectangle.getPeriods().get(j);
long from = period.getFrom() * 1000;
long to = period.getTo() * 1000;
Edge isLocatedIn = g.addEdge(null, v, place, "isLocatedIn");
Edge isContaining = g.addEdge(null, place, v, "isContaining");
isLocatedIn.setProperty("from", from);
isLocatedIn.setProperty("to", to);
isContaining.setProperty("from", from);
isContaining.setProperty("to", to);
}
}
// Process Indoor Points
for( int i = 0 ; i < indoorPeriods.size() ; i++ )
{
Period indoor = indoorPeriods.get(i);
Vertex place = g.addVertex(null);
place.setProperty("ltype", "Place.indoor");
place.setProperty("longitude", indoor.getLongitude());
place.setProperty("latitude", indoor.getLatitude());
place.setProperty("from", indoor.getFrom());
place.setProperty("to", indoor.getTo());
}
g.commit();
g.shutdown();
}
@SuppressWarnings("unused")
public void doVisualization(List<String> clusterMembers, String manipulatedString, List<Period> indoorPeriods, HttpServletRequest request) throws IOException
{
System.out.println(" [Location Clusterer] : Make Regions for Clusters.............");
List<ClusterRectangle> clusterRectangles = new ArrayList<ClusterRectangle>();
for( int i = 0 ; i < clusterMembers.size() ; i++ )
{
makeRLocationData("/home/jack/RTest/Data"+i+".txt", clusterMembers.get(i));
makeRLocationClusters("/home/jack/RTest/Cluster"+i+".txt", clusterMembers.get(i));
}
makeRIndoorPoint("/home/jack/RTest/Indoor.txt", indoorPeriods);
System.out.println(" [Location Clusterer] : [Debug] Getting Time periods for each cluster.............");
for(int i = 0 ; i < clusterRectangles.size() ; i++ )
{
ClusterRectangle cr = clusterRectangles.get(i);
List<Period> ps = cr.getPeriods();
System.out.println(cr.toString());
for(int j = 0 ; j < ps.size() ; j++ )
{
Period p = ps.get(j);
System.out.print("\t");
System.out.println(p.toString());
}
}
System.out.println(" [Location Clusterer] : Make R Script for Visualization.............");
int zoom = 14;
if( request.getParameter("zoom") != null )
zoom = Integer.parseInt(request.getParameter("zoom"));
makeRScript("/home/jack/RTest/Script.txt", manipulatedString, clusterMembers, zoom);
System.out.println(" [Location Clusterer] : Invoke R Command.............");
String[] cmd = new String[]{"R", "CMD", "BATCH", "/home/jack/RTest/Script.txt"};
Process process = new ProcessBuilder(cmd).start();
}
/**
* @param string
* @param indoorPeriods
*/
private void makeRIndoorPoint(String path, List<Period> indoorPeriods) {
String outStr = "lon\tlat\n";
for(int i = 0 ; i < indoorPeriods.size() ;i++ )
{
Period period = indoorPeriods.get(i);
double longitude = period.getLongitude();
double latitude = period.getLatitude();
outStr += "\t" + longitude + "\t" + latitude +"\n";
}
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
writer.write(outStr);
writer.close();
}
catch(Exception ex)
{
}
}
public List<Period> getIndoorData(String manipulatedString)
{
/*
* 127.36504046 36.3742216 9.0 1407381330
* 127.36506211 36.37423924 11.0 1407381340
* 127.36496859 36.37421911 11.0 1407381350
* 127.3648429 36.37416071 11.0 1407381360
* 127.36469597 36.37409721 11.0 1407381370
*/
List<Period> indoorPeriods = new ArrayList<Period>();
String[] lines = manipulatedString.split("\n");
boolean isIndoorStart = false;
Period period = new Period();
long lastPeriod = 0;
for(int i = 0 ; i < lines.length ; i++)
{
String line = lines[i];
String[] elements = line.split("\t");
if( elements.length == 4 )
{
int numSatellites = (int)Double.parseDouble(elements[2]);
if( isIndoorStart == false && numSatellites < 6)
{
isIndoorStart = true;
period = new Period();
period.setLongitude(Double.parseDouble(elements[0]));
period.setLatitude(Double.parseDouble(elements[1]));
period.setFrom(Long.parseLong(elements[3]));
}
else if( isIndoorStart == false && numSatellites != 0 )
{
// Keep going
}
else if( isIndoorStart == true && numSatellites == 0 )
{
// Keep going
}
else if( isIndoorStart == true && numSatellites >= 6 )
{
isIndoorStart = false;
period.setTo(Long.parseLong(elements[3]));
// We assume that Staying a place over 10 minutes is indoor
if( period.getTo() - period.getFrom() >= 600 )
{
//System.out.println(" [Location Clusterer] : [DEBUG] Success " + (period.getTo() - period.getFrom()));
indoorPeriods.add(period);
}
else
{
//System.out.println(" [Location Clusterer] : [DEBUG] Fail " + (period.getTo() - period.getFrom()));
}
}
lastPeriod = Long.parseLong(elements[3]);
}
}
if( isIndoorStart == true )
{
period.setTo(lastPeriod);
// We assume that Staying a place over 10 minutes is indoor
if( period.getTo() - period.getFrom() >= 600 )
{
//System.out.println(" [Location Clusterer] : [DEBUG] Success " + (period.getTo() - period.getFrom()));
indoorPeriods.add(period);
}
else
{
//System.out.println(" [Location Clusterer] : [DEBUG] Fail " + (period.getTo() - period.getFrom()));
}
}
return indoorPeriods;
}
public String makeQuery(HttpServletRequest request)
{
//stats.gauges.edd0562bfecd37a8.longitude,stats.gauges.edd0562bfecd37a8.latitude,stats.gauges.edd0562bfecd37a8.numSatellites
String target = null;
String from = null;
String until = null;
if( request.getParameter("target") != null )
target = (String)request.getParameter("target");
if( request.getParameter("from") != null )
from = (String)request.getParameter("from");
if( request.getParameter("until") != null)
until = (String)request.getParameter("until");
if( target == null)
return null;
String targetStr = String.format("target=stats.gauges.%s.longitude&target=stats.gauges.%s.latitude&target=stats.gauges.%s.numSatellites", target,target,target);
String url = "http://143.248.53.201/render?format=json";
if( from != null)
url += "&from=" + from;
if( until != null)
url += "&until=" + until;
url += "&" + targetStr;
return url;
}
public String filterIndoorData(String manipulatedString)
{
/*
* 127.36504046 36.3742216 9.0 1407381330
* 127.36506211 36.37423924 11.0 1407381340
* 127.36496859 36.37421911 11.0 1407381350
* 127.3648429 36.37416071 11.0 1407381360
* 127.36469597 36.37409721 11.0 1407381370
*/
String[] lines = manipulatedString.split("\n");
String ret ="";
for(int i = 0 ; i < lines.length ; i++)
{
String line = lines[i];
String[] elements = line.split("\t");
if( elements.length == 4 )
{
String numSateStr = elements[2];
double numSate = Double.parseDouble(numSateStr);
if( numSate > 4 )
{
ret += elements[0]+"\t"+elements[1]+"\t"+elements[3]+"\n";
}
}
}
return ret;
}
/**
* @param path
* @param string
*/
public ClusterRectangle getClusterRectangle(String clusterMembers) {
ClusterRectangle rec;
String[] lines = clusterMembers.split("\n");
double minLon = 1000 ;
double minLat = 1000 ;
double maxLon = 0 ;
double maxLat = 0 ;
List<GeoPoint> points = new ArrayList<GeoPoint>();
for( int i = 0 ; i < lines.length ; i++ )
{
double lon = Double.parseDouble(lines[i].split("\t")[0]);
double lat = Double.parseDouble(lines[i].split("\t")[1]);
long timestamp = Long.parseLong(lines[i].split("\t")[2]);
if( lon < minLon )
minLon = lon;
if( lat < minLat )
minLat = lat;
if( lon > maxLon )
maxLon = lon;
if( lat > maxLat )
maxLat = lat;
GeoPoint point = new GeoPoint(lon, lat, timestamp);
points.add(point);
}
rec = new ClusterRectangle(minLon, minLat, maxLon, maxLat);
rec.setMembers(points);
rec.computePeriods(60);
return rec;
}
/**
* @param path
* @param string
*/
public void makeRLocationClusters(String path, String clusterMembers) {
String outStr = "lon\tlat\n";
String[] lines = clusterMembers.split("\n");
double minLon = 1000 ;
double minLat = 1000 ;
double maxLon = 0 ;
double maxLat = 0 ;
for( int i = 0 ; i < lines.length ; i++ )
{
double lon = Double.parseDouble(lines[i].split("\t")[0]);
double lat = Double.parseDouble(lines[i].split("\t")[1]);
if( lon < minLon )
minLon = lon;
if( lat < minLat )
minLat = lat;
if( lon > maxLon )
maxLon = lon;
if( lat > maxLat )
maxLat = lat;
}
outStr += minLon + "\t" + minLat + "\n";
outStr += maxLon + "\t" + minLat + "\n";
outStr += maxLon + "\t" + maxLat + "\n";
outStr += minLon + "\t" + maxLat + "\n";
outStr += minLon + "\t" + minLat + "\n";
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
writer.write(outStr);
writer.close();
}
catch(Exception ex)
{
}
}
public List<String> filterNullClusters(List<String> clusterMembers)
{
List<String> filteredClusters = new ArrayList<String>();
for(int i = 0 ; i < clusterMembers.size() ; i++ )
{
String clusterMember = clusterMembers.get(i);
if( !clusterMember.equals(""))
{
filteredClusters.add(clusterMember);
}
}
return filteredClusters;
}
public List<String> getClusterMembers(String locData, String[] centers)
{
List<String> ret = new ArrayList<String>();
for( int i = 0 ; i < centers.length ; i++ )
{
String x = "";
ret.add(x);
}
String[] lines = locData.split("\n");
for( int i = 0 ; i < lines.length ; i++ )
{
// will be index of this point
int clusterIndex = -1;
double distance = 1000000000;
for( int j = 0 ; j < centers.length ; j++ )
{
// each center delimited by \t
String centerStr = centers[j];
String[] points = lines[i].split("\t");
String[] centerPoint = centerStr.split("\t");
double[] diff = new double[centerPoint.length];
for( int k = 0 ; k < centerPoint.length ; k ++ )
{
diff[k] = Double.parseDouble(points[k])-Double.parseDouble(centerPoint[k]);
}
// compute distance
double sqrSum = 0;
for( int k = 0 ; k < diff.length ; k++ )
{
sqrSum += diff[k]*diff[k];
}
double curDist = Math.sqrt(sqrSum);
if( curDist < distance )
{
distance = curDist;
clusterIndex = j;
}
}
String currStr = ret.get(clusterIndex);
currStr += lines[i] + "\n";
ret.set(clusterIndex, currStr);
}
return ret;
}
public String[] getCenters(List<Cluster> clusters)
{
String[] centers = new String[clusters.size()];
for(int i = 0 ; i < clusters.size() ; i++)
{
Cluster cluster = clusters.get(i);
String str = "";
for(int j = 0 ; j < cluster.getCenter().size() ; j++ )
{
// get(j) : j 's double value
double value = cluster.getCenter().get(j);
str += value + "\t";
}
str = str.substring(0, str.length()-1);
System.out.println("Cluster id: " + cluster.getId() + " center: " + str);
centers[i] = str;
}
return centers;
}
public static void makeRScript(String path, String locData, List<String> clusters, int zoom)
{
double minLon = 1000 ;
double minLat = 1000 ;
double maxLon = 0 ;
double maxLat = 0 ;
String[] lines = locData.split("\n");
for( int i = 0 ; i < lines.length ; i++ )
{
double lon = Double.parseDouble(lines[i].split("\t")[0]);
double lat = Double.parseDouble(lines[i].split("\t")[1]);
if( lon < minLon )
minLon = lon;
if( lat < minLat )
minLat = lat;
if( lon > maxLon )
maxLon = lon;
if( lat > maxLat )
maxLat = lat;
}
String script = "";
script += "require(mapproj)\n";
script += "require(ggmap)\n";
script += "myLocation <- c(" + (minLon) + "," + (minLat) + "," + (maxLon) + "," + (maxLat) + ")\n";
script += "myMap <- get_map(location=myLocation, source=\"google\", zoom="+zoom+", maptype=\"roadmap\")\n";
for(int i = 0 ; i < clusters.size() ; i++ )
{
script += "location" + i +" <- data.frame( read.table(\"/home/jack/RTest/Data"+i+".txt\", header=TRUE, sep=\"\\t\", row.names = NULL ))\n";
script += "cluster" + i +" <- data.frame( read.table(\"/home/jack/RTest/Cluster"+i+".txt\", header=TRUE, sep=\"\\t\", row.names = NULL ))\n";
script += "location" + i +"\n";
script += "cluster" + i +"\n";
}
script += "indoor <- data.frame( read.table(\"/home/jack/RTest/Indoor.txt\", header=TRUE, sep=\"\\t\", row.names = NULL ))\n";
script += "indoor\n";
script += "ggmap(myMap)";
for(int i = 0 ; i < clusters.size() ; i++ )
{
if( i == 0 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C1+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C1+"\", size=0.6, data=location"+i+", lineend = 'round') + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C1+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 1 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C2+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C2+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C2+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 2 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C3+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C3+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C3+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 3 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C4+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C4+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C4+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 4 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C5+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C5+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C5+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 5 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C6+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C6+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C6+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 6 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C7+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C7+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C7+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 7 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C8+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C8+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C8+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 8 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C9+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C9+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C9+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 9 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C10+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C10+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C10+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 10 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C11+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C11+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C11+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 11 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C12+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C12+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C12+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 12 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C13+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C13+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C13+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 13 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C14+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C14+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C14+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 14 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C15+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C15+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C15+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else if( i == 15 )
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C16+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C16+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C16+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
else
script += " + geom_point(aes(x=lon, y=lat), data=location"+i+", alpha=.7, color=\""+RColors.C17+"\", size=1.2) + geom_path(aes(x=lon, y=lat), colour=\""+RColors.C17+"\", size=0.6, data=location"+i+", lineend = 'round')+ geom_path(aes(x=lon, y=lat), colour=\""+RColors.C17+"\", size=0.6, data=cluster"+i+", lineend = 'round')";
}
script += " + geom_point(aes(x=lon, y=lat), data=indoor, alpha=.3, color=\"black\", size=5)";
script += "\nggsave(\"/home/jack/RTest/clustering.png\", dpi = 200)";
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
writer.write(script);
writer.close();
}
catch(Exception ex)
{
}
}
public static void makeRLocationClusters(String path, String[] centers)
{
String outStr = "lon\tlat\n";
for(int i = 0 ; i < centers.length ; i++ )
{
outStr += "\t";
outStr += centers[i] + "\n";
}
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
writer.write(outStr);
writer.close();
}
catch(Exception ex)
{
}
}
public static void makeRLocationData(String path, String locData)
{
String outStr = "lon\tlat\ttimestamp\n";
String[] lines = locData.split("\n");
// String prevLon ="";
// String prevLat ="";
for( int i = 0 ; i < lines.length ; i++ )
{
// String currLon = lines[i].split("\t")[0];
// String currLat = lines[i].split("\t")[1];
// if( prevLon.equals(currLon) && prevLat.equals(currLat))
// {
// continue;
// }
outStr += "\t";
outStr += lines[i] + "\n";
}
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
writer.write(outStr);
writer.close();
}
catch(Exception ex)
{
}
}
public List<Vector> getRandomPointVector(List<Vector> vectors, int numOfClusters)
{
List<Vector> retVector = new ArrayList<Vector>();
for( int i = 0 ; ( i < numOfClusters ) && ( i < vectors.size() ) ;i++ )
{
Vector vector = vectors.get(i);
retVector.add(vector);
}
return retVector;
}
public List<Cluster> runKMeansClustering(String points, int numOfClusters)
{
List<Vector> vectors = new ArrayList<Vector>();
//Parse points
//&
//Prepare list of vectors
String[] pointArray = points.split("\n");
for( int i = 0 ; i < pointArray.length ; i++ )
{
String pointStr = pointArray[i];
String[] axis = pointStr.split("\t");
double[] axisNum = new double[axis.length-1];
for( int j = 0 ; j < axis.length-1 ; j++ )
{
axisNum[j] = Double.parseDouble(axis[j]);
}
vectors.add(new DenseVector(axisNum));
}
// Choose Random Points, choose first [numOfClusters] points
List<Vector> randomPoints = getRandomPointVector(vectors, numOfClusters);
if( randomPoints.size() == 0 )
return null;
List<Cluster> clusters = new ArrayList<Cluster>();
int clusterId = 0;
for( Vector v : randomPoints )
{
clusters.add(new Cluster(v, clusterId++, new EuclideanDistanceMeasure()));
}
// number of cluster =
// Threshold of convergence = 0.01
List<List<Cluster>> finalClusters = KMeansClusterer.clusterPoints(vectors, clusters, new EuclideanDistanceMeasure(), 10000, 0.0000000000001);
return finalClusters.get(finalClusters.size()-1);
}
public String fusionGraphiteData(JSONArray jArr)
{
// Keep names
ArrayList<String> names = new ArrayList<String>();
ArrayList<JSONArray> dataBunch = new ArrayList<JSONArray>();
ArrayList<JSONArray> timeStamps = new ArrayList<JSONArray>();
for( int i = 0 ; i < jArr.length() ; i++ )
{
JSONObject jObj = jArr.getJSONObject(i);
names.add(jObj.getString("target"));
JSONArray dataBunchArr = new JSONArray();
JSONArray timeStampsArr= new JSONArray();
for( int j = 0 ; j < jObj.getJSONArray("datapoints").length() ; j++ )
{
JSONArray dataArr = jObj.getJSONArray("datapoints").getJSONArray(j);
Object left = dataArr.get(0);
Object right = dataArr.get(1);
if( left.toString().equals("null") )
continue;
dataBunchArr.put(left.toString());
timeStampsArr.put(right.toString());
}
dataBunch.add(dataBunchArr);
timeStamps.add(timeStampsArr);
}
if( dataBunch.size() < 1 )
{
return null;
}
int size = dataBunch.get(0).length();
String retStr = "";
for( int i = 0 ; i < size ; i++ )
{
for( int j = 0 ; j < dataBunch.size() ; j++ )
{
retStr += dataBunch.get(j).getString(i);
retStr += "\t";
}
retStr += timeStamps.get(0).getString(i) ;
retStr += "\n";
}
return retStr;
}
public String getDataFromGraphite(String url) throws IOException
{
// HTTP Get Request, refer to "http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/"
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
return response.toString();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}