root / trunk / compiler / ooasCompiler / src / org / momut / ooas / visitors / optimisation / OoaRemoveTrivialNondetChoiceCompositionVisitor.java @ 9
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 |
|
27 |
|
28 |
package org.momut.ooas.visitors.optimisation; |
29 |
|
30 |
import org.momut.ooas.ast.AstNodeTypeEnum; |
31 |
import org.momut.ooas.ast.IAst; |
32 |
import org.momut.ooas.ast.identifiers.Identifier; |
33 |
import org.momut.ooas.ast.identifiers.IdentifierKind; |
34 |
import org.momut.ooas.ast.identifiers.MethodIdentifier; |
35 |
import org.momut.ooas.ast.identifiers.NamedActionIdentifier; |
36 |
import org.momut.ooas.ast.statements.Block; |
37 |
import org.momut.ooas.ast.statements.GuardedCommand; |
38 |
import org.momut.ooas.ast.statements.NondetBlock; |
39 |
import org.momut.ooas.ast.statements.Statement; |
40 |
import org.momut.ooas.ast.statements.StatementKind; |
41 |
import org.momut.ooas.ast.types.OoActionSystemType; |
42 |
import org.momut.ooas.ast.types.Type; |
43 |
import org.momut.ooas.parser.ParserState; |
44 |
import org.momut.ooas.utils.exceptions.NotImplementedException; |
45 |
import org.momut.ooas.utils.exceptions.InternalCompilerException; |
46 |
import org.momut.ooas.utils.exceptions.OoasCompilerRuntimeException; |
47 |
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor; |
48 |
|
49 |
/**
|
50 |
* Similar (and should be run after) the RemoveTrivialPrioritizedCompositionVisitor.
|
51 |
* Removes Nondet-Choice blocks that only have one choice...
|
52 |
* @author KrennW
|
53 |
*
|
54 |
*/
|
55 |
public final class OoaRemoveTrivialNondetChoiceCompositionVisitor extends OoaCompleteAstTraversalVisitor { |
56 |
/// <summary>
|
57 |
/// replaces the nondetBlock by its child
|
58 |
/// </summary>
|
59 |
private IAst ConvertNondetBlock(NondetBlock nondetBlock, IAst parent)
|
60 |
{ |
61 |
if (nondetBlock.statements().size() <= 1 && nondetBlock.isSimpleBlock()) |
62 |
{ |
63 |
final Block newBlock = (Block)nondetBlock.statements().peekFirst();
|
64 |
newBlock.SetParentScope(nondetBlock.GetParentScope()); |
65 |
switch (parent.nodeType())
|
66 |
{ |
67 |
case type:
|
68 |
switch (((Type)parent).kind()) |
69 |
{ |
70 |
case OoActionSystemType:
|
71 |
final OoActionSystemType atype = (OoActionSystemType)parent;
|
72 |
final Block ablock = atype.doOdBlock();
|
73 |
if (ablock != nondetBlock) // ref equ. |
74 |
throw new InternalCompilerException("Internal Error: Cannot replace doOdBlock."); |
75 |
atype.SetDoOdBlock(newBlock); |
76 |
break;
|
77 |
default:
|
78 |
throw new NotImplementedException(); |
79 |
} |
80 |
break;
|
81 |
case statement:
|
82 |
switch (((Statement)parent).kind()) |
83 |
{ |
84 |
case SeqBlock:
|
85 |
case PrioBlock:
|
86 |
final Block aBlock = (Block)parent;
|
87 |
final int idx = aBlock.statements().indexOf(nondetBlock); |
88 |
if (idx == -1) |
89 |
throw new OoasCompilerRuntimeException(); // must not happen?! |
90 |
aBlock.statements().set(idx, newBlock); |
91 |
break;
|
92 |
case GuardedCommand:
|
93 |
final GuardedCommand gc = (GuardedCommand)parent;
|
94 |
if(gc.body() != nondetBlock) // ref equ. |
95 |
throw new InternalCompilerException("Internal Error: Cannot replace guarded command body."); |
96 |
gc.SetBody(newBlock); |
97 |
break;
|
98 |
default:
|
99 |
throw new NotImplementedException(); |
100 |
} |
101 |
break;
|
102 |
case identifier:
|
103 |
switch (((Identifier)parent).kind())
|
104 |
{ |
105 |
case NamedActionIdentifier:
|
106 |
final NamedActionIdentifier action = (NamedActionIdentifier)parent;
|
107 |
if (action.body() != nondetBlock) // ref equ. |
108 |
throw new InternalCompilerException("Internal Error: Cannot replace action body."); |
109 |
action.SetBody(newBlock); |
110 |
break;
|
111 |
case MethodIdentifier:
|
112 |
final MethodIdentifier method = (MethodIdentifier)parent;
|
113 |
if (method.body() != nondetBlock) // ref equ. |
114 |
throw new InternalCompilerException("Internal Error: Cannot replace method body."); |
115 |
method.SetBody(newBlock); |
116 |
break;
|
117 |
default:
|
118 |
throw new NotImplementedException(); |
119 |
} |
120 |
break;
|
121 |
default:
|
122 |
throw new NotImplementedException(); |
123 |
} |
124 |
m_changes = true;
|
125 |
return newBlock;
|
126 |
} |
127 |
else
|
128 |
return nondetBlock;
|
129 |
} |
130 |
|
131 |
|
132 |
@Override
|
133 |
protected void VisitAstElement(IAst element, IAst parent) |
134 |
{ |
135 |
if ( (element.nodeType() == AstNodeTypeEnum.statement) &&
|
136 |
(((Statement)element).kind() == StatementKind.NondetBlock) &&
|
137 |
// we're only interested in actionblock and actionbody composition, not in type composition
|
138 |
!(parent.nodeType() == AstNodeTypeEnum.identifier && |
139 |
((Identifier)parent).kind() == IdentifierKind.List)) |
140 |
{ |
141 |
final IAst newchild = ConvertNondetBlock((NondetBlock)element, parent);
|
142 |
super.VisitAstElement(newchild, parent);
|
143 |
} |
144 |
else
|
145 |
super.VisitAstElement(element, parent);
|
146 |
} |
147 |
|
148 |
private boolean m_changes = false; |
149 |
|
150 |
public OoaRemoveTrivialNondetChoiceCompositionVisitor(ParserState aState)
|
151 |
{ |
152 |
super (aState);
|
153 |
} |
154 |
|
155 |
|
156 |
public boolean changedAst() { |
157 |
return m_changes;
|
158 |
} |
159 |
|
160 |
} |