Example #1
0
 public void testError() {
   Node bad = addDirectNode.newInstance(testLibrary, "bad");
   TestDirtyListener listener = new TestDirtyListener();
   bad.addDirtyListener(listener);
   bad.setValue("v1", 12);
   bad.setValue("v2", 3);
   // Since the node starts out as dirty, setting values doesn't increase the counter.
   assertEquals(0, listener.dirtyCounter);
   // This code inherits the default code, which doesn't throw an error.
   bad.update();
   assertEquals(15, bad.getOutputValue());
   // Updating the code marks it as clean.
   assertFalse(bad.isDirty());
   assertEquals(1, listener.updatedCounter);
   assertEquals(0, listener.dirtyCounter);
   // This code causes a division by zero.
   bad.setValue("_code", new PythonCode("def cook(self):\n  return 1 / 0"));
   assertEquals(1, listener.dirtyCounter);
   // We just changed a parameter value, so the node is dirty.
   assertTrue(bad.isDirty());
   // Processing will fail.
   assertProcessingError(bad, "integer division or modulo by zero");
   // After processing failed, events are still called,
   // and the node is marked clean. Output is set to null.
   assertFalse(bad.isDirty());
   assertNull(bad.getOutputValue());
   assertEquals(2, listener.updatedCounter);
   assertEquals(1, listener.dirtyCounter);
 }
Example #2
0
  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());
  }
Example #3
0
 public boolean save() {
   Node node = ForumManager.getCloud().getNode(id);
   node.setValue("mode", mode);
   node.setValue("body", body);
   node.setValue("encoding", encoding);
   node.commit();
   return true;
 }
Example #4
0
 public void testMMB1546() {
   Cloud cloud = getCloud();
   Transaction t = cloud.getTransaction("test0");
   Node nt = t.getNode(newNode);
   nt.setValue("title", "bla");
   // t.cancel(); _DONT_ cancel
   Node nc = cloud.getNode(newNode);
   nc.setValue("title", "bloe");
   nc.commit();
   assertEquals("bloe", nc.getStringValue("title"));
   assertEquals("bloe", cloud.getNode(newNode).getStringValue("title"));
   t.cancel();
   assertEquals("bloe", cloud.getNode(newNode).getStringValue("title"));
 }
Example #5
0
 /** Test if setting a stamp expressions marks the correct nodes as dirty. */
 public void testStampExpression() {
   Node number1 = numberNode.newInstance(testLibrary, "number1");
   Node stamp1 = Node.ROOT_NODE.newInstance(testLibrary, "stamp1", Integer.class);
   stamp1.addPort("value");
   stamp1.getPort("value").connect(number1);
   // The code prepares upstream dependencies for stamping, processes them and negates the output.
   String stampCode =
       "def cook(self):\n"
           + "  context.put('my_a', 99)\n"
           + "  self.node.stampDirty()\n"
           + "  self.node.updateDependencies(context)\n"
           + "  return -self.value # Negate the output";
   stamp1.setValue("_code", new PythonCode(stampCode));
   Parameter pValue = number1.getParameter("value");
   // Set number1 to a regular value. This should not influence the stamp operation.
   pValue.set(12);
   stamp1.update();
   assertEquals(-12, stamp1.getOutputValue());
   // Set number1 to an expression. Since we're not using stamp, nothing strange should happen to
   // the output.
   pValue.setExpression("2 + 1");
   stamp1.update();
   assertEquals(-3, stamp1.getOutputValue());
   // Set number1 to an unknown stamp expression. The default value will be picked.
   pValue.setExpression("stamp(\"xxx\", 19)");
   stamp1.update();
   assertEquals(-19, stamp1.getOutputValue());
   // Set number1 to the my_a stamp expression. The expression will be picked up.
   pValue.setExpression("stamp(\"my_a\", 33)");
   stamp1.update();
   assertEquals(-99, stamp1.getOutputValue());
 }
Example #6
0
 public void testBasicClone() {
   Node myNode = Node.ROOT_NODE.newInstance(testLibrary, "myNode");
   myNode.setValue("_code", new JavaMethodWrapper(getClass(), "_addParameter"));
   myNode.update();
   assertTrue(myNode.hasParameter("myparam"));
   assertEquals("myvalue", myNode.getValue("myparam"));
 }
Example #7
0
 public void testSetValue() {
   // 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);
   nodeC.setValue("a", 10F);
   nodeC.setValue("b", 20F);
   nodeC.setValue("c", 30F);
   assertEquals(1F, nodeA.asFloat("a"));
   assertEquals(2F, nodeB.asFloat("b"));
   assertEquals(10F, nodeC.asFloat("a"));
   assertEquals(20F, nodeC.asFloat("b"));
   assertEquals(30F, nodeC.asFloat("c"));
 }
Example #8
0
  /** Test if print messages get output. */
  public void testOutput() {
    ProcessingContext ctx;
    PythonCode helloCode = new PythonCode("def cook(self): print 'hello'");
    Node test = Node.ROOT_NODE.newInstance(testLibrary, "test");
    test.setValue("_code", helloCode);
    ctx = new ProcessingContext();
    test.update(ctx);
    assertEquals("hello\n", ctx.getOutput());

    // Try this in a network. All the output of the nodes should be merged.
    Node parent = Node.ROOT_NODE.newInstance(testLibrary, "parent");
    Node child = parent.create(Node.ROOT_NODE, "child");
    child.setValue("_code", helloCode);
    child.setRendered();
    ctx = new ProcessingContext();
    parent.update(ctx);
    assertEquals("hello\n", ctx.getOutput());
  }
  ////////////////////////////////////////////////
  // createFaultResponseNode
  ////////////////////////////////////////////////
  private Node createFaultResponseNode(int errCode, String errDescr) {

    // <s:Fault>
    Node faultNode = new Node(SOAP.XMLNS + SOAP.DELIM + SOAP.FAULT);

    // <faultcode>s:Client</faultcode>
    Node faultCodeNode = new Node(SOAP.FAULT_CODE);

    faultCodeNode.setValue(SOAP.XMLNS + SOAP.DELIM + FAULT_CODE);
    faultNode.addNode(faultCodeNode);

    // <faultstring>UPnPError</faultstring>
    Node faultStringNode = new Node(SOAP.FAULT_STRING);

    faultStringNode.setValue(FAULT_STRING);
    faultNode.addNode(faultStringNode);

    // <detail>
    Node detailNode = new Node(SOAP.DETAIL);

    faultNode.addNode(detailNode);

    // <UPnPError xmlns="urn:schemas-upnp-org:control-1-0">
    Node upnpErrorNode = new Node(FAULT_STRING);

    upnpErrorNode.setAttribute("xmlns", Control.XMLNS);
    detailNode.addNode(upnpErrorNode);

    // <errorCode>error code</errorCode>
    Node errorCodeNode = new Node(SOAP.ERROR_CODE);

    errorCodeNode.setValue(errCode);
    upnpErrorNode.addNode(errorCodeNode);

    // <errorDescription>error string</errorDescription>
    Node errorDesctiprionNode = new Node(SOAP.ERROR_DESCRIPTION);

    errorDesctiprionNode.setValue(errDescr);
    upnpErrorNode.addNode(errorDesctiprionNode);

    return faultNode;
  }
Example #10
0
 public void testDisconnect() {
   Node net1 = testNetworkNode.newInstance(testLibrary, "net1");
   Node number1 = net1.create(numberNode);
   Node number2 = net1.create(numberNode);
   Node multiAdd1 = net1.create(multiAddNode);
   number1.setValue("value", 5);
   number2.setValue("value", 8);
   multiAdd1.getPort("values").connect(number1);
   multiAdd1.getPort("values").connect(number2);
   multiAdd1.update();
   assertFalse(multiAdd1.isDirty());
   assertEquals(2, multiAdd1.getPort("values").getValues().size());
   assertEquals(13, multiAdd1.getOutputValue());
   multiAdd1.disconnect();
   assertTrue(multiAdd1.isDirty());
   assertFalse(multiAdd1.isConnected());
   assertFalse(number1.isConnected());
   assertFalse(number2.isConnected());
   multiAdd1.update();
   assertEquals(0, multiAdd1.getPort("values").getValues().size());
   assertEquals(0, multiAdd1.getOutputValue());
 }
 public void insertAfter(Node node, Object object) {
   Node newNode = new Node();
   node.setValue(object);
   int before = node.getAfter() + 1 <= size() ? node.getAfter() + 1 : -1;
   int after = node.getAfter() - 1;
   node.setBefore(before);
   node.setAfter(after);
   list.add(node.getAfter(), newNode);
   for (int i = node.getAfter() + 1; i < size(); i++) {
     get(i).setAfter(get(i).getAfter() + 1);
     get(i).setBefore(get(i).getBefore() + 1);
   }
 }
 public boolean push(T item) {
   boolean result = false;
   Node<T> node = new Node<T>();
   node.setValue(item);
   if (head == null) {
     head = node;
     result = true;
   } else {
     node.setNextNode(head);
     head = node;
   }
   size++;
   return result;
 }
Example #13
0
 public void testDirty() {
   Node n = numberNode.newInstance(testLibrary, "number1");
   assertTrue(n.isDirty());
   n.update();
   assertFalse(n.isDirty());
   n.setValue("value", 12);
   assertTrue(n.isDirty());
   n.update();
   assertFalse(n.isDirty());
   n.getParameter("value").set(13);
   assertTrue(n.isDirty());
   n.update();
   assertFalse(n.isDirty());
 }
Example #14
0
  private Node createPropertySetNode(String varName, String value) {
    Node propSetNode = new Node(/*XMLNS + SOAP.DELIM + */ PROPERTYSET);

    propSetNode.setNameSpace(XMLNS, Subscription.XMLNS);

    Node propNode = new Node(/*XMLNS + SOAP.DELIM + */ PROPERTY);
    propSetNode.addNode(propNode);

    // Thanks for Giordano Sassaroli <*****@*****.**> (05/22/03)
    // Node varNameNode = new Node(XMLNS + SOAP.DELIM + varName);
    Node varNameNode = new Node(varName);
    varNameNode.setValue(value);
    propNode.addNode(varNameNode);

    return propSetNode;
  }
Example #15
0
 /**
  * Replaces the
  *
  * @param index
  * @param value
  * @return
  */
 public Builder set(int index, SEXP value) {
   if (index < 0) {
     throw new IndexOutOfBoundsException("index must be > 0");
   }
   if (head == null) {
     add(Null.INSTANCE);
   }
   Node node = head;
   int nodeIndex = 0;
   while (nodeIndex != index) {
     if (node.nextNode == Null.INSTANCE) {
       add(Null.INSTANCE);
     }
     node = node.getNextNode();
     nodeIndex++;
   }
   node.setValue(value);
   return this;
 }
Example #16
0
  /** 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 int findBestMove(Node currentNode, int best) {
    int turn = currentNode.getBoard().CurrentPlayer();

    // set the current value of the current node to compare with children node
    if (turn != player) currentNode.setValue(Float.POSITIVE_INFINITY);
    else currentNode.setValue(Float.NEGATIVE_INFINITY);

    // Check each pit on your side to find the best move. */
    for (int i = 0; i < 6; i++)
      if (currentNode.getBoard().validMove(i)) {
        try {
          MancalaGameState newBoard = currentNode.getBoard().copy();
          try {
            newBoard.play(i);
          } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }

          Node newNode = new Node(newBoard, currentNode.getDepth() + 1);

          newNode.setParent(currentNode);
          currentNode.setChild(newNode, i);

          // CUT OFF
          if (newNode.getBoard().checkEndGame() || (newNode.getDepth() >= cutoffDepth)) {

            RegressionState aState = new RegressionState(newNode.getBoard(), player);
            newNode.setValue(predictedValue(aState, weight));

          } else findBestMove(newNode, best);

          // alpha-beta pruning
          // AI = MAX
          // pick the child with larger value
          if (currentNode.getBoard().CurrentPlayer() == player) {
            if (currentNode.getChild(i) != null) {
              if (currentNode.getChild(i).getValue() > currentNode.getValue()) {
                currentNode.setValue(currentNode.getChild(i).getValue());
                best = i;
              }
            }
            currentNode.deleteChild(i);

            // alpha cut off if our value is greater than ANY
            // player/MIN parent value

            Node nodePtr = currentNode;
            while (nodePtr.getParent() != null) {
              nodePtr = nodePtr.getParent();
              if ((nodePtr.getBoard().CurrentPlayer != player)
                  && (currentNode.getValue() > nodePtr.getValue())) {
                nodePtr = null;
                return best;
              }
            }

            nodePtr = null;
          }

          // Player = MIN
          // pick the child with smaller value
          if (currentNode.getBoard().CurrentPlayer() != player) {
            if (currentNode.getChild(i) != null) {
              if (currentNode.getChild(i).getValue() < currentNode.getValue()) {
                currentNode.setValue(currentNode.getChild(i).getValue());
                best = i;
              }
            }
            currentNode.deleteChild(i);

            // beta cut off if our value is less than ANY
            // computer/MAX parent value
            Node nodePtr = currentNode;
            while (nodePtr.getParent() != null) {
              nodePtr = nodePtr.getParent();
              if ((nodePtr.getBoard().CurrentPlayer() == player)
                  && (currentNode.getValue() < nodePtr.getValue())) {
                nodePtr = null;
                return best;
              }
            }
            nodePtr = null;
          }
        } catch (java.lang.OutOfMemoryError e) {
          System.out.println("OUT OF MEM");
          return -1;
        }
      }
    return best; // return the best move
  }
Example #18
0
  private Boolean sendMail(HttpServletRequest req, Node node, String email) {
    boolean send = false;

    Cloud cloud = node.getCloud();
    String emailbuilder = "email";
    try {
      Module sendmail = cloud.getCloudContext().getModule("sendmail");
      emailbuilder = sendmail.getProperty("emailbuilder");
    } catch (NotFoundException nfe) {
      log.warn("No email module " + nfe);
    }

    if (cloud.hasNodeManager(emailbuilder)) {

      NodeManager nm = cloud.getNodeManager(emailbuilder);
      Node message = nm.createNode();

      String host = req.getHeader("host");
      if (host == null || "".equals(host)) {
        try {
          host = java.net.InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException uhe) {
          log.warn("No host: " + uhe);
        }
      }
      String from = "downloader@" + host;
      // do a quick check if we've got something more or less valid
      Pattern p = Pattern.compile(".+@.+\\.[a-z]+");
      Matcher m = p.matcher(from);
      if (!m.matches()) {
        from = "*****@*****.**";
      }

      String mediaTitle = node.getStringValue("title");
      String mediaUrl = getProperty(node, URL_KEY);
      StringBuilder body = new StringBuilder();

      body.append("*This is an automated message / Dit is een geautomatiseerd bericht*");
      body.append("\n\n*English*");
      body.append("\n\nDear,");
      body.append("\n\nWe have received your file belonging to media item titled '")
          .append(mediaTitle)
          .append("'. ");
      body.append("In about 1 hour, you can find your submission at: ");
      body.append("http://").append(host).append("/media/").append(node.getNumber());
      body.append("\n\nKind regards,");
      body.append("\n\n").append(host);

      body.append("\n\n\n*Nederlands*");
      body.append("\n\nBeste,");
      body.append("\n\nWe hebben je bestand voor het media item met de titel '")
          .append(mediaTitle)
          .append("' ontvangen. ");
      body.append("Je kunt je bijdrage hier over circa een uur terugvinden: ");
      body.append("http://").append(host).append("/media/").append(node.getNumber());
      body.append("\n\nMet vriendelijke groet,");
      body.append("\n\n").append(host);

      message.setValue("from", from);
      message.setValue("to", email);
      message.setValue("subject", "Download complete / Download voltooid");
      message.setValue("body", body.toString());
      message.commit();

      Function mail = message.getFunction("mail");
      Parameters mail_params = mail.createParameters();
      mail_params.set("type", "oneshot");
      mail.getFunctionValue(mail_params);

      if (log.isDebugEnabled()) {
        log.debug("Message download ready send to: " + email);
      }
      send = true;
    } else {
      log.warn("Can not send message - no emailbuilder installed.");
    }

    return send;
  }
 public Node add(Integer value) {
   Node node = new Node();
   node.setValue(value);
   return add(node);
 }
 /**
  * Update the value in position
  *
  * @param position
  * @param element
  */
 public void set(int position, T element) {
   Node<T> node = getNode(position);
   node.setValue(element);
 }