2010-01-24 22:09:50 +01:00
|
|
|
package com.metaweb.gridlock.model;
|
|
|
|
|
2010-01-25 23:51:25 +01:00
|
|
|
import java.io.Serializable;
|
2010-01-24 22:09:50 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2010-01-27 02:48:42 +01:00
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
2010-02-01 20:16:09 +01:00
|
|
|
import org.json.JSONWriter;
|
2010-01-24 22:09:50 +01:00
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
import com.metaweb.gridlock.Jsonizable;
|
2010-02-01 00:15:50 +01:00
|
|
|
import com.metaweb.gridlock.expr.HasFields;
|
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
public class Recon implements Serializable, HasFields, Jsonizable {
|
2010-01-25 23:51:25 +01:00
|
|
|
private static final long serialVersionUID = 8906257833709315762L;
|
|
|
|
|
2010-01-24 22:09:50 +01:00
|
|
|
static public enum Judgment {
|
|
|
|
None,
|
|
|
|
Approve,
|
|
|
|
New
|
|
|
|
}
|
|
|
|
|
|
|
|
public Map<String, Object> features = new HashMap<String, Object>();
|
|
|
|
public List<ReconCandidate> candidates = new LinkedList<ReconCandidate>();
|
2010-01-27 02:48:42 +01:00
|
|
|
public Judgment judgment = Judgment.None;
|
2010-02-01 00:15:50 +01:00
|
|
|
public ReconCandidate match = null;
|
2010-01-27 02:48:42 +01:00
|
|
|
|
2010-02-02 21:50:35 +01:00
|
|
|
public Recon dup() {
|
|
|
|
Recon r = new Recon();
|
|
|
|
r.features.putAll(new HashMap<String, Object>(features));
|
|
|
|
r.candidates.addAll(candidates);
|
|
|
|
r.judgment = judgment;
|
|
|
|
r.match = match;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2010-02-01 00:15:50 +01:00
|
|
|
@Override
|
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
if ("best".equals(name)) {
|
|
|
|
return candidates.size() > 0 ? candidates.get(0) : null;
|
|
|
|
} else if ("judgment".equals(name) || "judgement".equals(name)) {
|
|
|
|
return judgmentToString();
|
|
|
|
} else if ("approved".equals(name)) {
|
|
|
|
return judgment == Judgment.Approve;
|
|
|
|
} else if ("new".equals(name)) {
|
|
|
|
return judgment == Judgment.New;
|
|
|
|
} else if ("match".equals(name)) {
|
|
|
|
return match;
|
2010-02-01 21:57:52 +01:00
|
|
|
} else if ("features".equals(name)) {
|
|
|
|
return new HasFields() {
|
|
|
|
@Override
|
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
return features.get(name);
|
|
|
|
}
|
|
|
|
};
|
2010-02-01 00:15:50 +01:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String judgmentToString() {
|
|
|
|
if (judgment == Judgment.Approve) {
|
|
|
|
return "approve";
|
2010-01-27 02:48:42 +01:00
|
|
|
} else if (judgment == Judgment.New) {
|
2010-02-01 00:15:50 +01:00
|
|
|
return "new";
|
|
|
|
} else {
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Features implements HasFields {
|
|
|
|
@Override
|
|
|
|
public Object getField(String name, Properties bindings) {
|
|
|
|
return features.get(name);
|
2010-01-27 02:48:42 +01:00
|
|
|
}
|
|
|
|
}
|
2010-02-01 20:16:09 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("j");
|
|
|
|
writer.value(judgmentToString());
|
2010-02-01 21:57:52 +01:00
|
|
|
|
|
|
|
if (match != null) {
|
|
|
|
writer.key("m");
|
|
|
|
match.write(writer, options);
|
2010-02-02 21:50:35 +01:00
|
|
|
} else {
|
|
|
|
writer.key("c"); writer.array();
|
|
|
|
for (ReconCandidate c : candidates) {
|
|
|
|
c.write(writer, options);
|
|
|
|
}
|
|
|
|
writer.endArray();
|
2010-02-01 21:57:52 +01:00
|
|
|
}
|
|
|
|
|
2010-02-01 20:16:09 +01:00
|
|
|
writer.endObject();
|
|
|
|
}
|
2010-01-24 22:09:50 +01:00
|
|
|
}
|