1
|
package eu.dnetlib.msro.workflows.graph;
|
2
|
|
3
|
import java.io.StringWriter;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.stream.Collectors;
|
8
|
|
9
|
import org.apache.commons.lang3.StringUtils;
|
10
|
|
11
|
import eu.dnetlib.msro.workflows.procs.Env;
|
12
|
|
13
|
/**
|
14
|
* Created by michele on 19/11/15.
|
15
|
*/
|
16
|
public class GraphNode {
|
17
|
|
18
|
public static final String SUCCESS_NODE = "success";
|
19
|
|
20
|
private final String name;
|
21
|
private final String type;
|
22
|
private final boolean isStart;
|
23
|
private final boolean isJoin;
|
24
|
private final boolean isSucessNode;
|
25
|
private final Map<String, GraphNodeParameter> nodeParams;
|
26
|
|
27
|
private GraphNode(final String name,
|
28
|
final String type,
|
29
|
final boolean isStart,
|
30
|
final boolean isJoin,
|
31
|
final boolean isSuccessNode,
|
32
|
final Map<String, GraphNodeParameter> nodeParams) {
|
33
|
this.name = name;
|
34
|
this.type = type;
|
35
|
this.isStart = isStart;
|
36
|
this.isJoin = isJoin;
|
37
|
this.isSucessNode = isSuccessNode;
|
38
|
this.nodeParams = nodeParams;
|
39
|
}
|
40
|
|
41
|
public static GraphNode newNode(final String name,
|
42
|
final String type,
|
43
|
final Map<String, GraphNodeParameter> nodeParams) {
|
44
|
return new GraphNode(name, type, false, false, false, nodeParams);
|
45
|
}
|
46
|
|
47
|
public static GraphNode newStartNode(final String name,
|
48
|
final String type,
|
49
|
final Map<String, GraphNodeParameter> nodeParams) {
|
50
|
return new GraphNode(name, type, true, false, false, nodeParams);
|
51
|
}
|
52
|
|
53
|
public static GraphNode newJoinNode(final String name,
|
54
|
final String type,
|
55
|
final Map<String, GraphNodeParameter> nodeParams) {
|
56
|
return new GraphNode(name, type, false, true, false, nodeParams);
|
57
|
}
|
58
|
|
59
|
public static GraphNode newSuccessNode() {
|
60
|
return new GraphNode(SUCCESS_NODE, null, false, true, true, null);
|
61
|
}
|
62
|
|
63
|
public String getName() {
|
64
|
return this.name;
|
65
|
}
|
66
|
|
67
|
public String getType() {
|
68
|
return this.type;
|
69
|
}
|
70
|
|
71
|
public boolean isStart() {
|
72
|
return this.isStart;
|
73
|
}
|
74
|
|
75
|
public boolean isJoin() {
|
76
|
return this.isJoin;
|
77
|
}
|
78
|
|
79
|
public boolean isSucessNode() {
|
80
|
return this.isSucessNode;
|
81
|
}
|
82
|
|
83
|
@Override
|
84
|
public String toString() {
|
85
|
final StringWriter sw = new StringWriter();
|
86
|
sw.append("[ name: ");
|
87
|
sw.append(this.name);
|
88
|
if (StringUtils.isNotBlank(this.type)) {
|
89
|
sw.append(", type: ");
|
90
|
sw.append(this.type);
|
91
|
}
|
92
|
if (isStart()) {
|
93
|
sw.append(" - isStart");
|
94
|
}
|
95
|
if (isJoin()) {
|
96
|
sw.append(" - isJoin");
|
97
|
}
|
98
|
sw.append(" ]");
|
99
|
return sw.toString();
|
100
|
}
|
101
|
|
102
|
public Map<String, GraphNodeParameter> getNodeParams() {
|
103
|
return this.nodeParams;
|
104
|
}
|
105
|
|
106
|
public Map<String, Object> resolveParamsWithNoEnv() {
|
107
|
return resolveParams(null);
|
108
|
}
|
109
|
|
110
|
@SuppressWarnings("unchecked")
|
111
|
public Map<String, Object> resolveParams(final Env env) {
|
112
|
final Map<String, Object> params = new HashMap<>();
|
113
|
|
114
|
if (this.nodeParams != null) {
|
115
|
|
116
|
for (final Map.Entry<String, GraphNodeParameter> e : this.nodeParams.entrySet()) {
|
117
|
final String pName = e.getKey();
|
118
|
final GraphNodeParameter param = e.getValue();
|
119
|
|
120
|
if (param.isEnvParam()) {
|
121
|
params.put(pName, resolveFromEnv(param, env));
|
122
|
} else if (param.isMap()) {
|
123
|
|
124
|
final Map<String, Object> map = new HashMap<>();
|
125
|
|
126
|
for (final Map.Entry<String, GraphNodeParameter> e1 : ((Map<String, GraphNodeParameter>) param.getValue()).entrySet()) {
|
127
|
map.put(e1.getKey(), e1.getValue().isEnvParam() ? resolveFromEnv(e1.getValue(), env) : e1.getValue().getValue());
|
128
|
}
|
129
|
|
130
|
params.put(pName, map);
|
131
|
|
132
|
} else if (param.isList()) {
|
133
|
params.put(pName,
|
134
|
((List<GraphNodeParameter>) param.getValue())
|
135
|
.stream()
|
136
|
.map(p -> p.isEnvParam() ? resolveFromEnv(p, env) : p.getValue())
|
137
|
.collect(Collectors.toList()));
|
138
|
|
139
|
} else {
|
140
|
params.put(pName, param.getValue());
|
141
|
}
|
142
|
|
143
|
}
|
144
|
}
|
145
|
|
146
|
return params;
|
147
|
}
|
148
|
|
149
|
private Object resolveFromEnv(final GraphNodeParameter param, final Env env) {
|
150
|
return env != null ? env.getAttribute(param.getEnvVariable()) : "[this value will be resolved using the runtime ENV]";
|
151
|
}
|
152
|
|
153
|
}
|