View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
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.jtrolog;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Map;
27  
28  import io.github.prolobjectlink.prolog.AbstractProvider;
29  import io.github.prolobjectlink.prolog.PrologAtom;
30  import io.github.prolobjectlink.prolog.PrologConverter;
31  import io.github.prolobjectlink.prolog.PrologDouble;
32  import io.github.prolobjectlink.prolog.PrologEngine;
33  import io.github.prolobjectlink.prolog.PrologFloat;
34  import io.github.prolobjectlink.prolog.PrologInteger;
35  import io.github.prolobjectlink.prolog.PrologJavaConverter;
36  import io.github.prolobjectlink.prolog.PrologList;
37  import io.github.prolobjectlink.prolog.PrologLogger;
38  import io.github.prolobjectlink.prolog.PrologLong;
39  import io.github.prolobjectlink.prolog.PrologProvider;
40  import io.github.prolobjectlink.prolog.PrologStructure;
41  import io.github.prolobjectlink.prolog.PrologTerm;
42  import io.github.prolobjectlink.prolog.PrologVariable;
43  import jTrolog.engine.Prolog;
44  import jTrolog.parser.Parser;
45  import jTrolog.terms.Struct;
46  import jTrolog.terms.Term;
47  
48  /**
49   * 
50   * @author Jose Zalacain
51   * @since 1.0
52   */
53  public class JTrolog extends AbstractProvider implements PrologProvider {
54  
55  	private static final PrologLogger logger = new JTrologLogger();
56  	static final String VERSION = Prolog.VERSION;
57  	static final String NAME = "jTrolog";
58  
59  	public JTrolog() {
60  		super(new JTrologConverter());
61  	}
62  
63  	JTrolog(PrologConverter<Term> converter) {
64  		super(converter);
65  	}
66  
67  	public PrologTerm prologNil() {
68  		return new JTrologNil(this);
69  	}
70  
71  	public PrologTerm prologCut() {
72  		return new JTrologCut(this);
73  	}
74  
75  	public PrologTerm prologFail() {
76  		return new JTrologFail(this);
77  	}
78  
79  	public PrologTerm prologTrue() {
80  		return new JTrologTrue(this);
81  	}
82  
83  	public PrologTerm prologFalse() {
84  		return new JTrologFalse(this);
85  	}
86  
87  	public PrologTerm prologEmpty() {
88  		return new JTrologEmpty(this);
89  	}
90  
91  	public PrologTerm prologInclude(String file) {
92  		return newStructure("include", newAtom(file));
93  	}
94  
95  	// engine
96  
97  	public PrologEngine newEngine() {
98  		Prolog prolog = new Prolog();
99  		return new JTrologEngine(this, prolog);
100 	}
101 
102 	public PrologEngine newEngine(String path) {
103 		PrologEngine engine = newEngine();
104 		engine.consult(path);
105 		return engine;
106 	}
107 
108 	// parser helpers
109 
110 	public PrologTerm parseTerm(String term) {
111 		return toTerm(new Parser(term).nextTerm(false), PrologTerm.class);
112 	}
113 
114 	public PrologTerm[] parseTerms(String stringTerms) {
115 		List<PrologTerm> list = new ArrayList<PrologTerm>();
116 		Parser parser = new Parser(stringTerms);
117 		Term term = parser.nextTerm(false);
118 		while (term instanceof Struct) {
119 			Struct struct = (Struct) term;
120 			if (struct.name.equals(",") && struct.arity == 2) {
121 				list.add(toTerm(struct.getArg(0), PrologTerm.class));
122 				term = struct.getArg(1);
123 			} else {
124 				list.add(toTerm(term, PrologTerm.class));
125 				term = parser.nextTerm(false);
126 			}
127 		}
128 		return list.toArray(new PrologTerm[0]);
129 	}
130 
131 	// terms
132 
133 	public PrologAtom newAtom(String functor) {
134 		return new JTrologAtom(this, functor);
135 	}
136 
137 	public PrologFloat newFloat(Number value) {
138 		return new JTrologFloat(this, value);
139 	}
140 
141 	public PrologDouble newDouble(Number value) {
142 		return new JTrologDouble(this, value);
143 	}
144 
145 	public PrologInteger newInteger(Number value) {
146 		return new JTrologInteger(this, value);
147 	}
148 
149 	public PrologLong newLong(Number value) {
150 		return new JTrologLong(this, value);
151 	}
152 
153 	public PrologVariable newVariable(int position) {
154 		if (position < 0) {
155 			throw new IllegalArgumentException("Not allowed negative position");
156 		}
157 		return new JTrologVariable(this, position + 1);
158 	}
159 
160 	public PrologVariable newVariable(String name, int position) {
161 		if (position < 0) {
162 			throw new IllegalArgumentException("Not allowed negative position");
163 		}
164 		return new JTrologVariable(this, name, position + 1);
165 	}
166 
167 	public PrologList newList() {
168 		return new JTrologEmpty(this);
169 	}
170 
171 	public PrologList newList(PrologTerm[] arguments) {
172 		if (arguments != null && arguments.length > 0) {
173 			return new JTrologList(this, arguments);
174 		}
175 		return new JTrologEmpty(this);
176 	}
177 
178 	public PrologList newList(PrologTerm head, PrologTerm tail) {
179 		return new JTrologList(this, head, tail);
180 	}
181 
182 	public PrologList newList(PrologTerm[] arguments, PrologTerm tail) {
183 		return new JTrologList(this, arguments, tail);
184 	}
185 
186 	public PrologStructure newStructure(String functor, PrologTerm... arguments) {
187 		return new JTrologStructure(this, functor, arguments);
188 	}
189 
190 	public PrologTerm newStructure(PrologTerm left, String operator, PrologTerm right) {
191 		return new JTrologStructure(this, left, operator, right);
192 	}
193 
194 	public PrologTerm newReference(Object reference) {
195 		return new JTrologReference(this, reference);
196 	}
197 
198 	public final PrologTerm newEntry(PrologTerm key, PrologTerm value) {
199 		return new JTrologEntry(this, key, value);
200 	}
201 
202 	public final PrologTerm newEntry(Object key, Object value) {
203 		PrologJavaConverter transformer = getJavaConverter();
204 		PrologTerm keyTerm = transformer.toTerm(key);
205 		PrologTerm valueTerm = transformer.toTerm(value);
206 		return new JTrologEntry(this, keyTerm, valueTerm);
207 	}
208 
209 	public final PrologTerm newMap(Map<PrologTerm, PrologTerm> map) {
210 		return new JTrologMap(this, map);
211 	}
212 
213 	public final PrologTerm newMap(int initialCapacity) {
214 		return new JTrologMap(this, initialCapacity);
215 	}
216 
217 	public final PrologTerm newMap() {
218 		return new JTrologMap(this);
219 	}
220 
221 	public PrologTerm falseReference() {
222 		return newReference(false);
223 	}
224 
225 	public PrologTerm trueReference() {
226 		return newReference(true);
227 	}
228 
229 	public PrologTerm nullReference() {
230 		return newReference(null);
231 	}
232 
233 	public PrologTerm voidReference() {
234 		return newReference(void.class);
235 	}
236 
237 	public PrologJavaConverter getJavaConverter() {
238 		return new JTrologJavaConverter(this);
239 	}
240 
241 	public PrologLogger getLogger() {
242 		return logger;
243 	}
244 
245 	@Override
246 	public String toString() {
247 		return "JTrolog [converter=" + converter + "]";
248 	}
249 
250 }