Skip to content

Commit d9f5e83

Browse files
authored
Merge pull request #3756 from rjwills28/fix_indexof_exceptions
Do not throw exception in indexOf() function if PV has not yet connected
2 parents b9019cc + bcaff09 commit d9f5e83

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

core/formula/src/main/java/org/csstudio/apputil/formula/enums/IndexOfFunction.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.csstudio.apputil.formula.enums;
22

33
import org.csstudio.apputil.formula.spi.FormulaFunction;
4+
import org.epics.vtype.Alarm;
45
import org.epics.vtype.Display;
56
import org.epics.vtype.Time;
7+
import org.epics.vtype.VDouble;
68
import org.epics.vtype.VEnum;
79
import org.epics.vtype.VInt;
810
import org.epics.vtype.VType;
@@ -52,6 +54,18 @@ public VType compute(VType... args) throws Exception
5254
Display.none());
5355
} else
5456
{
57+
if (args[0] instanceof VDouble)
58+
{
59+
final VDouble v = (VDouble)args[0];
60+
if (v.getValue().isNaN() &&
61+
(v.getAlarm() == Alarm.none() || v.getAlarm() == Alarm.disconnected()))
62+
{
63+
// Connection has not yet been made to the PV
64+
// so don't throw an exception yet
65+
return args[0];
66+
}
67+
}
68+
// Otherwise throw exception
5569
throw new Exception("Function " + getName() + " requires an enum argument " + Arrays.toString(args));
5670
}
5771

core/formula/src/test/java/org/csstudio/apputil/formula/FormulaUnitTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@
77
******************************************************************************/
88
package org.csstudio.apputil.formula;
99

10+
import org.csstudio.apputil.formula.enums.IndexOfFunction;
11+
1012
import org.epics.vtype.Alarm;
1113
import org.epics.vtype.AlarmSeverity;
1214
import org.epics.vtype.AlarmStatus;
15+
import org.epics.vtype.Display;
1316
import org.epics.vtype.Time;
17+
import org.epics.vtype.VDouble;
1418
import org.epics.vtype.VString;
19+
import org.epics.vtype.VType;
1520
import org.junit.jupiter.api.BeforeAll;
1621
import org.junit.jupiter.api.Test;
1722
import org.phoebus.core.vtypes.VTypeHelper;
@@ -293,6 +298,31 @@ public void testSPI() throws Exception {
293298
}
294299
}
295300

301+
@Test
302+
public void testEnums() throws Exception {
303+
Formula f = new Formula("enumOf(1, arrayOf(1,2,3), arrayOf(\"a\",\"b\",\"c\"))");
304+
assertEquals("b", VTypeHelper.toString(f.eval()));
305+
306+
f = new Formula("indexOf(enumOf(1, arrayOf(1,2,3), arrayOf(\"a\",\"b\",\"c\")))");
307+
assertEquals(1, VTypeHelper.toDouble(f.eval()));
308+
309+
// Exception is thrown
310+
IndexOfFunction indexFunc = new IndexOfFunction();
311+
try {
312+
indexFunc.compute(VDouble.of(3.2, Alarm.none(), Time.now(), Display.none()));
313+
} catch (Exception ex) {
314+
assertTrue(ex.getMessage().contains("Function indexOf requires an enum argument"));
315+
}
316+
317+
// Disconnected or not set values should not throw an exception
318+
// and should return the same input.
319+
VType input = VDouble.of(Double.NaN, Alarm.none(), Time.now(), Display.none());
320+
assertEquals(indexFunc.compute(input), input);
321+
322+
input = VDouble.of(Double.NaN, Alarm.disconnected(), Time.now(), Display.none());
323+
assertEquals(indexFunc.compute(input), input);
324+
}
325+
296326
@Test
297327
public void testVariableDetermination() throws Exception {
298328
Formula f = new Formula("RFQ_Vac:Pump2:Pressure < 10", true);

0 commit comments

Comments
 (0)