Skip to content

Commit 2c4a791

Browse files
committed
undo graph not a node.
1 parent 6d4abc0 commit 2c4a791

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/main/java/com/marginallyclever/nodegraphcore/Graph.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* @author Dan Royer
2222
* @since 2022-02-01
2323
*/
24-
public class Graph {
24+
public class Graph extends Node {
2525
private static final Logger logger = LoggerFactory.getLogger(Graph.class);
2626

2727
/**
@@ -48,7 +48,7 @@ public class Graph {
4848
* Constructor for subclasses to call. Creates an empty {@link Graph}.
4949
*/
5050
public Graph() {
51-
super();
51+
super("Graph");
5252
}
5353

5454
/**
@@ -149,7 +149,7 @@ public void remove(Graph graph) {
149149
* @param n the subject from which all connections should be removed.
150150
*/
151151
public void removeConnectionsToNode(Node n) {
152-
ArrayList<Connection> toRemove = new ArrayList<>();
152+
var toRemove = new ArrayList<Connection>();
153153
for(Connection c : connections) {
154154
for(int i = 0; i<n.getNumPorts(); ++i) {
155155
Port<?> v = n.getPort(i);
@@ -368,16 +368,20 @@ public void updateBounds() {
368368
}
369369

370370
public @Nonnull JSONObject toJSON() {
371-
JSONObject jo = new JSONObject();
371+
JSONObject jo = super.toJSON();
372372
jo.put("nodes",getAllNodesAsJSON());
373373
jo.put("connections",getAllNodeConnectionsAsJSON());
374+
// TODO save input and output ports.
374375
return jo;
375376
}
376377

378+
377379
public void fromJSON(JSONObject jo) throws JSONException {
378380
clear();
381+
super.fromJSON(jo);
379382
parseAllNodesFromJSON(jo.getJSONArray("nodes"));
380383
parseAllConnectionsFromJSON(jo.getJSONArray("connections"));
384+
// TODO load input and output ports.
381385
}
382386

383387
private void parseAllNodesFromJSON(JSONArray arr) throws JSONException {

src/test/java/com/marginallyclever/nodegraphcore/TestGraphCore.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public void beforeEach() {
5454
@Test
5555
public void testSaveEmptyGraph() {
5656
var json = graph.toJSON();
57-
assertEquals("{\"nodes\":[],\"connections\":[]}",
57+
json.put("uniqueID","19781eb0-4009-4f5e-bc68-4e70de1b5181");
58+
assertEquals("{\"variables\":[],\"nodes\":[],\"name\":\"Graph\","
59+
+"\"rectangle\":{\"x\":0,\"width\":150,\"y\":0,\"height\":50},"
60+
+"\"label\":\"\",\"uniqueID\":\"19781eb0-4009-4f5e-bc68-4e70de1b5181\",\"connections\":[]}",
5861
json.toString());
5962
}
6063

@@ -158,7 +161,7 @@ public void testAllNodesToJSONAndBack() {
158161
* confirm a {@link Graph} can be serialized and de-serialized.
159162
*/
160163
@Test
161-
public void testModelToJSONAndBack() {
164+
public void testGraphToJSONAndBack() {
162165
buildAddTwoConstants();
163166
JSONObject a = graph.toJSON();
164167
Graph modelB = new Graph();
@@ -170,7 +173,7 @@ public void testModelToJSONAndBack() {
170173
* confirm clearing a {@link Graph} really does set it back to nothing.
171174
*/
172175
@Test
173-
public void testModelClears() {
176+
public void testGraphClears() {
174177
buildAddTwoConstants();
175178
graph.clear();
176179
assertEquals((new Graph()).toString(), graph.toString());
@@ -180,8 +183,8 @@ public void testModelClears() {
180183
* confirm registering an already registered node does not throw an exception and does not double-register.
181184
*/
182185
@Test
183-
public void testFactoryWontRegisterTwoNodesWithSameName() throws Exception {
184-
assertThrows(GraphException.class,()-> NodeFactory.loadRegistries());
186+
public void testFactoryWontRegisterTwoNodesWithSameName() {
187+
assertThrows(GraphException.class, NodeFactory::loadRegistries);
185188
}
186189

187190
/**
@@ -192,7 +195,7 @@ public void testFactoryWontRegisterTwoNodesWithSameName() throws Exception {
192195
* @param <T>
193196
* @throws Exception
194197
*/
195-
private <T> void testNodeVariableToJSONAndBack(Class<T> myClass,T instA,T instB) throws Exception {
198+
private <T> void testNodeVariableToJSONAndBack(Class<T> myClass,T instA,T instB) {
196199
Port<?> a = new Input<>(myClass.getSimpleName(),myClass,instA);
197200
Port<?> b = new Input<>(myClass.getSimpleName(),myClass,instB);
198201

@@ -214,7 +217,7 @@ private <T> void testNodeVariableToJSONAndBack(Class<T> myClass,T instA,T instB)
214217
* @throws Exception if serialization fails.
215218
*/
216219
@Test
217-
public void testNodeVariablesToJSONAndBack() throws Exception {
220+
public void testNodeVariablesToJSONAndBack() {
218221
/* removed: it's not possible to serialize back to abstract classes
219222
testNodeVariableToJSONAndBack(Object.class, new Object(),new Object());
220223
testNodeVariableToJSONAndBack(Number.class, 1.2,0.0);
@@ -234,7 +237,7 @@ public void testNodeVariablesToJSONAndBack() throws Exception {
234237
* </ul>
235238
*/
236239
@Test
237-
public void testAddTwoModelsTogether() {
240+
public void testAddTwoGraphsTogether() {
238241
buildAddTwoConstants();
239242

240243
ThreadPoolScheduler scheduler = new ThreadPoolScheduler();
@@ -260,6 +263,8 @@ public void testAddTwoModelsTogether() {
260263
modelB.add(new Connection(a0,2,m,0));
261264
modelB.add(new Connection(a1,2,m,1));
262265

266+
// TODO and input and output ports.
267+
263268
// submit all the Adds to the scheduler
264269
int count = 0;
265270
for(Node n : modelB.getNodes()) {

0 commit comments

Comments
 (0)