diff --git a/src/main/java/demo/DataDemo.java b/src/main/java/demo/DataDemo.java index d3e7eab91..5e5623aa5 100644 --- a/src/main/java/demo/DataDemo.java +++ b/src/main/java/demo/DataDemo.java @@ -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; @@ -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 { @@ -50,8 +50,8 @@ public static void main(String[] args) throws IOException { } logger.info("Parsing graph to sequence..."); - Template t = TemplateParser.parseJsonToTemplate(json); - List processSequence = TemplateParser.parseWorkflowToSequence(t.workflows().get("datademo")); + Template t = Template.parse(json); + List processSequence = WorkflowProcessSorter.sortProcessNodes(t.workflows().get("datademo")); List> futureList = new ArrayList<>(); for (ProcessNode n : processSequence) { diff --git a/src/main/java/demo/Demo.java b/src/main/java/demo/Demo.java index 19396dc2e..28f8aaf7f 100644 --- a/src/main/java/demo/Demo.java +++ b/src/main/java/demo/Demo.java @@ -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; @@ -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 { @@ -50,8 +50,8 @@ public static void main(String[] args) throws IOException { } logger.info("Parsing graph to sequence..."); - Template t = TemplateParser.parseJsonToTemplate(json); - List processSequence = TemplateParser.parseWorkflowToSequence(t.workflows().get("demo")); + Template t = Template.parse(json); + List processSequence = WorkflowProcessSorter.sortProcessNodes(t.workflows().get("demo")); List> futureList = new ArrayList<>(); for (ProcessNode n : processSequence) { diff --git a/src/main/java/demo/TemplateParseDemo.java b/src/main/java/demo/TemplateParseDemo.java index 6e9800861..49e712ee2 100644 --- a/src/main/java/demo/TemplateParseDemo.java +++ b/src/main/java/demo/TemplateParseDemo.java @@ -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; @@ -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 { @@ -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 e : t.workflows().entrySet()) { logger.info("Parsing {} workflow.", e.getKey()); - TemplateParser.parseWorkflowToSequence(e.getValue()); + WorkflowProcessSorter.sortProcessNodes(e.getValue()); } } } diff --git a/src/main/java/org/opensearch/flowframework/template/Template.java b/src/main/java/org/opensearch/flowframework/template/Template.java index 2e353605b..327a508b5 100644 --- a/src/main/java/org/opensearch/flowframework/template/Template.java +++ b/src/main/java/org/opensearch/flowframework/template/Template.java @@ -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; @@ -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. * diff --git a/src/main/java/org/opensearch/flowframework/template/TemplateParser.java b/src/main/java/org/opensearch/flowframework/template/WorkflowProcessSorter.java similarity index 79% rename from src/main/java/org/opensearch/flowframework/template/TemplateParser.java rename to src/main/java/org/opensearch/flowframework/template/WorkflowProcessSorter.java index 62affec40..d1d56187d 100644 --- a/src/main/java/org/opensearch/flowframework/template/TemplateParser.java +++ b/src/main/java/org/opensearch/flowframework/template/WorkflowProcessSorter.java @@ -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; @@ -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 parseWorkflowToSequence(Workflow workflow) { + public static List sortProcessNodes(Workflow workflow) { List sortedNodes = topologicalSort(workflow.nodes(), workflow.edges()); List nodes = new ArrayList<>(); diff --git a/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java b/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java index 85a4a15c2..f7bdea986 100644 --- a/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java +++ b/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java @@ -31,7 +31,7 @@ public class TemplateParserTests extends OpenSearchTestCase { private static List 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 diff --git a/src/test/java/org/opensearch/flowframework/template/TemplateTests.java b/src/test/java/org/opensearch/flowframework/template/TemplateTests.java index c079d7cf3..ed3fc459b 100644 --- a/src/test/java/org/opensearch/flowframework/template/TemplateTests.java +++ b/src/test/java/org/opensearch/flowframework/template/TemplateTests.java @@ -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());