Skip to content

Commit

Permalink
cleanup some flow apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Schuh committed Jan 2, 2023
1 parent 621dfaa commit 679ae30
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 86 deletions.
4 changes: 0 additions & 4 deletions src/main/java/io/github/robbilie/nodegreen/Context.java

This file was deleted.

25 changes: 15 additions & 10 deletions src/main/java/io/github/robbilie/nodegreen/Flow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,32 @@

import java.util.*;

public class Flow {
public static Flow create(Flow parent, ObjectNode global, ObjectNode config) {
public class Flow implements IFlow {
public static Flow create(IFlow parent, ObjectNode global, ObjectNode config) {
return new Flow(parent, global, config);
}
public static Flow create(ObjectNode global) {
return new Flow(global);
public static Flow create(IFlow parent, ObjectNode global) {
return new Flow(parent, global);
}

Flow parent;
IFlow parent;

ObjectNode global;
ObjectNode flow;
Map<String, INode> activeNodes = new HashMap<>();

Boolean isGlobalFlow;
public Flow(ObjectNode global) {
public Flow(IFlow parent, ObjectNode global) {
this.parent = parent;
this.isGlobalFlow = true;
this.global = global;
this.flow = global;
}
public Flow(Flow parent, ObjectNode global, ObjectNode flow) {
public Flow(IFlow parent, ObjectNode global, ObjectNode flow) {
this.parent = parent;
this.isGlobalFlow = false;
this.global = global;
this.flow = flow;
this.parent = parent;
}

public void start() {
Expand Down Expand Up @@ -70,8 +71,8 @@ public void send(List<SendEvent> sendEvents) {
}

public static void handleOnSend(Flow flow, List<SendEvent> sendEvents) {
for (int i = 0; i < sendEvents.size(); i++) {
Flow.handlePreRoute(flow, sendEvents.get(i));
for (SendEvent sendEvent : sendEvents) {
Flow.handlePreRoute(flow, sendEvent);
}
}

Expand Down Expand Up @@ -141,6 +142,10 @@ public void stop(Set<String> stopList, Set<String> removedList) {
});
}

public Map<String, INode> getActiveNodes() {
return this.activeNodes;
}

public static void stopNode(INode node, Boolean removed) {
node.close(removed);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/robbilie/nodegreen/FlowUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public static ObjectNode parseConfig(List<JsonNode> input) throws JsonProcessing
((ObjectNode)flow.get("configs"))[n[prop]]._users.push(n.id)
}
}*/
if (n.has("z") && !((ObjectNode)flow.get("subflows")).has(n.get("z").asText())) {
if (n.has("z") && !flow.get("subflows").has(n.get("z").asText())) {

if (!((ObjectNode)flow.get("flows")).has(n.get("z").asText())) {
if (!flow.get("flows").has(n.get("z").asText())) {
ObjectNode tab = new ObjectMapper().createObjectNode();
tab.put("type", "tab");
tab.put("id", n.get("z").asText());
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/github/robbilie/nodegreen/Flows.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.github.robbilie.nodegreen;

import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.*;

public class Flows implements IFlow {

public Map<String,Flow> activeFlows = new HashMap<>();
public List<String> activeFlowIds = new ArrayList<>();
ObjectNode activeFlowConfig;
Map<String, String> activeNodesToFlow = new HashMap<>();
public void start(ObjectNode newFlowConfig) {
this.stop();
this.activeFlowConfig = newFlowConfig;
this.activeFlows = new HashMap<>();
this.activeFlowIds = new ArrayList<>();
this.activeNodesToFlow = new HashMap<>();
Flow global = Flow.create(this, this.activeFlowConfig);
this.activeFlows.put("global", global);
this.activeFlowIds.add("global");
for (Iterator<String> it = this.activeFlowConfig.get("flows").fieldNames(); it.hasNext(); ) {
String id = it.next();
if (!this.activeFlows.containsKey(id)) {
this.activeFlows.put(id, Flow.create(this, this.activeFlowConfig, (ObjectNode) this.activeFlowConfig.get("flows").get(id)));
this.activeFlowIds.add(id);
}
}
for (String id : this.activeFlowIds) {
this.activeFlows.get(id).start();
Map<String, INode> activeNodes = this.activeFlows.get(id).getActiveNodes();
for(String nid : activeNodes.keySet()) {
this.activeNodesToFlow.put(nid, id);
}
}
}

public void stop() {
List<String> reverseActiveFlowIds = this.activeFlowIds.subList(0, this.activeFlowIds.size());
Collections.reverse(reverseActiveFlowIds);
for (String id : reverseActiveFlowIds) {
this.activeFlows.get(id).stop();
}
this.activeFlows = new HashMap<>();
this.activeFlowIds = new ArrayList<>();
this.activeNodesToFlow = new HashMap<>();
}

public INode getNode(String id) {
if (this.activeNodesToFlow.containsKey(id) && this.activeFlows.containsKey(this.activeNodesToFlow.get(id))) {
return this.activeFlows.get(this.activeNodesToFlow.get(id)).getNode(id);
}
for (String flowId : this.activeFlows.keySet()) {
INode node = activeFlows.get(flowId).getNode(id);
if (node != null) {
return node;
}
}
return null;
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/github/robbilie/nodegreen/IFlow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.robbilie.nodegreen;

public interface IFlow {
INode getNode(String id);
}
1 change: 0 additions & 1 deletion src/main/java/io/github/robbilie/nodegreen/INode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public interface INode {
void receive(ObjectNode msg);
void onInput(Consumer<ObjectNode> callback);
void onClose(Consumer<INode> callback);
Context getContext();
void close();
void close(Boolean removed);
}
18 changes: 4 additions & 14 deletions src/main/java/io/github/robbilie/nodegreen/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class Node implements INode {
Flow flow;
List<List<String>> wires = new ArrayList<>();
Integer wireCount = 0;
Context context;
public Node(Flow flow, JsonNode config) {
this.config = config;
this.id = config.get("id").asText();
Expand Down Expand Up @@ -53,13 +52,6 @@ void updateWires(JsonNode wires) {
}
}

public Context getContext() {
if (this.context == null) {
this.context = new Context();
}
return this.context;
}

public void onInput(Consumer<ObjectNode> callback) {
this.inputCallbacks.add(callback);
}
Expand All @@ -70,8 +62,7 @@ public void onClose(Consumer<INode> callback) {

void emitInput(ObjectNode arg) {
int c = this.inputCallbacks.size();
for (int i = 0; i < c; i++) {
Consumer<ObjectNode> cb = this.inputCallbacks.get(i);
for (Consumer<ObjectNode> cb : this.inputCallbacks) {
cb.accept(arg);
}
}
Expand Down Expand Up @@ -104,8 +95,8 @@ public void send(List<ObjectNode> msgs) {
List<String> wires = this.wires.get(i);
if (i < msgs.size()) {
ObjectNode msg = msgs.get(i);
for (int j = 0; j < wires.size(); j++) {
sendEvents.add(new SendEvent(msg, this.id, this, i, wires.get(j), null, msgSent));
for (String wire : wires) {
sendEvents.add(new SendEvent(msg, this.id, this, i, wire, null, msgSent));
msgSent = true;
}
}
Expand All @@ -122,8 +113,7 @@ public void close() {
}

public void close(Boolean removed) {
for (int i = 0; i < this.closeCallbacks.size(); i++) {
Consumer<INode> callback = this.closeCallbacks.get(i);
for (Consumer<INode> callback : this.closeCallbacks) {
callback.accept(this);
}
this.removeAllInputListeners();
Expand Down
35 changes: 1 addition & 34 deletions src/main/java/io/github/robbilie/nodegreen/RED.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,6 @@
package io.github.robbilie.nodegreen;

import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.*;
import java.util.stream.Collectors;

public class RED {

public static Nodes nodes = new Nodes();
public static Map<String,Flow> activeFlows = new HashMap<>();
public static void start(ObjectNode activeFlowConfig) {
RED.stop();
RED.activeFlows = new HashMap<>();
RED.activeFlows.put("global", Flow.create(activeFlowConfig));
for (Iterator<String> it = activeFlowConfig.get("flows").fieldNames(); it.hasNext(); ) {
String id = it.next();
if (!RED.activeFlows.containsKey(id)) {
RED.activeFlows.put(id, Flow.create(RED.activeFlows.get("global"), activeFlowConfig, (ObjectNode) activeFlowConfig.get("flows").get(id)));
}
}
for (Iterator<String> it = RED.activeFlows.keySet().iterator(); it.hasNext(); ) {
String id = it.next();
RED.activeFlows.get(id).start();
}
}

public static void stop() {
List<String> activeFlowIds = RED.activeFlows.keySet().stream().filter(id -> !id.equals("global")).collect(Collectors.toList());
if (RED.activeFlows.containsKey("global")) {
activeFlowIds.add("global");
}
for (Iterator<String> it = activeFlowIds.iterator(); it.hasNext(); ) {
String id = it.next();
RED.activeFlows.get(id).stop();
}
RED.activeFlows = new HashMap<>();
}
public static Flows flows = new Flows();
}
9 changes: 5 additions & 4 deletions src/test/java/io/github/robbilie/nodegreen/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -20,8 +21,8 @@ public class MainTest {
@Test
void mainTest() {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("flows.json").getFile());
String json = null;
File file = new File(Objects.requireNonNull(classLoader.getResource("flows.json")).getFile());
String json;
try {
json = new String(Files.readAllBytes(file.toPath()));
} catch (IOException e) {
Expand All @@ -40,7 +41,7 @@ void mainTest() {

System.out.println(config);

RED.start(config);
RED.flows.start(config);

assertEquals(1, SelectThemeNode.nodes.size());

Expand All @@ -49,7 +50,7 @@ void mainTest() {
SelectThemeNode.nodes.get(id).receive(msg);
}

RED.stop();
RED.flows.stop();

for (String id : SelectThemeNode.nodes.keySet()) {
ObjectNode msg = new ObjectMapper().createObjectNode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.robbilie.nodegreen.Context;
import io.github.robbilie.nodegreen.Flow;
import io.github.robbilie.nodegreen.Node;
import io.github.robbilie.nodegreen.RED;

public class CompositionNode extends Node {
public CompositionNode(Flow flow, JsonNode config) {
super(flow, config);
System.out.println(config.get("compositions").asText());
System.out.println(flow.getNode(config.get("compositions").asText()));
System.out.println(((CompositionsNode) flow.getNode(config.get("compositions").asText())).compositions);
System.out.println(((CompositionsNode) RED.flows.getNode(config.get("compositions").asText())).compositions);
this.onInput((ObjectNode msg) -> {
Context context = this.getContext();
System.out.println("Composition: " + msg.toString());
msg.put("payload", "test");
this.send(msg);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.robbilie.nodegreen.nodes;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.robbilie.nodegreen.Context;
import io.github.robbilie.nodegreen.Flow;
import io.github.robbilie.nodegreen.Node;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.robbilie.nodegreen.Context;
import io.github.robbilie.nodegreen.Flow;
import io.github.robbilie.nodegreen.INode;
import io.github.robbilie.nodegreen.Node;
Expand All @@ -17,7 +16,6 @@ public SelectThemeNode(Flow flow, JsonNode config) {
super(flow, config);
SelectThemeNode.nodes.put(this.id, this);
this.onInput((ObjectNode msg) -> {
Context context = this.getContext();
msg.put("payload", "test");
this.send(msg);
});
Expand Down
16 changes: 8 additions & 8 deletions src/test/resources/flows.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"flows": [
{
"id": "defaultAssets",
"id": "default-assets",
"type": "assets",
"assets": [
{
Expand All @@ -11,7 +11,7 @@
]
},
{
"id": "defaultCompositions",
"id": "default-compositions",
"type": "compositions",
"compositions": [
{
Expand All @@ -21,9 +21,9 @@
]
},
{
"id": "main",
"id": "default",
"type": "tab",
"label": "Main",
"label": "default",
"disabled": false,
"info": "",
"env": []
Expand All @@ -34,18 +34,18 @@
"wires": [["5678"]],
"x": 210,
"y": 280,
"z": "main"
"z": "default"
},
{
"id": "5678",
"type": "composition",
"assets": "defaultAssets",
"compositions": "defaultCompositions",
"assets": "default-assets",
"compositions": "default-compositions",
"composition": "007",
"wires": [],
"x": 510,
"y": 280,
"z": "main"
"z": "default"
}
],
"rev": ""
Expand Down

0 comments on commit 679ae30

Please sign in to comment.