aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java
diff options
context:
space:
mode:
authorRncLsn <rnc.lsn@gmail.com>2015-05-09 14:26:28 +0100
committerRncLsn <rnc.lsn@gmail.com>2015-05-09 14:26:28 +0100
commit870eb35e38f746ed73f233070dcf3bdaf4687e02 (patch)
tree41ef0c1e18057aedc9f0f94d242cf6450d22025f /src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java
parent5be5fd3daa0d50980fb3791e904e035cdbca254f (diff)
downloadACQuA-870eb35e38f746ed73f233070dcf3bdaf4687e02.tar.gz
ACQuA-870eb35e38f746ed73f233070dcf3bdaf4687e02.zip
Adding (not completed) log of partial answers in Json.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java65
1 files changed, 49 insertions, 16 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java
index 183cbe1..cd86282 100644
--- a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java
+++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java
@@ -15,6 +15,9 @@ import uk.ac.ox.cs.pagoda.util.Namespace;
15import java.lang.reflect.Type; 15import java.lang.reflect.Type;
16import java.util.HashMap; 16import java.util.HashMap;
17import java.util.Map; 17import java.util.Map;
18import java.util.StringTokenizer;
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
18 21
19public class AnswerTuple { 22public class AnswerTuple {
20 23
@@ -42,31 +45,31 @@ public class AnswerTuple {
42 for (int i = 0; i < arity; ++i) m_tuple[i] = sup.m_tuple[i]; 45 for (int i = 0; i < arity; ++i) m_tuple[i] = sup.m_tuple[i];
43 } 46 }
44 47
45 private AnswerTuple(String m_str) { 48// private AnswerTuple(String m_str) {
46 this.m_str = m_str; 49// this.m_str = m_str;
47 } 50// }
48 51
49 public int getArity() { 52 public int getArity() {
50 return m_tuple.length; 53 return m_tuple.length;
51 } 54 }
52 55
53 public int hashCode() { 56 public int hashCode() {
54 return toString().hashCode(); 57// return toString().hashCode();
55// int code = 0; 58 int code = 0;
56// for (int i = 0; i < m_tuple.length; ++i) 59 for (int i = 0; i < m_tuple.length; ++i)
57// code = code * 1997 + m_tuple[i].hashCode(); 60 code = code * 1997 + m_tuple[i].hashCode();
58// return code; 61 return code;
59 } 62 }
60 63
61 public boolean equals(Object obj) { 64 public boolean equals(Object obj) {
62 if (!(obj instanceof AnswerTuple)) return false; 65 if (!(obj instanceof AnswerTuple)) return false;
63 AnswerTuple that = (AnswerTuple) obj; 66 AnswerTuple that = (AnswerTuple) obj;
64 if (m_tuple.length != that.m_tuple.length) return false; 67 if (m_tuple.length != that.m_tuple.length) return false;
65 for (int i = 0; i < m_tuple.length; ++i) 68 for (int i = 0; i < m_tuple.length; ++i)
66 if (!m_tuple[i].equals(that.m_tuple[i])) 69 if (!m_tuple[i].equals(that.m_tuple[i]))
67 return false; 70 return false;
68 return true; 71 return true;
69// return toString().equals(obj.toString()); 72// return toString().equals(obj.toString());
70 } 73 }
71 74
72 public String toString() { 75 public String toString() {
@@ -77,7 +80,7 @@ public class AnswerTuple {
77 if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual) 80 if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual)
78 sb.append("<").append(((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI()).append(">"); 81 sb.append("<").append(((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI()).append(">");
79 else if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.BlankNode) { 82 else if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.BlankNode) {
80 sb.append(((uk.ac.ox.cs.JRDFox.model.BlankNode) m_tuple[i]).toString()); 83 sb.append(m_tuple[i].toString());
81 } 84 }
82 else { 85 else {
83 Literal l = (Literal) m_tuple[i]; 86 Literal l = (Literal) m_tuple[i];
@@ -146,10 +149,40 @@ public class AnswerTuple {
146 149
147 } 150 }
148 151
149 public class AnswerTupleDeserializer implements JsonDeserializer<AnswerTuple> { 152 static final Pattern owlLiteralRegex = Pattern.compile("^\"(?<lexicalForm>[^@]+(@(?<langTag>.+))?)\"(^^<(?<dataType>.+)>)?$");
153
154 public static class AnswerTupleDeserializer implements JsonDeserializer<AnswerTuple> {
150 public AnswerTuple deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) 155 public AnswerTuple deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
151 throws JsonParseException { 156 throws JsonParseException {
152 return new AnswerTuple(json.getAsJsonPrimitive().getAsString()); 157 String tuplesString = json.getAsJsonPrimitive().getAsString();
158 StringTokenizer tokenizer = new StringTokenizer(SEPARATOR);
159 GroundTerm[] terms = new GroundTerm[tokenizer.countTokens()];
160
161 // TODO test parsing
162 for (int i = 0; i < tokenizer.countTokens(); i++) {
163 String token = tokenizer.nextToken();
164 if (token.charAt(0) == '<') {
165 terms[i] = uk.ac.ox.cs.JRDFox.model.Individual.create(token.substring(1,token.length()-1));
166 }
167 else if (token.charAt(0) == '"') {
168 Matcher matcher = owlLiteralRegex.matcher(token);
169 if(matcher.matches()) {
170 String lexicalForm = matcher.group("lexicalForm");
171 String dataTypeIRI = matcher.group("dataType");
172 Datatype dataType;
173 if (dataTypeIRI.isEmpty()) dataType = Datatype.RDF_PLAIN_LITERAL;
174 else dataType = uk.ac.ox.cs.JRDFox.model.Datatype.value(dataTypeIRI);
175 terms[i] = uk.ac.ox.cs.JRDFox.model.Literal.create(lexicalForm, dataType);
176 }
177 else {
178 throw new IllegalArgumentException("The given json does not represent a valid AnswerTuple");
179 }
180 }
181 else {
182 terms[i] = uk.ac.ox.cs.JRDFox.model.BlankNode.create(token);
183 }
184 }
185 return new AnswerTuple(terms);
153 } 186 }
154 } 187 }
155 188