-
Notifications
You must be signed in to change notification settings - Fork 0
/
Addition.java
129 lines (102 loc) · 4.48 KB
/
Addition.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
package org.lisapark.octopus.core.processor;
import org.lisapark.octopus.ProgrammerException;
import org.lisapark.octopus.core.Persistable;
import org.lisapark.octopus.core.ValidationException;
import org.lisapark.octopus.core.event.Event;
import org.lisapark.octopus.core.runtime.ProcessorContext;
import java.util.Map;
import java.util.UUID;
/**
* This {@link Processor} is used to adding two inputs together and producing an output.
* <p/>
* Addition is a mathematical operation that represents combining collections of objects together into a larger
* collection. It is signified by the plus sign (+).
*
* @author dave sinclair(david.sinclair@lisa-park.com)
*/
@Persistable
public class Addition extends Processor<Void> {
private static final String DEFAULT_NAME = "Addition";
private static final String DEFAULT_DESCRIPTION = "Add 2 operands";
/**
* Addition takes two inputs
*/
private static final int FIRST_INPUT_ID = 1;
private static final int SECOND_INPUT_ID = 2;
private static final int OUTPUT_ID = 1;
protected Addition(UUID id, String name, String description) {
super(id, name, description);
}
protected Addition(UUID id, Addition additionToCopy) {
super(id, additionToCopy);
}
protected Addition(Addition additionToCopy) {
super(additionToCopy);
}
public ProcessorInput getFirstInput() {
// there are two inputs for addition
return getInputs().get(0);
}
public ProcessorInput getSecondInput() {
// there are two inputs for addition
return getInputs().get(1);
}
@Override
public Addition newInstance() {
return new Addition(UUID.randomUUID(), this);
}
@Override
public Addition copyOf() {
return new Addition(this);
}
@Override
public CompiledProcessor<Void> compile() throws ValidationException {
validate();
// we copy all the inputs and output taking a "snapshot" of this processor so we are isolated of changes
Addition copy = copyOf();
return new CompiledAddition(copy);
}
/**
* Returns a new {@link Addition} processor configured with all the appropriate
* {@link org.lisapark.octopus.core.parameter.Parameter}s, {@link org.lisapark.octopus.core.Input}s and {@link org.lisapark.octopus.core.Output}.
*
* @return new {@link Addition}
*/
public static Addition newTemplate() {
UUID processorId = UUID.randomUUID();
Addition addition = new Addition(processorId, DEFAULT_NAME, DEFAULT_DESCRIPTION);
// two double inputs
ProcessorInput<Double> firstInput = ProcessorInput.doubleInputWithId(FIRST_INPUT_ID).name("First Operand").description("First operand for addition").build();
addition.addInput(firstInput);
ProcessorInput<Double> secondInput = ProcessorInput.doubleInputWithId(SECOND_INPUT_ID).name("Second Operand").description("Second operand for addition").build();
addition.addInput(secondInput);
addition.addJoin(firstInput, secondInput);
// double output
try {
addition.setOutput(ProcessorOutput.doubleOutputWithId(OUTPUT_ID).nameAndDescription("Total").attributeName("sum"));
} catch (ValidationException ex) {
// this should NOT happen. It means we created the Addition with an invalid attribute name
throw new ProgrammerException(ex);
}
return addition;
}
static class CompiledAddition extends CompiledProcessor<Void> {
private final String firstAttributeName;
private final String secondAttributeName;
protected CompiledAddition(Addition addition) {
super(addition);
firstAttributeName = addition.getFirstInput().getSourceAttributeName();
secondAttributeName = addition.getSecondInput().getSourceAttributeName();
}
@Override
public Object processEvent(ProcessorContext<Void> ctx, Map<Integer, Event> eventsByInputId) {
Event firstEvent = eventsByInputId.get(FIRST_INPUT_ID);
Event secondEvent = eventsByInputId.get(SECOND_INPUT_ID);
Double firstOperand = firstEvent.getAttributeAsDouble(firstAttributeName);
Double secondOperand = secondEvent.getAttributeAsDouble(secondAttributeName);
firstOperand = (firstOperand == null) ? 0 : firstOperand;
secondOperand = (secondOperand == null) ? 0 : secondOperand;
return firstOperand + secondOperand;
}
}
}