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 package io.github.prolobjectlink.prolog;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Map;
33
34
35
36
37
38
39
40 public abstract class AbstractQuery extends AbstractIterator<Collection<PrologTerm>> implements PrologQuery {
41
42
43 protected final AbstractEngine engine;
44
45 public AbstractQuery(AbstractEngine engine) {
46 this.engine = engine;
47 }
48
49 public final PrologEngine getEngine() {
50 return engine;
51 }
52
53 public final PrologProvider getProvider() {
54 return engine.getProvider();
55 }
56
57 protected final <K extends PrologTerm> K toTerm(Object o, Class<K> from) {
58 return engine.toTerm(o, from);
59 }
60
61 protected final <K extends PrologTerm, V extends Object> Map<String, PrologTerm>[] toTermMapArray(
62 Map<String, V>[] map, Class<K> from) {
63 return engine.toTermMapArray(map, from);
64 }
65
66 protected final <K> K fromTerm(PrologTerm term, Class<K> to) {
67 return engine.fromTerm(term, to);
68 }
69
70 protected final boolean contains(List<Map<String, PrologTerm>> maps, Map<String, PrologTerm> map) {
71 for (Map<String, PrologTerm> m : maps) {
72 if (m.equals(map)) {
73 return true;
74 }
75 }
76 return false;
77 }
78
79 protected final boolean contains(List<PrologTerm[]> arrays, PrologTerm[] array) {
80 for (PrologTerm[] a : arrays) {
81 if (Arrays.equals(a, array)) {
82 return true;
83 }
84 }
85 return false;
86 }
87
88 protected final PrologLogger getLogger() {
89 return getProvider().getLogger();
90 }
91
92 public final Iterator<Collection<PrologTerm>> iterator() {
93 return new PrologQueryIterator(this);
94 }
95
96 public final boolean hasNext() {
97 return hasMoreSolutions();
98 }
99
100 public final Collection<PrologTerm> next() {
101
102
103 return nextVariablesSolution().values();
104 }
105
106 @Override
107 public final void remove() {
108
109 nextSolution();
110 }
111
112 public final Map<String, PrologTerm> one() {
113 return oneVariablesSolution();
114 }
115
116 public final List<Map<String, PrologTerm>> nths(int n) {
117 Map<String, PrologTerm>[] maps = nVariablesSolutions(n);
118 return Arrays.asList(maps);
119 }
120
121 public final Map<String, PrologTerm> more() {
122 return nextVariablesSolution();
123 }
124
125 public final List<Object> oneResult() {
126 PrologTerm[] terms = oneSolution();
127 return getProvider().getJavaConverter().toObjectList(terms);
128 }
129
130 public final List<Object> nextResult() {
131 PrologTerm[] terms = nextSolution();
132 return getProvider().getJavaConverter().toObjectList(terms);
133 }
134
135 public final List<List<Object>> nResult(int n) {
136 PrologTerm[][] terms = allSolutions();
137 return getProvider().getJavaConverter().toObjectLists(terms);
138 }
139
140 public final List<List<Object>> allResults() {
141 PrologTerm[][] terms = allSolutions();
142 return getProvider().getJavaConverter().toObjectLists(terms);
143 }
144
145 public final Map<String, Object> oneVariablesResult() {
146 Map<String, PrologTerm> map = oneVariablesSolution();
147 return getProvider().getJavaConverter().toObjectMap(map);
148 }
149
150 @Override
151 public final Map<String, Object> nextVariablesResult() {
152 Map<String, PrologTerm> map = nextVariablesSolution();
153 return getProvider().getJavaConverter().toObjectMap(map);
154 }
155
156 @Override
157 public final List<Map<String, Object>> nVariablesResults(int n) {
158 Map<String, PrologTerm>[] maps = nVariablesSolutions(n);
159 return getProvider().getJavaConverter().toObjectMaps(maps);
160 }
161
162 public final List<Map<String, Object>> allVariablesResults() {
163 Map<String, PrologTerm>[] maps = allVariablesSolutions();
164 return getProvider().getJavaConverter().toObjectMaps(maps);
165 }
166
167 @Override
168 public int hashCode() {
169 final int prime = 31;
170 int result = 1;
171 result = prime * result + ((engine == null) ? 0 : engine.hashCode());
172 return result;
173 }
174
175 @Override
176 public boolean equals(Object obj) {
177 if (this == obj)
178 return true;
179 if (obj == null)
180 return false;
181 if (getClass() != obj.getClass())
182 return false;
183 AbstractQuery other = (AbstractQuery) obj;
184 if (engine == null) {
185 if (other.engine != null)
186 return false;
187 } else if (!engine.equals(other.engine)) {
188 return false;
189 }
190 return true;
191 }
192
193 }