Skip to content

Commit

Permalink
changed to Input and Output
Browse files Browse the repository at this point in the history
DockReceiving is now dock/Input
DockShipping is now dock/Output
  • Loading branch information
i-make-robots committed Jan 3, 2025
1 parent 1541df8 commit fcc7d58
Show file tree
Hide file tree
Showing 40 changed files with 193 additions and 114 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/marginallyclever/nodegraphcore/Connection.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.marginallyclever.nodegraphcore;

import com.marginallyclever.nodegraphcore.dock.Dock;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;
import org.json.JSONException;
import org.json.JSONObject;

Expand Down Expand Up @@ -45,8 +48,8 @@ public Connection(Node inNode, int inVariableIndex, Node outNode, int outVariabl
setInput(inNode,inVariableIndex);
setOutput(outNode,outVariableIndex);

((DockShipping<?>)inNode.getVariable(inVariableIndex)).addTo(this);
((DockReceiving<?>)outNode.getVariable(outVariableIndex)).setFrom(this);
((Output<?>)inNode.getVariable(inVariableIndex)).addTo(this);
((Input<?>)outNode.getVariable(outVariableIndex)).setFrom(this);
}

/**
Expand Down Expand Up @@ -103,7 +106,7 @@ public boolean isInputValid() {
if(inNode==null) return false;
if(inVariableIndex==-1) return false;
if(inNode.getNumVariables() <= inVariableIndex) return false;
if(!(inNode.getVariable(inVariableIndex) instanceof DockShipping)) return false;
if(!(inNode.getVariable(inVariableIndex) instanceof Output)) return false;
return true;
}

Expand All @@ -114,7 +117,7 @@ public boolean isOutputValid() {
if(outNode==null) return false;
if(outVariableIndex==-1) return false;
if(outNode.getNumVariables() <= outVariableIndex) return false;
if(!(outNode.getVariable(outVariableIndex) instanceof DockReceiving)) return false;
if(!(outNode.getVariable(outVariableIndex) instanceof Input)) return false;
return true;
}

Expand All @@ -127,7 +130,7 @@ public void setInput(Node n, int variableIndex) {
inNode = n;
inVariableIndex = variableIndex;
if(n!=null) {
((DockShipping<?>) n.getVariable(variableIndex)).addTo(this);
((Output<?>) n.getVariable(variableIndex)).addTo(this);
}
}

Expand All @@ -140,7 +143,7 @@ public void setOutput(Node n, int variableIndex) {
outNode = n;
outVariableIndex = variableIndex;
if(n!=null) {
((DockReceiving<?>) n.getVariable(variableIndex)).setFrom(this);
((Input<?>) n.getVariable(variableIndex)).setFrom(this);
}
}

Expand Down Expand Up @@ -181,8 +184,8 @@ public boolean isConnectedTo(Node node) {
* Disconnects from all {@link Node}s.
*/
public void disconnectAll() {
if(getInVariable()!=null) ((DockShipping<?>)getInVariable()).removeTo(this);
if(getOutVariable()!=null) ((DockReceiving<?>)getOutVariable()).removeFrom(this);
if(getInVariable()!=null) ((Output<?>)getInVariable()).removeTo(this);
if(getOutVariable()!=null) ((Input<?>)getOutVariable()).removeFrom(this);
setInput(null,0);
setOutput(null,0);
queue.clear();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.marginallyclever.nodegraphcore;

import com.marginallyclever.nodegraphcore.dock.Dock;

import java.awt.*;

/**
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/marginallyclever/nodegraphcore/Graph.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.marginallyclever.nodegraphcore;

import com.marginallyclever.nodegraphcore.dock.Dock;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -176,13 +179,13 @@ public ConnectionPointInfo getNearestConnectionPoint(Point point, double r) {
for(Node n : nodes) {
for(int i = 0; i < n.getNumVariables(); ++i) {
Dock<?> v = n.getVariable(i);
if(v instanceof DockReceiving) {
if(v instanceof Input) {
double r2 = v.getInPosition().distanceSq(point);
if (r2 < rr) {
rr = r2;
info = new ConnectionPointInfo(n, i, ConnectionPointInfo.IN);
}
} else if(v instanceof DockShipping) {
} else if(v instanceof Output) {
double r2 = v.getOutPosition().distanceSq(point);
if (r2 < rr) {
rr = r2;
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/marginallyclever/nodegraphcore/Node.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.marginallyclever.nodegraphcore;

import com.marginallyclever.nodegraphcore.dock.Dock;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.json.RectangleDAO4JSON;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -287,8 +289,8 @@ private void guaranteeSameNumberOfVariables(JSONArray vars) throws JSONException
public int countReceivingConnections() {
int count = 0;
for(Dock<?> v : variables) {
if(v instanceof DockReceiving) {
if(((DockReceiving<?>)v).hasConnection()) {
if(v instanceof Input) {
if(((Input<?>)v).hasConnection()) {
count++;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public Packet(T data) {
this.data=data;
}

T getData() {
public T getData() {
return data;
}
}
16 changes: 10 additions & 6 deletions src/main/java/com/marginallyclever/nodegraphcore/Subgraph.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.marginallyclever.nodegraphcore;

import com.marginallyclever.nodegraphcore.dock.Dock;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;

import java.awt.*;
import java.util.ArrayList;

Expand Down Expand Up @@ -75,7 +79,7 @@ private void extractSupergraphOutputs(Node n) {
System.out.println("SupergraphOutput "+n.getUniqueName());
for(int i=0;i<n.getNumVariables();++i) {
Dock<?> v = n.getVariable(i);
if(v instanceof DockReceiving) {
if(v instanceof Input) {
System.out.println("found output "+v.getName());
addToPairs(v);
}
Expand All @@ -92,7 +96,7 @@ private void extractSupergraphInputs(Node n) {
System.out.println("SupergraphInput "+n.getUniqueName());
for(int i=0;i<n.getNumVariables();++i) {
Dock<?> v = n.getVariable(i);
if(v instanceof DockShipping) {
if(v instanceof Output) {
System.out.println("found input "+v.getName());
addToPairs(v);
}
Expand All @@ -108,8 +112,8 @@ private void extractSupergraphInputs(Node n) {
*/
private int sortVariables(VariablePair a, VariablePair b) {
// all input first
int aIn = (a.subVariable instanceof DockReceiving)?1:0;
int bIn = (b.subVariable instanceof DockReceiving)?1:0;
int aIn = (a.subVariable instanceof Input)?1:0;
int bIn = (b.subVariable instanceof Input)?1:0;
if(aIn != bIn) return aIn-bIn;
// then sort by name alphabetically
return a.subVariable.getName().compareTo(b.subVariable.getName());
Expand All @@ -135,10 +139,10 @@ public Graph getGraph() {
@Override
public void update() {
for(VariablePair p : pairs) {
if(p.superVariable instanceof DockReceiving) {
if(p.superVariable instanceof Input) {
p.subVariable.setValue(p.superVariable.getValue());
}
if(p.superVariable instanceof DockShipping) {
if(p.superVariable instanceof Output) {
p.superVariable.setValue(p.subVariable.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.marginallyclever.nodegraphcore;
package com.marginallyclever.nodegraphcore.dock;

import com.marginallyclever.nodegraphcore.Connection;
import com.marginallyclever.nodegraphcore.DAO4JSONFactory;
import com.marginallyclever.nodegraphcore.json.RectangleDAO4JSON;
import org.json.JSONException;
import org.json.JSONObject;

import java.awt.*;

/**
* Describes an input or output connection with for a {@link Node} and stores the value at that connection.
* Nodes connect to each other through {@link Dock}s linked by {@link Connection}s.
* @author Dan Royer
* @since 2022-02-01
*/
Expand Down Expand Up @@ -44,15 +46,15 @@ public abstract class Dock<T> {

/**
* Constructor for subclasses to call.
* @param _name the variable name
* @param name the variable name
* @param type the variable type
* @param startingValue the starting value
* @throws IllegalArgumentException if input and output are true at the same time.
*/
protected Dock(String _name, Class<T> type, T startingValue) throws IllegalArgumentException {
protected Dock(String name, Class<T> type, T startingValue) throws IllegalArgumentException {
super();
this.type = type;
this.name = _name;
this.name = name;
this.value = startingValue;
this.rectangle.setBounds(0,0,DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.marginallyclever.nodegraphcore;
package com.marginallyclever.nodegraphcore.dock;

public class DockReceiving<T> extends Dock<T> {
import com.marginallyclever.nodegraphcore.Connection;
import com.marginallyclever.nodegraphcore.Packet;

public class Input<T> extends Dock<T> {
private Connection from;

public DockReceiving(String _name, Class<T> type, T startingValue) throws IllegalArgumentException {
super(_name,type,startingValue);
public Input(String name, Class<T> type, T startingValue) throws IllegalArgumentException {
super(name,type,startingValue);
}

public void receive() {
Expand Down Expand Up @@ -39,6 +42,6 @@ public boolean hasConnection() {
*/
@Override
public Dock<T> createInverse() {
return new DockShipping<T>(name,type,value);
return new Output<T>(name,type,value);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.marginallyclever.nodegraphcore;
package com.marginallyclever.nodegraphcore.dock;

import com.marginallyclever.nodegraphcore.Connection;
import com.marginallyclever.nodegraphcore.Packet;

import java.util.ArrayList;
import java.util.List;

public class DockShipping<T> extends Dock<T> {
public class Output<T> extends Dock<T> {
private List<Connection> to = new ArrayList<>();

public DockShipping(String _name, Class<T> type, T startingValue) throws IllegalArgumentException {
public Output(String _name, Class<T> type, T startingValue) throws IllegalArgumentException {
super(_name,type,startingValue);
}

Expand Down Expand Up @@ -38,6 +41,6 @@ public boolean outputHasRoom() {
*/
@Override
public Dock<T> createInverse() {
return new DockReceiving<>(name,type,value);
return new Input<>(name,type,value);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.marginallyclever.nodegraphcore.nodes;

import com.marginallyclever.nodegraphcore.*;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;

/**
* Counts from start to end in increment sized steps, aka `for(i=start;i!=end;i+=increment)'
* @author Dan Royer
* @since 2022-02-01
*/
public class Counter extends Node {
private final DockReceiving<Integer> start = new DockReceiving<>("start",Integer.class,0);
private final DockReceiving<Integer> end = new DockReceiving<>("end",Integer.class,1);
private final DockReceiving<Integer> increment = new DockReceiving<>("increment",Integer.class,1);
private final DockShipping<Integer> output = new DockShipping<>("output",Integer.class,0);
private final Input<Integer> start = new Input<>("start",Integer.class,0);
private final Input<Integer> end = new Input<>("end",Integer.class,1);
private final Input<Integer> increment = new Input<>("increment",Integer.class,1);
private final Output<Integer> output = new Output<>("output",Integer.class,0);
private boolean done=false;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.marginallyclever.nodegraphcore.nodes;

import com.marginallyclever.nodegraphcore.*;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;

/**
* {@link SupergraphInput} for a {@link Boolean}.
* @author Dan Royer
* @since 2022-02-01
*/
public class LoadBoolean extends Node implements SupergraphInput {
private final DockReceiving<Boolean> value = new DockReceiving<>("value",Boolean.class,false);
private final DockReceiving<Integer> qty = new DockReceiving<>("qty",Integer.class,1);
private final DockShipping<Boolean> output = new DockShipping<>("output",Boolean.class,false);
private final Input<Boolean> value = new Input<>("value",Boolean.class,false);
private final Input<Integer> qty = new Input<>("qty",Integer.class,1);
private final Output<Boolean> output = new Output<>("output",Boolean.class,false);
private boolean done=false;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.marginallyclever.nodegraphcore.nodes;

import com.marginallyclever.nodegraphcore.*;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;

/**
* {@link SupergraphInput} for a {@link Number}.
* @author Dan Royer
* @since 2022-02-01
*/
public class LoadNumber extends Node implements SupergraphInput {
private final DockReceiving<Number> value = new DockReceiving<>("value",Number.class,0);
private final DockReceiving<Integer> qty = new DockReceiving<>("qty",Integer.class,1);
private final DockShipping<Number> output = new DockShipping<>("output",Number.class,0);
private final Input<Number> value = new Input<>("value",Number.class,0);
private final Input<Integer> qty = new Input<>("qty",Integer.class,1);
private final Output<Number> output = new Output<>("output",Number.class,0);
private boolean done=false;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package com.marginallyclever.nodegraphcore.nodes;

import com.marginallyclever.nodegraphcore.*;
import com.marginallyclever.nodegraphcore.dock.Input;
import com.marginallyclever.nodegraphcore.dock.Output;

/**
* {@link SupergraphInput} for a {@link String}.
* @author Dan Royer
* @since 2022-02-01
*/
public class LoadString extends Node implements SupergraphInput {
private final DockReceiving<String> value = new DockReceiving<>("value",String.class,"");
private final DockReceiving<Integer> qty = new DockReceiving<>("qty",Integer.class,1);
private final DockShipping<String> output = new DockShipping<>("output",String.class,"");
private final Input<String> value = new Input<>("value",String.class,"");
private final Input<Integer> qty = new Input<>("qty",Integer.class,1);
private final Output<String> output = new Output<>("output",String.class,"");
private boolean done=false;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.marginallyclever.nodegraphcore.nodes;

import com.marginallyclever.nodegraphcore.Node;
import com.marginallyclever.nodegraphcore.DockReceiving;
import com.marginallyclever.nodegraphcore.dock.Input;

/**
* sends A as a string to <pre>System.out.println()</pre>.
* @author Dan Royer
* @since 2022-02-01
*/
public class PrintToStdOut extends Node {
private final DockReceiving<Object> a = new DockReceiving("A",Object.class,null);
private final Input<Object> a = new Input("A",Object.class,null);

/**
* Constructor for subclasses to call.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.marginallyclever.nodegraphcore.nodes;

import com.marginallyclever.nodegraphcore.DockShipping;
import com.marginallyclever.nodegraphcore.dock.Output;
import com.marginallyclever.nodegraphcore.Node;
import com.marginallyclever.nodegraphcore.Packet;

Expand All @@ -10,7 +10,7 @@
* @since 2023-09-27
*/
public class TickCount extends Node {
private final DockShipping<Integer> output = new DockShipping<>("output",Integer.class,0);
private final Output<Integer> output = new Output<>("output",Integer.class,0);
private long tickCount=0;

/**
Expand Down
Loading

0 comments on commit fcc7d58

Please sign in to comment.