View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jpl7
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    * 
10   * 1. Redistributions of source code must retain the above copyright notice,
11   *    this list of conditions and the following disclaimer.
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * #L%
28   */
29  package io.github.prolobjectlink.prolog.jpl7;
30  
31  import static org.jpl7.JPL.JFALSE;
32  import static org.jpl7.JPL.JNULL;
33  import static org.jpl7.JPL.JTRUE;
34  import static org.jpl7.JPL.JVOID;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  import java.util.Map;
39  
40  import org.jpl7.Term;
41  import org.jpl7.Util;
42  
43  import io.github.prolobjectlink.prolog.AbstractProvider;
44  import io.github.prolobjectlink.prolog.PrologAtom;
45  import io.github.prolobjectlink.prolog.PrologConverter;
46  import io.github.prolobjectlink.prolog.PrologDouble;
47  import io.github.prolobjectlink.prolog.PrologFloat;
48  import io.github.prolobjectlink.prolog.PrologInteger;
49  import io.github.prolobjectlink.prolog.PrologJavaConverter;
50  import io.github.prolobjectlink.prolog.PrologList;
51  import io.github.prolobjectlink.prolog.PrologLogger;
52  import io.github.prolobjectlink.prolog.PrologLong;
53  import io.github.prolobjectlink.prolog.PrologProvider;
54  import io.github.prolobjectlink.prolog.PrologStructure;
55  import io.github.prolobjectlink.prolog.PrologTerm;
56  import io.github.prolobjectlink.prolog.PrologVariable;
57  
58  /**
59   * 
60   * @author Jose Zalacain
61   * @since 1.0
62   */
63  public abstract class JplProvider extends AbstractProvider implements PrologProvider {
64  
65  	static final PrologLogger logger = new JplLogger();
66  
67  	protected JplProvider(PrologConverter<Term> adapter) {
68  		super(adapter);
69  	}
70  
71  	public final PrologTerm prologNil() {
72  		return new JplNil(this);
73  	}
74  
75  	public final PrologTerm prologCut() {
76  		return new JplCut(this);
77  	}
78  
79  	public final PrologTerm prologFail() {
80  		return new JplFail(this);
81  	}
82  
83  	public final PrologTerm prologTrue() {
84  		return new JplTrue(this);
85  	}
86  
87  	public final PrologTerm prologFalse() {
88  		return new JplFalse(this);
89  	}
90  
91  	public final PrologTerm prologEmpty() {
92  		return new JplEmpty(this);
93  	}
94  
95  	public final PrologTerm prologInclude(String file) {
96  		return newStructure("consult", newAtom(file));
97  	}
98  
99  	public final PrologTerm parseTerm(String term) {
100 		return toTerm(Util.textToTerm(term), PrologTerm.class);
101 	}
102 
103 	public final PrologTerm[] parseTerms(String stringTerms) {
104 		PrologTerm[] a = new PrologTerm[0];
105 		Term ptr = Util.textToTerm(stringTerms);
106 		List<PrologTerm> terms = new ArrayList<PrologTerm>();
107 		while (ptr.isCompound() && ptr.hasFunctor(",", 2)) {
108 			terms.add(toTerm(ptr.arg(1), PrologTerm.class));
109 			ptr = ptr.arg(2);
110 		}
111 		terms.add(toTerm(ptr, PrologTerm.class));
112 		return terms.toArray(a);
113 	}
114 
115 	public final PrologAtom newAtom(String functor) {
116 		return new JplAtom(this, functor);
117 	}
118 
119 	public final PrologFloat newFloat(Number value) {
120 		return new JplFloat(this, value);
121 	}
122 
123 	public final PrologDouble newDouble(Number value) {
124 		return new JplDouble(this, value);
125 	}
126 
127 	public final PrologInteger newInteger(Number value) {
128 		return new JplInteger(this, value);
129 	}
130 
131 	public final PrologLong newLong(Number value) {
132 		return new JplLong(this, value);
133 	}
134 
135 	public final PrologVariable newVariable(int position) {
136 		return new JplVariable(this);
137 	}
138 
139 	public final PrologVariable newVariable(String name, int position) {
140 		return new JplVariable(this, name);
141 	}
142 
143 	public final PrologList newList() {
144 		return new JplList(this);
145 	}
146 
147 	public final PrologList newList(PrologTerm[] arguments) {
148 		return new JplList(this, arguments);
149 	}
150 
151 	public final PrologList newList(PrologTerm head, PrologTerm tail) {
152 		return new JplList(this, head, tail);
153 	}
154 
155 	public final PrologList newList(PrologTerm[] arguments, PrologTerm tail) {
156 		return new JplList(this, arguments, tail);
157 	}
158 
159 	public final PrologStructure newStructure(String functor, PrologTerm... arguments) {
160 		return new JplStructure(this, functor, arguments);
161 	}
162 
163 	public final PrologTerm newStructure(PrologTerm left, String operator, PrologTerm right) {
164 		return new JplStructure(this, left, operator, right);
165 	}
166 
167 	public final PrologTerm newEntry(PrologTerm key, PrologTerm value) {
168 		return new JplEntry(this, key, value);
169 	}
170 
171 	public final PrologTerm newEntry(Object key, Object value) {
172 		PrologJavaConverter transformer = getJavaConverter();
173 		PrologTerm keyTerm = transformer.toTerm(key);
174 		PrologTerm valueTerm = transformer.toTerm(value);
175 		return new JplEntry(this, keyTerm, valueTerm);
176 	}
177 
178 	public final PrologTerm newMap(Map<PrologTerm, PrologTerm> map) {
179 		return new JplMap(this, map);
180 	}
181 
182 	public final PrologTerm newMap(int initialCapacity) {
183 		return new JplMap(this, initialCapacity);
184 	}
185 
186 	public final PrologTerm newMap() {
187 		return new JplMap(this);
188 	}
189 
190 	public final PrologTerm newReference(Object reference) {
191 		return new JplReference(this, reference);
192 	}
193 
194 	public final PrologTerm falseReference() {
195 		return new JplReference(this, JFALSE);
196 	}
197 
198 	public final PrologTerm trueReference() {
199 		return new JplReference(this, JTRUE);
200 	}
201 
202 	public final PrologTerm nullReference() {
203 		return new JplReference(this, JNULL);
204 	}
205 
206 	public final PrologTerm voidReference() {
207 		return new JplReference(this, JVOID);
208 	}
209 
210 	public final PrologLogger getLogger() {
211 		return logger;
212 	}
213 
214 }