Revision 7
Added by Willibald K. over 8 years ago
OoaPrintActionsVisitor.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.visitors.analysis; |
|
29 |
|
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.Collections; |
|
32 |
import java.util.HashSet; |
|
33 |
|
|
34 |
import org.momut.ooas.ast.expressions.CallExpression; |
|
35 |
import org.momut.ooas.ast.expressions.Expression; |
|
36 |
import org.momut.ooas.ast.expressions.ExpressionKind; |
|
37 |
import org.momut.ooas.ast.expressions.UnaryOperator; |
|
38 |
import org.momut.ooas.ast.identifiers.Identifier; |
|
39 |
import org.momut.ooas.ast.identifiers.IdentifierKind; |
|
40 |
import org.momut.ooas.ast.identifiers.MainModule; |
|
41 |
import org.momut.ooas.ast.identifiers.NamedActionIdentifier; |
|
42 |
import org.momut.ooas.ast.identifiers.ParameterIdentifier; |
|
43 |
import org.momut.ooas.ast.types.FunctionType; |
|
44 |
import org.momut.ooas.ast.types.UlyssesType; |
|
45 |
import org.momut.ooas.codegen.prolog.OoaPrologIdentifier; |
|
46 |
import org.momut.ooas.parser.ParserMessage; |
|
47 |
import org.momut.ooas.parser.ParserState; |
|
48 |
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor; |
|
49 |
|
|
50 |
|
|
51 |
public class OoaPrintActionsVisitor extends OoaCompleteAstTraversalVisitor { |
|
52 |
|
|
53 |
private final ArrayList<String> m_result = new ArrayList<>(); |
|
54 |
private final OoaPrologIdentifier.Factory m_idFactory; |
|
55 |
|
|
56 |
|
|
57 |
@SuppressWarnings("unused") |
|
58 |
private void printNamedActions(MainModule mainModule) |
|
59 |
{ |
|
60 |
for (final Identifier x: mainModule.instance().symbols().symbolList()) |
|
61 |
{ |
|
62 |
if (x.kind() != IdentifierKind.NamedActionIdentifier) |
|
63 |
continue; |
|
64 |
|
|
65 |
final NamedActionIdentifier action = (NamedActionIdentifier)x; |
|
66 |
final FunctionType actionType = (FunctionType) action.type(); |
|
67 |
String ctrObs = ""; |
|
68 |
switch(actionType.functionType()) { |
|
69 |
default: |
|
70 |
// something we do not know... |
|
71 |
throw new UnsupportedOperationException(); |
|
72 |
case Continuous: |
|
73 |
// we do not support continuous actions here |
|
74 |
throw new UnsupportedOperationException(); |
|
75 |
case Internal: |
|
76 |
case Method: |
|
77 |
// we do not print methods or internal actions |
|
78 |
continue; |
|
79 |
case Observable: |
|
80 |
// yes, report it |
|
81 |
ctrObs = "obs"; |
|
82 |
break; |
|
83 |
case Controllable: |
|
84 |
// yes, report it |
|
85 |
ctrObs = "ctr"; |
|
86 |
break; |
|
87 |
} |
|
88 |
|
|
89 |
final OoaPrologIdentifier pIdent = m_idFactory.create(); |
|
90 |
action.Accept(pIdent); |
|
91 |
|
|
92 |
final StringBuilder parameter = new StringBuilder(); |
|
93 |
if (action.parameter().size() > 0) { |
|
94 |
parameter.append("("); |
|
95 |
int z = 0; |
|
96 |
for (final ParameterIdentifier param: action.parameter()) { |
|
97 |
if (z != 0) { |
|
98 |
parameter.append(", "); |
|
99 |
} else |
|
100 |
z++; |
|
101 |
final OoaPrologIdentifier pParamIdent = m_idFactory.create(); |
|
102 |
param.Accept(pParamIdent); |
|
103 |
parameter.append(pParamIdent.toString()); |
|
104 |
} |
|
105 |
parameter.append(")"); |
|
106 |
} |
|
107 |
m_result.add(String.format("%s %s%s", ctrObs, pIdent.toString().replaceAll("'", ""), parameter.toString())); |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
@Override |
|
112 |
public void visit(MainModule mainModule) { |
|
113 |
//printNamedActions(mainModule); |
|
114 |
mainModule.instance().doOdBlock().Accept(this); |
|
115 |
final StringBuilder b = new StringBuilder(); |
|
116 |
b.append("List of controllable/observable actions used:"); |
|
117 |
b.append(System.lineSeparator()); |
|
118 |
Collections.sort(m_result); |
|
119 |
int i = 0; |
|
120 |
for (final String s: m_result) { |
|
121 |
i ++; |
|
122 |
b.append(String.format("\t%s:\t%s",i,s)); |
|
123 |
b.append(System.lineSeparator()); |
|
124 |
} |
|
125 |
|
|
126 |
m_ParserState.AddMessage(new ParserMessage("",0,0, b.toString())); |
|
127 |
} |
|
128 |
|
|
129 |
HashSet<String> m_seenActions = new HashSet<String>(); |
|
130 |
|
|
131 |
@Override |
|
132 |
public void visit(CallExpression e) |
|
133 |
{ |
|
134 |
final FunctionType actionType = (FunctionType) e.child().type(); |
|
135 |
String ctrObs = ""; |
|
136 |
switch(actionType.functionType()) { |
|
137 |
default: |
|
138 |
// something we do not know... |
|
139 |
throw new UnsupportedOperationException(); |
|
140 |
case Continuous: |
|
141 |
// we do not support continuous actions here |
|
142 |
throw new UnsupportedOperationException(); |
|
143 |
case Internal: |
|
144 |
case Method: |
|
145 |
// we do not print methods or internal actions |
|
146 |
//continue; |
|
147 |
return; |
|
148 |
case Observable: |
|
149 |
// yes, report it |
|
150 |
ctrObs = "obs"; |
|
151 |
break; |
|
152 |
case Controllable: |
|
153 |
// yes, report it |
|
154 |
ctrObs = "ctr"; |
|
155 |
break; |
|
156 |
} |
|
157 |
final OoaPrologIdentifier pIdent = m_idFactory.create(); |
|
158 |
e.child().Accept(pIdent); |
|
159 |
|
|
160 |
final StringBuilder parameter = new StringBuilder(); |
|
161 |
if (e.arguments().size() > 0) { |
|
162 |
int z = 0; |
|
163 |
for (final UlyssesType ut: actionType.parameter()) { |
|
164 |
if (e.arguments().size() <= z) |
|
165 |
continue; |
|
166 |
Expression arg = e.arguments().get(z); |
|
167 |
while (arg.kind() == ExpressionKind.Cast) |
|
168 |
arg = ((UnaryOperator)arg).child(); |
|
169 |
|
|
170 |
if (z != 0) { |
|
171 |
parameter.append(", "); |
|
172 |
} else |
|
173 |
z++; |
|
174 |
parameter.append(ut.toString()); |
|
175 |
parameter.append(" #: "); |
|
176 |
parameter.append( Integer.toString(ut.valueCount()) ); |
|
177 |
parameter.append(" [call-type: "); |
|
178 |
parameter.append(arg.type().toString()); |
|
179 |
parameter.append(" #: "); |
|
180 |
parameter.append( Integer.toString(arg.type().valueCount()) ); |
|
181 |
parameter.append("]"); |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
final String actionString = String.format("%s %s(%s)", ctrObs, pIdent.toString().replaceAll("'", ""), parameter.toString()); |
|
186 |
if (!m_seenActions.contains(actionString)) { |
|
187 |
m_seenActions.add(actionString); |
|
188 |
m_result.add(actionString); |
|
189 |
} |
|
190 |
}; |
|
191 |
|
|
192 |
|
|
193 |
@Override |
|
194 |
public String toString() { |
|
195 |
return m_result.toString(); |
|
196 |
} |
|
197 |
|
|
198 |
public OoaPrintActionsVisitor(ParserState aState) { |
|
199 |
super(aState); |
|
200 |
m_idFactory = new OoaPrologIdentifier.Factory(); |
|
201 |
} |
|
202 |
} |
|
28 |
package org.momut.ooas.visitors.analysis; |
|
29 |
|
|
30 |
import java.util.ArrayList; |
|
31 |
import java.util.Collections; |
|
32 |
import java.util.HashSet; |
|
33 |
|
|
34 |
import org.momut.ooas.ast.expressions.CallExpression; |
|
35 |
import org.momut.ooas.ast.expressions.Expression; |
|
36 |
import org.momut.ooas.ast.expressions.ExpressionKind; |
|
37 |
import org.momut.ooas.ast.expressions.UnaryOperator; |
|
38 |
import org.momut.ooas.ast.identifiers.Identifier; |
|
39 |
import org.momut.ooas.ast.identifiers.IdentifierKind; |
|
40 |
import org.momut.ooas.ast.identifiers.MainModule; |
|
41 |
import org.momut.ooas.ast.identifiers.NamedActionIdentifier; |
|
42 |
import org.momut.ooas.ast.identifiers.ParameterIdentifier; |
|
43 |
import org.momut.ooas.ast.types.FunctionType; |
|
44 |
import org.momut.ooas.ast.types.UlyssesType; |
|
45 |
import org.momut.ooas.codegen.prolog.OoaPrologIdentifier; |
|
46 |
import org.momut.ooas.parser.ParserMessage; |
|
47 |
import org.momut.ooas.parser.ParserState; |
|
48 |
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor; |
|
49 |
|
|
50 |
|
|
51 |
public class OoaPrintActionsVisitor extends OoaCompleteAstTraversalVisitor { |
|
52 |
|
|
53 |
private final ArrayList<String> m_result = new ArrayList<>(); |
|
54 |
private final OoaPrologIdentifier.Factory m_idFactory; |
|
55 |
|
|
56 |
|
|
57 |
@SuppressWarnings("unused") |
|
58 |
private void printNamedActions(MainModule mainModule) |
|
59 |
{ |
|
60 |
for (final Identifier x: mainModule.instance().symbols().symbolList()) |
|
61 |
{ |
|
62 |
if (x.kind() != IdentifierKind.NamedActionIdentifier) |
|
63 |
continue; |
|
64 |
|
|
65 |
final NamedActionIdentifier action = (NamedActionIdentifier)x; |
|
66 |
final FunctionType actionType = (FunctionType) action.type(); |
|
67 |
String ctrObs = ""; |
|
68 |
switch(actionType.functionType()) { |
|
69 |
default: |
|
70 |
// something we do not know... |
|
71 |
throw new UnsupportedOperationException(); |
|
72 |
case Continuous: |
|
73 |
// we do not support continuous actions here |
|
74 |
throw new UnsupportedOperationException(); |
|
75 |
case Internal: |
|
76 |
case Method: |
|
77 |
// we do not print methods or internal actions |
|
78 |
continue; |
|
79 |
case Observable: |
|
80 |
// yes, report it |
|
81 |
ctrObs = "obs"; |
|
82 |
break; |
|
83 |
case Controllable: |
|
84 |
// yes, report it |
|
85 |
ctrObs = "ctr"; |
|
86 |
break; |
|
87 |
} |
|
88 |
|
|
89 |
final OoaPrologIdentifier pIdent = m_idFactory.create(); |
|
90 |
action.Accept(pIdent); |
|
91 |
|
|
92 |
final StringBuilder parameter = new StringBuilder(); |
|
93 |
if (action.parameter().size() > 0) { |
|
94 |
parameter.append("("); |
|
95 |
int z = 0; |
|
96 |
for (final ParameterIdentifier param: action.parameter()) { |
|
97 |
if (z != 0) { |
|
98 |
parameter.append(", "); |
|
99 |
} else |
|
100 |
z++; |
|
101 |
final OoaPrologIdentifier pParamIdent = m_idFactory.create(); |
|
102 |
param.Accept(pParamIdent); |
|
103 |
parameter.append(pParamIdent.toString()); |
|
104 |
} |
|
105 |
parameter.append(")"); |
|
106 |
} |
|
107 |
m_result.add(String.format("%s %s%s", ctrObs, pIdent.toString().replaceAll("'", ""), parameter.toString())); |
|
108 |
} |
|
109 |
} |
|
110 |
|
|
111 |
@Override |
|
112 |
public void visit(MainModule mainModule) { |
|
113 |
//printNamedActions(mainModule); |
|
114 |
mainModule.instance().doOdBlock().Accept(this); |
|
115 |
final StringBuilder b = new StringBuilder(); |
|
116 |
b.append("List of controllable/observable actions used:"); |
|
117 |
b.append(System.lineSeparator()); |
|
118 |
Collections.sort(m_result); |
|
119 |
int i = 0; |
|
120 |
for (final String s: m_result) { |
|
121 |
i ++; |
|
122 |
b.append(String.format("\t%s:\t%s",i,s)); |
|
123 |
b.append(System.lineSeparator()); |
|
124 |
} |
|
125 |
|
|
126 |
m_ParserState.AddMessage(new ParserMessage("",0,0, b.toString())); |
|
127 |
} |
|
128 |
|
|
129 |
HashSet<String> m_seenActions = new HashSet<String>(); |
|
130 |
|
|
131 |
@Override |
|
132 |
public void visit(CallExpression e) |
|
133 |
{ |
|
134 |
final FunctionType actionType = (FunctionType) e.child().type(); |
|
135 |
String ctrObs = ""; |
|
136 |
switch(actionType.functionType()) { |
|
137 |
default: |
|
138 |
// something we do not know... |
|
139 |
throw new UnsupportedOperationException(); |
|
140 |
case Continuous: |
|
141 |
// we do not support continuous actions here |
|
142 |
throw new UnsupportedOperationException(); |
|
143 |
case Internal: |
|
144 |
case Method: |
|
145 |
// we do not print methods or internal actions |
|
146 |
//continue; |
|
147 |
return; |
|
148 |
case Observable: |
|
149 |
// yes, report it |
|
150 |
ctrObs = "obs"; |
|
151 |
break; |
|
152 |
case Controllable: |
|
153 |
// yes, report it |
|
154 |
ctrObs = "ctr"; |
|
155 |
break; |
|
156 |
} |
|
157 |
final OoaPrologIdentifier pIdent = m_idFactory.create(); |
|
158 |
e.child().Accept(pIdent); |
|
159 |
|
|
160 |
final StringBuilder parameter = new StringBuilder(); |
|
161 |
if (e.arguments().size() > 0) { |
|
162 |
int z = 0; |
|
163 |
for (final UlyssesType ut: actionType.parameter()) { |
|
164 |
if (e.arguments().size() <= z) |
|
165 |
continue; |
|
166 |
Expression arg = e.arguments().get(z); |
|
167 |
while (arg.kind() == ExpressionKind.Cast) |
|
168 |
arg = ((UnaryOperator)arg).child(); |
|
169 |
|
|
170 |
if (z != 0) { |
|
171 |
parameter.append(", "); |
|
172 |
} else |
|
173 |
z++; |
|
174 |
parameter.append(ut.toString()); |
|
175 |
parameter.append(" #: "); |
|
176 |
parameter.append( Integer.toString(ut.valueCount()) ); |
|
177 |
parameter.append(" [call-type: "); |
|
178 |
parameter.append(arg.type().toString()); |
|
179 |
parameter.append(" #: "); |
|
180 |
parameter.append( Integer.toString(arg.type().valueCount()) ); |
|
181 |
parameter.append("]"); |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
185 |
final String actionString = String.format("%s %s(%s)", ctrObs, pIdent.toString().replaceAll("'", ""), parameter.toString()); |
|
186 |
if (!m_seenActions.contains(actionString)) { |
|
187 |
m_seenActions.add(actionString); |
|
188 |
m_result.add(actionString); |
|
189 |
} |
|
190 |
}; |
|
191 |
|
|
192 |
|
|
193 |
@Override |
|
194 |
public String toString() { |
|
195 |
return m_result.toString(); |
|
196 |
} |
|
197 |
|
|
198 |
public OoaPrintActionsVisitor(ParserState aState) { |
|
199 |
super(aState); |
|
200 |
m_idFactory = new OoaPrologIdentifier.Factory(); |
|
201 |
} |
|
202 |
} |
Also available in: Unified diff
changing java, cpp, hpp files to unix line endings