diff --git a/src/main/java/com/metaweb/gridworks/model/Recon.java b/src/main/java/com/metaweb/gridworks/model/Recon.java index aecbf21e8..9296c688d 100644 --- a/src/main/java/com/metaweb/gridworks/model/Recon.java +++ b/src/main/java/com/metaweb/gridworks/model/Recon.java @@ -1,8 +1,8 @@ package com.metaweb.gridworks.model; import java.io.Serializable; +import java.util.ArrayList; import java.util.HashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; @@ -12,7 +12,7 @@ import org.json.JSONWriter; import com.metaweb.gridworks.Jsonizable; import com.metaweb.gridworks.expr.HasFields; - + public class Recon implements Serializable, HasFields, Jsonizable { private static final long serialVersionUID = 8906257833709315762L; @@ -60,7 +60,7 @@ public class Recon implements Serializable, HasFields, Jsonizable { final public long id; public Object[] features = new Object[Feature_max]; - public List candidates = new LinkedList(); + public List candidates; public Judgment judgment = Judgment.None; public ReconCandidate match = null; @@ -79,6 +79,20 @@ public class Recon implements Serializable, HasFields, Jsonizable { return r; } + public void addCandidate(ReconCandidate candidate) { + if (candidates == null) { + candidates = new ArrayList(3); + } + candidates.add(candidate); + } + + public ReconCandidate getBestCandidate() { + if (candidates != null && candidates.size() > 0) { + return candidates.get(0); + } + return null; + } + public Object getFeature(int feature) { return feature < features.length ? features[feature] : null; } @@ -104,7 +118,7 @@ public class Recon implements Serializable, HasFields, Jsonizable { public Object getField(String name, Properties bindings) { if ("best".equals(name)) { - return candidates.size() > 0 ? candidates.get(0) : null; + return candidates != null && candidates.size() > 0 ? candidates.get(0) : null; } else if ("judgment".equals(name) || "judgement".equals(name)) { return judgmentToString(); } else if ("matched".equals(name)) { @@ -142,8 +156,10 @@ public class Recon implements Serializable, HasFields, Jsonizable { match.write(writer, options); } else { writer.key("c"); writer.array(); - for (ReconCandidate c : candidates) { - c.write(writer, options); + if (candidates != null) { + for (ReconCandidate c : candidates) { + c.write(writer, options); + } } writer.endArray(); } diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java index 2c78bd0c4..71cdb3740 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconMatchBestCandidatesOperation.java @@ -12,6 +12,7 @@ import com.metaweb.gridworks.model.AbstractOperation; import com.metaweb.gridworks.model.Cell; import com.metaweb.gridworks.model.Column; import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.model.ReconCandidate; import com.metaweb.gridworks.model.Row; import com.metaweb.gridworks.model.Recon.Judgment; import com.metaweb.gridworks.model.changes.CellChange; @@ -71,16 +72,19 @@ public class ReconMatchBestCandidatesOperation extends EngineDependentMassCellOp public boolean visit(Project project, int rowIndex, Row row, boolean contextual) { if (cellIndex < row.cells.size()) { Cell cell = row.cells.get(cellIndex); - if (cell.recon != null && cell.recon.candidates.size() > 0) { - Cell newCell = new Cell( - cell.value, - cell.recon.dup() - ); - newCell.recon.match = newCell.recon.candidates.get(0); - newCell.recon.judgment = Judgment.Matched; - - CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); - cellChanges.add(cellChange); + if (cell.recon != null) { + ReconCandidate candidate = cell.recon.getBestCandidate(); + if (candidate != null) { + Cell newCell = new Cell( + cell.value, + cell.recon.dup() + ); + newCell.recon.match = candidate; + newCell.recon.judgment = Judgment.Matched; + + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); + cellChanges.add(cellChange); + } } } return false; diff --git a/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java b/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java index 287300824..56181962a 100644 --- a/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java +++ b/src/main/java/com/metaweb/gridworks/operations/ReconOperation.java @@ -362,7 +362,8 @@ public class ReconOperation extends EngineDependentOperation { Recon recon = new Recon(); try { int length = results.length(); - for (int i = 0; i < length && recon.candidates.size() < 3; i++) { + int count = 0; + for (int i = 0; i < length && count < 3; i++) { JSONObject result = results.getJSONObject(i); if (!result.has("name")) { continue; @@ -402,7 +403,8 @@ public class ReconOperation extends EngineDependentOperation { } } - recon.candidates.add(candidate); + recon.addCandidate(candidate); + count++; } } catch (JSONException e) { e.printStackTrace();