Skip to content

Commit

Permalink
Initial code commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Shelton committed Aug 14, 2017
1 parent f62719f commit 901536e
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/com/forecast/testplugin/CSVDataStructure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.forecast.testplugin;

import com.forecast.lib.data.DataStructure;
import com.forecast.lib.graph.DataAlias;
import com.forecast.lib.graph.DataAliasStub;
import com.forecast.lib.graph.DataPoint;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* Created by sheltonz on 4/4/2017.
*/
public class CSVDataStructure extends DataStructure {

public CSVDataStructure() {
super("CSV", "*.csv", "*.txt");
}

@Override
public List<DataPoint> importData(File file, final DataAlias xAlias, final DataAlias yAlias) {
final List<DataPoint> dataPoints = new ArrayList<>();
try {
final List<String> tempList = Util.readFileAsDocument(file);
for (final String s : tempList) {
final String[] split = s.split(",");
final double posX = xAlias.convertValue(split[0].trim());
final double posY = yAlias.convertValue(split[1].trim());
dataPoints.add(new DataPoint(new DataAliasStub(posX, split[0].trim()), new DataAliasStub(posY, split[1].trim())));
}
} catch (IOException e) {
e.printStackTrace();
}
return dataPoints;
}
}
51 changes: 51 additions & 0 deletions src/com/forecast/testplugin/DateDataAlias.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.forecast.testplugin;

import com.forecast.lib.graph.DataAlias;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* Created by sheltonz on 4/6/2017.
*/
public class DateDataAlias extends DataAlias {
private final List<SimpleDateFormat> dateFormats = new ArrayList<>();

private SimpleDateFormat lastFormat = null;

public DateDataAlias() {
super("Date");
dateFormats.add(new SimpleDateFormat("dd-MMM-yy"));
dateFormats.add(new SimpleDateFormat("dd-MM-yy"));
dateFormats.add(new SimpleDateFormat("dd-MM-yyyy"));
dateFormats.add(new SimpleDateFormat("dd-MMM-yyyy"));
dateFormats.add(new SimpleDateFormat("dd/MMM/yy"));
dateFormats.add(new SimpleDateFormat("dd/MM/yy"));
dateFormats.add(new SimpleDateFormat("dd/MM/yyyy"));
dateFormats.add(new SimpleDateFormat("dd/MMM/yyyy"));
}

@Override
public String convertData(double data) {
final Date date = new Date((long) data);
if (lastFormat == null) {
return dateFormats.get(0).format(date);
}
return lastFormat.format(date);
}

@Override
public double convertValue(String value) {
for (final SimpleDateFormat f : dateFormats) {
try {
lastFormat = f;
return f.parse(value).getTime();
} catch (final Exception e) {
// e.printStackTrace();
}
}
return Double.MIN_VALUE;
}
}
62 changes: 62 additions & 0 deletions src/com/forecast/testplugin/ExponentialPrediction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.forecast.testplugin;

import com.forecast.lib.graph.DataAlias;
import com.forecast.lib.graph.DataAliasStub;
import com.forecast.lib.graph.DataPoint;
import com.forecast.lib.prediction.Prediction;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Zachary on 4/3/2017.
*/
public class ExponentialPrediction extends Prediction {

private DataPoint lastPoint;

public ExponentialPrediction() {
super("Exponential");
}

private double calculateSlope(List<DataPoint> dataPoints) {
this.lastPoint = dataPoints.get(dataPoints.size() - 1);
final List<Double> tempList = new ArrayList<Double>();
double lastTime = -1;
double lastPrice = -1;
for (final DataPoint p : dataPoints) {
if (lastTime == -1 && lastPrice == -1) {
lastTime = p.getPosX().getData();
lastPrice = p.getPosY().getData();
continue;
}
tempList.add((p.getPosY().getData() - lastPrice) / (p.getPosX().getData() - lastTime));
// System.out.println(getDays(p.getDate()));
// System.out.println(new SimpleDateFormat("MM/dd/yyyy").format(p.getDate().getTime()));
// System.out.println((p.getPrice() - lastPoint) / (getDays(p.getDate()) - lastTime));
lastTime = p.getPosX().getData();
lastPrice = p.getPosY().getData();
}
double result = 0;
for (final double d : tempList) {
result += d;
}
return result / tempList.size();
}

@Override
protected DataPoint predictPosX(final DataPoint dataPoint, final DataAlias alias, final List<DataPoint> dataPoints) {
final double slope = calculateSlope(dataPoints);
final double data = (((dataPoint.getPosY().getData() - lastPoint.getPosY().getData()) / slope) + lastPoint.getPosX().getData());
final DataAliasStub stub = new DataAliasStub(data, alias.convertData(data));
return new DataPoint(stub, dataPoint.getPosY());
}

@Override
protected DataPoint predictPosY(final DataPoint dataPoint, final DataAlias alias, final List<DataPoint> dataPoints) {
final double slope = calculateSlope(dataPoints);
final double data = slope * (dataPoint.getPosX().getData() - lastPoint.getPosX().getData()) + lastPoint.getPosY().getData();
final DataAliasStub stub = new DataAliasStub(data, alias.convertData(data));
return new DataPoint(dataPoint.getPosX(), stub);
}
}
24 changes: 24 additions & 0 deletions src/com/forecast/testplugin/GenericDataAlias.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.forecast.testplugin;

import com.forecast.lib.graph.DataAlias;

/**
* Created by sheltonz on 4/5/2017.
*/
public class GenericDataAlias extends DataAlias {

public GenericDataAlias() {
super("Generic");
}

@Override
public String convertData(double data) {
return String.format("%f", data);
}

@Override
public double convertValue(String value) {
return Double.parseDouble(value);
}

}
48 changes: 48 additions & 0 deletions src/com/forecast/testplugin/LinearPrediction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.forecast.testplugin;

import com.forecast.lib.graph.DataAlias;
import com.forecast.lib.graph.DataAliasStub;
import com.forecast.lib.graph.DataPoint;
import com.forecast.lib.prediction.Prediction;

import java.util.List;

/**
* Created by sheltonz on 4/10/2017.
*/
public class LinearPrediction extends Prediction {
public LinearPrediction() {
super("Linear");
}

private double generateSlope(final List<DataPoint> dataPoints) {
double totalSlope = 0;
for (final DataPoint p : dataPoints) {
totalSlope += p.getPosY().getData() / p.getPosX().getData();
}
return totalSlope / dataPoints.size();
}

private double calculateYIntercept(final DataPoint dataPoint, final double slope) {
return dataPoint.getPosY().getData() + (slope * dataPoint.getPosX().getData());
}

@Override
protected DataPoint predictPosX(DataPoint dataPoint, DataAlias alias, List<DataPoint> dataPoints) {
final DataPoint firstPoint = dataPoints.get(0);
final double slope = generateSlope(dataPoints);
final double data = (dataPoint.getPosY().getData() - calculateYIntercept(firstPoint, slope)) / slope;
final DataAliasStub stub = new DataAliasStub(data, alias.convertData(data));
return new DataPoint(stub, dataPoint.getPosY());

}

@Override
protected DataPoint predictPosY(DataPoint dataPoint, DataAlias alias, List<DataPoint> dataPoints) {
final DataPoint firstPoint = dataPoints.get(0);
final double slope = generateSlope(dataPoints);
final double data = slope * dataPoint.getPosX().getData() + calculateYIntercept(firstPoint, slope);
final DataAliasStub stub = new DataAliasStub(data, alias.convertData(data));
return new DataPoint(dataPoint.getPosX(), stub);
}
}
39 changes: 39 additions & 0 deletions src/com/forecast/testplugin/TestPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.forecast.testplugin;

import com.forecast.lib.Plugin;
import com.forecast.lib.data.DataStructure;
import com.forecast.lib.graph.DataAlias;
import com.forecast.lib.prediction.Prediction;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Zachary on 4/3/2017.
*/
public class TestPlugin implements Plugin {

@Override
public List<Prediction> predictions() {
final List<Prediction> predictions = new ArrayList<>();
predictions.add(new ExponentialPrediction());
predictions.add(new LinearPrediction());
return predictions;
}

@Override
public List<DataStructure> dataStructures() {
final List<DataStructure> dataStructures = new ArrayList<>();
dataStructures.add(new CSVDataStructure());
return dataStructures;
}

@Override
public List<DataAlias> dataAliases() {
final List<DataAlias> aliases = new ArrayList<>();
aliases.add(new GenericDataAlias());
aliases.add(new DateDataAlias());
return aliases;
}

}
24 changes: 24 additions & 0 deletions src/com/forecast/testplugin/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.forecast.testplugin;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by sheltonz on 4/5/2017.
*/
public class Util {

public static List<String> readFileAsDocument(final File file) throws IOException {
final Reader reader = new FileReader(file);
final BufferedReader bufferedReader = new BufferedReader(reader);
final List<String> contents = new ArrayList<String>();
String line = null;
while ((line = bufferedReader.readLine()) != null) {
contents.add(line);
}
reader.close();
bufferedReader.close();
return contents;
}
}
8 changes: 8 additions & 0 deletions src/plugin.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
id : "generic",
name : "Starter Pack",
description : "A simple plugin with generic data tools",
author: "Zachary Shelton",
version : 1.0,
mainClass : "com.forecast.testplugin.TestPlugin"
}

0 comments on commit 901536e

Please sign in to comment.