Method: getMonitoredValues()

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.util.ArrayList;
27: import java.util.List;
28:
29: import org.overture.interpreter.values.UpdatableValue;
30: import org.overture.interpreter.values.Value;
31: import org.overture.interpreter.values.ValueListenerList;
32:
33: public class ValueValidationExpression implements IValidationExpression
34: {
35:
36:         public enum BinaryOps
37:         {
38:                 GREATER(">"), LESS("<"), EQ("="), GREATEREQ(">="), LESSEQ("<=");
39:
40:                 public final String syntax;
41:
42:                 private BinaryOps(String syntax)
43:                 {
44:                         this.syntax = syntax;
45:                 }
46:         }
47:
48:         private String[] leftName;
49:         private String[] rightName;
50:         private BinaryOps binaryOp;
51:         public ValueObserver leftValue;
52:         public ValueObserver rightValue;
53:         private ConjectureDefinition conjecture;
54:
55:         public ValueValidationExpression(String[] leftName, BinaryOps binaryOp,
56:                         String[] rightName)
57:         {
58:                 this.leftName = leftName;
59:                 this.binaryOp = binaryOp;
60:                 this.rightName = rightName;
61:
62:         }
63:
64:         public void setConjecture(ConjectureDefinition c)
65:         {
66:                 this.conjecture = c;
67:         }
68:
69:         @Override
70:         public String toString()
71:         {
72:                 StringBuffer s = new StringBuffer();
73:
74:                 s.append("ValueValidationExpression: ");
75:                 s.append(printValueName(this.leftName));
76:                 s.append(" ");
77:                 s.append(this.binaryOp);
78:                 s.append(" ");
79:                 s.append(printValueName(this.rightName));
80:
81:                 return s.toString();
82:         }
83:
84:         private String printValueName(String[] name)
85:         {
86:                 if (name.length == 2)
87:                 {
88:                         return name[0] + "`" + name[1];
89:                 } else
90:                 {
91:                         return name[0] + "`" + name[1] + "." + name[2];
92:                 }
93:         }
94:
95:         public List<String[]> getMonitoredValues()
96:         {
97:                 ArrayList<String[]> res = new ArrayList<String[]>();
98:                 res.add(leftName);
99:                 res.add(rightName);
100:                 return res;
101:
102:         }
103:
104:         public boolean isTrue()
105:         {
106:                 // System.out.println("Checking: " + this.toString());
107:                 boolean result = false;
108:
109:                 switch (binaryOp)
110:                 {
111:                         case EQ:
112:                                 result = leftValue.getValue() == rightValue.getValue();
113:                                 break;
114:                         case LESS:
115:                                 result = leftValue.getValue() < rightValue.getValue();
116:                                 break;
117:                         case LESSEQ:
118:                                 result = leftValue.getValue() <= rightValue.getValue();
119:                                 break;
120:                         case GREATER:
121:                                 result = leftValue.getValue() > rightValue.getValue();
122:                                 break;
123:                         case GREATEREQ:
124:                                 result = leftValue.getValue() >= rightValue.getValue();
125:                                 break;
126:                         default:
127:                                 break;
128:                 }
129:
130:                 return result;
131:         }
132:
133:         public void associateVariable(String[] strings, Value v)
134:         {
135:                 if (isStringsEqual(strings, leftName))
136:                 {
137:                         leftValue = new ValueObserver(strings, v, this);
138:                         if (v instanceof UpdatableValue)
139:                         {
140:                                 UpdatableValue uv = (UpdatableValue) v;
141:                                 if (uv.listeners != null)
142:                                 {
143:                                         uv.listeners.add(leftValue);
144:                                 } else
145:                                 {
146:                                         uv.listeners = new ValueListenerList(leftValue);
147:                                 }
148:                         }
149:
150:                 } else
151:                 {
152:                         rightValue = new ValueObserver(strings, v, this);
153:                         if (v instanceof UpdatableValue)
154:                         {
155:                                 UpdatableValue uv = (UpdatableValue) v;
156:                                 if (uv.listeners != null)
157:                                 {
158:                                         uv.listeners.add(rightValue);
159:                                 } else
160:                                 {
161:                                         uv.listeners = new ValueListenerList(rightValue);
162:                                 }
163:                         }
164:
165:                 }
166:
167:         }
168:
169:         public boolean isValueMonitored(String[] strings)
170:         {
171:                 return isStringsEqual(strings, leftName)
172:                                 || isStringsEqual(strings, rightName);
173:         }
174:
175:         private boolean isStringsEqual(String[] strings1, String[] strings2)
176:         {
177:                 if (strings1.length == strings2.length)
178:                 {
179:                         for (int i = 0; i < strings2.length; i++)
180:                         {
181:                                 if (!strings1[i].equals(strings2[i]))
182:                                 {
183:                                         return false;
184:                                 }
185:                         }
186:                         return true;
187:                 } else
188:                 {
189:                         return false;
190:                 }
191:
192:         }
193:
194:         public void valueChanged(ValueObserver valueObserver)
195:         {
196:                 // System.out.println("Value Validation Expression: after value change: " + valueObserver.v.toString());
197:                 conjecture.valueChanged(this);
198:         }
199: }