Project

General

Profile

« Previous | Next » 

Revision 58106

migration to spark

View differences:

GraphLoader.java
5 5
import java.util.List;
6 6
import java.util.Map;
7 7
import java.util.Set;
8
import java.util.regex.Matcher;
9
import java.util.regex.Pattern;
8 10
import java.util.stream.Collectors;
9 11

  
10 12
import javax.annotation.Resource;
......
29 31
 */
30 32
public class GraphLoader {
31 33

  
32
	private static final Log log = LogFactory.getLog(GraphLoader.class);
34
    private static final Log log = LogFactory.getLog(GraphLoader.class);
33 35

  
34
	private NodeHelper nodeHelper;
36
    private NodeHelper nodeHelper;
35 37

  
36
	@Resource(name = "propertyFetcher")
37
	private PropertyFetcher propertyFetcher;
38
    private String regExRef = "\\$\\{(\\w*)\\}";
38 39

  
39
	public Graph loadGraph(final Document doc, final Map<String, String> globalParams) throws MSROException {
40
		final Graph graph = new Graph();
40
    final Pattern pattern = Pattern.compile(regExRef, Pattern.MULTILINE);
41 41

  
42
		for (final Object o : doc.selectNodes("//CONFIGURATION/WORKFLOW/NODE")) {
43
			final Element n = (Element) o;
44
			final String nodeName = n.valueOf("@name");
45
			final String nodeType = n.valueOf("@type");
46
			final boolean isStart = StringUtils.equalsIgnoreCase(n.valueOf("@isStart"), "true");
47
			final boolean isJoin = StringUtils.equalsIgnoreCase(n.valueOf("@isJoin"), "true");
48 42

  
49
			final Map<String, GraphNodeParameter> params = calculateParamsForNode(n, globalParams);
43
    @Resource(name = "propertyFetcher")
44
    private PropertyFetcher propertyFetcher;
50 45

  
51
			if (isStart) {
52
				graph.addNode(GraphNode.newStartNode(nodeName, nodeType, params));
53
			} else if (isJoin) {
54
				graph.addNode(GraphNode.newJoinNode(nodeName, nodeType, params));
55
			} else {
56
				graph.addNode(GraphNode.newNode(nodeName, nodeType, params));
57
			}
46
    public Graph loadGraph(final Document doc, final Map<String, String> globalParams) throws MSROException {
47
        final Graph graph = new Graph();
58 48

  
59
			for (final Object o1 : n.selectNodes(".//ARC")) {
60
				final Element a = (Element) o1;
61
				final String arcName = a.valueOf("@name");
62
				final String to = a.valueOf("@to");
63
				graph.addArc(new Arc(StringUtils.isNotBlank(arcName) ? arcName : Arc.DEFAULT_ARC, nodeName, to));
64
			}
49
        for (final Object o : doc.selectNodes("//CONFIGURATION/WORKFLOW/NODE")) {
50
            final Element n = (Element) o;
51
            final String nodeName = n.valueOf("@name");
52
            final String nodeType = n.valueOf("@type");
53
            final boolean isStart = StringUtils.equalsIgnoreCase(n.valueOf("@isStart"), "true");
54
            final boolean isJoin = StringUtils.equalsIgnoreCase(n.valueOf("@isJoin"), "true");
65 55

  
66
			graph.addNode(GraphNode.newSuccessNode());
67
		}
56
            final Map<String, GraphNodeParameter> params = calculateParamsForNode(n, globalParams);
68 57

  
69
		checkValidity(graph);
58
            if (isStart) {
59
                graph.addNode(GraphNode.newStartNode(nodeName, nodeType, params));
60
            } else if (isJoin) {
61
                graph.addNode(GraphNode.newJoinNode(nodeName, nodeType, params));
62
            } else {
63
                graph.addNode(GraphNode.newNode(nodeName, nodeType, params));
64
            }
70 65

  
71
		return graph;
72
	}
66
            for (final Object o1 : n.selectNodes(".//ARC")) {
67
                final Element a = (Element) o1;
68
                final String arcName = a.valueOf("@name");
69
                final String to = a.valueOf("@to");
70
                graph.addArc(new Arc(StringUtils.isNotBlank(arcName) ? arcName : Arc.DEFAULT_ARC, nodeName, to));
71
            }
73 72

  
74
	public Map<String, GraphNodeParameter> calculateParamsForNode(final Node node, final Map<String, String> globalParams) {
73
            graph.addNode(GraphNode.newSuccessNode());
74
        }
75 75

  
76
		final Map<String, GraphNodeParameter> params = new HashMap<>();
76
        checkValidity(graph);
77 77

  
78
		if (node != null) {
79
			for (final Object o : node.selectNodes(".//PARAM")) {
80
				final Element p = (Element) o;
78
        return graph;
79
    }
81 80

  
82
				final String pName = p.valueOf("@name");
83
				final GraphNodeParameter pValue = calculateSimpleValue((Element) o, globalParams);
81
    public Map<String, GraphNodeParameter> calculateParamsForNode(final Node node, final Map<String, String> globalParams) {
84 82

  
85
				if (pValue != null) {
86
					params.put(pName, pValue);
87
				} else if (p.selectSingleNode("./MAP") != null) {
83
        final Map<String, GraphNodeParameter> params = new HashMap<>();
88 84

  
89
					@SuppressWarnings("unchecked")
90
					final Map<String, GraphNodeParameter> map = ((List<Element>) p.selectNodes("./MAP/ENTRY"))
91
							.stream()
92
							.collect(Collectors.toMap(
93
											e -> e.valueOf("@key"),
94
											e -> {
95
												final GraphNodeParameter gnp = calculateSimpleValue(e, globalParams);
96
												if (gnp == null) {
97
													final String msg = String.format("missing value for param: \"%s\"", e.valueOf("@key"));
98
													log.debug(msg);
99
													return GraphNodeParameter.newNullParam();
100
												}
101
												return gnp;
102
											}));
85
        if (node != null) {
86
            for (final Object o : node.selectNodes(".//PARAM")) {
87
                final Element p = (Element) o;
103 88

  
104
					params.put(pName, GraphNodeParameter.newMapParam(map));
89
                final String pName = p.valueOf("@name");
90
                final GraphNodeParameter pValue = calculateSimpleValue((Element) o, globalParams);
105 91

  
106
				} else if (p.selectSingleNode("./LIST") != null) {
107
					@SuppressWarnings("unchecked")
108
					final List<GraphNodeParameter> list = ((List<Element>) p.selectNodes("./LIST/ITEM"))
109
							.stream()
110
							.map(e -> calculateSimpleValue(e, globalParams))
111
							.collect(Collectors.toList());
112
					params.put(pName, GraphNodeParameter.newListParam(list));
113
				}
114
			}
115
		}
92
                if (pValue != null) {
93
                    params.put(pName, pValue);
94
                } else if (p.selectSingleNode("./MAP") != null) {
116 95

  
117
		return params;
118
	}
96
                    @SuppressWarnings("unchecked") final Map<String, GraphNodeParameter> map = ((List<Element>) p.selectNodes("./MAP/ENTRY"))
97
                            .stream()
98
                            .collect(Collectors.toMap(
99
                                    e -> e.valueOf("@key"),
100
                                    e -> {
101
                                        final GraphNodeParameter gnp = calculateSimpleValue(e, globalParams);
102
                                        if (gnp == null) {
103
                                            final String msg = String.format("missing value for param: \"%s\"", e.valueOf("@key"));
104
                                            log.debug(msg);
105
                                            return GraphNodeParameter.newNullParam();
106
                                        }
107
                                        return gnp;
108
                                    }));
119 109

  
120
	private GraphNodeParameter calculateSimpleValue(final Element elem, final Map<String, String> globalParams) {
121
		final String value = elem.valueOf("@value");
122
		final String ref = elem.valueOf("@ref");
123
		final String prop = elem.valueOf("@property");
124
		final String envRef = elem.valueOf("@env");
110
                    params.put(pName, GraphNodeParameter.newMapParam(map));
125 111

  
126
		if (StringUtils.isNotBlank(ref) && StringUtils.isNotBlank(globalParams.get(ref))) {
127
			return GraphNodeParameter.newSimpleParam(globalParams.get(ref));
128
		} else if (StringUtils.isNotBlank(envRef)) {
129
			return GraphNodeParameter.newEnvParam(envRef);
130
		} else if (StringUtils.isNotBlank(value)) {
131
			return GraphNodeParameter.newSimpleParam(value);
132
		} else if (StringUtils.isNotBlank(prop)) {
133
			return GraphNodeParameter.newSimpleParam(this.propertyFetcher.getProperty(prop));
134
		} else {
135
			return null;
136
		}
137
	}
112
                } else if (p.selectSingleNode("./LIST") != null) {
113
                    @SuppressWarnings("unchecked") final List<GraphNodeParameter> list = ((List<Element>) p.selectNodes("./LIST/ITEM"))
114
                            .stream()
115
                            .map(e -> calculateSimpleValue(e, globalParams))
116
                            .collect(Collectors.toList());
117
                    params.put(pName, GraphNodeParameter.newListParam(list));
118
                }
119
            }
120
        }
138 121

  
139
	private void checkValidity(final Graph graph) throws MSROException {
122
        return params;
123
    }
140 124

  
141
		final Set<String> nodesFromArcs = new HashSet<String>();
125
    private GraphNodeParameter calculateSimpleValue(final Element elem, final Map<String, String> globalParams) {
126
        String value = elem.valueOf("@value");
127
        final String ref = elem.valueOf("@ref");
128
        final String prop = elem.valueOf("@property");
129
        final String envRef = elem.valueOf("@env");
142 130

  
143
		boolean foundSuccess = false;
144
		boolean foundStart = false;
131
        if (StringUtils.isNotBlank(ref) && StringUtils.isNotBlank(globalParams.get(ref))) {
132
            return GraphNodeParameter.newSimpleParam(globalParams.get(ref));
133
        } else if (StringUtils.isNotBlank(envRef)) {
134
            return GraphNodeParameter.newEnvParam(envRef);
135
        } else if (StringUtils.isNotBlank(value)) {
136
            Matcher matcher = pattern.matcher(value);
137
            while (matcher.find()) {
138
                final String rName = matcher.group(1);
139
                final String rValue = globalParams.get(rName);
140
                if (StringUtils.isBlank(rValue)) {
141
                	return null;
142
				}
143
                value = value.replaceAll(Pattern.quote(matcher.group(0)), rValue);
144
				System.out.println("NEW VALUE "+value);
145
            }
146
            return GraphNodeParameter.newSimpleParam(value);
147
        } else if (StringUtils.isNotBlank(prop)) {
148
            return GraphNodeParameter.newSimpleParam(this.propertyFetcher.getProperty(prop));
149
        } else {
150
            return null;
151
        }
145 152

  
146
		for (final Arc arc : graph.getArcs()) {
147
			if (StringUtils.isBlank(arc.getFrom()) || StringUtils.isBlank(arc.getFrom())) { throw new MSROException("Invalid arc: missing from e/o to"); }
148
			if (StringUtils.equals(arc.getTo(), GraphNode.SUCCESS_NODE)) {
149
				foundSuccess = true;
150
			}
151
			nodesFromArcs.add(arc.getFrom());
152
			nodesFromArcs.add(arc.getTo());
153
		}
153
    }
154 154

  
155
		if (!foundSuccess) { throw new MSROException("Arc to success not found"); }
155
    private void checkValidity(final Graph graph) throws MSROException {
156 156

  
157
		final Set<String> diff = Sets.symmetricDifference(graph.nodeNames(), nodesFromArcs);
158
		if (!diff.isEmpty()) { throw new MSROException("Missing or invalid nodes in arcs: " + diff); }
157
        final Set<String> nodesFromArcs = new HashSet<String>();
159 158

  
160
		for (final GraphNode n : graph.nodes()) {
161
			if (StringUtils.isBlank(n.getName())) { throw new MSROException("Invalid node: missing name"); }
162
			if (n.isStart()) {
163
				foundStart = true;
164
			}
165
			if (!this.nodeHelper.isValidType(n.getType())) { throw new MSROException("Invalid node type: " + n.getType()); }
166
		}
167
		if (!foundStart) { throw new MSROException("Start node not found"); }
168
	}
159
        boolean foundSuccess = false;
160
        boolean foundStart = false;
169 161

  
170
	public NodeHelper getNodeHelper() {
171
		return this.nodeHelper;
172
	}
162
        for (final Arc arc : graph.getArcs()) {
163
            if (StringUtils.isBlank(arc.getFrom()) || StringUtils.isBlank(arc.getFrom())) {
164
                throw new MSROException("Invalid arc: missing from e/o to");
165
            }
166
            if (StringUtils.equals(arc.getTo(), GraphNode.SUCCESS_NODE)) {
167
                foundSuccess = true;
168
            }
169
            nodesFromArcs.add(arc.getFrom());
170
            nodesFromArcs.add(arc.getTo());
171
        }
173 172

  
174
	@Required
175
	public void setNodeHelper(final NodeHelper nodeHelper) {
176
		this.nodeHelper = nodeHelper;
177
	}
173
        if (!foundSuccess) {
174
            throw new MSROException("Arc to success not found");
175
        }
178 176

  
177
        final Set<String> diff = Sets.symmetricDifference(graph.nodeNames(), nodesFromArcs);
178
        if (!diff.isEmpty()) {
179
            throw new MSROException("Missing or invalid nodes in arcs: " + diff);
180
        }
181

  
182
        for (final GraphNode n : graph.nodes()) {
183
            if (StringUtils.isBlank(n.getName())) {
184
                throw new MSROException("Invalid node: missing name");
185
            }
186
            if (n.isStart()) {
187
                foundStart = true;
188
            }
189
            if (!this.nodeHelper.isValidType(n.getType())) {
190
                throw new MSROException("Invalid node type: " + n.getType());
191
            }
192
        }
193
        if (!foundStart) {
194
            throw new MSROException("Start node not found");
195
        }
196
    }
197

  
198
    public NodeHelper getNodeHelper() {
199
        return this.nodeHelper;
200
    }
201

  
202
    @Required
203
    public void setNodeHelper(final NodeHelper nodeHelper) {
204
        this.nodeHelper = nodeHelper;
205
    }
206

  
179 207
}

Also available in: Unified diff