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

serialization for BioTree!

parent 960b23c7
Branches serial
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 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();
}
}
}
......@@ -65,17 +65,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> implements Serializable {
System.out.println(kdt.toString());
try {
FileOutputStream fileOut =
new FileOutputStream("kdtree.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(kdt);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/kdtree.ser");
} catch (IOException i) {
i.printStackTrace();
}
kdt.writeToFile("kdtree.ser");
}
private class KDNode implements Serializable{
......@@ -114,6 +104,7 @@ public class KDT<KeyVal extends Comparable<KeyVal>> implements Serializable {
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;
}
......@@ -201,6 +192,20 @@ public class KDT<KeyVal extends Comparable<KeyVal>> implements Serializable {
return toString(root, "");
}
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 = "";
......
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