Package: POContextStack

POContextStack

nameinstructionbranchcomplexitylinemethod
POContextStack()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
checkType(PExp, PType)
M: 2 C: 25
93%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 9
90%
M: 0 C: 1
100%
clearStateContexts()
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getContextNode(PExp)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getGenerator()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getLast_Vars()
M: 0 C: 22
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getName()
M: 0 C: 36
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getObligation(String)
M: 83 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
getPredWithContext(PExp)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
indentNewLines(String, String)
M: 40 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 8 C: 0
0%
M: 1 C: 0
0%
noteType(PExp, PType)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
pop()
M: 2 C: 26
93%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 6
86%
M: 0 C: 1
100%
setGenerator(UniqueNameGenerator)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (C) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.pog.contexts;
25:
26: import java.util.ListIterator;
27: import java.util.Map;
28: import java.util.Stack;
29:
30: import org.overture.ast.expressions.AVariableExp;
31: import org.overture.ast.expressions.PExp;
32: import org.overture.ast.intf.lex.ILexNameToken;
33: import org.overture.ast.types.PType;
34: import org.overture.pog.pub.IPOContext;
35: import org.overture.pog.pub.IPOContextStack;
36: import org.overture.pog.utility.UniqueNameGenerator;
37:
38: @SuppressWarnings("serial")
39: public class POContextStack extends Stack<IPOContext> implements
40:                 IPOContextStack
41: {
42:         private UniqueNameGenerator gen;
43:
44:         @Override
45:         public void setGenerator(UniqueNameGenerator gen)
46:         {
47:                 this.gen = gen;
48:         }
49:
50:         @Override
51:         public UniqueNameGenerator getGenerator()
52:         {
53:                 return gen;
54:         }
55:
56:         /**
57:          * Pop a non-stateful context from the Stack. Stateful contexts can be removed with {@link #clearStateContexts()}
58:          */
59:         @Override
60:         public synchronized IPOContext pop()
61:         {
62:
63:                 IPOContext obj = peek();
64:                 int len = size();
65:•                for (int i = len - 1; i >= 0; i--)
66:                 {
67:•                        if (!this.get(i).isStateful())
68:                         {
69:                                 removeElementAt(i);
70:                                 return obj;
71:                         }
72:                 }
73:
74:                 return obj;
75:         }
76:
77:         public PExp getPredWithContext(PExp initialPredicate)
78:         {
79:                 return getContextNode(initialPredicate);
80:         }
81:
82:         private PExp getContextNode(PExp stitchPoint)
83:         {
84:
85:•                for (int i = this.size() - 1; i >= 0; i--)
86:                 {
87:                         IPOContext ctxt = this.get(i);
88:•                        if (!(ctxt instanceof PONameContext))
89:                         {
90:                                 stitchPoint = ctxt.getContextNode(stitchPoint);
91:                         }
92:                 }
93:                 return stitchPoint;
94:         }
95:
96:         /*
97:          * (non-Javadoc)
98:          * @see org.overture.pog.IPOContextStack#getName()
99:          */
100:         @Override
101:         public String getName()
102:         {
103:                 StringBuilder result = new StringBuilder();
104:                 String prefix = "";
105:
106:•                for (IPOContext ctxt : this)
107:                 {
108:                         String name = ctxt.getName();
109:
110:•                        if (name.length() > 0)
111:                         {
112:                                 result.append(prefix);
113:                                 result.append(name);
114:                                 prefix = ", ";
115:                         }
116:                 }
117:
118:                 return result.toString();
119:         }
120:
121:         /*
122:          * (non-Javadoc)
123:          * @see org.overture.pog.IPOContextStack#getObligation(java.lang.String)
124:          */
125:         @Override
126:         public String getObligation(String root)
127:         {
128:                 StringBuilder result = new StringBuilder();
129:                 String spacing = " ";
130:                 String indent = "";
131:                 StringBuilder tail = new StringBuilder();
132:
133:•                for (IPOContext ctxt : this)
134:                 {
135:                         String po = ctxt.getContext();
136:
137:•                        if (po.length() > 0)
138:                         {
139:                                 result.append(indent);
140:                                 result.append("(");
141:                                 result.append(indentNewLines(po, indent));
142:                                 result.append("\n");
143:                                 indent = indent + spacing;
144:                                 tail.append(")");
145:                         }
146:                 }
147:
148:                 result.append(indent);
149:                 result.append(indentNewLines(root, indent));
150:                 result.append(tail);
151:                 result.append("\n");
152:
153:                 return result.toString();
154:         }
155:
156:         private String indentNewLines(String line, String indent)
157:         {
158:                 StringBuilder sb = new StringBuilder();
159:                 String[] parts = line.split("\n");
160:                 String prefix = "";
161:
162:•                for (int i = 0; i < parts.length; i++)
163:                 {
164:                         sb.append(prefix);
165:                         sb.append(parts[i]);
166:                         prefix = "\n" + indent;
167:                 }
168:
169:                 return sb.toString();
170:         }
171:
172:         public void noteType(PExp exp, PType PType)
173:         {
174:                 this.peek().noteType(exp, PType);
175:         }
176:
177:         public PType checkType(PExp exp, PType expected)
178:         {
179:                 ListIterator<IPOContext> p = this.listIterator(size());
180:
181:•                while (p.hasPrevious())
182:                 {
183:                         IPOContext c = p.previous();
184:
185:•                        if (c.isScopeBoundary())
186:                         {
187:                                 break; // Change of name scope for expressions.
188:                         }
189:
190:                         PType t = c.checkType(exp);
191:
192:•                        if (t != null)
193:                         {
194:                                 return t;
195:                         }
196:                 }
197:
198:                 return expected;
199:         }
200:
201:         @Override
202:         public void clearStateContexts()
203:         {
204:                 int len = size();
205:
206:•                for (int i = len - 1; i > 0; i--)
207:                 {
208:•                        if (this.get(i).isStateful())
209:                         {
210:                                 removeElementAt(i);
211:                         }
212:                 }
213:
214:         }
215:
216:         @Override
217:         public Map<ILexNameToken, AVariableExp> getLast_Vars()
218:         {
219:                 ListIterator<IPOContext> p = this.listIterator(size());
220:
221:•                while (p.hasPrevious())
222:                 {
223:                         IPOContext c = p.previous();
224:•                        if (c instanceof StatefulContext)
225:                         {
226:                                 return ((StatefulContext) c).getLast_vars();
227:                         }
228:
229:                 }
230:                 return null;
231:         }
232:
233: }