Package: TimingInvariantsParser

TimingInvariantsParser

nameinstructionbranchcomplexitylinemethod
TimingInvariantsParser()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
decodeArg(String)
M: 222 C: 0
0%
M: 30 C: 0
0%
M: 16 C: 0
0%
M: 43 C: 0
0%
M: 1 C: 0
0%
parse(File)
M: 75 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
parse(String)
M: 221 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 38 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /***************************************************************************
2: *
3: *        Copyright (c) 2009 IHA
4: *
5: *        Author: Kenneth Lausdahl and Augusto Ribeiro
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.interpreter.runtime.validation;
25:
26: import java.io.BufferedReader;
27: import java.io.File;
28: import java.io.FileReader;
29: import java.io.IOException;
30: import java.util.ArrayList;
31: import java.util.Arrays;
32: import java.util.List;
33: import java.util.Vector;
34:
35: import org.overture.interpreter.messages.rtlog.RTMessage.MessageType;
36: import org.overture.interpreter.runtime.validation.ValueValidationExpression.BinaryOps;
37:
38: public class TimingInvariantsParser
39: {
40:         int counter = 1;
41:
42:         public List<ConjectureDefinition> parse(File file) throws IOException
43:         {
44:                 StringBuffer contents = new StringBuffer();
45:                 BufferedReader reader = null;
46:                 boolean enabled = false;
47:
48:                 try
49:                 {
50:                         reader = new BufferedReader(new FileReader(file));
51:                         String text = null;
52:
53:                         // repeat until all lines is read
54:•                        while ((text = reader.readLine()) != null)
55:                         {
56:•                                if (text.replaceAll("\\s", " ").startsWith("/* timing invariants"))
57:                                 {
58:                                         enabled = true;
59:                                         continue;
60:                                 }
61:
62:•                                if (text.replaceAll("\\s", "").startsWith("*/"))
63:                                 {
64:                                         enabled = false;
65:                                 }
66:
67:•                                if (enabled && text.trim().length() > 0)
68:                                 {
69:                                         contents.append("\n" + text);
70:                                 }
71:                         }
72:                         return parse(contents.toString());
73:
74:                 } finally
75:                 {
76:                         try
77:                         {
78:•                                if (reader != null)
79:                                 {
80:                                         reader.close();
81:                                 }
82:                         } catch (IOException e)
83:                         {
84:                                 e.printStackTrace();
85:                         }
86:                 }
87:
88:         }
89:
90:         private List<ConjectureDefinition> parse(String contents)
91:         {
92:                 List<ConjectureDefinition> defs = new Vector<ConjectureDefinition>();
93:
94:                 try
95:                 {
96:                         int indexOfLParn = contents.indexOf('(');
97:
98:•                        while ((indexOfLParn = contents.indexOf('(')) != -1)
99:                         {
100:                                 String propName = contents.substring(0, indexOfLParn).trim();
101:                                 contents = contents.substring(indexOfLParn + 1);
102:                                 int end = contents.indexOf(';') - 1;
103:                                 String propBody = contents.substring(0, end);
104:                                 contents = contents.substring(end + 2);
105:
106:                                 String[] elements = propBody.split(",");
107:                                 List<String> elemsFirst = Arrays.asList(elements);
108:                                 List<String> elems = null;
109:•                                if (elemsFirst.size() > 3)
110:                                 {
111:                                         elems = new ArrayList<String>();
112:                                         elems.add(elemsFirst.get(0) + "," + elemsFirst.get(1));
113:                                         elems.add(elemsFirst.get(2));
114:                                         elems.add(elemsFirst.get(3));
115:                                 } else
116:                                 {
117:                                         elems = elemsFirst;
118:                                 }
119:
120:                                 List<IValidationExpression> args = decodeArg(elems.get(0));
121:
122:                                 OperationValidationExpression initOp = null;
123:                                 ValueValidationExpression initValue = null;
124:                                 OperationValidationExpression endOp = null;
125:                                 int interval = 0;
126:
127:                                 initOp = (OperationValidationExpression) args.get(0);
128:•                                if (args.size() > 1)
129:                                 {
130:                                         initValue = (ValueValidationExpression) args.get(1);
131:                                 }
132:
133:                                 args = decodeArg(elems.get(1));
134:
135:                                 endOp = (OperationValidationExpression) args.get(0);
136:
137:                                 args = decodeArg(elems.get(2));
138:
139:                                 interval = ((IntegerContainer) args.get(0)).getValue();
140:
141:•                                if (propName.equals("deadlineMet"))
142:                                 {
143:
144:                                         defs.add(new DeadlineMet("C" + counter++, initOp, initValue, endOp, (int) (interval * 1E6)));
145:•                                } else if (propName.equals("separate"))
146:                                 {
147:                                         defs.add(new Separate("C" + counter++, initOp, initValue, endOp, (int) (interval * 1E6)));
148:                                 }
149:
150:                         }
151:                 } catch (IndexOutOfBoundsException e)
152:                 {
153:                         e.printStackTrace();
154:                 }
155:
156:                 return defs;
157:         }
158:
159:         private List<IValidationExpression> decodeArg(String string)
160:         {
161:                 List<IValidationExpression> res = new ArrayList<IValidationExpression>();
162:                 string = string.trim();
163:•                if (string.startsWith("("))
164:                 {
165:                         string = string.substring(1, string.length() - 1);
166:                         String[] dividedString = string.split(",");
167:•                        for (String s : dividedString)
168:                         {
169:                                 res.addAll(decodeArg(s));
170:                         }
171:
172:•                } else if (string.startsWith("#"))
173:                 {
174:                         MessageType type = MessageType.Request;
175:
176:•                        if (string.startsWith("#req"))
177:                         {
178:                                 type = MessageType.Request;
179:•                        } else if (string.startsWith("#act"))
180:                         {
181:                                 type = MessageType.Activate;
182:•                        } else if (string.startsWith("#fin"))
183:                         {
184:                                 type = MessageType.Completed;
185:                         }
186:
187:                         string = string.substring(string.indexOf("(") + 1, string.length() - 1);
188:
189:                         String[] name = string.split("`");
190:
191:                         res.add(new OperationValidationExpression(name[1], name[0], type));
192:
193:•                } else if (string.matches("\\d+\\W\\w\\w"))
194:                 {
195:                         res.add(new IntegerContainer(Integer.valueOf(string.substring(0, string.indexOf(' ')))));
196:•                } else if (string.matches("\\w+`\\w+[.\\w+]*\\W+\\w+`\\w+"))
197:                 {
198:                         ValueValidationExpression.BinaryOps binop = BinaryOps.EQ;
199:
200:•                        if (string.contains(BinaryOps.EQ.syntax))
201:                         {
202:                                 binop = BinaryOps.EQ;
203:•                        } else if (string.contains(BinaryOps.GREATER.syntax))
204:                         {
205:                                 binop = BinaryOps.GREATER;
206:•                        } else if (string.contains(BinaryOps.GREATEREQ.syntax))
207:                         {
208:                                 binop = BinaryOps.GREATEREQ;
209:•                        } else if (string.contains(BinaryOps.LESS.syntax))
210:                         {
211:                                 binop = BinaryOps.LESS;
212:•                        } else if (string.contains(BinaryOps.LESSEQ.syntax))
213:                         {
214:                                 binop = BinaryOps.LESSEQ;
215:                         }
216:
217:                         String left = string.substring(0, string.indexOf(binop.syntax));
218:                         String right = string.substring(string.indexOf(binop.syntax)
219:                                         + binop.syntax.length());
220:
221:                         String[] leftName;
222:•                        if (left.contains("`"))
223:                         {
224:                                 leftName = left.split("`");
225:                         } else
226:                         {
227:                                 leftName = left.split(".");
228:                         }
229:
230:                         String[] rightName;
231:•                        if (right.contains("`"))
232:                         {
233:                                 rightName = right.split("`");
234:                         } else
235:                         {
236:                                 rightName = right.split(".");
237:                         }
238:                         res.add(new ValueValidationExpression(leftName, binop, rightName));
239:                 }
240:                 return res;
241:
242:         }
243:
244: }