1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13
|
|
14
|
|
15
|
|
16
|
|
17
|
|
18
|
|
19
|
|
20
|
|
21
|
|
22
|
|
23
|
|
24
|
|
25
|
|
26
|
|
27
|
|
28
|
package org.momut.ooas.codegen.prolog;
|
29
|
|
30
|
import org.momut.ooas.ast.identifiers.EnumIdentifier;
|
31
|
import org.momut.ooas.ast.identifiers.Identifier;
|
32
|
import org.momut.ooas.ast.identifiers.LandmarkIdentifier;
|
33
|
import org.momut.ooas.ast.types.BoolType;
|
34
|
import org.momut.ooas.ast.types.CharType;
|
35
|
import org.momut.ooas.ast.types.EnumType;
|
36
|
import org.momut.ooas.ast.types.FloatType;
|
37
|
import org.momut.ooas.ast.types.FunctionType;
|
38
|
import org.momut.ooas.ast.types.IntType;
|
39
|
import org.momut.ooas.ast.types.ListType;
|
40
|
import org.momut.ooas.ast.types.MapType;
|
41
|
import org.momut.ooas.ast.types.OoActionSystemInstance;
|
42
|
import org.momut.ooas.ast.types.OoActionSystemType;
|
43
|
import org.momut.ooas.ast.types.OpaqueType;
|
44
|
import org.momut.ooas.ast.types.QrType;
|
45
|
import org.momut.ooas.ast.types.TupleType;
|
46
|
import org.momut.ooas.ast.types.UlyssesType;
|
47
|
import org.momut.ooas.ast.types.ValuedEnumType;
|
48
|
import org.momut.ooas.codegen.OoasCodeEmitter;
|
49
|
import org.momut.ooas.utils.exceptions.NotImplementedException;
|
50
|
import org.momut.ooas.visitors.OoaTypeVisitor;
|
51
|
|
52
|
public class OoaPrologType extends OoaTypeVisitor
|
53
|
{
|
54
|
public static class Factory
|
55
|
{
|
56
|
public OoaPrologType create(OoaPrologIdentifier.Factory idFactory, Scratchbook scratchbook) { return new OoaPrologType(idFactory, scratchbook); }
|
57
|
}
|
58
|
|
59
|
protected final Scratchbook m_scratchbook;
|
60
|
|
61
|
public static void EmitType(UlyssesType aType, OoaPrologIdentifier.Factory idFactory, Scratchbook scratchbook)
|
62
|
{
|
63
|
final OoaPrologType ptype = new OoaPrologType(idFactory, scratchbook);
|
64
|
final OoasCodeEmitter m_emitter = new OoasCodeEmitter();
|
65
|
|
66
|
if (scratchbook.definedTypes().contains(aType))
|
67
|
return;
|
68
|
|
69
|
scratchbook.definedTypes().add(aType);
|
70
|
|
71
|
switch (aType.kind())
|
72
|
{
|
73
|
case OoActionSystemType:
|
74
|
aType.Accept(ptype);
|
75
|
final OoActionSystemType classtype = (OoActionSystemType)aType;
|
76
|
|
77
|
if (classtype.objects().size() == 0 && classtype.derivedObjects().size() == 0)
|
78
|
{
|
79
|
m_emitter.AppendLine(String.format("type( %s, nil).", ptype.toString()));
|
80
|
}
|
81
|
else
|
82
|
{
|
83
|
m_emitter.Append(String.format("type( %s, X) :- member(X,[null,", ptype.toString()));
|
84
|
boolean firste = true;
|
85
|
for (final OoActionSystemInstance inst: classtype.objects())
|
86
|
{
|
87
|
if (firste)
|
88
|
firste = false;
|
89
|
else
|
90
|
m_emitter.Append(", ");
|
91
|
m_emitter.Append(String.format("'%s'", inst.Name));
|
92
|
}
|
93
|
firste = true;
|
94
|
if (classtype.objects().size() > 0 && classtype.derivedObjects().size() > 0)
|
95
|
m_emitter.Append(", ");
|
96
|
for (final OoActionSystemInstance inst: classtype.derivedObjects())
|
97
|
{
|
98
|
if (firste)
|
99
|
firste = false;
|
100
|
else
|
101
|
m_emitter.Append(", ");
|
102
|
m_emitter.Append(String.format("'%s'", inst.Name));
|
103
|
}
|
104
|
m_emitter.AppendLine("]).");
|
105
|
}
|
106
|
break;
|
107
|
case EnumeratedType:
|
108
|
aType.Accept(ptype);
|
109
|
if (aType instanceof ValuedEnumType)
|
110
|
{
|
111
|
final ValuedEnumType venum = (ValuedEnumType)aType;
|
112
|
m_emitter.Append(String.format("type(%s, X) :- member(X,[", ptype.toString()));
|
113
|
boolean nocomma = true;
|
114
|
for (final Identifier val: venum.symbolTable().symbolList())
|
115
|
{
|
116
|
final EnumIdentifier id = (EnumIdentifier)val;
|
117
|
assert(id.HaveValue());
|
118
|
if (nocomma)
|
119
|
nocomma = false;
|
120
|
else
|
121
|
m_emitter.Append(", ");
|
122
|
m_emitter.Append(id.Value());
|
123
|
}
|
124
|
m_emitter.AppendLine("]).");
|
125
|
}
|
126
|
else
|
127
|
{
|
128
|
final EnumType enumtype = (EnumType)aType;
|
129
|
m_emitter.AppendLine(String.format("type( %s, X) :- X in 0..%s, labeling([],[X]).", ptype.toString(), enumtype.listOfEnumSymbols().size() - 1));
|
130
|
}
|
131
|
break;
|
132
|
case IntType:
|
133
|
aType.Accept(ptype);
|
134
|
final IntType inttype = (IntType)aType;
|
135
|
m_emitter.AppendLine(String.format("type( %s, X) :- X in %s..%s, labeling([],[X]).", ptype.toString().replace("-", ""), inttype.rangeLow(), inttype.rangeHigh()));
|
136
|
break;
|
137
|
case FloatType:
|
138
|
throw new NotImplementedException();
|
139
|
|
140
|
case Any:
|
141
|
break;
|
142
|
|
143
|
case ListType:
|
144
|
final ListType listType = (ListType)aType;
|
145
|
aType.Accept(ptype);
|
146
|
m_emitter.Append(String.format("type( %s, ", ptype.toString()));
|
147
|
m_emitter.Append("[");
|
148
|
Boolean first = true;
|
149
|
final OoaPrologType pType = new OoaPrologType(idFactory, scratchbook);
|
150
|
listType.innerType().Accept(pType);
|
151
|
|
152
|
for (int i = 0; i < listType.maxNumberOfElements(); i++)
|
153
|
{
|
154
|
if (!first)
|
155
|
{
|
156
|
m_emitter.Append(", ");
|
157
|
}
|
158
|
first = false;
|
159
|
m_emitter.Append(pType.toString());
|
160
|
}
|
161
|
m_emitter.AppendLine("]).");
|
162
|
break;
|
163
|
|
164
|
case MapType:
|
165
|
throw new NotImplementedException();
|
166
|
|
167
|
case FunctionType:
|
168
|
case OpaqueType:
|
169
|
case Null:
|
170
|
throw new NotImplementedException();
|
171
|
|
172
|
case TupleType:
|
173
|
final TupleType tupleType = (TupleType)aType;
|
174
|
aType.Accept(ptype);
|
175
|
m_emitter.Append(String.format("type( %s, ", ptype.toString()));
|
176
|
m_emitter.Append("[");
|
177
|
first = true;
|
178
|
|
179
|
for (final UlyssesType type: tupleType.innerTypes())
|
180
|
{
|
181
|
final OoaPrologType iType = new OoaPrologType(idFactory, scratchbook);
|
182
|
type.Accept(iType);
|
183
|
|
184
|
if (!first)
|
185
|
{
|
186
|
m_emitter.Append(", ");
|
187
|
}
|
188
|
first = false;
|
189
|
m_emitter.Append(iType.toString());
|
190
|
}
|
191
|
m_emitter.AppendLine("]).");
|
192
|
break;
|
193
|
|
194
|
case QrType:
|
195
|
aType.Accept(ptype);
|
196
|
final QrType qrtype = (QrType)aType;
|
197
|
|
198
|
final StringBuilder landmarks = new StringBuilder();
|
199
|
int cntr = 0;
|
200
|
for (final LandmarkIdentifier s: qrtype.landmarks())
|
201
|
{
|
202
|
if (cntr != 0)
|
203
|
landmarks.append(", ");
|
204
|
else
|
205
|
cntr++;
|
206
|
|
207
|
final OoaPrologIdentifier identifierVisitor = idFactory.create();
|
208
|
s.Accept(identifierVisitor);
|
209
|
landmarks.append(identifierVisitor.toString());
|
210
|
}
|
211
|
m_emitter.AppendLine(String.format("qspace( %s, [%s] ).", ptype.toString(), landmarks.toString()));
|
212
|
break;
|
213
|
default:
|
214
|
throw new NotImplementedException();
|
215
|
}
|
216
|
scratchbook.typeDefinitions().append(m_emitter.toString());
|
217
|
}
|
218
|
|
219
|
|
220
|
private final OoasCodeEmitter m_emitter = new OoasCodeEmitter();
|
221
|
private final OoaPrologIdentifier.Factory m_idFactory;
|
222
|
|
223
|
public @Override
|
224
|
void visit(CharType charType)
|
225
|
{
|
226
|
m_emitter.Append("char");
|
227
|
}
|
228
|
|
229
|
public @Override
|
230
|
void visit(IntType intType)
|
231
|
{
|
232
|
final OoaPrologIdentifier id = m_idFactory.create();
|
233
|
intType.identifier().Accept(id);
|
234
|
EmitType(intType, m_idFactory, m_scratchbook);
|
235
|
m_emitter.Append(id.toString());
|
236
|
}
|
237
|
|
238
|
public @Override
|
239
|
void visit(BoolType boolType)
|
240
|
{
|
241
|
m_emitter.Append("bool");
|
242
|
}
|
243
|
|
244
|
public @Override
|
245
|
void visit(FloatType floatType)
|
246
|
{
|
247
|
throw new NotImplementedException();
|
248
|
}
|
249
|
|
250
|
public @Override
|
251
|
void visit(EnumType enumType)
|
252
|
{
|
253
|
final OoaPrologIdentifier id = m_idFactory.create();
|
254
|
enumType.identifier().Accept(id);
|
255
|
EmitType(enumType, m_idFactory, m_scratchbook);
|
256
|
m_emitter.Append(id.toString());
|
257
|
}
|
258
|
|
259
|
public @Override
|
260
|
void visit(ListType listType)
|
261
|
{
|
262
|
final OoaPrologIdentifier id = m_idFactory.create();
|
263
|
listType.identifier().Accept(id);
|
264
|
EmitType(listType, m_idFactory, m_scratchbook);
|
265
|
m_emitter.Append(id.toString());
|
266
|
|
267
|
}
|
268
|
|
269
|
public @Override
|
270
|
void visit(MapType mapType)
|
271
|
{
|
272
|
throw new NotImplementedException();
|
273
|
}
|
274
|
|
275
|
public @Override
|
276
|
void visit(QrType qrType)
|
277
|
{
|
278
|
final OoaPrologIdentifier id = m_idFactory.create();
|
279
|
qrType.identifier().Accept(id);
|
280
|
m_emitter.Append(id.toString());
|
281
|
|
282
|
}
|
283
|
|
284
|
public @Override
|
285
|
void visit(TupleType tupleType)
|
286
|
{
|
287
|
final OoaPrologIdentifier id = m_idFactory.create();
|
288
|
tupleType.identifier().Accept(id);
|
289
|
EmitType(tupleType, m_idFactory, m_scratchbook);
|
290
|
m_emitter.Append(id.toString());
|
291
|
}
|
292
|
|
293
|
public @Override
|
294
|
void visit(FunctionType functionType)
|
295
|
{
|
296
|
throw new NotImplementedException();
|
297
|
}
|
298
|
|
299
|
public @Override
|
300
|
void visit(OoActionSystemType ooActionSystemType)
|
301
|
{
|
302
|
final OoaPrologIdentifier id = m_idFactory.create();
|
303
|
ooActionSystemType.identifier().Accept(id);
|
304
|
m_emitter.Append(id.toString());
|
305
|
}
|
306
|
|
307
|
public @Override
|
308
|
void visit(OpaqueType opaqueType)
|
309
|
{
|
310
|
throw new NotImplementedException();
|
311
|
}
|
312
|
|
313
|
|
314
|
@Override
|
315
|
public String toString()
|
316
|
{
|
317
|
return m_emitter.toString();
|
318
|
}
|
319
|
|
320
|
public String GetInternalTypeDefinitions()
|
321
|
{
|
322
|
final OoasCodeEmitter result = new OoasCodeEmitter();
|
323
|
|
324
|
result.AppendLine("type(bool, X) :- member(X,[fail,true]).");
|
325
|
result.AppendLine("type(char, X) :- X in 0..255, labeling([],[X]).");
|
326
|
|
327
|
return result.toString();
|
328
|
}
|
329
|
|
330
|
protected OoaPrologType(OoaPrologIdentifier.Factory idFactory, Scratchbook scratchbook)
|
331
|
{
|
332
|
m_idFactory = idFactory;
|
333
|
m_scratchbook = scratchbook;
|
334
|
}
|
335
|
}
|