-
Notifications
You must be signed in to change notification settings - Fork 14
/
QueryResultGenerator.java
185 lines (154 loc) · 5.82 KB
/
QueryResultGenerator.java
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
/**
* Copyright (C) 2016 Czech Technical University in Prague
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.ontodriver.owlapi.query;
import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
import cz.cvut.kbss.owl2query.model.GroundTerm;
import cz.cvut.kbss.owl2query.model.QueryResult;
import cz.cvut.kbss.owl2query.model.ResultBinding;
import cz.cvut.kbss.owl2query.model.Variable;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLObject;
import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class QueryResultGenerator {
private final OWLDataFactory dataFactory = new OWLDataFactoryImpl();
QueryResult<OWLObject> generate(List<String> variableNames, List<List<Object>> values) {
final List<Variable<OWLObject>> vars = variableNames.stream().map(name -> {
final Variable<OWLObject> var = mock(Variable.class);
when(var.getName()).thenReturn(name);
return var;
}).collect(Collectors.toList());
final QueryResult<OWLObject> result = new QueryResultImpl(vars);
initData(result, values);
return result;
}
private void initData(QueryResult<OWLObject> result, List<List<Object>> values) {
if (values.isEmpty()) {
return;
}
assert result.getResultVars().size() == values.get(0).size();
for (List<Object> row : values) {
final ResultBinding<OWLObject> binding = new ResultBindingImpl();
for (int i = 0; i < row.size(); i++) {
final Object value = row.get(i);
if (value == null) {
binding.put(result.getResultVars().get(i), null);
continue;
}
final GroundTerm<OWLObject> gt = mock(GroundTerm.class);
final OWLObject owlValue;
if (value instanceof URI) {
owlValue = dataFactory.getOWLNamedIndividual(IRI.create(value.toString()));
} else {
owlValue = OwlapiUtils.createOWLLiteralFromValue(value, dataFactory, "en");
}
when(gt.getWrappedObject()).thenReturn(owlValue);
binding.put(result.getResultVars().get(i), gt);
}
result.add(binding);
}
}
private static class QueryResultImpl implements QueryResult<OWLObject> {
private final List<ResultBinding<OWLObject>> bindings = new ArrayList<>();
private final List<Variable<OWLObject>> variables;
private QueryResultImpl(List<Variable<OWLObject>> variables) {
this.variables = variables;
}
@Override
public boolean add(ResultBinding<OWLObject> resultBinding) {
return bindings.add(resultBinding);
}
@Override
public List<Variable<OWLObject>> getResultVars() {
return Collections.unmodifiableList(variables);
}
@Override
public boolean isDistinct() {
return false;
}
@Override
public boolean isEmpty() {
return bindings.isEmpty();
}
@Override
public int size() {
return bindings.size();
}
@Override
public Iterator<ResultBinding<OWLObject>> iterator() {
return bindings.iterator();
}
}
private static class ResultBindingImpl implements ResultBinding<OWLObject> {
private final Map<Variable<OWLObject>, GroundTerm<OWLObject>> map = new HashMap<>();
@Override
public ResultBinding<OWLObject> clone() {
return null;
}
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public GroundTerm<OWLObject> get(Object key) {
return map.get(key);
}
@Override
public GroundTerm<OWLObject> put(Variable<OWLObject> key, GroundTerm<OWLObject> value) {
return map.put(key, value);
}
@Override
public GroundTerm<OWLObject> remove(Object key) {
return map.remove(key);
}
@Override
public void putAll(Map<? extends Variable<OWLObject>, ? extends GroundTerm<OWLObject>> m) {
map.putAll(m);
}
@Override
public void clear() {
map.clear();
}
@Override
public Set<Variable<OWLObject>> keySet() {
return map.keySet();
}
@Override
public Collection<GroundTerm<OWLObject>> values() {
return map.values();
}
@Override
public Set<Entry<Variable<OWLObject>, GroundTerm<OWLObject>>> entrySet() {
return map.entrySet();
}
}
}