View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jpl
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jpl;
23  
24  import static jpl.JPL.JFALSE;
25  import static jpl.JPL.JNULL;
26  import static jpl.JPL.JTRUE;
27  import static jpl.JPL.JVOID;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  import java.util.Map;
32  
33  import io.github.prolobjectlink.prolog.AbstractProvider;
34  import io.github.prolobjectlink.prolog.PrologAtom;
35  import io.github.prolobjectlink.prolog.PrologConverter;
36  import io.github.prolobjectlink.prolog.PrologDouble;
37  import io.github.prolobjectlink.prolog.PrologFloat;
38  import io.github.prolobjectlink.prolog.PrologInteger;
39  import io.github.prolobjectlink.prolog.PrologJavaConverter;
40  import io.github.prolobjectlink.prolog.PrologList;
41  import io.github.prolobjectlink.prolog.PrologLogger;
42  import io.github.prolobjectlink.prolog.PrologLong;
43  import io.github.prolobjectlink.prolog.PrologProvider;
44  import io.github.prolobjectlink.prolog.PrologStructure;
45  import io.github.prolobjectlink.prolog.PrologTerm;
46  import io.github.prolobjectlink.prolog.PrologVariable;
47  import jpl.Term;
48  import jpl.Util;
49  
50  /**
51   * @author Jose Zalacain
52   * @since 1.0
53   */
54  public abstract class JplProvider extends AbstractProvider implements PrologProvider {
55  
56  	static final PrologLogger logger = new JplLogger();
57  
58  	public JplProvider(PrologConverter<Term> adapter) {
59  		super(adapter);
60  	}
61  
62  	public final PrologTerm prologNil() {
63  		return new JplNil(this);
64  	}
65  
66  	public final PrologTerm prologCut() {
67  		return new JplCut(this);
68  	}
69  
70  	public final PrologTerm prologFail() {
71  		return new JplFail(this);
72  	}
73  
74  	public final PrologTerm prologTrue() {
75  		return new JplTrue(this);
76  	}
77  
78  	public final PrologTerm prologFalse() {
79  		return new JplFalse(this);
80  	}
81  
82  	public final PrologTerm prologEmpty() {
83  		return new JplEmpty(this);
84  	}
85  
86  	public final PrologTerm prologInclude(String file) {
87  		return newStructure("consult", newAtom(file));
88  	}
89  
90  	public final PrologTerm parseTerm(String term) {
91  		return toTerm(Util.textToTerm(term), PrologTerm.class);
92  	}
93  
94  	public final PrologTerm[] parseTerms(String stringTerms) {
95  		PrologTerm[] a = new PrologTerm[0];
96  		Term ptr = Util.textToTerm(stringTerms);
97  		List<PrologTerm> terms = new ArrayList<PrologTerm>();
98  		while (ptr.isCompound() && ptr.hasFunctor(",", 2)) {
99  			terms.add(toTerm(ptr.arg(1), PrologTerm.class));
100 			ptr = ptr.arg(2);
101 		}
102 		terms.add(toTerm(ptr, PrologTerm.class));
103 		return terms.toArray(a);
104 	}
105 
106 	public final PrologAtom newAtom(String functor) {
107 		return new JplAtom(this, functor);
108 	}
109 
110 	public final PrologFloat newFloat(Number value) {
111 		return new JplFloat(this, value);
112 	}
113 
114 	public final PrologDouble newDouble(Number value) {
115 		return new JplDouble(this, value);
116 	}
117 
118 	public final PrologInteger newInteger(Number value) {
119 		return new JplInteger(this, value);
120 	}
121 
122 	public final PrologLong newLong(Number value) {
123 		return new JplLong(this, value);
124 	}
125 
126 	public final PrologVariable newVariable(int position) {
127 		return new JplVariable(this);
128 	}
129 
130 	public final PrologVariable newVariable(String name, int position) {
131 		return new JplVariable(this, name);
132 	}
133 
134 	public final PrologList newList() {
135 		return new JplList(this);
136 	}
137 
138 	public final PrologList newList(PrologTerm[] arguments) {
139 		return new JplList(this, arguments);
140 	}
141 
142 	public final PrologList newList(PrologTerm head, PrologTerm tail) {
143 		return new JplList(this, head, tail);
144 	}
145 
146 	public final PrologList newList(PrologTerm[] arguments, PrologTerm tail) {
147 		return new JplList(this, arguments, tail);
148 	}
149 
150 	public final PrologStructure newStructure(String functor, PrologTerm... arguments) {
151 		return new JplStructure(this, functor, arguments);
152 	}
153 
154 	public final PrologTerm newStructure(PrologTerm left, String operator, PrologTerm right) {
155 		return new JplStructure(this, left, operator, right);
156 	}
157 
158 	public final PrologTerm newEntry(PrologTerm key, PrologTerm value) {
159 		return new JplEntry(this, key, value);
160 	}
161 
162 	public final PrologTerm newEntry(Object key, Object value) {
163 		PrologJavaConverter transformer = getJavaConverter();
164 		PrologTerm keyTerm = transformer.toTerm(key);
165 		PrologTerm valueTerm = transformer.toTerm(value);
166 		return new JplEntry(this, keyTerm, valueTerm);
167 	}
168 
169 	public final PrologTerm newMap(Map<PrologTerm, PrologTerm> map) {
170 		return new JplMap(this, map);
171 	}
172 
173 	public final PrologTerm newMap(int initialCapacity) {
174 		return new JplMap(this, initialCapacity);
175 	}
176 
177 	public final PrologTerm newMap() {
178 		return new JplMap(this);
179 	}
180 
181 	public final PrologTerm newReference(Object reference) {
182 		return new JplReference(this, reference);
183 	}
184 
185 	public final PrologTerm falseReference() {
186 		return new JplReference(this, JFALSE);
187 	}
188 
189 	public final PrologTerm trueReference() {
190 		return new JplReference(this, JTRUE);
191 	}
192 
193 	public final PrologTerm nullReference() {
194 		return new JplReference(this, JNULL);
195 	}
196 
197 	public final PrologTerm voidReference() {
198 		return new JplReference(this, JVOID);
199 	}
200 
201 	public final PrologLogger getLogger() {
202 		return logger;
203 	}
204 
205 }