View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
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   * Partial implementation of {@link PrologQuery} interface.
36   * 
37   * @author Jose Zalacain
38   * @since 1.0
39   */
40  public abstract class AbstractQuery extends AbstractIterator<Collection<PrologTerm>> implements PrologQuery {
41  
42  	// engine for execute queries
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 		// don't check has next
102 		// don't raise NoSuchElementException
103 		return nextVariablesSolution().values();
104 	}
105 
106 	@Override
107 	public final void remove() {
108 		// skip invoking next
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 }