View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jlog
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 General Public License as
9    * published by the Free Software Foundation, either version 3 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 Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jlog;
23  
24  import static io.github.prolobjectlink.prolog.AbstractConverter.SIMPLE_ATOM_REGEX;
25  import static io.github.prolobjectlink.prolog.PrologLogger.ERROR_LOADING_BUILT_INS;
26  
27  import java.io.IOException;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  import io.github.prolobjectlink.prolog.AbstractProvider;
32  import io.github.prolobjectlink.prolog.PrologAtom;
33  import io.github.prolobjectlink.prolog.PrologConverter;
34  import io.github.prolobjectlink.prolog.PrologDouble;
35  import io.github.prolobjectlink.prolog.PrologEngine;
36  import io.github.prolobjectlink.prolog.PrologFloat;
37  import io.github.prolobjectlink.prolog.PrologInteger;
38  import io.github.prolobjectlink.prolog.PrologJavaConverter;
39  import io.github.prolobjectlink.prolog.PrologList;
40  import io.github.prolobjectlink.prolog.PrologLogger;
41  import io.github.prolobjectlink.prolog.PrologLong;
42  import io.github.prolobjectlink.prolog.PrologProvider;
43  import io.github.prolobjectlink.prolog.PrologStructure;
44  import io.github.prolobjectlink.prolog.PrologTerm;
45  import io.github.prolobjectlink.prolog.PrologVariable;
46  import ubc.cs.JLog.Foundation.jKnowledgeBase;
47  import ubc.cs.JLog.Foundation.jPrologFileServices;
48  import ubc.cs.JLog.Foundation.jPrologServices;
49  import ubc.cs.JLog.Parser.pOperatorRegistry;
50  import ubc.cs.JLog.Parser.pParseStream;
51  import ubc.cs.JLog.Parser.pPredicateRegistry;
52  import ubc.cs.JLog.Terms.jPredicateTerms;
53  import ubc.cs.JLog.Terms.jTerm;
54  
55  /**
56   * 
57   * @author Jose Zalacain
58   * @since 1.0
59   */
60  public class JLog extends AbstractProvider implements PrologProvider {
61  
62  	private final jKnowledgeBase kb = new jKnowledgeBase();
63  	private final pOperatorRegistry or = new pOperatorRegistry();
64  	private final pPredicateRegistry pr = new pPredicateRegistry();
65  	private final jPrologServices prolog = new jPrologServices(kb, pr, or);
66  
67  	private static final String DOT = ".";
68  	private static final String BUILT_INS = "builtins";
69  	private static final PrologLogger logger = new JLogLogger();
70  	protected static final Map<String, String> FUNCTORS = new HashMap<String, String>();
71  
72  	public JLog() {
73  		this(new JLogConverter());
74  	}
75  
76  	public JLog(PrologConverter<jTerm> converter) {
77  		super(converter);
78  		prolog.setFileServices(new jPrologFileServices());
79  		try {
80  			prolog.loadLibrary(BUILT_INS);
81  		} catch (IOException e) {
82  			getLogger().error(getClass(), ERROR_LOADING_BUILT_INS, e);
83  		}
84  	}
85  
86  	public PrologTerm prologNil() {
87  		return new JLogNil(this);
88  	}
89  
90  	public PrologTerm prologCut() {
91  		return new JLogCut(this);
92  	}
93  
94  	public PrologTerm prologFail() {
95  		return new JLogFail(this);
96  	}
97  
98  	public PrologTerm prologTrue() {
99  		return new JLogTrue(this);
100 	}
101 
102 	public PrologTerm prologFalse() {
103 		return new JLogFalse(this);
104 	}
105 
106 	public PrologTerm prologEmpty() {
107 		return new JLogEmpty(this);
108 	}
109 
110 	public PrologTerm prologInclude(String file) {
111 		return newStructure("consult", newAtom(file));
112 	}
113 
114 	public PrologTerm parseTerm(String str) {
115 		String s = str.charAt(str.length() - 1) == '.' ? str : str + DOT;
116 		return toTerm(new pParseStream(s, kb, pr, or).parseTerm(), PrologTerm.class);
117 	}
118 
119 	public PrologTerm[] parseTerms(String str) {
120 		String s = str.charAt(str.length() - 1) == '.' ? str : str + DOT;
121 		jPredicateTerms terms = new pParseStream(s, kb, pr, or).parseQuery();
122 		PrologTerm[] prologTerms = new PrologTerm[terms.size()];
123 		for (int i = 0; i < prologTerms.length; i++) {
124 			PrologTerm term = toTerm(terms.elementAt(i), PrologTerm.class);
125 			if (term != null && term.isCompound()) {
126 				String functor = term.getFunctor();
127 				PrologTerm[] args = term.getArguments();
128 				if (!functor.matches(SIMPLE_ATOM_REGEX)) {
129 					StringBuilder buffer = new StringBuilder();
130 					buffer.append("'");
131 					buffer.append(functor);
132 					buffer.append("'");
133 					String quoted = "" + buffer + "";
134 					FUNCTORS.put(functor, quoted);
135 					term = newStructure(quoted, args);
136 				}
137 			}
138 			prologTerms[i] = term;
139 		}
140 		return prologTerms;
141 	}
142 
143 	public PrologEngine newEngine() {
144 		return new JLogEngine(this);
145 	}
146 
147 	public PrologEngine newEngine(String path) {
148 		PrologEngine engine = newEngine();
149 		engine.consult(path);
150 		return engine;
151 	}
152 
153 	public PrologAtom newAtom(String functor) {
154 		if (!functor.matches(SIMPLE_ATOM_REGEX)) {
155 			return new JLogAtom(this, "'" + functor + "'");
156 		}
157 		return new JLogAtom(this, functor);
158 	}
159 
160 	public PrologFloat newFloat(Number value) {
161 		return new JLogFloat(this, value);
162 	}
163 
164 	public PrologDouble newDouble(Number value) {
165 		return new JLogDouble(this, value);
166 	}
167 
168 	public PrologInteger newInteger(Number value) {
169 		return new JLogInteger(this, value);
170 	}
171 
172 	public PrologLong newLong(Number value) {
173 		return new JLogLong(this, value);
174 	}
175 
176 	public PrologVariable newVariable(int position) {
177 		return new JLogVariable(this);
178 	}
179 
180 	public PrologVariable newVariable(String name, int position) {
181 		return new JLogVariable(this, name);
182 	}
183 
184 	public PrologList newList() {
185 		return new JLogEmpty(this);
186 	}
187 
188 	public PrologList newList(PrologTerm[] arguments) {
189 		if (arguments != null && arguments.length > 0) {
190 			return new JLogList(this, arguments);
191 		}
192 		return new JLogEmpty(this);
193 	}
194 
195 	public PrologList newList(PrologTerm head, PrologTerm tail) {
196 		return new JLogList(this, head, tail);
197 	}
198 
199 	public PrologList newList(PrologTerm[] arguments, PrologTerm tail) {
200 		return new JLogList(this, arguments, tail);
201 	}
202 
203 	public PrologStructure newStructure(String functor, PrologTerm... arguments) {
204 		return new JLogStructure(this, functor, arguments);
205 	}
206 
207 	public PrologTerm newStructure(PrologTerm left, String operator, PrologTerm right) {
208 		return new JLogStructure(this, left, operator, right);
209 	}
210 
211 	public final PrologTerm newEntry(PrologTerm key, PrologTerm value) {
212 		return new JLogEntry(this, key, value);
213 	}
214 
215 	public final PrologTerm newEntry(Object key, Object value) {
216 		PrologJavaConverter transformer = getJavaConverter();
217 		PrologTerm keyTerm = transformer.toTerm(key);
218 		PrologTerm valueTerm = transformer.toTerm(value);
219 		return new JLogEntry(this, keyTerm, valueTerm);
220 	}
221 
222 	public final PrologTerm newMap(Map<PrologTerm, PrologTerm> map) {
223 		return new JLogMap(this, map);
224 	}
225 
226 	public final PrologTerm newMap(int initialCapacity) {
227 		return new JLogMap(this, initialCapacity);
228 	}
229 
230 	public final PrologTerm newMap() {
231 		return new JLogMap(this);
232 	}
233 
234 	public PrologTerm newReference(Object reference) {
235 		return new JLogReference(this, reference);
236 	}
237 
238 	public PrologTerm falseReference() {
239 		return newReference(false);
240 	}
241 
242 	public PrologTerm trueReference() {
243 		return newReference(true);
244 	}
245 
246 	public PrologTerm nullReference() {
247 		return newReference(null);
248 	}
249 
250 	public PrologTerm voidReference() {
251 		return newReference(void.class);
252 	}
253 
254 	public PrologJavaConverter getJavaConverter() {
255 		return new JLogJavaConverter(this);
256 	}
257 
258 	public PrologLogger getLogger() {
259 		return logger;
260 	}
261 
262 	@Override
263 	public String toString() {
264 		return "JLog [converter=" + converter + "]";
265 	}
266 
267 	@Override
268 	public int hashCode() {
269 		final int prime = 31;
270 		int result = super.hashCode();
271 		result = prime * result + prolog.hashCode();
272 		return result;
273 	}
274 
275 	@Override
276 	public boolean equals(Object obj) {
277 		if (this == obj)
278 			return true;
279 		if (!super.equals(obj))
280 			return false;
281 		if (getClass() != obj.getClass())
282 			return false;
283 		JLog other = (JLog) obj;
284 		return prolog.equals(other.prolog);
285 	}
286 
287 }