From f868953e6dbf6a2a969478c7d3f9a70cb3273563 Mon Sep 17 00:00:00 2001 From: Schankula Christopher <schankuc@mcmaster.ca> Date: Mon, 5 Mar 2018 13:04:32 -0500 Subject: [PATCH] only call classification API if needed --- src/biotree/BioTree.java | 51 ++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/biotree/BioTree.java b/src/biotree/BioTree.java index 98b6683..acdd46f 100644 --- a/src/biotree/BioTree.java +++ b/src/biotree/BioTree.java @@ -1,4 +1,4 @@ -package biotree; + package biotree; import java.io.IOException; @@ -13,6 +13,10 @@ public class BioTree { init(); processRecord(125125); processRecord(125125); + processRecord(125125); + processRecord(125125); + processRecord(125125); + processRecord(125125); processRecord(125123); processRecord(125122); processRecord(125392); @@ -86,33 +90,34 @@ public class BioTree { */ private static void processTaxonId(int taxonId) { TaxonNode[] newNodes = null; - try { - newNodes = WormsAPI.idToClassification(taxonId); - } catch (IOException | ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - TaxonNode tx = nodes.get(newNodes[newNodes.length - 1].getTaxonId()); + TaxonNode tx = nodes.get(taxonId); if (tx != null) { tx.incCount(); } else { + try { + System.out.println("API Call"); + newNodes = WormsAPI.idToClassification(taxonId); + } catch (IOException | ParseException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } newNodes[newNodes.length - 1].incCount(); - } - for (int i = newNodes.length - 1; i >= 0; i--) { - tx = newNodes[i]; - TaxonNode result = nodes.get(tx.getTaxonId()); - TaxonNode parent = null; - if (i > 0) { - parent = nodes.get(newNodes[i - 1].getTaxonId()); - if (parent == null) parent = newNodes[i - 1]; + + for (int i = newNodes.length - 1; i >= 0; i--) { + tx = newNodes[i]; + TaxonNode result = nodes.get(tx.getTaxonId()); + TaxonNode parent = null; + if (i > 0) { + parent = nodes.get(newNodes[i - 1].getTaxonId()); + if (parent == null) parent = newNodes[i - 1]; + } + if (result == null) { //if node is not found, add it + nodes.put(tx.getTaxonId(), tx); + tx.setParent(parent); + if (parent != null) parent.addChild(tx); + } else + break; //stop loop if this node already exists in the tree } - if (result == null) { //if node is not found, add it - nodes.put(tx.getTaxonId(), tx); - tx.setParent(parent); - if (parent != null) parent.addChild(tx); - } else - break; //stop loop if this node already exists in the tree - } } -- GitLab