Project

General

Profile

Revision 7

Added by Willibald K. over 8 years ago

changing java, cpp, hpp files to unix line endings

View differences:

OoaPrologType.java
1
/**

2
  *

3
  *                      OOAS Compiler

4
  *

5
  *       Copyright 2015, AIT Austrian Institute of Technology.

6
  * This code is based on the C# Version of the OOAS Compiler, which is

7
  * copyright 2015 by the Institute of Software Technology, Graz University

8
  * of Technology with portions copyright by the AIT Austrian Institute of

9
  * Technology. All rights reserved.

10
  *

11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.

12
  *

13
  * If you modify the file please update the list of contributors below to in-

14
  * clude your name. Please also stick to the coding convention of using TABs

15
  * to do the basic (block-level) indentation and spaces for anything after

16
  * that. (Enable the display of special chars and it should be pretty obvious

17
  * what this means.) Also, remove all trailing whitespace.

18
  *

19
  * Contributors:

20
  *               Willibald Krenn (AIT)

21
  *               Stephan Zimmerer (AIT)

22
  *               Markus Demetz (AIT)

23
  *               Christoph Czurda (AIT)

24
  *

25
  */

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26 26

  
27 27

  
28
package org.momut.ooas.codegen.prolog;

29

  
28
package org.momut.ooas.codegen.prolog;
29

  
30 30
import org.momut.ooas.ast.identifiers.EnumIdentifier;
31 31
import org.momut.ooas.ast.identifiers.Identifier;
32 32
import org.momut.ooas.ast.identifiers.LandmarkIdentifier;
......
48 48
import org.momut.ooas.codegen.OoasCodeEmitter;
49 49
import org.momut.ooas.utils.exceptions.NotImplementedException;
50 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
			// m_emitter.Append(String.format("type( %s, X) :- member(X,[ 'null'", ptype.ToString()));
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(); // must not hit this
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*/ @Override
224
	void visit(CharType charType)
225
	{
226
		m_emitter.Append("char");
227
	}
228

  
229
	public /*override*/ @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*/ @Override
239
	void visit(BoolType boolType)
240
	{
241
		m_emitter.Append("bool");
242
	}
243

  
244
	public /*override*/ @Override
245
	void visit(FloatType floatType)
246
	{
247
		throw new NotImplementedException();
248
	}
249

  
250
	public /*override*/ @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*/ @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*/ @Override
270
	void visit(MapType mapType)
271
	{
272
		throw new NotImplementedException();
273
	}
274

  
275
	public /*override*/ @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
		// m_emitter.Append(qrType.identifier.tokenText.ToLower());
282
	}
283

  
284
	public /*override*/ @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*/ @Override
294
	void visit(FunctionType functionType)
295
	{
296
		throw new NotImplementedException();
297
	}
298

  
299
	public /*override*/ @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*/ @Override
308
	void visit(OpaqueType opaqueType)
309
	{
310
		throw new NotImplementedException();
311
	}
312

  
313

  
314
	@Override
315
	public /*override*/ 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
}
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
			// m_emitter.Append(String.format("type( %s, X) :- member(X,[ 'null'", ptype.ToString()));
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(); // must not hit this
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*/ @Override
224
	void visit(CharType charType)
225
	{
226
		m_emitter.Append("char");
227
	}
228

  
229
	public /*override*/ @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*/ @Override
239
	void visit(BoolType boolType)
240
	{
241
		m_emitter.Append("bool");
242
	}
243

  
244
	public /*override*/ @Override
245
	void visit(FloatType floatType)
246
	{
247
		throw new NotImplementedException();
248
	}
249

  
250
	public /*override*/ @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*/ @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*/ @Override
270
	void visit(MapType mapType)
271
	{
272
		throw new NotImplementedException();
273
	}
274

  
275
	public /*override*/ @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
		// m_emitter.Append(qrType.identifier.tokenText.ToLower());
282
	}
283

  
284
	public /*override*/ @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*/ @Override
294
	void visit(FunctionType functionType)
295
	{
296
		throw new NotImplementedException();
297
	}
298

  
299
	public /*override*/ @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*/ @Override
308
	void visit(OpaqueType opaqueType)
309
	{
310
		throw new NotImplementedException();
311
	}
312

  
313

  
314
	@Override
315
	public /*override*/ 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
}

Also available in: Unified diff