aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java
blob: 1e5fbd44adf016a865dd0d0371bfdf2a94a53dab (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package uk.ac.ox.cs.pagoda.query;

import com.google.gson.*;
import org.semanticweb.HermiT.model.Constant;
import org.semanticweb.HermiT.model.Individual;
import org.semanticweb.HermiT.model.Term;
import org.semanticweb.HermiT.model.Variable;
import uk.ac.ox.cs.JRDFox.JRDFStoreException;
import uk.ac.ox.cs.JRDFox.model.Datatype;
import uk.ac.ox.cs.JRDFox.model.GroundTerm;
import uk.ac.ox.cs.JRDFox.model.Literal;
import uk.ac.ox.cs.JRDFox.store.TupleIterator;
import uk.ac.ox.cs.pagoda.util.Namespace;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class AnswerTuple {
	
	public static final String SEPARATOR = "\t";
	String m_str = null;
	GroundTerm[] m_tuple;
	
	public AnswerTuple(TupleIterator iter, int arity) {
		m_tuple = new GroundTerm[arity];
		try {
			for (int i = 0; i < arity; ++i)
				m_tuple[i] = iter.getGroundTerm(i);
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		}
	}
	
	public AnswerTuple(GroundTerm[] terms) {
		m_tuple = terms;
	}

//	private AnswerTuple(String m_str) {
//		this.m_str = m_str;
//	}

	private AnswerTuple(AnswerTuple sup, int arity) {
		m_tuple = new GroundTerm[arity];
		for(int i = 0; i < arity; ++i) m_tuple[i] = sup.m_tuple[i];
	}

	/**
	 * It returns the first argument if its arity equals length, a new AnswerTuple otherwise.
	 */
	public static AnswerTuple create(AnswerTuple extendedTuple, int length) {
		if(length == extendedTuple.getArity()) return extendedTuple;
		else return new AnswerTuple(extendedTuple, length);
	}
	
	public int getArity() {
		return m_tuple.length;
	}

	public int hashCode() {
//		return toString().hashCode();
		int code = 0;
		for (int i = 0; i < m_tuple.length; ++i)
			code = code * 1997 + m_tuple[i].hashCode();
		return code;
	}

	public boolean equals(Object obj) {
		if (!(obj instanceof AnswerTuple)) return false;
		AnswerTuple that = (AnswerTuple) obj;
		if (m_tuple.length != that.m_tuple.length) return false;
		for (int i = 0; i < m_tuple.length; ++i)
			if (!m_tuple[i].equals(that.m_tuple[i]))
				return false;
		return true;
//		return toString().equals(obj.toString());
	}
	
	public String toString() {
		if(m_str != null) return m_str;
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < m_tuple.length; ++i) {
			if (sb.length() != 0) sb.append(SEPARATOR);
			if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual)
				sb.append("<").append(((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI()).append(">");
			else if (m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.BlankNode) {
				sb.append(m_tuple[i].toString());
			}
			else {
				Literal l = (Literal) m_tuple[i];
				sb.append('"').append(l.getLexicalForm()).append("\"");
				if (!l.getDatatype().equals(Datatype.XSD_STRING) && !l.getDatatype().equals(Datatype.RDF_PLAIN_LITERAL))
					sb.append("^^<").append(l.getDatatype().getIRI()).append(">");
			}
		}
		return m_str = sb.toString();
	}

	public GroundTerm getGroundTerm(int i) {
		return m_tuple[i];
	}
	
	public Map<Variable, Term> getAssignment(String[] vars) {
		Map<Variable, Term> map = new HashMap<Variable, Term>();
		int index = 0;
		Term t;
		for (String var: vars) {
			if(m_tuple[index] instanceof uk.ac.ox.cs.JRDFox.model.Individual)
				t = Individual.create((((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[index]).getIRI()));
			else {
				uk.ac.ox.cs.JRDFox.model.Literal l = (uk.ac.ox.cs.JRDFox.model.Literal) m_tuple[index];
				t = Constant.create(l.getLexicalForm(), l.getDatatype().getIRI());
			}
			map.put(Variable.create(var), t);
			++index;
		}
		return map;
	}

	public boolean hasAuxPredicate() {
		String iri;
		for (int i = 0; i < m_tuple.length; ++i)
			if ((m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual)) {
				iri = ((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI();
				if(iri.startsWith(Namespace.PAGODA_AUX) || iri.contains("_AUX") || iri.contains("_neg") || iri.contains("internal:def"))
					return true;
			}
		return false;
	}

	public boolean hasAnonymousIndividual() {
		String iri;
		for(int i = 0; i < m_tuple.length; ++i)
			if((m_tuple[i] instanceof uk.ac.ox.cs.JRDFox.model.Individual)) {
				iri = ((uk.ac.ox.cs.JRDFox.model.Individual) m_tuple[i]).getIRI();
				if(iri.startsWith(Namespace.PAGODA_ANONY) || iri.startsWith(Namespace.KARMA_ANONY))
					return true;
			}
		return false;
	}

	public static class AnswerTupleSerializer implements JsonSerializer<AnswerTuple> {

		public JsonElement serialize(AnswerTuple src, Type typeOfSrc, JsonSerializationContext context) {
			return new JsonPrimitive(src.toString());
		}

	}

	public static class AnswerTupleDeserializer implements JsonDeserializer<AnswerTuple> {
		public AnswerTuple deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
				throws JsonParseException {
			String tuplesString = json.getAsJsonPrimitive().getAsString();
//			StringTokenizer tokenizer = new StringTokenizer(tuplesString, SEPARATOR);
			StringTokenizer tokenizer = new StringTokenizer(tuplesString);
			int tokensCount = tokenizer.countTokens();
			GroundTerm[] terms = new GroundTerm[tokensCount];

			// TODO test parsing
			for(int i = 0; i < tokensCount; i++) {
				String token = tokenizer.nextToken();
				if (token.charAt(0) == '<') {
					terms[i] = uk.ac.ox.cs.JRDFox.model.Individual.create(token.substring(1,token.length()-1));
				}
				else if (token.charAt(0) == '"') {
					Datatype datatype;
					String lexicalForm;
					if(token.contains("^^")) {
						String[] lexicalFormAndType = token.split("^^");
						lexicalForm = lexicalFormAndType[0];
						datatype = Datatype.value(lexicalFormAndType[1]);
					}
					else {
						lexicalForm = token.substring(1, token.length() - 1);
						// TODO check
//						datatype = token.contains("@") ? Datatype.RDF_PLAIN_LITERAL : Datatype.XSD_STRING;
						datatype = Datatype.XSD_STRING;
					}
					terms[i] = uk.ac.ox.cs.JRDFox.model.Literal.create(lexicalForm, datatype);
				}
				else {
					terms[i] = uk.ac.ox.cs.JRDFox.model.BlankNode.create(token);
				}
			}

			return new AnswerTuple(terms);
		}
	}

}