Skip to content

Commit

Permalink
Refactor TemplateParser to WorkflowProcessSorter
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 22, 2023
1 parent ef06aad commit 8283d38
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/main/java/demo/DataDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.opensearch.common.io.PathUtils;
import org.opensearch.flowframework.template.ProcessNode;
import org.opensearch.flowframework.template.Template;
import org.opensearch.flowframework.template.TemplateParser;
import org.opensearch.flowframework.template.WorkflowProcessSorter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -26,7 +26,7 @@
import java.util.stream.Collectors;

/**
* Demo class exercising {@link TemplateParser}. This will be moved to a unit test.
* Demo class exercising {@link WorkflowProcessSorter}. This will be moved to a unit test.
*/
public class DataDemo {

Expand All @@ -50,8 +50,8 @@ public static void main(String[] args) throws IOException {
}

logger.info("Parsing graph to sequence...");
Template t = TemplateParser.parseJsonToTemplate(json);
List<ProcessNode> processSequence = TemplateParser.parseWorkflowToSequence(t.workflows().get("datademo"));
Template t = Template.parse(json);
List<ProcessNode> processSequence = WorkflowProcessSorter.sortProcessNodes(t.workflows().get("datademo"));
List<CompletableFuture<?>> futureList = new ArrayList<>();

for (ProcessNode n : processSequence) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/demo/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.opensearch.common.io.PathUtils;
import org.opensearch.flowframework.template.ProcessNode;
import org.opensearch.flowframework.template.Template;
import org.opensearch.flowframework.template.TemplateParser;
import org.opensearch.flowframework.template.WorkflowProcessSorter;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand All @@ -26,7 +26,7 @@
import java.util.stream.Collectors;

/**
* Demo class exercising {@link TemplateParser}. This will be moved to a unit test.
* Demo class exercising {@link WorkflowProcessSorter}. This will be moved to a unit test.
*/
public class Demo {

Expand All @@ -50,8 +50,8 @@ public static void main(String[] args) throws IOException {
}

logger.info("Parsing graph to sequence...");
Template t = TemplateParser.parseJsonToTemplate(json);
List<ProcessNode> processSequence = TemplateParser.parseWorkflowToSequence(t.workflows().get("demo"));
Template t = Template.parse(json);
List<ProcessNode> processSequence = WorkflowProcessSorter.sortProcessNodes(t.workflows().get("demo"));
List<CompletableFuture<?>> futureList = new ArrayList<>();

for (ProcessNode n : processSequence) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/demo/TemplateParseDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.io.PathUtils;
import org.opensearch.flowframework.template.Template;
import org.opensearch.flowframework.template.TemplateParser;
import org.opensearch.flowframework.template.WorkflowProcessSorter;
import org.opensearch.flowframework.workflow.Workflow;

import java.io.IOException;
Expand All @@ -22,7 +22,7 @@
import java.util.Map.Entry;

/**
* Demo class exercising {@link TemplateParser}. This will be moved to a unit test.
* Demo class exercising {@link WorkflowProcessSorter}. This will be moved to a unit test.
*/
public class TemplateParseDemo {

Expand All @@ -45,14 +45,14 @@ public static void main(String[] args) throws IOException {
return;
}

Template t = TemplateParser.parseJsonToTemplate(json);
Template t = Template.parse(json);

System.out.println(t.toJson());
System.out.println(t.toYaml());

for (Entry<String, Workflow> e : t.workflows().entrySet()) {
logger.info("Parsing {} workflow.", e.getKey());
TemplateParser.parseWorkflowToSequence(e.getValue());
WorkflowProcessSorter.sortProcessNodes(e.getValue());
}
}
}
19 changes: 19 additions & 0 deletions src/main/java/org/opensearch/flowframework/template/Template.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
package org.opensearch.flowframework.template;

import org.opensearch.Version;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.common.xcontent.yaml.YamlXContent;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
Expand Down Expand Up @@ -227,6 +229,23 @@ public static Template parse(XContentParser parser) throws IOException {
return new Template(name, description, useCase, operations, templateVersion, compatibilityVersion, userInputs, workflows);
}

/**
* Parse a JSON use case template
*
* @param json A string containing a JSON representation of a use case template
* @return A {@link Template} represented by the JSON.
* @throws IOException on failure to parse
*/
public static Template parse(String json) throws IOException {
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
json
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return parse(parser);
}

/**
* Builds an XContent object representing a map of String keys to String values.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.workflow.Workflow;
import org.opensearch.flowframework.workflow.WorkflowData;
import org.opensearch.flowframework.workflow.WorkflowStep;
import org.opensearch.flowframework.workflow.WorkflowStepFactory;

import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -32,44 +27,24 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* Utility class for parsing templates.
* Utility class converting a workflow of nodes and edges into a topologically sorted list of Process Nodes.
*/
public class TemplateParser {
public class WorkflowProcessSorter {

private static final Logger logger = LogManager.getLogger(TemplateParser.class);
private static final Logger logger = LogManager.getLogger(WorkflowProcessSorter.class);

/**
* Prevent instantiating this class.
*/
private TemplateParser() {}

/**
* Parse a JSON use case template
*
* @param json A string containing a JSON representation of a use case template
* @return A {@link Template} represented by the JSON.
* @throws IOException on failure to parse
*/
public static Template parseJsonToTemplate(String json) throws IOException {
logger.info("Parsing template...");
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
json
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return Template.parse(parser);
}
private WorkflowProcessSorter() {}

/**
* Parse a JSON representation of nodes and edges into a topologically sorted list of process nodes.
* @param workflow A string containing a JSON representation of nodes and edges
* Sort a workflow into a topologically sorted list of process nodes.
* @param workflow A workflow with (unsorted) nodes and edges which define predecessors and successors
* @return A list of Process Nodes sorted topologically. All predecessors of any node will occur prior to it in the list.
*/
public static List<ProcessNode> parseWorkflowToSequence(Workflow workflow) {
public static List<ProcessNode> sortProcessNodes(Workflow workflow) {
List<WorkflowNode> sortedNodes = topologicalSort(workflow.nodes(), workflow.edges());

List<ProcessNode> nodes = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TemplateParserTests extends OpenSearchTestCase {
private static List<String> parse(String json) throws IOException {
XContentParser parser = TemplateTestJsonUtil.jsonToParser(json);
Workflow w = Workflow.parse(parser);
return TemplateParser.parseWorkflowToSequence(w).stream().map(ProcessNode::id).collect(Collectors.toList());
return WorkflowProcessSorter.sortProcessNodes(w).stream().map(ProcessNode::id).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void testTemplate() throws IOException {
String json = TemplateTestJsonUtil.parseToJson(template);
assertEquals(expectedJson, json);

Template templateX = TemplateParser.parseJsonToTemplate(json);
Template templateX = Template.parse(json);
assertEquals("test", templateX.name());
assertEquals("a test template", templateX.description());
assertEquals("test use case", templateX.useCase());
Expand Down

0 comments on commit 8283d38

Please sign in to comment.