Skip to content
Snippets Groups Projects
Commit 6a82a76a authored by Christopher Schankula's avatar Christopher Schankula :earth_africa:
Browse files

Merge branch 'serial' into 'TeamA'

serialization of KDTree, BST and BioTree

See merge request schankuc/2XB3!7
parents a0ae6ae9 9bb40710
No related branches found
No related tags found
No related merge requests found
......@@ -7,3 +7,4 @@
*.blg
*DS_Store*
*.csv
*.ser
package data;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import org.json.simple.parser.ParseException;
import search.BST;
import sort.KDT;
public class BioTree {
public class BioTree implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4291273291916906661L;
//FIXME: replace with a single kd-tree
private static BST<Integer, TaxonNode> idNodes = new BST<Integer, TaxonNode>();
private static BST<String, TaxonNode> strNodes = new BST<String, TaxonNode>();
private static BST<String, Integer> incorrectNames = new BST<String, Integer>();
public static void main(String[] args) throws IOException, ParseException {
BioTree.processRecord("Micropterus dolomieui");
BioTree.processRecord(123123);
//BioTree.write("biotree");
BioTree.init("biotree");
System.out.println(idNodes.get(2));
}
/**
......@@ -29,9 +42,53 @@ public class BioTree {
*
* @param fn
* Filename to read from
* @return
*/
public static void init(String fn) {
BST<Integer, TaxonNode> idNodes = null;
try {
FileInputStream fileIn = new FileInputStream(fn+"/idnodes.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
idNodes = (BST<Integer, TaxonNode>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
BioTree.idNodes = idNodes;
BST<String, TaxonNode> strNodes = null;
try {
FileInputStream fileIn = new FileInputStream(fn+"/strNodes.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
strNodes = (BST<String, TaxonNode>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
BioTree.strNodes = strNodes;
BST<String, Integer> incorrectNames = null;
try {
FileInputStream fileIn = new FileInputStream(fn+"/incorNames.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
incorrectNames = (BST<String, Integer>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
BioTree.incorrectNames = incorrectNames;
}
/**
......@@ -42,7 +99,41 @@ public class BioTree {
* Filename to write to
*/
public static void write(String fn) {
try {
FileOutputStream fileOut =
new FileOutputStream(fn+"/idNodes.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(BioTree.idNodes);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
try {
FileOutputStream fileOut =
new FileOutputStream(fn+"/strNodes.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(BioTree.strNodes);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
try {
FileOutputStream fileOut =
new FileOutputStream(fn+"/incorNames.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(BioTree.incorrectNames);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
/**
......
package data;
import java.io.Serializable;
import java.util.ArrayList;
/**
......@@ -11,7 +12,11 @@ import java.util.ArrayList;
* https://stackoverflow.com/questions/2697182/how-to-use-an-array-list
*/
public class TaxonNode {
public class TaxonNode implements Serializable {
/**
*
*/
private static final long serialVersionUID = -317741592166253773L;
private final int taxonId;
private final TaxonType taxonType;
private final String name;
......
package sandbox;
public class Point implements Comparable<Point> {
import java.io.Serializable;
public class Point implements Comparable<Point>, Serializable {
/**
*
*/
private static final long serialVersionUID = 5361956730616676054L;
private final int x;
private final int y;
......
package search;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class BST<Key extends Comparable<Key>, Value> {
import sort.KDT;
public class BST<Key extends Comparable<Key>, Value> implements Serializable {
/**
*
*/
public static void main(String[] args) {
//BST<Integer,Integer> bst = new BST<Integer, Integer>();
//bst.put(75, 9);
//bst.writeToFile("bst.ser");
BST<Integer, Integer> bst = new BST<Integer, Integer>("bst.ser");
System.out.println(bst.get(75));
}
private static final long serialVersionUID = 8775155124761510511L;
private Node root;
private class Node{
public BST(String fn) {
BST<Key,Value> bst = null;
try {
FileInputStream fileIn = new FileInputStream(fn);
ObjectInputStream in = new ObjectInputStream(fileIn);
bst = (BST<Key,Value>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
this.root = bst.root;
}
public BST() {
root = null;
}
private class Node implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8145778479611668151L;
private Key key;
private Value val;
private Node left, right;
......@@ -183,4 +229,18 @@ public class BST<Key extends Comparable<Key>, Value> {
if (x == null) return 0;
else return Math.max(height (x.left), height(x.right)) + 1;
}
public void writeToFile(String fn) {
try {
FileOutputStream fileOut =
new FileOutputStream(fn);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
}
package sort;
public interface GeneralCompare<T> {
import java.io.Serializable;
public interface GeneralCompare<T> extends Serializable {
/**
* Compare two Comparable elements based on an arbitrary ordering definition.
* @param a1 The first value to be compared.
......
package sort;
import sandbox.Point;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class KDT<KeyVal extends Comparable<KeyVal>> {
Node root;
public class KDT<KeyVal extends Comparable<KeyVal>> implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8807259801436570835L;
/**
*
*/
KDNode root;
ArrayList<GeneralCompare<KeyVal>> axes;
public static void main(String[] args) {
......@@ -26,7 +40,8 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
Point[] pts = {p1, p2, p3, p4, p5, p6};
KDT<Point> kdt = new KDT<Point>(axes, pts);
//KDT<Point> kdt = new KDT<Point>(axes, pts);
KDT<Point> kdt = new KDT<Point>("kdtree.ser");
System.out.println(kdt.size());
System.out.println(kdt.height());
......@@ -49,25 +64,57 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
}
System.out.println(kdt.toString());
kdt.writeToFile("kdtree.ser");
}
private class Node{
private class KDNode implements Serializable{
/**
*
*/
private static final long serialVersionUID = -664511393872278542L;
/**
*
*/
private KeyVal keyval;
private Node left, right;
private KDNode left, right;
private int n;
public Node(KeyVal keyval, int n) {
public KDNode(KeyVal keyval, int n) {
this.keyval = keyval;
this.n = n;
}
}
/**
* Load a kd-tree from a serialized file.
* @param fn
*/
public KDT(String fn) {
KDT<KeyVal> kdt = null;
try {
FileInputStream fileIn = new FileInputStream(fn);
ObjectInputStream in = new ObjectInputStream(fileIn);
kdt = (KDT<KeyVal>) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
} catch (ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
}
//https://stackoverflow.com/questions/26327956/set-this-in-a-class
this.root = kdt.root;
this.axes = kdt.axes;
}
public KDT(ArrayList<GeneralCompare<KeyVal>> axes, Comparable<KeyVal>[] keyvals) {
this.axes = axes;
root = buildTree(keyvals, 0, keyvals.length - 1, 0);
}
private Node buildTree(Comparable<KeyVal>[] keyvals, int lo, int hi, int depth) {
private KDNode buildTree(Comparable<KeyVal>[] keyvals, int lo, int hi, int depth) {
if (lo > hi) return null;
int axis = depth % getK();
......@@ -76,7 +123,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
KeyVal median = (KeyVal) keyvals[mid];
//TODO: fix size
Node newNode = new Node(median, 0);
KDNode newNode = new KDNode(median, 0);
newNode.left = buildTree(keyvals, lo, mid - 1, depth + 1);
newNode.right = buildTree(keyvals, mid + 1, hi, depth + 1);
......@@ -90,7 +137,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return result;
}
private void rangeSearch(Node x, ArrayList<GeneralRange<KeyVal>> range, ArrayList<KeyVal> result, int depth) {
private void rangeSearch(KDNode x, ArrayList<GeneralRange<KeyVal>> range, ArrayList<KeyVal> result, int depth) {
if (x == null) return;
int axis = depth % getK();
GeneralRange<KeyVal> rg = range.get(axis);
......@@ -127,12 +174,12 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return height(root);
}
private int height(Node x) {
private int height(KDNode x) {
if (x == null) return 0;
return 1 + Math.max(height(x.left), height(x.right));
}
private int size(Node x) {
private int size(KDNode x) {
if (x == null) return 0;
else return x.n;
}
......@@ -145,7 +192,21 @@ public class KDT<KeyVal extends Comparable<KeyVal>> {
return toString(root, "");
}
private String toString(Node x, String depth) {
public void writeToFile(String fn) {
try {
FileOutputStream fileOut =
new FileOutputStream(fn);
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
private String toString(KDNode x, String depth) {
if (x == null) return depth + "null\n";
String result = "";
result += depth + x.keyval.toString() + "\n";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment