1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
52
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 }