Project

General

Profile

Revision 7

Added by Willibald K. over 8 years ago

changing java, cpp, hpp files to unix line endings

View differences:

OoaPrologVisitor.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

  
30
import java.util.ArrayList;
31
import java.util.Date;
28
package org.momut.ooas.codegen.prolog;
32 29

  
30
import java.util.ArrayList;
31
import java.util.Date;
32

  
33 33
import org.momut.ooas.ast.identifiers.AttributeIdentifier;
34 34
import org.momut.ooas.ast.identifiers.Identifier;
35 35
import org.momut.ooas.ast.identifiers.IdentifierKind;
......
46 46
import org.momut.ooas.parser.ParserState;
47 47
import org.momut.ooas.utils.OoasCompilerHashMap;
48 48
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor;
49

  
50
public class OoaPrologVisitor extends OoaCompleteAstTraversalVisitor
51
{
52
	protected OoasCodeEmitter m_emitter;
53
	protected final Scratchbook m_scratchbook = new Scratchbook();
54

  
55
//	private String PrologIdentifier(Identifier id)
56
//	{
57
//		OoaPrologIdentifier identifierVisitor = m_idFactory.create();
58
//		id.Accept(identifierVisitor);
59
//		return identifierVisitor.toString();
60
//	}
61

  
62
	protected void ExportMethodsAndActions(MainModule mainModule)
63
	{
64
		final StringBuilder varActions = new StringBuilder();
65
		m_emitter.AppendLine();
66
		m_emitter.AppendLine("% action system");
67
		m_emitter.AppendLineIncIndent("as :- ");
68

  
69
		/* export methods */
70
		m_emitter.AppendLine("methods (");
71
		ExportMethods(mainModule);
72
		m_emitter.AppendLine("), ");
73
		m_emitter.AppendLine("actions (");
74
		ExportNamedActions(mainModule, varActions);
75
		m_emitter.AppendLine("), ");
76
		m_emitter.AppendLine("dood (");
77

  
78
		ExportDoOdBlock(mainModule.instance().doOdBlock(), m_emitter);
79

  
80

  
81
		m_emitter.AppendLine("), qdes (none)");
82
		m_emitter.DecIndent();
83
		m_emitter.AppendLine(".");
84
		m_emitter.AppendLine("");
85

  
86
		m_emitter.Append(varActions.toString());
87
	}
88

  
89
	private void ExportMethods(MainModule mainModule)
90
	{
91
		int y = 0;
92
		for (final Identifier x: mainModule.instance().symbols().symbolList())
93
		{
94
			if (x.kind() == IdentifierKind.MethodIdentifier)
95
			{
96
				if (y != 0)
97
					m_emitter.AppendLine(", ");
98
				else
99
					y++;
100

  
101
				final MethodIdentifier theMethod = (MethodIdentifier)x;
102
				final OoaPrologIdentifier pIdent = m_idFactory.create();
103
				theMethod.Accept(pIdent);
104

  
105
				/* method */
106
				final StringBuilder parameter = new StringBuilder();
107
				if (theMethod.parameter().size() > 0 || ((FunctionType)theMethod.type()).returnType() != null)
108
				{
109
					parameter.append("(");
110
					int z = 0;
111
					for (final ParameterIdentifier param: theMethod.parameter())
112
					{
113
						if (z != 0)
114
						{
115
							parameter.append(", ");
116
						}
117
						else
118
							z++;
119
						final OoaPrologIdentifier pParamIdent = m_idFactory.create();
120
						param.Accept(pParamIdent);
121
						parameter.append(pParamIdent.toString());
122
					}
123

  
124

  
125
					if (((FunctionType)theMethod.type()).returnType() != null)
126
					{
127
						if (z != 0)
128
							parameter.append(", ");
129
						parameter.append("RESULT");
130
					}
131
					parameter.append(")");
132
				}
133
				m_emitter.AppendLineIncIndent(String.format("%s%s = (", pIdent.toString(), parameter.toString()));
134

  
135
				final OoaPrologStatement pBody = createStatementVisitor();
136
				((MethodIdentifier)x).body().Accept(pBody);
137
				m_emitter.Append(pBody.toString());
138
				m_emitter.Append(")");
139
				m_emitter.DecIndent();
140
			}
141
		}
142
		if (y == 0)
143
			m_emitter.Append(" none ");
144
	}
145

  
146
	private void ExportNamedActions(MainModule mainModule, StringBuilder varActions)
147
	{
148
		int y = 0;
149
		for (final Identifier x: mainModule.instance().symbols().symbolList())
150
		{
151
			if (x.kind() == IdentifierKind.NamedActionIdentifier)
152
			{
153
				if (y != 0)
154
					m_emitter.AppendLine(", ");
155
				else
156
					y++;
157
				final NamedActionIdentifier action = (NamedActionIdentifier)x;
158
				final OoaPrologIdentifier pIdent = m_idFactory.create();
159
				action.Accept(pIdent);
160

  
161
				if (((FunctionType)action.type()).functionType() != FunctionTypeEnum.Continuous)
162
				{
163
					/* discrete actions */
164
					final StringBuilder parameter = new StringBuilder();
165
					final StringBuilder parametertype = new StringBuilder();
166
					if (action.parameter().size() > 0)
167
					{
168
						parameter.append("(");
169
						parametertype.append("(");
170
						int z = 0;
171
						for (final ParameterIdentifier param: action.parameter())
172
						{
173
							if (z != 0)
174
							{
175
								parameter.append(", ");
176
								parametertype.append(", ");
177
							}
178
							else
179
								z++;
180
							final OoaPrologIdentifier pParamIdent = m_idFactory.create();
181
							param.Accept(pParamIdent);
182
							parameter.append(pParamIdent.toString());
183

  
184
							final OoaPrologType pParamType = createTypeVisitor();  // bugfix me!
185
							param.type().Accept(pParamType);
186
							parametertype.append(pParamType.toString());
187

  
188
						}
189
						parametertype.append(")");
190
						parameter.append(")");
191
					}
192
					//if (parametertype.Length > 0)
193
					//    varActions.AppendLine(String.format("%s%s.", pIdent.toString(), parametertype.toString()));
194
					m_emitter.AppendLineIncIndent(String.format("%s%s::", pIdent.toString(), parameter.toString()));
195
				}
196
				else
197
				{
198
					// we do not support continuous actions anymore.
199
					throw new UnsupportedOperationException();
200
//					/* observable state vars outside... */
201
//					final StringBuilder parameter = new StringBuilder();
202
//					int z = 0;
203
//					for (final Statement stmt : ((GuardedCommand)action.body().statements().peekFirst()).body().statements())
204
//					{
205
//						final QualitativeConstraintStatement qstmt = (QualitativeConstraintStatement)stmt;
206
//						if (qstmt.operation() == QualitativeConstraintOperation.Equal
207
//								&& qstmt.variable1().kind() == IdentifierKind.AttributeIdentifier
208
//								&& ((AttributeIdentifier)qstmt.variable1()).isObservable())
209
//						{
210
//							if (z != 0)
211
//								parameter.append(", ");
212
//							else
213
//								z++;
214
//							qstmt.tag = true;
215
//							final OoaPrologIdentifier pParamIdent1 = m_idFactory.create();
216
//							final OoaPrologIdentifier pParamIdent2 = m_idFactory.create();
217
//							qstmt.variable0().Accept(pParamIdent1);
218
//							qstmt.variable1().Accept(pParamIdent2);
219
//							parameter.append(String.format("%s(%s)", pParamIdent2.toString(), pParamIdent1.toString()));
220
//						}
221
//					}
222
//					m_emitter.AppendLineIncIndent(String.format("[%s]::", parameter.toString()));
223
				}
224

  
225
				final OoaPrologStatement pBody = createStatementVisitor();
226
				((NamedActionIdentifier)x).body().Accept(pBody);
227
				m_emitter.Append(pBody.toString());
228
				m_emitter.DecIndent();
229

  
230
			}
231
		}
232
	}
233

  
234
	private void ExportDoOdBlock(Block block, OoasCodeEmitter m_emitter)
235
	{
236
		final OoaPrologStatement statement = createStatementVisitor();
237
		block.Accept(statement);
238
		m_emitter.Append(statement.toString());
239
	}
240

  
241
	protected int ExportActions(MainModule mainModule, String predName, FunctionTypeEnum actionKind)
242
	{
243
		m_emitter.AppendLine("");
244
		m_emitter.AppendLine("%emit " + actionKind.toString() + " actions");
245
		m_emitter.Append(predName + "([");
246
		int y = 0;
247
		for (final Identifier x: mainModule.instance().symbols().symbolList())
248
		{
249
			if (x.kind() == IdentifierKind.NamedActionIdentifier)
250
			{
251
				if (((FunctionType)((NamedActionIdentifier)x).type()).functionType() == actionKind)
252
				{
253
					if (y != 0)
254
						m_emitter.Append(", ");
255
					else
256
						y++;
257
					final OoaPrologIdentifier pIdent = m_idFactory.create();
258

  
259
					((NamedActionIdentifier)x).Accept(pIdent);
260
					m_emitter.Append(pIdent.toString());
261
				}
262
			}
263
		}
264
		m_emitter.AppendLine("]).");
265
		return y;
266
	}
267

  
268
	protected int ExportControllableActions(MainModule mainModule) {
269
		return ExportActions(mainModule, "input", FunctionTypeEnum.Controllable);
270
	}
271

  
272
	protected int ExportObservableActions(MainModule mainModule) {
273
		return ExportActions(mainModule, "output", FunctionTypeEnum.Observable);
274
	}
275

  
276
	protected void ExportInitialState(MainModule mainModule)
277
	{
278
		m_emitter.AppendLine("");
279
		m_emitter.AppendLine("%emit initial state");
280
		final ArrayList<String> initPreds = new ArrayList<String>();
281
		final ArrayList<String> initVars = new ArrayList<String>();
282
		int y = 0;
283
		for (final Identifier x: mainModule.instance().symbols().symbolList())
284
		{
285
			if (x.kind() == IdentifierKind.AttributeIdentifier)
286
			{
287
				final OoaPrologExpression anExprVis = createExpressionVisitor();
288
				((AttributeIdentifier)x).initializer().Accept(anExprVis);
289
				assert(anExprVis.tmpVariables().size() == 1);
290
				final String initPredName = String.format("attr_%s_init(%s)", x.tokenText(), anExprVis.tmpVariables().get(0));
291
				m_emitter.AppendLine(String.format("%s :- %s 1=1.", initPredName, anExprVis.toString()));
292
				initPreds.add(initPredName);
293
				initVars.add(anExprVis.tmpVariables().get(0));
294
			}
295
		}
296

  
297

  
298
		m_emitter.Append("init([");
299
		y = 0;
300
		for (final String x: initVars)
301
		{
302
			if (y != 0)
303
				m_emitter.Append(",");
304
			else
305
				y++;
306
			m_emitter.Append(x);
307
		}
308
		m_emitter.Append("]) :-");
309
		y = 0;
310
		for (final String x: initPreds)
311
		{
312
			if (y != 0)
313
				m_emitter.Append(",");
314
			else
315
				y++;
316
			m_emitter.Append(x);
317
		}
318
		m_emitter.AppendLine(".");
319
	}
320

  
321
	protected int ExportState(MainModule mainModule)
322
	{
323
		m_emitter.AppendLine("");
324
		m_emitter.AppendLine("%emit state definition");
325
		m_emitter.Append("state_def([");
326
		int y = 0;
327
		for (final Identifier x: mainModule.instance().symbols().symbolList())
328
		{
329
			if (x.kind() == IdentifierKind.AttributeIdentifier)
330
			{
331
				if (y != 0)
332
					m_emitter.Append(", ");
333
				else
334
					y++;
335
				final OoaPrologIdentifier pid = m_idFactory.create();
336
				x.Accept(pid);
337
				m_emitter.Append(pid.toString());
338
			}
339
		}
340
		m_emitter.AppendLine("]).");
341
		return y;
342
	}
343

  
344
	protected void ExportVariables(MainModule mainModule)
345
	{
346
		m_emitter.AppendLine("");
347
		m_emitter.AppendLine("%emit type of state variables");
348
		final OoasCompilerHashMap<String, StringBuilder> varlist = new OoasCompilerHashMap<String, StringBuilder>();
349
		for (final Identifier x: mainModule.instance().symbols().symbolList())
350
		{
351
			if (x.kind() == IdentifierKind.AttributeIdentifier)
352
			{
353
				final OoaPrologIdentifier pid = m_idFactory.create();
354
				final OoaPrologType ptype = createTypeVisitor();
355
				x.Accept(pid);
356
				x.type().Accept(ptype);
357

  
358
				final String key = x.type().kind() != TypeKind.QrType ? ptype.toString() : "qvar";
359
				if (varlist.containsKey(key))
360
				{
361
					varlist.get(key).append(String.format(", %s", pid.toString()));
362
				}
363
				else
364
				{
365
					varlist.putSafe(key, new StringBuilder(String.format("[%s", pid.toString())));
366
				}
367
			}
368
		}
369
		for (final String key: varlist.keySet())
370
			m_emitter.AppendLine(String.format("var(%s], %s).", varlist.get(key), key));
371
	}
372

  
373
	protected void ExportTypes(MainModule mainModule)
374
	{
375
		for (final Identifier x: mainModule.symbolTable().symbolList())
376
		{
377
			if (x.kind() == IdentifierKind.TypeIdentifier)
378
				OoaPrologType.EmitType(((TypeIdentifier)x).type(), m_idFactory, m_scratchbook);
379
		}
380
	}
381

  
382

  
383
	@Override
384
	public /*override*/ void visit(MainModule mainModule)
385
	{
386
		m_emitter.AppendLine("% OOAS Compiler Generated");
387
//		m_emitter.AppendLine(String.format("%%%s", Mogentes.Program.GetProgramVersion().toString().Replace(Environment.NewLine, Environment.NewLine + "%")));
388
		m_emitter.AppendLine("% Code-Gen Version: 0.2");
389
		m_emitter.Append("%        Generated: "); m_emitter.AppendLine(new Date().toString());
390
		m_emitter.AppendLine("%------------------------------------------------------------------------");
391
		m_emitter.AppendLine("");
392
		m_emitter.AppendLine(String.format(":- module(%s, [var/2, input/1, searchDepth/1, qspace/2]).", m_nameSpace));
393
		m_emitter.AppendLine(":- use_module(library(file_systems)).");
394
		m_emitter.AppendLine(":- use_module(library(process)).");
395
		m_emitter.AppendLine(":- use_module(library(clpfd)).");
396
		m_emitter.AppendLine(":- use_module(library(clpb)).");
397
		m_emitter.AppendLine(":- use_module(library(lists)).");
398
		m_emitter.AppendLine(":- use_module(library(avl)).");
399
		m_emitter.AppendLine(":- use_module(library(ordsets)).");
400
		m_emitter.AppendLine(":- use_module(library(system)).");
401
		m_emitter.AppendLine("% :- use_module(library(gauge)). % this is a debug lib not supported on Win.");
402
		m_emitter.AppendLine(":- use_module(library(terms)).");
403
		m_emitter.AppendLine(":- use_module(library(sets)).");
404
		m_emitter.AppendLine(":- use_module(library(random)).");
405

  
406
		m_emitter.AppendLine(":- use_module(ulysseslib, _, [ulyssesListHead/2, ulyssesListLength/2, ulyssesListTail/2, ulyssesListConc/3, ulyssesTupleAccess/3, ulyssesListAccess/3, ulyssesListWrite/4, ulyssesMapAccess/3]).");
407

  
408
		m_emitter.AppendLine(":- public(as/0).");
409
		m_emitter.AppendLine(":- dynamic(as/0).");
410
		m_emitter.AppendLine(":- dynamic(qstate_init/1).");
411
		m_emitter.AppendLine(":- dynamic(qstate_constraints/1).");
412
		m_emitter.AppendLine(":- public(qstate_init/1).");
413
		m_emitter.AppendLine(":- public(qstate_constraints/1).");
414

  
415
		/*definition of search depth*/
416
		m_emitter.AppendLine("");
417
		m_emitter.AppendLine("% maximal search depth (change at will)");
418
		// FIXME: HACK
419
		m_emitter.AppendLine(String.format("searchDepth(%s).", m_maxSearchDepth));
420
		m_emitter.AppendLine("");
421

  
422
		/*definition of types*/
423
		ExportTypes(mainModule);
424
		/* variable definition */
425
		ExportVariables(mainModule);
426

  
427
		/* state definition */
428
		ExportState(mainModule);
429
		/* initial state */
430
		ExportInitialState(mainModule);
431
		/* input actions (controllables) */
432
		ExportControllableActions(mainModule);
433
		/* output actions (observables) */
434
		ExportObservableActions(mainModule);
435
		/* named actions */
436
		ExportMethodsAndActions(mainModule);
437

  
438
		/* all types */
439
		m_emitter.AppendLine("%definition of types");
440
		m_emitter.AppendLine(m_scratchbook.typeDefinitions().toString());
441
		m_emitter.AppendLine(createTypeVisitor().GetInternalTypeDefinitions());
442

  
443
		for(final String line: m_ParserState.mappingInformation)
444
			m_emitter.AppendLine("% " + line);
445
	}
446

  
447

  
448
	@Override
449
	public /*override*/ String toString()
450
	{
451
		return m_emitter.toString();
452
	}
453

  
454
	protected OoaPrologIdentifier createIdentifierVisitor() { return m_idFactory.create(); }
455
	protected OoaPrologType createTypeVisitor() { return m_typeFactory.create(m_idFactory, m_scratchbook); }
456
	protected OoaPrologExpression createExpressionVisitor() { return m_exprFactory.create(m_idFactory, m_typeFactory, m_scratchbook); }
457
	protected OoaPrologStatement createStatementVisitor() { return m_stmtFactory.create(m_exprFactory, m_idFactory, m_typeFactory, m_scratchbook); }
458
	protected final String m_nameSpace;
459
	protected final int m_maxSearchDepth;
460

  
461

  
462
	private final  OoaPrologExpression.Factory m_exprFactory;
463
	private final OoaPrologIdentifier.Factory m_idFactory;
464
	private final OoaPrologStatement.Factory m_stmtFactory;
465
	private final OoaPrologType.Factory m_typeFactory;
466

  
467
	protected OoaPrologVisitor(
468
			ParserState aState,
469
			int maxSearchDepth,
470
			String nameSpace,
471
			OoaPrologExpression.Factory exprFactory,
472
			OoaPrologIdentifier.Factory idFactory,
473
			OoaPrologStatement.Factory  stmtFactory,
474
			OoaPrologType.Factory       tpeFactory)
475
	{
476
		super(aState);
477
		m_maxSearchDepth = maxSearchDepth;
478
		m_nameSpace = nameSpace;
479
		m_emitter = new OoasCodeEmitter();
480
		m_exprFactory = exprFactory;
481
		m_idFactory = idFactory;
482
		m_stmtFactory = stmtFactory;
483
		m_typeFactory = tpeFactory;
484
	}
485

  
486
	public OoaPrologVisitor(ParserState aState, int maxSearchDepth, String nameSpace) {
487
		this(
488
				aState,
489
				maxSearchDepth,
490
				nameSpace,
491
				new OoaPrologExpression.Factory(),
492
				new OoaPrologIdentifier.Factory(),
493
				new OoaPrologStatement.Factory(),
494
				new OoaPrologType.Factory());
495
	}
496
}
49

  
50
public class OoaPrologVisitor extends OoaCompleteAstTraversalVisitor
51
{
52
	protected OoasCodeEmitter m_emitter;
53
	protected final Scratchbook m_scratchbook = new Scratchbook();
54

  
55
//	private String PrologIdentifier(Identifier id)
56
//	{
57
//		OoaPrologIdentifier identifierVisitor = m_idFactory.create();
58
//		id.Accept(identifierVisitor);
59
//		return identifierVisitor.toString();
60
//	}
61

  
62
	protected void ExportMethodsAndActions(MainModule mainModule)
63
	{
64
		final StringBuilder varActions = new StringBuilder();
65
		m_emitter.AppendLine();
66
		m_emitter.AppendLine("% action system");
67
		m_emitter.AppendLineIncIndent("as :- ");
68

  
69
		/* export methods */
70
		m_emitter.AppendLine("methods (");
71
		ExportMethods(mainModule);
72
		m_emitter.AppendLine("), ");
73
		m_emitter.AppendLine("actions (");
74
		ExportNamedActions(mainModule, varActions);
75
		m_emitter.AppendLine("), ");
76
		m_emitter.AppendLine("dood (");
77

  
78
		ExportDoOdBlock(mainModule.instance().doOdBlock(), m_emitter);
79

  
80

  
81
		m_emitter.AppendLine("), qdes (none)");
82
		m_emitter.DecIndent();
83
		m_emitter.AppendLine(".");
84
		m_emitter.AppendLine("");
85

  
86
		m_emitter.Append(varActions.toString());
87
	}
88

  
89
	private void ExportMethods(MainModule mainModule)
90
	{
91
		int y = 0;
92
		for (final Identifier x: mainModule.instance().symbols().symbolList())
93
		{
94
			if (x.kind() == IdentifierKind.MethodIdentifier)
95
			{
96
				if (y != 0)
97
					m_emitter.AppendLine(", ");
98
				else
99
					y++;
100

  
101
				final MethodIdentifier theMethod = (MethodIdentifier)x;
102
				final OoaPrologIdentifier pIdent = m_idFactory.create();
103
				theMethod.Accept(pIdent);
104

  
105
				/* method */
106
				final StringBuilder parameter = new StringBuilder();
107
				if (theMethod.parameter().size() > 0 || ((FunctionType)theMethod.type()).returnType() != null)
108
				{
109
					parameter.append("(");
110
					int z = 0;
111
					for (final ParameterIdentifier param: theMethod.parameter())
112
					{
113
						if (z != 0)
114
						{
115
							parameter.append(", ");
116
						}
117
						else
118
							z++;
119
						final OoaPrologIdentifier pParamIdent = m_idFactory.create();
120
						param.Accept(pParamIdent);
121
						parameter.append(pParamIdent.toString());
122
					}
123

  
124

  
125
					if (((FunctionType)theMethod.type()).returnType() != null)
126
					{
127
						if (z != 0)
128
							parameter.append(", ");
129
						parameter.append("RESULT");
130
					}
131
					parameter.append(")");
132
				}
133
				m_emitter.AppendLineIncIndent(String.format("%s%s = (", pIdent.toString(), parameter.toString()));
134

  
135
				final OoaPrologStatement pBody = createStatementVisitor();
136
				((MethodIdentifier)x).body().Accept(pBody);
137
				m_emitter.Append(pBody.toString());
138
				m_emitter.Append(")");
139
				m_emitter.DecIndent();
140
			}
141
		}
142
		if (y == 0)
143
			m_emitter.Append(" none ");
144
	}
145

  
146
	private void ExportNamedActions(MainModule mainModule, StringBuilder varActions)
147
	{
148
		int y = 0;
149
		for (final Identifier x: mainModule.instance().symbols().symbolList())
150
		{
151
			if (x.kind() == IdentifierKind.NamedActionIdentifier)
152
			{
153
				if (y != 0)
154
					m_emitter.AppendLine(", ");
155
				else
156
					y++;
157
				final NamedActionIdentifier action = (NamedActionIdentifier)x;
158
				final OoaPrologIdentifier pIdent = m_idFactory.create();
159
				action.Accept(pIdent);
160

  
161
				if (((FunctionType)action.type()).functionType() != FunctionTypeEnum.Continuous)
162
				{
163
					/* discrete actions */
164
					final StringBuilder parameter = new StringBuilder();
165
					final StringBuilder parametertype = new StringBuilder();
166
					if (action.parameter().size() > 0)
167
					{
168
						parameter.append("(");
169
						parametertype.append("(");
170
						int z = 0;
171
						for (final ParameterIdentifier param: action.parameter())
172
						{
173
							if (z != 0)
174
							{
175
								parameter.append(", ");
176
								parametertype.append(", ");
177
							}
178
							else
179
								z++;
180
							final OoaPrologIdentifier pParamIdent = m_idFactory.create();
181
							param.Accept(pParamIdent);
182
							parameter.append(pParamIdent.toString());
183

  
184
							final OoaPrologType pParamType = createTypeVisitor();  // bugfix me!
185
							param.type().Accept(pParamType);
186
							parametertype.append(pParamType.toString());
187

  
188
						}
189
						parametertype.append(")");
190
						parameter.append(")");
191
					}
192
					//if (parametertype.Length > 0)
193
					//    varActions.AppendLine(String.format("%s%s.", pIdent.toString(), parametertype.toString()));
194
					m_emitter.AppendLineIncIndent(String.format("%s%s::", pIdent.toString(), parameter.toString()));
195
				}
196
				else
197
				{
198
					// we do not support continuous actions anymore.
199
					throw new UnsupportedOperationException();
200
//					/* observable state vars outside... */
201
//					final StringBuilder parameter = new StringBuilder();
202
//					int z = 0;
203
//					for (final Statement stmt : ((GuardedCommand)action.body().statements().peekFirst()).body().statements())
204
//					{
205
//						final QualitativeConstraintStatement qstmt = (QualitativeConstraintStatement)stmt;
206
//						if (qstmt.operation() == QualitativeConstraintOperation.Equal
207
//								&& qstmt.variable1().kind() == IdentifierKind.AttributeIdentifier
208
//								&& ((AttributeIdentifier)qstmt.variable1()).isObservable())
209
//						{
210
//							if (z != 0)
211
//								parameter.append(", ");
212
//							else
213
//								z++;
214
//							qstmt.tag = true;
215
//							final OoaPrologIdentifier pParamIdent1 = m_idFactory.create();
216
//							final OoaPrologIdentifier pParamIdent2 = m_idFactory.create();
217
//							qstmt.variable0().Accept(pParamIdent1);
218
//							qstmt.variable1().Accept(pParamIdent2);
219
//							parameter.append(String.format("%s(%s)", pParamIdent2.toString(), pParamIdent1.toString()));
220
//						}
221
//					}
222
//					m_emitter.AppendLineIncIndent(String.format("[%s]::", parameter.toString()));
223
				}
224

  
225
				final OoaPrologStatement pBody = createStatementVisitor();
226
				((NamedActionIdentifier)x).body().Accept(pBody);
227
				m_emitter.Append(pBody.toString());
228
				m_emitter.DecIndent();
229

  
230
			}
231
		}
232
	}
233

  
234
	private void ExportDoOdBlock(Block block, OoasCodeEmitter m_emitter)
235
	{
236
		final OoaPrologStatement statement = createStatementVisitor();
237
		block.Accept(statement);
238
		m_emitter.Append(statement.toString());
239
	}
240

  
241
	protected int ExportActions(MainModule mainModule, String predName, FunctionTypeEnum actionKind)
242
	{
243
		m_emitter.AppendLine("");
244
		m_emitter.AppendLine("%emit " + actionKind.toString() + " actions");
245
		m_emitter.Append(predName + "([");
246
		int y = 0;
247
		for (final Identifier x: mainModule.instance().symbols().symbolList())
248
		{
249
			if (x.kind() == IdentifierKind.NamedActionIdentifier)
250
			{
251
				if (((FunctionType)((NamedActionIdentifier)x).type()).functionType() == actionKind)
252
				{
253
					if (y != 0)
254
						m_emitter.Append(", ");
255
					else
256
						y++;
257
					final OoaPrologIdentifier pIdent = m_idFactory.create();
258

  
259
					((NamedActionIdentifier)x).Accept(pIdent);
260
					m_emitter.Append(pIdent.toString());
261
				}
262
			}
263
		}
264
		m_emitter.AppendLine("]).");
265
		return y;
266
	}
267

  
268
	protected int ExportControllableActions(MainModule mainModule) {
269
		return ExportActions(mainModule, "input", FunctionTypeEnum.Controllable);
270
	}
271

  
272
	protected int ExportObservableActions(MainModule mainModule) {
273
		return ExportActions(mainModule, "output", FunctionTypeEnum.Observable);
274
	}
275

  
276
	protected void ExportInitialState(MainModule mainModule)
277
	{
278
		m_emitter.AppendLine("");
279
		m_emitter.AppendLine("%emit initial state");
280
		final ArrayList<String> initPreds = new ArrayList<String>();
281
		final ArrayList<String> initVars = new ArrayList<String>();
282
		int y = 0;
283
		for (final Identifier x: mainModule.instance().symbols().symbolList())
284
		{
285
			if (x.kind() == IdentifierKind.AttributeIdentifier)
286
			{
287
				final OoaPrologExpression anExprVis = createExpressionVisitor();
288
				((AttributeIdentifier)x).initializer().Accept(anExprVis);
289
				assert(anExprVis.tmpVariables().size() == 1);
290
				final String initPredName = String.format("attr_%s_init(%s)", x.tokenText(), anExprVis.tmpVariables().get(0));
291
				m_emitter.AppendLine(String.format("%s :- %s 1=1.", initPredName, anExprVis.toString()));
292
				initPreds.add(initPredName);
293
				initVars.add(anExprVis.tmpVariables().get(0));
294
			}
295
		}
296

  
297

  
298
		m_emitter.Append("init([");
299
		y = 0;
300
		for (final String x: initVars)
301
		{
302
			if (y != 0)
303
				m_emitter.Append(",");
304
			else
305
				y++;
306
			m_emitter.Append(x);
307
		}
308
		m_emitter.Append("]) :-");
309
		y = 0;
310
		for (final String x: initPreds)
311
		{
312
			if (y != 0)
313
				m_emitter.Append(",");
314
			else
315
				y++;
316
			m_emitter.Append(x);
317
		}
318
		m_emitter.AppendLine(".");
319
	}
320

  
321
	protected int ExportState(MainModule mainModule)
322
	{
323
		m_emitter.AppendLine("");
324
		m_emitter.AppendLine("%emit state definition");
325
		m_emitter.Append("state_def([");
326
		int y = 0;
327
		for (final Identifier x: mainModule.instance().symbols().symbolList())
328
		{
329
			if (x.kind() == IdentifierKind.AttributeIdentifier)
330
			{
331
				if (y != 0)
332
					m_emitter.Append(", ");
333
				else
334
					y++;
335
				final OoaPrologIdentifier pid = m_idFactory.create();
336
				x.Accept(pid);
337
				m_emitter.Append(pid.toString());
338
			}
339
		}
340
		m_emitter.AppendLine("]).");
341
		return y;
342
	}
343

  
344
	protected void ExportVariables(MainModule mainModule)
345
	{
346
		m_emitter.AppendLine("");
347
		m_emitter.AppendLine("%emit type of state variables");
348
		final OoasCompilerHashMap<String, StringBuilder> varlist = new OoasCompilerHashMap<String, StringBuilder>();
349
		for (final Identifier x: mainModule.instance().symbols().symbolList())
350
		{
351
			if (x.kind() == IdentifierKind.AttributeIdentifier)
352
			{
353
				final OoaPrologIdentifier pid = m_idFactory.create();
354
				final OoaPrologType ptype = createTypeVisitor();
355
				x.Accept(pid);
356
				x.type().Accept(ptype);
357

  
358
				final String key = x.type().kind() != TypeKind.QrType ? ptype.toString() : "qvar";
359
				if (varlist.containsKey(key))
360
				{
361
					varlist.get(key).append(String.format(", %s", pid.toString()));
362
				}
363
				else
364
				{
365
					varlist.putSafe(key, new StringBuilder(String.format("[%s", pid.toString())));
366
				}
367
			}
368
		}
369
		for (final String key: varlist.keySet())
370
			m_emitter.AppendLine(String.format("var(%s], %s).", varlist.get(key), key));
371
	}
372

  
373
	protected void ExportTypes(MainModule mainModule)
374
	{
375
		for (final Identifier x: mainModule.symbolTable().symbolList())
376
		{
377
			if (x.kind() == IdentifierKind.TypeIdentifier)
378
				OoaPrologType.EmitType(((TypeIdentifier)x).type(), m_idFactory, m_scratchbook);
379
		}
380
	}
381

  
382

  
383
	@Override
384
	public /*override*/ void visit(MainModule mainModule)
385
	{
386
		m_emitter.AppendLine("% OOAS Compiler Generated");
387
//		m_emitter.AppendLine(String.format("%%%s", Mogentes.Program.GetProgramVersion().toString().Replace(Environment.NewLine, Environment.NewLine + "%")));
388
		m_emitter.AppendLine("% Code-Gen Version: 0.2");
389
		m_emitter.Append("%        Generated: "); m_emitter.AppendLine(new Date().toString());
390
		m_emitter.AppendLine("%------------------------------------------------------------------------");
391
		m_emitter.AppendLine("");
392
		m_emitter.AppendLine(String.format(":- module(%s, [var/2, input/1, searchDepth/1, qspace/2]).", m_nameSpace));
393
		m_emitter.AppendLine(":- use_module(library(file_systems)).");
394
		m_emitter.AppendLine(":- use_module(library(process)).");
395
		m_emitter.AppendLine(":- use_module(library(clpfd)).");
396
		m_emitter.AppendLine(":- use_module(library(clpb)).");
397
		m_emitter.AppendLine(":- use_module(library(lists)).");
398
		m_emitter.AppendLine(":- use_module(library(avl)).");
399
		m_emitter.AppendLine(":- use_module(library(ordsets)).");
400
		m_emitter.AppendLine(":- use_module(library(system)).");
401
		m_emitter.AppendLine("% :- use_module(library(gauge)). % this is a debug lib not supported on Win.");
402
		m_emitter.AppendLine(":- use_module(library(terms)).");
403
		m_emitter.AppendLine(":- use_module(library(sets)).");
404
		m_emitter.AppendLine(":- use_module(library(random)).");
405

  
406
		m_emitter.AppendLine(":- use_module(ulysseslib, _, [ulyssesListHead/2, ulyssesListLength/2, ulyssesListTail/2, ulyssesListConc/3, ulyssesTupleAccess/3, ulyssesListAccess/3, ulyssesListWrite/4, ulyssesMapAccess/3]).");
407

  
408
		m_emitter.AppendLine(":- public(as/0).");
409
		m_emitter.AppendLine(":- dynamic(as/0).");
410
		m_emitter.AppendLine(":- dynamic(qstate_init/1).");
411
		m_emitter.AppendLine(":- dynamic(qstate_constraints/1).");
412
		m_emitter.AppendLine(":- public(qstate_init/1).");
413
		m_emitter.AppendLine(":- public(qstate_constraints/1).");
414

  
415
		/*definition of search depth*/
416
		m_emitter.AppendLine("");
417
		m_emitter.AppendLine("% maximal search depth (change at will)");
418
		// FIXME: HACK
419
		m_emitter.AppendLine(String.format("searchDepth(%s).", m_maxSearchDepth));
420
		m_emitter.AppendLine("");
421

  
422
		/*definition of types*/
423
		ExportTypes(mainModule);
424
		/* variable definition */
425
		ExportVariables(mainModule);
426

  
427
		/* state definition */
428
		ExportState(mainModule);
429
		/* initial state */
430
		ExportInitialState(mainModule);
431
		/* input actions (controllables) */
432
		ExportControllableActions(mainModule);
433
		/* output actions (observables) */
434
		ExportObservableActions(mainModule);
435
		/* named actions */
436
		ExportMethodsAndActions(mainModule);
437

  
438
		/* all types */
439
		m_emitter.AppendLine("%definition of types");
440
		m_emitter.AppendLine(m_scratchbook.typeDefinitions().toString());
441
		m_emitter.AppendLine(createTypeVisitor().GetInternalTypeDefinitions());
442

  
443
		for(final String line: m_ParserState.mappingInformation)
444
			m_emitter.AppendLine("% " + line);
445
	}
446

  
447

  
448
	@Override
449
	public /*override*/ String toString()
450
	{
451
		return m_emitter.toString();
452
	}
453

  
454
	protected OoaPrologIdentifier createIdentifierVisitor() { return m_idFactory.create(); }
455
	protected OoaPrologType createTypeVisitor() { return m_typeFactory.create(m_idFactory, m_scratchbook); }
456
	protected OoaPrologExpression createExpressionVisitor() { return m_exprFactory.create(m_idFactory, m_typeFactory, m_scratchbook); }
457
	protected OoaPrologStatement createStatementVisitor() { return m_stmtFactory.create(m_exprFactory, m_idFactory, m_typeFactory, m_scratchbook); }
458
	protected final String m_nameSpace;
459
	protected final int m_maxSearchDepth;
460

  
461

  
462
	private final  OoaPrologExpression.Factory m_exprFactory;
463
	private final OoaPrologIdentifier.Factory m_idFactory;
464
	private final OoaPrologStatement.Factory m_stmtFactory;
465
	private final OoaPrologType.Factory m_typeFactory;
466

  
467
	protected OoaPrologVisitor(
468
			ParserState aState,
469
			int maxSearchDepth,
470
			String nameSpace,
471
			OoaPrologExpression.Factory exprFactory,
472
			OoaPrologIdentifier.Factory idFactory,
473
			OoaPrologStatement.Factory  stmtFactory,
474
			OoaPrologType.Factory       tpeFactory)
475
	{
476
		super(aState);
477
		m_maxSearchDepth = maxSearchDepth;
478
		m_nameSpace = nameSpace;
479
		m_emitter = new OoasCodeEmitter();
480
		m_exprFactory = exprFactory;
481
		m_idFactory = idFactory;
482
		m_stmtFactory = stmtFactory;
483
		m_typeFactory = tpeFactory;
484
	}
485

  
486
	public OoaPrologVisitor(ParserState aState, int maxSearchDepth, String nameSpace) {
487
		this(
488
				aState,
489
				maxSearchDepth,
490
				nameSpace,
491
				new OoaPrologExpression.Factory(),
492
				new OoaPrologIdentifier.Factory(),
493
				new OoaPrologStatement.Factory(),
494
				new OoaPrologType.Factory());
495
	}
496
}

Also available in: Unified diff