Skip to content
Snippets Groups Projects
Commit 6d648c23 authored by Ray Liu's avatar Ray Liu
Browse files
web

# Conflicts:
#	src/search/Histogram.java
#	src/sort/Bound.java
parents ab884daf b093c717
No related branches found
No related tags found
No related merge requests found
Showing
with 394 additions and 249 deletions
......@@ -7,6 +7,8 @@
*.blg
*DS_Store*
*.csv
*/bin
*.ser
.idea/
*.class
logs/
\ No newline at end of file
File added
File added
server:
mkdir -p tomcat/webapps/Trawl/WEB-INF/classes
chmod u+x tomcat/bin/startup.sh
chmod u+x tomcat/bin/shutdown.sh
chmod u+x tomcat/bin/catalina.sh
cp -rf bin/* tomcat/webapps/Trawl/WEB-INF/classes
start: server
......
TOMCAT_DIR = tomcat/webapps/Trawl
BIN_DIR = tomcat/webapps/Trawl/bin
server: $(BIN_DIR)/TrawlExpert.class $(BIN_DIR)/../WEB-INF/classes/com/example/web/Director.class
javac -d $(TOMCAT_DIR)/WEB-INF/classes/ -classpath tomcat/lib/servlet-api.jar $(TOMCAT_DIR)/src/com/example/listener/StartUpContext.java
start:
tomcat/bin/startup.sh
stop:
tomcat/bin/shutdown.sh
$(BIN_DIR)/TrawlExpert.class: $(BIN_DIR)/data/BioTree.class $(BIN_DIR)/data/DataStore.class $(BIN_DIR)/data/Date.class $(BIN_DIR)/data/FileProcessor.class $(BIN_DIR)/data/Record.class $(BIN_DIR)/data/TaxonNode.class $(BIN_DIR)/data/TaxonType.class $(BIN_DIR)/data/WormsAPI.class $(BIN_DIR)/search/BasicSearch.class $(BIN_DIR)/search/BST.class $(BIN_DIR)/search/Histogram.class $(BIN_DIR)/sort/Bound.class $(BIN_DIR)/sort/GeneralCompare.class $(BIN_DIR)/sort/GeneralRange.class $(BIN_DIR)/sort/KDT.class $(BIN_DIR)/sort/MergeSort.class $(BIN_DIR)/sort/RangeHelper.class
javac -d $(TOMCAT_DIR)/WEB-INF/classes/ -classpath $(TOMCAT_DIR)/lib/servlet-api.jar:$(BIN_DIR):bin:json-simple-1.1.1.jar $(TOMCAT_DIR)/src/com/example/model/TrawlExpert.java
$(BIN_DIR)/data/BioTree.class: src/data/BioTree.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/BioTree.java
$(BIN_DIR)/data/DataStore.class: src/data/DataStore.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/DataStore.java
$(BIN_DIR)/data/Date.class: src/Data/date.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/Date.java
$(BIN_DIR)/data/FileProcessor.class: src/data/FileProcessor.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/FileProcessor.java
$(BIN_DIR)/data/Record.class: src/data/Record.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/Record.java
$(BIN_DIR)/data/TaxonNode.class: src/data/TaxonNode.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/TaxonNode.java
$(BIN_DIR)/data/TaxonType.class: src/data/TaxonType.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/TaxonType.java
$(BIN_DIR)/data/WormsAPI.class: src/data/WormsAPI.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/data/WormsAPI.java
$(BIN_DIR)/search/BasicSearch.class: src/search/BasicSearch.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/search/BasicSearch.java
$(BIN_DIR)/search/BST.class: src/search/BST.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/search/BST.java
$(BIN_DIR)/search/Histogram.class: src/search/Histogram.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/search/Histogram.java
$(BIN_DIR)/sort/Bound.class: src/sort/Bound.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/sort/Bound.java
$(BIN_DIR)/sort/GeneralCompare.class: src/sort/GeneralCompare.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/sort/GeneralCompare.java
$(BIN_DIR)/sort/GeneralRange.class: src/sort/GeneralRange.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/sort/GeneralRange.java
$(BIN_DIR)/sort/KDT.class: src/sort/KDT.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/sort/KDT.java
$(BIN_DIR)/sort/MergeSort.class: src/sort/MergeSort.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/sort/MergeSort.java
$(BIN_DIR)/sort/RangeHelper.class: src/sort/RangeHelper.java
javac -d $(BIN_DIR) -classpath bin::json-simple-1.1.1.jar src/sort/RangeHelper.java
$(BIN_DIR)/../WEB-INF/classes/com/example/web/Director.class: $(BIN_DIR)/com/example/web/Director.java
javac -d $(TOMCAT_DIR)/WEB-INF/classes -classpath src/tomcat/lib/servlet-api.jar:$(BIN_DIR) $(BIN_DIR)/com/example/web/Director.java
\ No newline at end of file
server: /TrawlExpert.java
javac -classpath
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.simple.parser.ParseException;
import data.BioTree;
import data.DataStore;
import data.Date;
import data.FileProcessor;
import data.Record;
import data.WormsAPI;
import search.BST;
import search.BasicSearch;
import search.BasicSearchResult;
import search.Histogram;
import sort.Bound;
import sort.GeneralRange;
import sort.KDT;
import sort.RangeHelper;
import utils.Stopwatch;
public class Main {
public static void main(String[] args) {
......@@ -27,7 +42,7 @@ public class Main {
BioTree.write("data/biotree/");
DataStore.records.writeToFile("data/kdt.ser");
};
BasicSearch.init();
init();
}
private static void printLogo() {
......@@ -53,4 +68,135 @@ public class Main {
" `-' `````````");
System.out.println("Loading.......");
}
public static void init() {
System.out.println("Welcome!");
while(true) {
System.out.println("Main Menu");
System.out.println("Available commands:");
System.out.println("\ttree [taxonId / scientific name]");
System.out.println("\trecords (taxonId / scientific name) [-t start end]");
System.out.print("> ");
Pattern pat = Pattern.compile("([a-zA-Z]+)[ ]?([0-9a-zA-Z ]+[0-9a-zA-Z])?[ ]?[-]?([a-zA-Z])?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?");
Scanner s = new Scanner(System.in);
String line = s.nextLine();
Matcher matcher = pat.matcher(line);
if (!matcher.find()) continue;
//tree
//tree taxonId
//tree scientific name
//records taxonId
//records scientific name
String command = matcher.group(1);
if (command.equals("records"))
rangeSearch(matcher);
else if (command.equals("tree"))
printTree(matcher);
}
}
private static void rangeSearch(Matcher matcher) {
Integer start = null;
Integer end = null;
if (matcher.group(3) != null)
if (matcher.group(3).equals("t")) {
if (matcher.group(4) != null)
start = Integer.parseInt(matcher.group(4));
if (matcher.group(5) != null)
end = Integer.parseInt(matcher.group(5));
}
Integer taxonId = null;
try {
taxonId = Integer.parseInt(matcher.group(2));
} catch (NumberFormatException e) {
if (taxonId == null) {
try {
taxonId = WormsAPI.nameToRecordID(matcher.group(2));
} catch (IOException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
BasicSearchResult result = BasicSearch.range(taxonId, start, end);
System.out.println("Found " + result.n() + " records in " + result.time() + " seconds.");
while(true) {
System.out.println("Available commands: list, histogram, sum, exit");
System.out.print("> ");
Scanner s = new Scanner(System.in);
String command = s.nextLine();
if (command.equals("list"))
printRecords(result.results());
else if (command.equals("histogram")) {
printHistogram(result.histogram());
} else if (command.equals("exit"))
return;
else if (command.equals("sum")) {
System.out.println(result.sum());
}
}
}
private static void printRecords(Iterable<Record> results) {
String format = "|%1$-45s|%2$-15s|%3$-15s|%4$-15s|%5$-15s|%6$-15s\n";
System.out.format(format, "Scientific Name", "IndividualCount", "Latitude", "Longitude","Year","Month","Day");
for (Record r: results) {
System.out.println(r);
}
}
private static void printTree(Matcher matcher) {
Integer taxonId;
String name;
if (matcher.group(2) == null)
BioTree.printTree();
else {
name = matcher.group(2);
try {
taxonId = Integer.parseInt(name);
BioTree.printTree(taxonId);
} catch (Exception e) {
BioTree.printTree(name);
}
}
System.out.println();
}
/**
* Prints a histogram based on a BST of records
*
* @param record -An BST of records
*/
public static void printHistogram(BST<Integer,Integer> record) {
int max = 0;
int scale = 100;
Iterable<Integer> results = record.keys();
for (Integer year: results) {
if (max < record.get(year)) max =record.get(year);
}
System.out.println(" |" + (new String(new char[scale]).replace('\0', '-')) + "|");
String format = "%1$-5d|%2$-" + (scale + 1) + "s";
for (Integer year: results) {
String s = "=";
int loopc = (int) ((float)(record.get(year)/ (float) max) * scale);
for (int j=0; j< loopc; j++) {
s+="=";
}
System.out.format(format, year, s);
System.out.println("| " + record.get(year));
}
System.out.format("Scale: one = is %d individuals.\n", max / scale);
}
}
......@@ -2,6 +2,7 @@ package data;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
......@@ -169,33 +170,10 @@ public class BioTree implements Serializable {
*/
public static Integer processRecord(String scientificName) throws IOException, ParseException {
//reverse lookup based on name, try adding the found taxonId.
System.out.println("Processing " + scientificName);
TaxonNode res = strNodes.get(scientificName);
if (res == null) {
System.out.println("Not already found.");
Integer incorrectNameId = incorrectNames.get(scientificName);
if (incorrectNameId != null) if (incorrectNameId == -1) return null;
res = idNodes.get(incorrectNameId);
}
Integer taxonId = null;
//if the taxonId was not found in the local database
if (res == null) {
try {
taxonId = WormsAPI.nameToID(scientificName);
} catch (Exception e) {
taxonId = WormsAPI.nameToRecordID(scientificName);
if (taxonId != null)
incorrectNames.put(scientificName, taxonId);
else {
incorrectNames.put(scientificName, -1);
}
}
}
else
taxonId = res.getTaxonId();
Integer taxonId = nameToTaxonId(scientificName);
System.out.println(scientificName + ": " + taxonId);
if (taxonId == null) return null;
if (taxonId == -999)
taxonId = WormsAPI.nameToRecordID(scientificName);
if (taxonId == -1) return null;
if (processTaxonId(taxonId)) return null;
return taxonId;
}
......@@ -259,6 +237,58 @@ public class BioTree implements Serializable {
return idNodes.get(taxonId);
}
/**
* Get the species at a given index (taxonId). This assumes that the
* node already exists or else it will return null. As such, it is best
* to use this function once all the data has been parsed and the BioTree
* has been built.
*
* @param i
* The speciesid (index) of the species.
* @return The Species object.
* @throws ParseException
* @throws IOException
*/
public static TaxonNode getTaxonRecord(String scientificName) throws IOException, ParseException {
return idNodes.get(nameToTaxonId(scientificName));
}
/**
* Get the TaxonNode containing information about the given scientific name.
* This assumes that thenode already exists locally or else it will return null.
* As such, it is best to use this function once all the data has been parsed
* and the BioTree has been built.
*
* @param scientificName
* The scientific name of the taxon.
* @return The Species object.
* @throws ParseException
* @throws IOException
*/
public static Integer nameToTaxonId(String scientificName) throws IOException, ParseException {
Integer taxonId;
//look up based on string literal, return if found
TaxonNode tx = strNodes.get(scientificName);
if (tx != null) return tx.getTaxonId();
else System.out.println(scientificName + " not in local db");
//look up in local incorrect names database, return if it exists
taxonId = incorrectNames.get(scientificName);
if (taxonId != null) {
tx = idNodes.get(taxonId);
if (tx != null) return tx.getTaxonId();
} else { //otherwise use Worms to look it up
System.out.println(scientificName + " not in incor db");
taxonId = WormsAPI.nameToRecordID(scientificName);
if (taxonId == null) //if nothing is found, mark this species as not existing.
incorrectNames.put(scientificName, -1);
else {
System.out.println(scientificName + " found in Worms: " + taxonId);
incorrectNames.put(scientificName, taxonId);
}
}
return taxonId;
}
public static Iterable<Integer> getNonEmptyChildren(int taxonId){
ArrayList<Integer> result = new ArrayList<Integer>();
......
......@@ -153,7 +153,9 @@ public class FileProcessor {
int errorCount=0; // TESTING ONLY
if (!matchEventId.group(10).equals("NA")) {
try{
al.add(createRecord(Integer.parseInt(matchEventId.group(3)), matchEventId.group(1), BioTree.processRecord(matchEventId.group(10)), Integer.parseInt(matchEventId.group(2)), Float.parseFloat(matchEventId.group(7)), Float.parseFloat(matchEventId.group(8)), Integer.parseInt(matchEventId.group(4)), Integer.parseInt(matchEventId.group(5)), Integer.parseInt(matchEventId.group(6))));
Integer taxonId = BioTree.processRecord(matchEventId.group(10));
if (taxonId != null)
al.add(createRecord(Integer.parseInt(matchEventId.group(3)), matchEventId.group(1), taxonId, Integer.parseInt(matchEventId.group(2)), Float.parseFloat(matchEventId.group(7)), Float.parseFloat(matchEventId.group(8)), Integer.parseInt(matchEventId.group(4)), Integer.parseInt(matchEventId.group(5)), Integer.parseInt(matchEventId.group(6))));
} catch(IOException e) {
System.out.println("Input Error:" + e);
} catch(NullPointerException e) {
......
......@@ -31,7 +31,7 @@ public class WormsAPI {
* System.out.println(taxnodes[i].getTaxonType());
* System.out.println(taxnodes[i].getName()); }
*/
System.out.println(nameToRecordID("Hello"));
}
/**
......@@ -107,8 +107,6 @@ public class WormsAPI {
int id = (int) (long) ((JSONObject)json.get(0)).get("AphiaID");
return id;
}
/**
......
package model;
import java.io.IOException;
import org.json.simple.parser.ParseException;
import data.BioTree;
import data.DataStore;
import data.FileProcessor;
import data.Record;
import data.TaxonNode;
import search.BasicSearch;
import search.BasicSearchResult;
import sort.KDT;
public class TrawlExpert {
......@@ -32,4 +36,22 @@ public class TrawlExpert {
public Iterable<Integer> listAllSpecies(){
return BioTree.getNonEmptyChildren(2);
}
public BasicSearchResult rangeSearch(Integer taxonId, Integer yearLo, Integer yearHi) {
return BasicSearch.range(taxonId, yearLo, yearHi);
}
public TaxonNode getTaxonRecord(Integer taxonId) {
return BioTree.getTaxonRecord(taxonId);
}
public TaxonNode getTaxonRecord(String scientificName) {
try {
return BioTree.getTaxonRecord(scientificName);
} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
package com.example.model;
import java.util.*;
public class TrawlExpert {
public List getBrands(String phylum) {
List brands = new ArrayList();
if (phylum.equals("Arthropoda")) {
brands.add("Some arthropoda data");
brands.add("Some more Arthropoda data");
}
else {
brands.add("This aint arthropoda");
brands.add("Under construction");
}
return brands;
}
}
// For Testing on Java's Console
// public static void main(String[]args){
// String color = "amber";
// System.out.println(brand.get(0));
// }
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>sandbox</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
package sandbox;
public interface GeneralCompare {
public int eval(Comparable a1, Comparable a2);
}
......@@ -7,19 +7,19 @@ public class Point implements Comparable<Point>, Serializable {
*
*/
private static final long serialVersionUID = 5361956730616676054L;
private final int x;
private final int y;
private final double x;
private final double y;
public Point(int x, int y) {
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public int getX() {
public double getX() {
return this.x;
}
public int getY() {
public double getY() {
return this.y;
}
......
......@@ -7,9 +7,9 @@ package sandbox;
public class TestLambdas {
public static void main(String[] args) {
//declare the instances of BinaryIntExpression
BinaryIntExpression b0;
BinaryIntExpression b1;
BinaryIntExpression b2;
GeneralCompare<Integer> b0;
GeneralCompare<Integer> b1;
GeneralCompare<Integer> b2;
BinaryIntExpression b3;
BinaryIntExpression b4;
BinaryIntExpression b5;
......@@ -34,13 +34,13 @@ public class TestLambdas {
};
//call the main function using the instances as a parameter.
result = binaryOperation(1,2,b0);
result = binaryIntegerOperation(1,2,b0);
System.out.println(result);
result = binaryOperation(1,2,b1);
result = binaryIntegerOperation(1,2,b1);
System.out.println(result);
result = binaryOperation(1,2,b2);
result = binaryIntegerOperation(1,2,b2);
System.out.println(result);
result = binaryOperation(1,2,b3);
......@@ -61,4 +61,9 @@ public class TestLambdas {
return exp.eval(a1, a2);
}
public static int binaryIntegerOperation(int a1, int a2, GeneralCompare<Integer> exp) {
//call the eval() function in the given BinaryIntExpression.
return exp.eval(a1, a2);
}
}
package search;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.json.simple.parser.ParseException;
import utils.Stopwatch;
import data.BioTree;
import data.DataStore;
import data.Date;
import data.Record;
import data.WormsAPI;
import sort.Bound;
import sort.GeneralRange;
import sort.RangeHelper;
/**
* Provides functionality for range searching the Record database.
* @author Christopher W. Schankula
*
*/
public class BasicSearch {
public static void init() {
System.out.println("Welcome!");
while(true) {
System.out.println("Main Menu");
System.out.println("Available commands:");
System.out.println("\ttree [taxonId / scientific name]");
System.out.println("\trecords (taxonId / scientific name) [-t start end]");
System.out.print("> ");
Pattern pat = Pattern.compile("([a-zA-Z]+)[ ]?([0-9a-zA-Z ]+[0-9a-zA-Z])?[ ]?[-]?([a-zA-Z])?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?([A-Za-z0-9]+)?[ ]?");
Scanner s = new Scanner(System.in);
String line = s.nextLine();
Matcher matcher = pat.matcher(line);
if (!matcher.find()) continue;
//tree
//tree taxonId
//tree scientific name
//records taxonId
//records scientific name
String command = matcher.group(1);
if (command.equals("records"))
rangeSearch(matcher);
else if (command.equals("tree"))
printTree(matcher);
}
}
private static void rangeSearch(Matcher matcher) {
Integer start = null;
Integer end = null;
/**
* Returns all records matching any of the children of the given TaxonID and in the
* date range given
* @param taxonId The TaxonID for which to search
* @param yearLo The lower bound on the year range
* @param yearHi The upper bound on the year range
* @return
*/
public static BasicSearchResult range(Integer taxonId, Integer yearLo, Integer yearHi){
GeneralRange<Record> a0 = RangeHelper.date(Bound.ANY);
if (matcher.group(3) != null)
if (matcher.group(3).equals("t")) {
if (matcher.group(4) != null)
start = Integer.parseInt(matcher.group(4));
if (matcher.group(5) != null)
end = Integer.parseInt(matcher.group(5));
Date lower = new Date(start,01,01);
Date upper = new Date(end+1,01,01);
a0 = RangeHelper.date(Bound.LOWHIGH, lower, upper);
}
Integer taxonId = null;
try {
taxonId = Integer.parseInt(matcher.group(2));
} catch (NumberFormatException e) {
if (taxonId == null) {
try {
taxonId = WormsAPI.nameToRecordID(matcher.group(2));
} catch (IOException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if ((yearLo != null) && (yearHi != null)) {
Date lower = new Date(yearLo,01,01);
Date upper = new Date(yearHi+1,01,01);
a0 = RangeHelper.date(Bound.LOWHIGH, lower, upper);
}
GeneralRange<Record> a2 = r -> 0;
......@@ -86,9 +39,9 @@ public class BasicSearch {
GeneralRange<Record> a1;
Stopwatch sw = new Stopwatch();
Iterable<Integer> searches = BioTree.getNonEmptyChildren(taxonId);
Stopwatch sw = new Stopwatch();
ArrayList<Record> results = new ArrayList<Record>();
for (Integer txId: searches) {
a1 = RangeHelper.taxonID(Bound.EQUALS, txId);
......@@ -98,53 +51,8 @@ public class BasicSearch {
results.addAll((Collection<? extends Record>) DataStore.records.rangeSearch(axes));
}
double time = sw.elapsedTime();
double elapsed = sw.elapsedTime();
System.out.println("Found " + ((ArrayList<Record>) results).size() + " records in " + elapsed + " seconds.");
while(true) {
System.out.println("Available commands: list, histogram, sum, exit");
System.out.print("> ");
Scanner s = new Scanner(System.in);
String command = s.nextLine();
if (command.equals("list"))
printRecords(results);
else if (command.equals("histogram")) {
Histogram.printHistogram(Histogram.histogram(results));
} else if (command.equals("exit"))
return;
else if (command.equals("sum")) {
int sum = Histogram.sum(results);
System.out.println(sum);
}
}
}
private static void printRecords(Iterable<Record> results) {
String format = "|%1$-45s|%2$-15s|%3$-15s|%4$-15s|%5$-15s|%6$-15s\n";
System.out.format(format, "Scientific Name", "IndividualCount", "Latitude", "Longitude","Year","Month","Day");
for (Record r: results) {
System.out.println(r);
}
}
private static void printTree(Matcher matcher) {
Integer taxonId;
String name;
if (matcher.group(2) == null)
BioTree.printTree();
else {
name = matcher.group(2);
try {
taxonId = Integer.parseInt(name);
BioTree.printTree(taxonId);
} catch (Exception e) {
BioTree.printTree(name);
}
}
System.out.println();
return new BasicSearchResult(results, time);
}
}
package search;
import java.util.ArrayList;
import data.Record;
public class BasicSearchResult {
private ArrayList<Record> results;
private final double time;
private BST<Integer, Integer> histogram;
private Integer sum;
public BasicSearchResult(ArrayList<Record> results, double time) {
this.results = results;
this.time = time;
}
public Iterable<Record> results() {
return (Iterable<Record>) results;
}
public int n() {
return results.size();
}
public double time() {
return time;
}
public BST<Integer, Integer> histogram() {
if (this.histogram == null)
this.histogram = Histogram.histogram(results());
return histogram;
}
public int sum() {
if (sum == null) {
sum = 0;
for (Record r: results())
sum += r.getCount();
}
return this.sum;
}
}
package search;
public class CC {
private boolean[] marked;
private int[] id;
private int count;
public static void main(String[] args) {
Graph g = new Graph(5);
g.addEdge(4, 3);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 2);
CC component = new CC(g);
System.out.println(component.connected(0, 2));
}
public CC(Graph G){
marked = new boolean[G.V()];
id = new int[G.V()];
for(int s = 0; s < G.V(); s++){
if(!marked[s]){
dfs(G,s);
count++;
}
}
}
private void dfs(Graph G, int v){
marked[v] = true;
id[v] = count;
for(int w : G.adj(v)){
if(!marked[w])
dfs(G, w);
}
}
public boolean connected(int v, int w){
return id[v] == id[w];
}
public int id(int v){
return id[v];
}
public int count(){
return count;
}
}
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