/
ImportConditional.java
1326 lines (1072 loc) · 55.7 KB
/
ImportConditional.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package jmri.jmrit.logixng.tools;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nonnull;
import jmri.*;
import jmri.implementation.DefaultConditionalAction;
import jmri.jmrit.entryexit.DestinationPoints;
import jmri.jmrit.logix.OBlock;
import jmri.jmrit.logix.Warrant;
import jmri.jmrit.logixng.*;
import jmri.jmrit.logixng.actions.Logix;
import jmri.jmrit.logixng.actions.*;
import jmri.jmrit.logixng.expressions.*;
import jmri.jmrit.logixng.util.TimerUnit;
/**
* Imports Logixs to LogixNG
*
* @author Daniel Bergqvist 2019
*/
public class ImportConditional {
private final jmri.Conditional _conditional;
private final ConditionalNG _conditionalNG;
private final boolean _dryRun;
/**
* Create instance of ImportConditional
* @param logix the parent Logix of the conditional to import
* @param conditional the Conditional to import
* @param logixNG the parent LogixNG that the new ConditionalNG will be added to
* @param sysName the system name of the new ConditionalNG
* @param dryRun true if import without creating any new beans,
* false if to create new beans
*/
public ImportConditional(
jmri.Logix logix,
Conditional conditional,
LogixNG logixNG,
String sysName,
boolean dryRun) {
_dryRun = dryRun;
_conditional = conditional;
String userName = conditional.getSystemName();
if (conditional.getUserName() != null) {
userName += ": " + conditional.getUserName();
}
if (!_dryRun) {
ConditionalNG conditionalNG = null;
int counter = 0;
while ((conditionalNG == null) && counter < 100) {
String name = counter > 0 ? " - " + Integer.toString(counter) : "";
conditionalNG = InstanceManager.getDefault(jmri.jmrit.logixng.ConditionalNG_Manager.class)
.createConditionalNG(userName + name);
counter++;
}
if (conditionalNG == null) throw new RuntimeException("Cannot create new ConditionalNG with name: \"" + userName + "\"");
_conditionalNG = conditionalNG;
// _conditionalNG = InstanceManager.getDefault(
// jmri.jmrit.logixng.ConditionalNG_Manager.class)
// .createConditionalNG(sysName, userName);
} else {
_conditionalNG = null;
}
// log.debug("Import Logix {} to LogixNG {}", _logix.getSystemName(), _logixNG.getSystemName());
// log.error("AA: Import Conditional {} to ConditionalNG {}", _conditional.getSystemName(), _conditionalNG.getSystemName());
}
public ConditionalNG getConditionalNG() {
return _conditionalNG;
}
public void doImport() throws SocketAlreadyConnectedException, JmriException {
/*
// This is only to remember to test everything Logix and Conditional supports
String systemName = "";
jmri.Logix l = null;
java.beans.PropertyChangeEvent evt = null;
jmri.Conditional c = null;
l.getConditional(systemName);
l.getConditionalByNumberOrder(0);
l.getEnabled();
c.cancelSensorTimer(systemName);
c.cancelTurnoutTimer(systemName);
c.getAntecedentExpression(); // Tested
c.getCopyOfActions();
c.getCopyOfStateVariables();
c.getLogicType(); // Tested
c.getTriggerOnChange(); // Tested
*/
// boolean triggerOnChange = _conditional.getTriggerOnChange();
// IfThenElse.Type type = triggerOnChange ? IfThenElse.Type.TRIGGER_ACTION : IfThenElse.Type.CONTINOUS_ACTION;
// IfThenElse ifThen = new IfThenElse(InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName(), null, type);
Logix logix = new Logix(InstanceManager.getDefault(DigitalActionManager.class).getAutoSystemName(), null);
logix.setExecuteOnChange(_conditional.getTriggerOnChange());
Conditional.AntecedentOperator ao = _conditional.getLogicType();
String antecedentExpression = _conditional.getAntecedentExpression();
List<ConditionalVariable> conditionalVariables = _conditional.getCopyOfStateVariables();
List<ConditionalAction> conditionalActions = _conditional.getCopyOfActions();
DigitalExpressionBean expression;
switch (ao) {
case ALL_AND:
expression = new And(InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName(), null);
break;
case ALL_OR:
expression = new Or(InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName(), null);
break;
case MIXED:
expression = new Antecedent(InstanceManager.getDefault(DigitalExpressionManager.class).getAutoSystemName(), null);
((Antecedent)expression).setAntecedent(antecedentExpression);
break;
default:
return;
}
// Is the Conditional a RTXINITIALIZER?
if ((conditionalVariables.size() == 1) && (conditionalVariables.get(0).getType().getItemType() == Conditional.ItemType.NONE)) {
expression =
new TriggerOnce(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
True trueExpression =
new True(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
if (!_dryRun) {
MaleSocket socket = InstanceManager.getDefault(DigitalExpressionManager.class)
.registerExpression(trueExpression);
expression.getChild(0).connect(socket);
}
} else {
buildExpression(expression, conditionalVariables);
}
DigitalBooleanMany many =
new DigitalBooleanMany(InstanceManager.getDefault(
DigitalBooleanActionManager.class).getAutoSystemName(), null);
buildAction(many, conditionalActions);
if (!_dryRun) {
MaleSocket expressionSocket = InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
logix.getExpressionSocket().connect(expressionSocket);
MaleSocket manySocket = InstanceManager.getDefault(DigitalBooleanActionManager.class).registerAction(many);
logix.getActionSocket().connect(manySocket);
MaleSocket logixAction = InstanceManager.getDefault(DigitalActionManager.class).registerAction(logix);
_conditionalNG.getChild(0).connect(logixAction);
}
}
private void buildExpression(DigitalExpressionBean expression, List<ConditionalVariable> conditionalVariables)
throws SocketAlreadyConnectedException, JmriException {
for (int i=0; i < conditionalVariables.size(); i++) {
jmri.ConditionalVariable cv = conditionalVariables.get(i);
NamedBean nb = cv.getBean();
AtomicBoolean isNegated = new AtomicBoolean(cv.isNegated());
DigitalExpressionBean newExpression;
switch (cv.getType().getItemType()) {
case SENSOR:
Sensor sn = (Sensor)nb;
newExpression = getSensorExpression(cv, sn);
break;
case TURNOUT:
Turnout tn = (Turnout)nb;
newExpression = getTurnoutExpression(cv, tn);
break;
case MEMORY:
Memory my = (Memory)nb;
newExpression = getMemoryExpression(cv, my);
break;
case LIGHT:
Light l = (Light)nb;
newExpression = getLightExpression(cv, l);
break;
case SIGNALHEAD:
SignalHead s = (SignalHead)nb;
newExpression = getSignalHeadExpression(cv, s, isNegated);
break;
case SIGNALMAST:
SignalMast sm = (SignalMast)nb;
newExpression = getSignalMastExpression(cv, sm, isNegated);
break;
case ENTRYEXIT:
DestinationPoints dp = (DestinationPoints)nb;
newExpression = getEntryExitExpression(cv, dp);
break;
case CONDITIONAL:
Conditional c = (Conditional)nb;
newExpression = getConditionalExpression(cv, c);
break;
case CLOCK:
newExpression = getFastClockExpression(cv);
break;
case WARRANT:
Warrant w = (Warrant)nb;
newExpression = getWarrantExpression(cv, w);
break;
case OBLOCK:
OBlock b = (OBlock)nb;
newExpression = getOBlockExpression(cv, b);
break;
default:
newExpression = null;
log.error("Unexpected type in ImportConditional.doImport(): {} -> {}", cv.getType().name(), cv.getType().getItemType().name());
break;
}
if (newExpression != null) {
boolean doTriggerActions = cv.doTriggerActions();
if (isNegated.get()) { // Some expressions have already handled Not
Not notExpression = new Not(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
if (!_dryRun) {
MaleSocket newExpressionSocket = InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(newExpression);
newExpressionSocket.setListen(doTriggerActions);
doTriggerActions = true; // We don't want the Not expression to disable listen.
notExpression.getChild(0).connect(newExpressionSocket);
}
newExpression = notExpression;
}
if (!_dryRun) {
MaleSocket newExpressionSocket = InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(newExpression);
newExpressionSocket.setListen(doTriggerActions);
expression.getChild(i).connect(newExpressionSocket);
}
} else {
log.error("ImportConditional.doImport() did not created an expression for type: {} -> {}", cv.getType().name(), cv.getType().getItemType().name());
}
}
}
private void buildAction(DigitalBooleanMany many, List<ConditionalAction> conditionalActions)
throws SocketAlreadyConnectedException, JmriException {
for (int i=0; i < conditionalActions.size(); i++) {
ConditionalAction ca = conditionalActions.get(i);
DigitalBooleanOnChange.Trigger trigger;
switch (ca.getOption()) {
case Conditional.ACTION_OPTION_ON_CHANGE_TO_TRUE:
trigger = DigitalBooleanOnChange.Trigger.CHANGE_TO_TRUE;
break;
case Conditional.ACTION_OPTION_ON_CHANGE_TO_FALSE:
trigger = DigitalBooleanOnChange.Trigger.CHANGE_TO_FALSE;
break;
case Conditional.ACTION_OPTION_ON_CHANGE:
trigger = DigitalBooleanOnChange.Trigger.CHANGE;
break;
default:
throw new InvalidConditionalActionException(
Bundle.getMessage("ActionBadTrigger", ca.getOption()));
}
DigitalBooleanActionBean booleanAction =
new DigitalBooleanOnChange(InstanceManager.getDefault(DigitalBooleanActionManager.class).getAutoSystemName(), null, trigger);
buildAction(booleanAction, ca);
if (!_dryRun) {
MaleSocket newBooleanActionSocket = InstanceManager.getDefault(DigitalBooleanActionManager.class).registerAction(booleanAction);
many.getChild(i).connect(newBooleanActionSocket);
}
}
}
private void buildAction(DigitalBooleanActionBean action, ConditionalAction conditionalAction)
throws SocketAlreadyConnectedException, JmriException {
String reference = null;
String devName = conditionalAction.getDeviceName();
if (devName != null && devName.length() > 0 && devName.charAt(0) == '@') {
reference = "{"+devName.substring(1)+"}";
}
NamedBean nb = conditionalAction.getBean();
// System.err.format("nb: %s%n", nb == null ? null : nb.getSystemName());
DigitalActionBean newAction;
switch (conditionalAction.getType().getItemType()) {
case SENSOR:
Sensor sn = (Sensor)nb;
newAction = getSensorAction(conditionalAction, sn, reference);
break;
case TURNOUT:
Turnout tn = (Turnout)nb;
newAction = getTurnoutAction(conditionalAction, tn, reference);
break;
case MEMORY:
Memory my = (Memory)nb;
newAction = getMemoryAction(conditionalAction, my, reference);
break;
case LIGHT:
Light l = (Light)nb;
newAction = getLightAction(conditionalAction, l, reference);
break;
case SIGNALHEAD:
SignalHead s = (SignalHead)nb;
newAction = getSignalHeadAction(conditionalAction, s, reference);
break;
case SIGNALMAST:
SignalMast sm = (SignalMast)nb;
newAction = getSignalMastAction(conditionalAction, sm, reference);
break;
case ENTRYEXIT:
DestinationPoints dp = (DestinationPoints)nb;
newAction = getEntryExitAction(conditionalAction, dp, reference);
break;
case WARRANT:
Warrant w = (Warrant)nb;
newAction = getWarrantAction(conditionalAction, w, reference);
break;
case OBLOCK:
OBlock b = (OBlock)nb;
newAction = getOBlockAction(conditionalAction, b, reference);
break;
case LOGIX:
newAction = getEnableLogixAction(conditionalAction);
break;
case CLOCK:
newAction = getClockAction(conditionalAction);
break;
case AUDIO:
newAction = getAudioAction(conditionalAction);
break;
case SCRIPT:
newAction = getScriptAction(conditionalAction);
break;
case OTHER:
Route r = (Route) nb;
newAction = getRouteAction(conditionalAction, r, reference);
break;
default:
newAction = null;
log.warn("Unexpected type in ImportConditional.doImport(): {} -> {}", conditionalAction.getType(), conditionalAction.getType().getItemType());
break;
}
if (newAction != null) {
if (!_dryRun) {
MaleSocket newActionSocket = InstanceManager.getDefault(DigitalActionManager.class).registerAction(newAction);
action.getChild(0).connect(newActionSocket);
}
} else {
log.error("ImportConditional.doImport() did not created an action for type: {} -> {}", conditionalAction.getType(), conditionalAction.getType().getItemType());
}
}
private DigitalExpressionBean getSensorExpression(@Nonnull ConditionalVariable cv, Sensor sn) throws JmriException {
ExpressionSensor expression =
new ExpressionSensor(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
// System.err.format("Sensor: %s%n", sn == null ? null : sn.getSystemName());
expression.setSensor(sn);
// cv.getDataString(); // SignalMast, Memory, OBlock
// cv.getNamedBeanData(); // Only for memory
// cv.getNum1(); // Clock, Memory
// cv.getNum2(); // Clock, Memory
switch (cv.getType()) {
case SENSOR_ACTIVE:
expression.setBeanState(ExpressionSensor.SensorState.Active);
break;
case SENSOR_INACTIVE:
expression.setBeanState(ExpressionSensor.SensorState.Inactive);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadSensorType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getTurnoutExpression(@Nonnull ConditionalVariable cv, Turnout tn) throws JmriException {
ExpressionTurnout expression =
new ExpressionTurnout(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setTurnout(tn);
switch (cv.getType()) {
case TURNOUT_CLOSED:
expression.setBeanState(ExpressionTurnout.TurnoutState.Closed);
break;
case TURNOUT_THROWN:
expression.setBeanState(ExpressionTurnout.TurnoutState.Thrown);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadTurnoutType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getMemoryExpression(@Nonnull ConditionalVariable cv, Memory my) throws JmriException {
ExpressionMemory expression =
new ExpressionMemory(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setMemory(my);
switch (cv.getNum1()) {
case ConditionalVariable.EQUAL:
expression.setMemoryOperation(ExpressionMemory.MemoryOperation.Equal);
break;
case ConditionalVariable.LESS_THAN:
expression.setMemoryOperation(ExpressionMemory.MemoryOperation.LessThan);
break;
case ConditionalVariable.LESS_THAN_OR_EQUAL:
expression.setMemoryOperation(ExpressionMemory.MemoryOperation.LessThanOrEqual);
break;
case ConditionalVariable.GREATER_THAN:
expression.setMemoryOperation(ExpressionMemory.MemoryOperation.GreaterThan);
break;
case ConditionalVariable.GREATER_THAN_OR_EQUAL:
expression.setMemoryOperation(ExpressionMemory.MemoryOperation.GreaterThanOrEqual);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadMemoryNum1", cv.getType().toString()));
}
Memory memory;
switch (cv.getType()) {
case MEMORY_EQUALS:
expression.setCompareTo(ExpressionMemory.CompareTo.Value);
expression.setCaseInsensitive(false);
expression.setConstantValue(cv.getDataString());
break;
case MEMORY_EQUALS_INSENSITIVE:
expression.setCompareTo(ExpressionMemory.CompareTo.Value);
expression.setCaseInsensitive(true);
expression.setConstantValue(cv.getDataString());
break;
case MEMORY_COMPARE:
expression.setCompareTo(ExpressionMemory.CompareTo.Memory);
expression.setCaseInsensitive(false);
expression.setOtherMemory(cv.getDataString());
memory = InstanceManager.getDefault(MemoryManager.class).getMemory(cv.getDataString());
if (memory == null) { // Logix allows the memory name in cv.getDataString() to be a system name without system prefix
memory = InstanceManager.getDefault(MemoryManager.class).provide(cv.getDataString());
expression.setOtherMemory(memory.getSystemName());
}
break;
case MEMORY_COMPARE_INSENSITIVE:
expression.setCompareTo(ExpressionMemory.CompareTo.Memory);
expression.setCaseInsensitive(true);
expression.setOtherMemory(cv.getDataString());
memory = InstanceManager.getDefault(MemoryManager.class).getMemory(cv.getDataString());
if (memory == null) { // Logix allows the memory name in cv.getDataString() to be a system name without system prefix
memory = InstanceManager.getDefault(MemoryManager.class).provide(cv.getDataString());
expression.setOtherMemory(memory.getSystemName());
}
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadMemoryType", cv.getType().toString()));
}
expression.setListenToOtherMemory(false);
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getLightExpression(@Nonnull ConditionalVariable cv, Light ln) throws JmriException {
ExpressionLight expression =
new ExpressionLight(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setLight(ln);
switch (cv.getType()) {
case LIGHT_ON:
expression.setBeanState(ExpressionLight.LightState.On);
break;
case LIGHT_OFF:
expression.setBeanState(ExpressionLight.LightState.Off);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadLightType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getSignalHeadExpression(
@Nonnull ConditionalVariable cv, SignalHead s, AtomicBoolean isNegated)
throws JmriException {
ExpressionSignalHead expression =
new ExpressionSignalHead(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setSignalHead(s);
ExpressionSignalHead.QueryType appearence =
isNegated.get() ? ExpressionSignalHead.QueryType.NotAppearance
: ExpressionSignalHead.QueryType.Appearance;
switch (cv.getType()) {
case SIGNAL_HEAD_RED:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.RED);
break;
case SIGNAL_HEAD_YELLOW:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.YELLOW);
break;
case SIGNAL_HEAD_GREEN:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.GREEN);
break;
case SIGNAL_HEAD_DARK:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.DARK);
break;
case SIGNAL_HEAD_FLASHRED:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.FLASHRED);
break;
case SIGNAL_HEAD_FLASHYELLOW:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.FLASHYELLOW);
break;
case SIGNAL_HEAD_FLASHGREEN:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.FLASHGREEN);
break;
case SIGNAL_HEAD_LUNAR:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.LUNAR);
break;
case SIGNAL_HEAD_FLASHLUNAR:
expression.setQueryType(appearence);
expression.setAppearance(SignalHead.FLASHLUNAR);
break;
case SIGNAL_HEAD_LIT:
expression.setQueryType(isNegated.get() ? ExpressionSignalHead.QueryType.NotLit : ExpressionSignalHead.QueryType.Lit);
break;
case SIGNAL_HEAD_HELD:
expression.setQueryType(isNegated.get() ? ExpressionSignalHead.QueryType.NotHeld : ExpressionSignalHead.QueryType.Held);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadSignalHeadType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
isNegated.set(false); // We have already handled this
return expression;
}
private DigitalExpressionBean getSignalMastExpression(
@Nonnull ConditionalVariable cv, SignalMast sm, AtomicBoolean isNegated)
throws JmriException {
ExpressionSignalMast expression =
new ExpressionSignalMast(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setSignalMast(sm);
ExpressionSignalMast.QueryType aspect =
isNegated.get() ? ExpressionSignalMast.QueryType.NotAspect
: ExpressionSignalMast.QueryType.Aspect;
switch (cv.getType()) {
case SIGNAL_MAST_ASPECT_EQUALS:
expression.setQueryType(aspect);
expression.setAspect(cv.getDataString());
break;
case SIGNAL_MAST_LIT:
expression.setQueryType(isNegated.get() ? ExpressionSignalMast.QueryType.NotLit : ExpressionSignalMast.QueryType.Lit);
break;
case SIGNAL_MAST_HELD:
expression.setQueryType(isNegated.get() ? ExpressionSignalMast.QueryType.NotHeld : ExpressionSignalMast.QueryType.Held);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadSignalMastType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
isNegated.set(false); // We have already handled this
return expression;
}
private DigitalExpressionBean getEntryExitExpression(@Nonnull ConditionalVariable cv, DestinationPoints dp) throws JmriException {
ExpressionEntryExit expression =
new ExpressionEntryExit(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setDestinationPoints(dp);
switch (cv.getType()) {
case ENTRYEXIT_ACTIVE:
expression.setBeanState(ExpressionEntryExit.EntryExitState.Active);
break;
case ENTRYEXIT_INACTIVE:
expression.setBeanState(ExpressionEntryExit.EntryExitState.Inactive);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadEntryExitType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getConditionalExpression(@Nonnull ConditionalVariable cv, Conditional cn) throws JmriException {
ExpressionConditional expression =
new ExpressionConditional(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setConditional(cn);
switch (cv.getType()) {
case CONDITIONAL_TRUE:
expression.setConditionalState(ExpressionConditional.ConditionalState.True);
break;
case CONDITIONAL_FALSE:
expression.setConditionalState(ExpressionConditional.ConditionalState.False);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadConditionalType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getFastClockExpression(@Nonnull ConditionalVariable cv) throws JmriException {
ExpressionClock expression =
new ExpressionClock(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
if (cv.getType() != Conditional.Type.FAST_CLOCK_RANGE) {
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadFastClockType", cv.getType().toString()));
}
expression.setType(ExpressionClock.Type.FastClock);
expression.setRange(ConditionalVariable.fixMidnight(cv.getNum1()), ConditionalVariable.fixMidnight(cv.getNum2()));
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getWarrantExpression(@Nonnull ConditionalVariable cv, Warrant w) throws JmriException {
ExpressionWarrant expression =
new ExpressionWarrant(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
expression.setWarrant(w);
switch (cv.getType()) {
case ROUTE_FREE:
expression.setBeanState(ExpressionWarrant.WarrantState.RouteFree);
break;
case ROUTE_OCCUPIED:
expression.setBeanState(ExpressionWarrant.WarrantState.RouteOccupied);
break;
case ROUTE_ALLOCATED:
expression.setBeanState(ExpressionWarrant.WarrantState.RouteAllocated);
break;
case ROUTE_SET:
expression.setBeanState(ExpressionWarrant.WarrantState.RouteSet);
break;
case TRAIN_RUNNING:
expression.setBeanState(ExpressionWarrant.WarrantState.TrainRunning);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadWarrantType", cv.getType().toString()));
}
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalExpressionBean getOBlockExpression(@Nonnull ConditionalVariable cv, OBlock b) throws JmriException {
ExpressionOBlock expression =
new ExpressionOBlock(InstanceManager.getDefault(DigitalExpressionManager.class)
.getAutoSystemName(), null);
OBlock.OBlockStatus oblockStatus = OBlock.OBlockStatus.getByName(cv.getDataString());
if (oblockStatus == null) {
throw new InvalidConditionalVariableException(
Bundle.getMessage("ConditionalBadOBlockDataString", cv.getDataString()));
}
expression.setOBlock(b);
expression.setBeanState(oblockStatus);
expression.setTriggerOnChange(cv.doTriggerActions());
return expression;
}
private DigitalActionBean getSensorAction(@Nonnull ConditionalAction ca, Sensor sn, String reference) throws JmriException {
switch (ca.getType()) {
case SET_SENSOR:
ActionSensor action =
new ActionSensor(InstanceManager.getDefault(DigitalActionManager.class)
.getAutoSystemName(), null);
if (reference != null) {
action.setAddressing(NamedBeanAddressing.Reference);
action.setReference(reference);
} else {
action.setAddressing(NamedBeanAddressing.Direct);
action.setSensor(sn);
}
switch (ca.getActionData()) {
case jmri.Route.TOGGLE:
action.setBeanState(ActionSensor.SensorState.Toggle);
break;
case Sensor.INACTIVE:
action.setBeanState(ActionSensor.SensorState.Inactive);
break;
case Sensor.ACTIVE:
action.setBeanState(ActionSensor.SensorState.Active);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ActionBadSensorState", ca.getActionData()));
}
return action;
case RESET_DELAYED_SENSOR:
case DELAYED_SENSOR:
ConditionalAction caTemp = new DefaultConditionalAction();
caTemp.setType(Conditional.Action.SET_SENSOR);
caTemp.setActionData(ca.getActionData());
DigitalActionBean subAction = getSensorAction(caTemp, sn, reference);
ExecuteDelayed delayedAction =
new ExecuteDelayed(InstanceManager.getDefault(DigitalActionManager.class)
.getAutoSystemName(), null);
String sNumber = ca.getActionString();
try {
int time = Integer.parseInt(sNumber);
delayedAction.setDelay(time);
delayedAction.setUnit(TimerUnit.Seconds);
} catch (NumberFormatException e) {
try {
float time = Float.parseFloat(sNumber);
delayedAction.setDelay((int) (time * 1000));
delayedAction.setUnit(TimerUnit.MilliSeconds);
} catch (NumberFormatException e2) {
// If here, assume that sNumber has the name of a memory.
// Logix supports this memory to have a floating point value
// but LogixNG requires this memory to have an integer value.
if (sNumber.charAt(0) == '@') {
sNumber = sNumber.substring(1);
}
delayedAction.setDelayAddressing(NamedBeanAddressing.Reference);
delayedAction.setDelayReference("{" + sNumber + "}");
delayedAction.setUnit(TimerUnit.Seconds);
}
}
delayedAction.setResetIfAlreadyStarted(ca.getType() == Conditional.Action.RESET_DELAYED_SENSOR);
if (!_dryRun) {
MaleSocket subActionSocket = InstanceManager.getDefault(DigitalActionManager.class)
.registerAction(subAction);
delayedAction.getChild(0).connect(subActionSocket);
}
return delayedAction;
case CANCEL_SENSOR_TIMERS:
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ActionBadSensorType", ca.getType().toString()));
}
}
private DigitalActionBean getTurnoutAction(@Nonnull ConditionalAction ca, Turnout tn, String reference) throws JmriException {
// System.err.format("Turnout: %s%n", tn == null ? null : tn.getSystemName());
ActionTurnout action;
// cv.getDataString(); // SignalMast, Memory, OBlock
// cv.getNamedBeanData(); // Only for memory
// cv.getNum1(); // Clock, Memory
// cv.getNum2(); // Clock, Memory
switch (ca.getType()) {
case SET_TURNOUT:
action = new ActionTurnout(InstanceManager.getDefault(DigitalActionManager.class)
.getAutoSystemName(), null);
if (reference != null) {
action.setAddressing(NamedBeanAddressing.Reference);
action.setReference(reference);
} else {
action.setAddressing(NamedBeanAddressing.Direct);
action.setTurnout(tn);
}
switch (ca.getActionData()) {
case jmri.Route.TOGGLE:
action.setBeanState(ActionTurnout.TurnoutState.Toggle);
break;
case Turnout.CLOSED:
action.setBeanState(ActionTurnout.TurnoutState.Closed);
break;
case Turnout.THROWN:
action.setBeanState(ActionTurnout.TurnoutState.Thrown);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ActionBadTurnoutState", ca.getActionData()));
}
break;
case RESET_DELAYED_TURNOUT:
case DELAYED_TURNOUT:
ConditionalAction caTemp = new DefaultConditionalAction();
caTemp.setType(Conditional.Action.SET_TURNOUT);
caTemp.setActionData(ca.getActionData());
DigitalActionBean subAction = getTurnoutAction(caTemp, tn, reference);
ExecuteDelayed delayedAction =
new ExecuteDelayed(InstanceManager.getDefault(DigitalActionManager.class)
.getAutoSystemName(), null);
String sNumber = ca.getActionString();
try {
int time = Integer.parseInt(sNumber);
delayedAction.setDelay(time);
delayedAction.setUnit(TimerUnit.Seconds);
} catch (NumberFormatException e) {
try {
float time = Float.parseFloat(sNumber);
delayedAction.setDelay((int) (time * 1000));
delayedAction.setUnit(TimerUnit.MilliSeconds);
} catch (NumberFormatException e2) {
// If here, assume that sNumber has the name of a memory.
// Logix supports this memory to have a floating point value
// but LogixNG requires this memory to have an integer value.
if (sNumber.charAt(0) == '@') {
sNumber = sNumber.substring(1);
}
delayedAction.setDelayAddressing(NamedBeanAddressing.Reference);
delayedAction.setDelayReference("{" + sNumber + "}");
delayedAction.setUnit(TimerUnit.Seconds);
}
}
delayedAction.setResetIfAlreadyStarted(ca.getType() == Conditional.Action.RESET_DELAYED_TURNOUT);
if (!_dryRun) {
MaleSocket subActionSocket = InstanceManager.getDefault(DigitalActionManager.class)
.registerAction(subAction);
delayedAction.getChild(0).connect(subActionSocket);
}
return delayedAction;
case LOCK_TURNOUT:
ActionTurnoutLock action2 = new ActionTurnoutLock(InstanceManager.getDefault(DigitalActionManager.class)
.getAutoSystemName(), null);
if (reference != null) {
action2.setAddressing(NamedBeanAddressing.Reference);
action2.setReference(reference);
} else {
action2.setAddressing(NamedBeanAddressing.Direct);
action2.setTurnout(tn);
}
switch (ca.getActionData()) {
case jmri.Route.TOGGLE:
action2.setTurnoutLock(ActionTurnoutLock.TurnoutLock.Toggle);
break;
case Turnout.LOCKED:
action2.setTurnoutLock(ActionTurnoutLock.TurnoutLock.Lock);
break;
case Turnout.UNLOCKED:
action2.setTurnoutLock(ActionTurnoutLock.TurnoutLock.Unlock);
break;
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ActionBadTurnoutLock", ca.getActionData()));
}
return action2;
case CANCEL_TURNOUT_TIMERS:
default:
throw new InvalidConditionalVariableException(
Bundle.getMessage("ActionBadTurnoutType", ca.getType().toString()));
}
// ca.getActionData();
// action.setTriggerOnChange(ca.doTriggerActions());
return action;
}
private DigitalActionBean getMemoryAction(@Nonnull ConditionalAction ca, Memory my, String reference) throws JmriException {
ActionMemory action;
action = new ActionMemory(InstanceManager.getDefault(DigitalActionManager.class)
.getAutoSystemName(), null);
if (reference != null) {
action.setAddressing(NamedBeanAddressing.Reference);
action.setReference(reference);
} else {
action.setAddressing(NamedBeanAddressing.Direct);
action.setMemory(my);
}