public void testBasicUsage() { Node dotNode = Node.ROOT_NODE.newInstance(testLibrary, "dotNode"); dotNode.addParameter("x", Parameter.Type.FLOAT); dotNode.addParameter("y", Parameter.Type.FLOAT); dotNode.setValue("_code", new JavaMethodWrapper(getClass(), "_dot")); dotNode.addParameter("_output", Parameter.Type.STRING); // Check default values assertEquals(0F, dotNode.getValue("x")); assertEquals(0F, dotNode.getValue("y")); assertEquals("", dotNode.getValue("_output")); // Process dotNode.update(); assertEquals("dot(0.0,0.0)", dotNode.getOutputValue()); // Create instance and change values Node dotInstance = dotNode.newInstance(testLibrary, "dotInstance"); dotInstance.setValue("x", 25F); dotInstance.setValue("y", 42F); dotInstance.update(); assertEquals("dot(25.0,42.0)", dotInstance.getOutputValue()); // Check that original hasn't changed assertEquals(0F, dotNode.asFloat("x")); assertEquals(0F, dotNode.asFloat("y")); assertEquals("dot(0.0,0.0)", dotNode.getOutputValue()); // Now let the instance use its own code dotInstance.setValue("_code", new JavaMethodWrapper(getClass(), "_dot2")); dotInstance.update(); assertEquals("dot2(25.0,42.0)", dotInstance.getOutputValue()); }
public void testGetValue() { // Inheritance: A <- B <- C Node nodeA = Node.ROOT_NODE.newInstance(testLibrary, "A"); nodeA.addParameter("a", Parameter.Type.FLOAT, 1F); Node nodeB = nodeA.newInstance(testLibrary, "B"); nodeB.addParameter("b", Parameter.Type.FLOAT, 2F); Node nodeC = nodeB.newInstance(testLibrary, "C"); nodeC.addParameter("c", Parameter.Type.FLOAT, 3F); assertEquals(1F, nodeC.asFloat("a")); assertEquals(2F, nodeC.asFloat("b")); assertEquals(3F, nodeC.asFloat("c")); }
public void testNodeAttributeEvent() { TestAttributeListener l = new TestAttributeListener(); Node test = Node.ROOT_NODE.newInstance(testLibrary, "test"); test.addNodeAttributeListener(l); // Setting the name to itself does not trigger an event. test.setName("test"); assertEquals(0, l.nameCounter); test.setName("newname"); assertEquals(1, l.nameCounter); Parameter p1 = test.addParameter("p1", Parameter.Type.FLOAT); assertEquals(1, l.parameterCounter); p1.setName("parameter1"); assertEquals(2, l.parameterCounter); // TODO: These trigger ParameterAttributeChanged // p1.setBoundingMethod(Parameter.BoundingMethod.HARD); // assertEquals(3, l.parameterCounter); // p1.setMinimumValue(0F); // assertEquals(4, l.parameterCounter); // Changing the value does not trigger the event. // The event only happens for metadata, not data. // If you want to catch that, use DirtyListener. p1.setValue(20F); assertEquals(2, l.parameterCounter); test.removeParameter("parameter1"); assertEquals(3, l.parameterCounter); }
/** * Test the behaviour of {@link Node#stampDirty()}. * * @throws ExpressionError if the expression causes an error. This indicates a regression. */ public void testMarkStampedDirty() throws ExpressionError { // Setup a graph where a <- b <- c. Node a = Node.ROOT_NODE.newInstance(testLibrary, "a", Integer.class); Node b = Node.ROOT_NODE.newInstance(testLibrary, "b", Integer.class); Node c = Node.ROOT_NODE.newInstance(testLibrary, "c", Integer.class); a.addParameter("a", Parameter.Type.INT); b.addParameter("b", Parameter.Type.INT); Port bIn = b.addPort("in"); Port cIn = c.addPort("in"); bIn.connect(a); cIn.connect(b); // Update the graph. This will make a, b and c clean. c.update(); assertFalse(a.isDirty()); assertFalse(b.isDirty()); assertFalse(c.isDirty()); // Set b to a stamped expression. This will make node b, and all of its dependencies, dirty. b.setExpression("b", "stamp(\"my_b\", 55)"); assertTrue(b.hasStampExpression()); assertFalse(a.isDirty()); assertTrue(b.isDirty()); assertTrue(c.isDirty()); // Update the graph, cleaning all of the nodes. c.update(); assertFalse(a.isDirty()); assertFalse(b.isDirty()); assertFalse(c.isDirty()); // Mark only stamped upstream nodes as dirty. This will make b dirty, and all of its // dependencies. c.stampDirty(); assertFalse(a.isDirty()); assertTrue(b.isDirty()); assertTrue(c.isDirty()); // Remove the expression and update. This will make all nodes clean again. b.clearExpression("b"); c.update(); // Node b will not be dirty, since everything was updated. assertFalse(b.isDirty()); // Since there are no nodes with stamp expressions, marking the stamped upstream nodes will have // no effect. c.stampDirty(); assertFalse(a.isDirty()); assertFalse(b.isDirty()); assertFalse(c.isDirty()); }
/** * Test the hasStampExpression on the node. * * <p>This method is used to determine if parameters/nodes should be marked as dirty when * re-evaluating upstream, which is what happens in the copy node. */ public void testHasStampExpression() { Node n = Node.ROOT_NODE.newInstance(testLibrary, "test"); Parameter pAlpha = n.addParameter("alpha", Parameter.Type.FLOAT); Parameter pBeta = n.addParameter("beta", Parameter.Type.FLOAT); assertFalse(n.hasStampExpression()); // Set the parameters to expressions that do not use the stamp function. pAlpha.setExpression(" 12 + 5"); pBeta.setExpression("random(1, 5, 10)"); assertFalse(n.hasStampExpression()); // Set one of the parameters to the stamp function. pBeta.setExpression("stamp(\"mybeta\", 42)"); assertTrue(n.hasStampExpression()); // Set the other parameter expression to a stamp function as well. pAlpha.setExpression("stamp(\"myalpha\", 0) * 5"); assertTrue(n.hasStampExpression()); // Clear out the expressions one by one. pAlpha.clearExpression(); assertTrue(n.hasStampExpression()); // Change the beta parameter to some other expression. pBeta.setExpression("85 - 6"); assertFalse(n.hasStampExpression()); }
/** Test if parameters with expressions are inherited correctly. */ public void testExpressionPropagation() { // Inheritance: A <- B Node nodeA = Node.ROOT_NODE.newInstance(testLibrary, "A"); Parameter pF = nodeA.addParameter("f", Parameter.Type.INT, 0); String expr1 = "12 + 5"; pF.setExpression(expr1); Node nodeB = nodeA.newInstance(testLibrary, "B"); assertEquals(expr1, nodeB.getParameter("f").getExpression()); // Changing the expression of A does not automatically change that of B. String expr2 = "4 * 2"; pF.setExpression(expr2); assertEquals(expr1, nodeB.getParameter("f").getExpression()); // Reverting to default does. nodeB.getParameter("f").revertToDefault(); assertEquals(expr2, nodeB.getParameter("f").getExpression()); }
/** This is the same test as ParameterTest#testExpressionDependencies, but at the Node level. */ public void testRemoveExpressionDependency() { Node net = testNetworkNode.newInstance(testLibrary, "net"); Node number1 = net.create(numberNode); number1.addParameter("bob", Parameter.Type.INT, 10); number1.setExpression("value", "bob"); number1.setRendered(); net.update(); assertEquals(10, net.getOutputValue()); number1.removeParameter("bob"); try { net.update(); fail(); } catch (ProcessingError e) { assertTrue(e.getCause().getMessage().toLowerCase().contains("cannot evaluate expression")); } assertTrue(net.hasError()); assertTrue(number1.hasError()); assertNull(net.getOutputValue()); }
/** Test propagation behaviour for parameters. */ public void testParameterPropagation() { // Inheritance: A <- B Node nodeA = Node.ROOT_NODE.newInstance(testLibrary, "A"); nodeA.addParameter("f", Parameter.Type.FLOAT, 1F); Node nodeB = nodeA.newInstance(testLibrary, "B"); // The parameters of A and B are not the same. assertNotSame(nodeA.getParameter("f"), nodeB.getParameter("f")); nodeA.setValue("f", 10F); // The value for the B parameter doesn't automatically change when A was changed. assertEquals(10F, nodeA.asFloat("f")); assertEquals(1F, nodeB.asFloat("f")); // Setting the value of B does not affect the value of A. nodeB.getParameter("f").setValue(55F); assertEquals(10F, nodeA.asFloat("f")); assertEquals(55F, nodeB.asFloat("f")); // Reverting to the default value will force B to load the new parameter value // from the prototype. nodeB.getParameter("f").revertToDefault(); assertEquals(10F, nodeB.asFloat("f")); }
public static void _addParameter(Node node, ProcessingContext ctx) { node.addParameter("myparam", Parameter.Type.STRING, "myvalue"); }