AbstractQuery.java

  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. import java.util.Arrays;
  28. import java.util.Collection;
  29. import java.util.Iterator;
  30. import java.util.List;
  31. import java.util.Map;

  32. /**
  33.  * Partial implementation of {@link PrologQuery} interface.
  34.  *
  35.  * @author Jose Zalacain
  36.  * @since 1.0
  37.  */
  38. public abstract class AbstractQuery extends AbstractIterator<Collection<PrologTerm>> implements PrologQuery {

  39.     // engine for execute queries
  40.     protected final AbstractEngine engine;

  41.     public AbstractQuery(AbstractEngine engine) {
  42.         this.engine = engine;
  43.     }

  44.     public final PrologEngine getEngine() {
  45.         return engine;
  46.     }

  47.     public final PrologProvider getProvider() {
  48.         return engine.getProvider();
  49.     }

  50.     protected final <K extends PrologTerm> K toTerm(Object o, Class<K> from) {
  51.         return engine.toTerm(o, from);
  52.     }

  53.     protected final <K extends PrologTerm, V extends Object> Map<String, PrologTerm>[] toTermMapArray(
  54.             Map<String, V>[] map, Class<K> from) {
  55.         return engine.toTermMapArray(map, from);
  56.     }

  57.     protected final <K> K fromTerm(PrologTerm term, Class<K> to) {
  58.         return engine.fromTerm(term, to);
  59.     }

  60.     protected final boolean contains(List<Map<String, PrologTerm>> maps, Map<String, PrologTerm> map) {
  61.         for (Map<String, PrologTerm> m : maps) {
  62.             if (m.equals(map)) {
  63.                 return true;
  64.             }
  65.         }
  66.         return false;
  67.     }

  68.     protected final boolean contains(List<PrologTerm[]> arrays, PrologTerm[] array) {
  69.         for (PrologTerm[] a : arrays) {
  70.             if (Arrays.equals(a, array)) {
  71.                 return true;
  72.             }
  73.         }
  74.         return false;
  75.     }

  76.     protected final PrologLogger getLogger() {
  77.         return getProvider().getLogger();
  78.     }

  79.     public final Iterator<Collection<PrologTerm>> iterator() {
  80.         return new PrologQueryIterator(this);
  81.     }

  82.     public final boolean hasNext() {
  83.         return hasMoreSolutions();
  84.     }

  85.     public final Collection<PrologTerm> next() {
  86.         // don't check has next
  87.         // don't raise NoSuchElementException
  88.         return nextVariablesSolution().values();
  89.     }

  90.     @Override
  91.     public final void remove() {
  92.         // skip invoking next
  93.         nextSolution();
  94.     }

  95.     public final Map<String, PrologTerm> one() {
  96.         return oneVariablesSolution();
  97.     }

  98.     public final List<Map<String, PrologTerm>> nths(int n) {
  99.         Map<String, PrologTerm>[] maps = nVariablesSolutions(n);
  100.         return Arrays.asList(maps);
  101.     }

  102.     public final Map<String, PrologTerm> more() {
  103.         return nextVariablesSolution();
  104.     }

  105.     public final List<Object> oneResult() {
  106.         PrologTerm[] terms = oneSolution();
  107.         return getProvider().getJavaConverter().toObjectList(terms);
  108.     }

  109.     public final List<Object> nextResult() {
  110.         PrologTerm[] terms = nextSolution();
  111.         return getProvider().getJavaConverter().toObjectList(terms);
  112.     }

  113.     public final List<List<Object>> nResult(int n) {
  114.         PrologTerm[][] terms = allSolutions();
  115.         return getProvider().getJavaConverter().toObjectLists(terms);
  116.     }

  117.     public final List<List<Object>> allResults() {
  118.         PrologTerm[][] terms = allSolutions();
  119.         return getProvider().getJavaConverter().toObjectLists(terms);
  120.     }

  121.     public final Map<String, Object> oneVariablesResult() {
  122.         Map<String, PrologTerm> map = oneVariablesSolution();
  123.         return getProvider().getJavaConverter().toObjectMap(map);
  124.     }

  125.     @Override
  126.     public final Map<String, Object> nextVariablesResult() {
  127.         Map<String, PrologTerm> map = nextVariablesSolution();
  128.         return getProvider().getJavaConverter().toObjectMap(map);
  129.     }

  130.     @Override
  131.     public final List<Map<String, Object>> nVariablesResults(int n) {
  132.         Map<String, PrologTerm>[] maps = nVariablesSolutions(n);
  133.         return getProvider().getJavaConverter().toObjectMaps(maps);
  134.     }

  135.     public final List<Map<String, Object>> allVariablesResults() {
  136.         Map<String, PrologTerm>[] maps = allVariablesSolutions();
  137.         return getProvider().getJavaConverter().toObjectMaps(maps);
  138.     }

  139.     @Override
  140.     public int hashCode() {
  141.         final int prime = 31;
  142.         int result = 1;
  143.         result = prime * result + ((engine == null) ? 0 : engine.hashCode());
  144.         return result;
  145.     }

  146.     @Override
  147.     public boolean equals(Object obj) {
  148.         if (this == obj)
  149.             return true;
  150.         if (obj == null)
  151.             return false;
  152.         if (getClass() != obj.getClass())
  153.             return false;
  154.         AbstractQuery other = (AbstractQuery) obj;
  155.         if (engine == null) {
  156.             if (other.engine != null)
  157.                 return false;
  158.         } else if (!engine.equals(other.engine)) {
  159.             return false;
  160.         }
  161.         return true;
  162.     }

  163. }