Revision 6
Added by Willibald K. over 8 years ago
OoaPrintActionsVisitor.java | ||
---|---|---|
28 | 28 |
package org.momut.ooas.visitors.analysis; |
29 | 29 |
|
30 | 30 |
import java.util.ArrayList; |
31 |
import java.util.Collections; |
|
31 | 32 |
import java.util.HashSet; |
32 |
|
|
33 |
import org.momut.ooas.ast.expressions.CallExpression; |
|
34 |
import org.momut.ooas.ast.identifiers.Identifier; |
|
35 |
import org.momut.ooas.ast.identifiers.IdentifierKind; |
|
36 |
import org.momut.ooas.ast.identifiers.MainModule; |
|
37 |
import org.momut.ooas.ast.identifiers.NamedActionIdentifier; |
|
38 |
import org.momut.ooas.ast.identifiers.ParameterIdentifier; |
|
39 |
import org.momut.ooas.ast.statements.Call; |
|
40 |
import org.momut.ooas.ast.types.FunctionType; |
|
41 |
import org.momut.ooas.codegen.prolog.OoaPrologIdentifier; |
|
42 |
import org.momut.ooas.parser.ParserMessage; |
|
43 |
import org.momut.ooas.parser.ParserState; |
|
44 |
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor; |
|
45 | 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 |
|
|
46 | 51 |
public class OoaPrintActionsVisitor extends OoaCompleteAstTraversalVisitor { |
47 | 52 |
|
48 | 53 |
private final ArrayList<String> m_result = new ArrayList<>(); |
... | ... | |
110 | 115 |
final StringBuilder b = new StringBuilder(); |
111 | 116 |
b.append("List of controllable/observable actions used:"); |
112 | 117 |
b.append(System.lineSeparator()); |
118 |
Collections.sort(m_result); |
|
119 |
int i = 0; |
|
113 | 120 |
for (final String s: m_result) { |
114 |
b.append(String.format("\t%s",s)); |
|
121 |
i ++; |
|
122 |
b.append(String.format("\t%s:\t%s",i,s)); |
|
115 | 123 |
b.append(System.lineSeparator()); |
116 | 124 |
} |
125 |
|
|
117 | 126 |
m_ParserState.AddMessage(new ParserMessage("",0,0, b.toString())); |
118 | 127 |
} |
119 | 128 |
|
120 | 129 |
HashSet<String> m_seenActions = new HashSet<String>(); |
121 | 130 |
|
122 | 131 |
@Override |
123 |
public void visit(Call call) { |
|
124 |
final CallExpression e = (CallExpression) call.callExpression(); |
|
125 |
// for (final FunctionIdentifier id : call.callExpression().callTargets()) { |
|
126 |
final FunctionType actionType = (FunctionType) e.child().type(); |
|
127 |
String ctrObs = ""; |
|
128 |
switch(actionType.functionType()) { |
|
129 |
default: |
|
130 |
// something we do not know... |
|
131 |
throw new UnsupportedOperationException(); |
|
132 |
case Continuous: |
|
133 |
// we do not support continuous actions here |
|
134 |
throw new UnsupportedOperationException(); |
|
135 |
case Internal: |
|
136 |
case Method: |
|
137 |
// we do not print methods or internal actions |
|
138 |
//continue; |
|
139 |
return; |
|
140 |
case Observable: |
|
141 |
// yes, report it |
|
142 |
ctrObs = "obs"; |
|
143 |
break; |
|
144 |
case Controllable: |
|
145 |
// yes, report it |
|
146 |
ctrObs = "ctr"; |
|
147 |
break; |
|
148 |
} |
|
149 |
final OoaPrologIdentifier pIdent = m_idFactory.create(); |
|
150 |
e.child().Accept(pIdent); |
|
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); |
|
151 | 159 |
|
152 |
final String actionString = String.format("%s %s", ctrObs, pIdent.toString().replaceAll("'", "")); |
|
153 |
if (!m_seenActions.contains(actionString)) { |
|
154 |
m_seenActions.add(actionString); |
|
155 |
m_result.add(actionString); |
|
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("]"); |
|
156 | 182 |
} |
157 |
// } |
|
158 |
} |
|
183 |
} |
|
159 | 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 |
|
|
160 | 193 |
@Override |
161 | 194 |
public String toString() { |
162 | 195 |
return m_result.toString(); |
Also available in: Unified diff
adding methods to query types for their number of elements, fixing an issue with the ActionPrinter