-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathExampleRandString.java
More file actions
128 lines (97 loc) · 3.08 KB
/
ExampleRandString.java
File metadata and controls
128 lines (97 loc) · 3.08 KB
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
package modules.examples;
import java.util.Properties;
import base.workbench.ModuleRunner;
import common.parallelization.CallbackReceiver;
import modules.CharPipe;
import modules.ModuleImpl;
import modules.OutputPort;
/**
* Creates a random string of defined length
*
* @author Christopher Kraus
*
*/
public class ExampleRandString extends modules.ModuleImpl {
//Add property keys:
public static final String PROPERTYKEY_STRLEN = "Length of the random String";
//Add properties variables:
private int strLength;
//Add variables:
private String string;
//Add I/O labels
private final String OUTPUTID = "output";
// Main method for stand-alone execution
public static void main(String[] args) throws Exception {
ModuleRunner.runStandAlone(ExampleRandString.class, args);
}
//Add constructors:
public ExampleRandString(CallbackReceiver callbackReceiver,
Properties properties) throws Exception {
super(callbackReceiver, properties);
//Add description for properties
this.getPropertyDescriptions().put(PROPERTYKEY_STRLEN,
"Length of the randomly composed String");
//Add default values
this.getPropertyDefaultValues().put(ModuleImpl.PROPERTYKEY_NAME,
"Random String Example Module");
this.getPropertyDefaultValues().put(PROPERTYKEY_STRLEN,
"5");
//Define I/O
OutputPort outputPort = new OutputPort(OUTPUTID, "Generated string.", this);
outputPort.addSupportedPipe(CharPipe.class);
super.addOutputPort(outputPort);
//Add module description
this.setDescription("Creates a random string of defined length.");
// You can override the automatic category selection (for example if a module is to be shown in "deprecated")
//this.setCategory("Examples");
}
//Add methods:
//Add setters:
public void setSeqString(String s) {
this.string += s;
}
//Add getters:
public String getString() {
return this.string;
}
//Add "applyProperties() method
@Override
public void applyProperties() throws Exception {
// Set defaults for properties not yet set
super.setDefaultsIfMissing();
// Apply own properties
if (this.getProperties().containsKey(PROPERTYKEY_STRLEN))
this.strLength = Integer.parseInt(this.getProperties().getProperty(
PROPERTYKEY_STRLEN));
// Apply parent object's properties (just the name variable actually)
super.applyProperties();
}
//Add "process()" method
@Override
public boolean process() throws Exception {
//create random string
int charNum;
charNum = (int) (Math.random()*2);
switch (charNum) {
case 0: this.string = "A";
break;
case 1: this.string = "B";
break;
}
for (int i = 0; i < (this.strLength - 1); i++) {
charNum = (int) (Math.random()*2);
switch (charNum) {
case 0: this.string += "A"; //adding "A" via concatenation
break;
case 1: this.setSeqString("B"); //adding "B" via setter method
break;
}
}
//write random string to output pipe with a try block
this.getOutputPorts().get(OUTPUTID).outputToAllCharPipes(this.string);
// close outputs
this.closeAllOutputs();
//success
return true;
}
}