Example #1
0
  public static List getNcMLElements(String path, Document doc) {

    // XPath doesn't support default namespaces, so we add nc as a prefix for the tags within the
    // namespace!!!
    if (!path.startsWith(NS_PREFIX_ON_TAG) && !path.startsWith("/")) path = NS_PREFIX_ON_TAG + path;

    Pattern pattern = Pattern.compile("/\\w");
    Matcher matcher = pattern.matcher(path);

    StringBuilder sb = new StringBuilder();
    int currentChar = 0;
    while (matcher.find()) {

      sb.append(path.substring(currentChar, matcher.start() - currentChar + 1));
      if (!sb.toString().endsWith("/")) sb.append("/");
      sb.append(NS_PREFIX_ON_TAG);
      currentChar = matcher.start() + 1;
    }

    sb.append(path.substring(currentChar, path.length()));

    XPath xpath;
    try {

      xpath = XPath.newInstance(sb.toString());
      xpath.addNamespace(NS_PREFIX, doc.getRootElement().getNamespaceURI());
      return xpath.selectNodes(doc);

    } catch (JDOMException e) {

      e.printStackTrace();
    }

    return null;
  }
 public List<String> getLocaleDirectories() throws JDOMException {
   List<Element> langAddedList = ((List<Element>) XPath.selectNodes(loopDoc, "//langAdded"));
   List<String> stringLocaleDirectories = new ArrayList<String>();
   List<Element> localeDirectories = new ArrayList<Element>();
   for (Element langAdded : langAddedList) {
     localeDirectories = langAdded.getChildren("language");
     for (Element localeDirectory : localeDirectories) {
       stringLocaleDirectories.add(localeDirectory.getValue());
     }
   }
   return stringLocaleDirectories;
 }
  public Map getEnvironmentDetails(MavenProjectInfo mavenProjectInfo, String phrescoTargetDir)
      throws JDOMException, IOException {
    System.setProperty(
        "javax.xml.parsers.SAXParserFactory",
        "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
    builder = new SAXBuilder();
    // disabling xml validation
    builder.setValidation(false);
    builder.setIgnoringElementContentWhitespace(true);
    envXML =
        builder.build(
            new File(
                mavenProjectInfo.getBaseDir()
                    + File.separator
                    + mavenProjectInfo
                        .getProject()
                        .getProperties()
                        .getProperty("phresco.environment.xml.file.path")));

    // Get server details
    List<Element> serverDetails = ((List<Element>) XPath.selectNodes(envXML, "//Server"));
    System.out.println("serverDetails => " + serverDetails);

    // Get database details
    List<Element> dbDetails = ((List<Element>) XPath.selectNodes(envXML, "//Database"));
    System.out.println("dbDetails => " + dbDetails);

    for (Element server : serverDetails) {
      stringEnvDetails.put("context", server.getChildText("context"));
      stringEnvDetails.put("deploy_dir", server.getChildText("deploy_dir"));
    }

    for (Element db : dbDetails) {
      stringEnvDetails.put("username", db.getChildText("username"));
      stringEnvDetails.put("password", db.getChildText("password"));
      stringEnvDetails.put("host", db.getChildText("host"));
      stringEnvDetails.put("dbname", db.getChildText("dbname"));
    }
    return stringEnvDetails;
  }
 public void getOutputs(Document NNetMap) throws JDOMException {
   Erudite_gui.output = new double[Erudite_gui.outputNID.length];
   int oN = 0;
   do {
     String outNodeID = Erudite_gui.outputNID[oN];
     Element outNode =
         (Element)
             XPath.selectSingleNode(
                 Erudite_gui.NNetMap, "/NNETWORK/SUBNET/LAYER/NEURODE[@N_ID='" + outNodeID + "']");
     Erudite_gui.output[oN] = java.lang.Double.parseDouble(outNode.getAttributeValue("ACTIVITY"));
     oN++;
   } while (oN < Erudite_gui.output.length);
 }
  public void trainNNet(Document nnet) throws JDOMException {
    // 1.search net map for all output layer neurodes, add to list, search for all hidden neurodes
    // append to list, search for all input neurodes append to list
    // 2.cycle through list, get EG. Cycle through list, MT adjust weights.
    java.util.concurrent.ExecutorService executor =
        java.util.concurrent.Executors.newFixedThreadPool(Erudite_gui.maxThreads);
    java.util.List nodeList =
        XPath.newInstance("//LAYER[@LAYER_NAME = 'OUTPUT']/NEURODE")
            .selectNodes(Erudite_gui.NNetMap);
    java.util.List Hnodes =
        XPath.newInstance("//LAYER[@LAYER_NAME = 'HIDDEN']/NEURODE")
            .selectNodes(Erudite_gui.NNetMap);
    nodeList.addAll(Hnodes);

    Iterator nodeEGlist = nodeList.iterator(); // iterate through list and calculate error gradients
    do {
      Element currentNode = (Element) nodeEGlist.next();
      getErrorGradient(currentNode);
    } while (nodeEGlist.hasNext());

    Iterator nodeAWlist = nodeList.iterator(); // iterate through list and adjust weights and biases
    do {
      Element currentNode = (Element) nodeAWlist.next();
      TrainNet train = new TrainNet();
      if (Erudite_gui.jCheckBox2.isSelected()) { // auto-adapting learning rate
        train.learningRate = -1.0;
      }
      train.neurode = currentNode;
      executor.execute(train);
    } while (nodeAWlist.hasNext());

    try {
      executor.shutdown();
      executor.awaitTermination(2, TimeUnit.SECONDS);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 public void clearNNet(Document NNetMap) throws JDOMException {
   java.util.concurrent.ExecutorService executor =
       java.util.concurrent.Executors.newFixedThreadPool(Erudite_gui.maxThreads);
   java.util.List nodes = XPath.newInstance("//NEURODE").selectNodes(NNetMap);
   Iterator itNeurodes = nodes.iterator();
   do {
     Element CurrentNode = (Element) itNeurodes.next();
     ClearNode clear = new ClearNode();
     clear.neurode = CurrentNode;
     executor.execute(clear);
   } while (itNeurodes.hasNext());
   try {
     executor.shutdown();
     executor.awaitTermination(1, TimeUnit.SECONDS);
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  private void loadInputs(Document NNetMap) {
    try {
      // use XPath to find all synapse elements recieving input data
      int iN = 0;
      do {
        String inNodeID = Erudite_gui.inputNID[iN];
        Element inNode =
            (Element)
                XPath.selectSingleNode(
                    Erudite_gui.NNetMap,
                    "/NNETWORK/SUBNET/LAYER/NEURODE[@N_ID='" + inNodeID + "']");
        inNode.setAttribute("ACTIVE", "1");
        inNode.setAttribute("ACTIVITY", String.valueOf(Erudite_gui.input[iN]));
        iN++;
      } while (iN < Erudite_gui.input.length);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  public void runNNet(Document NNetMap) throws JDOMException {
    java.util.concurrent.ExecutorService executor =
        java.util.concurrent.Executors.newFixedThreadPool(Erudite_gui.maxThreads);
    java.util.List nodes =
        XPath.newInstance("//NEURODE[SYNAPSE/@ORG_NEURODE != 'INPUT']").selectNodes(NNetMap);
    Iterator itNeurodes = nodes.iterator();
    do {
      EvaluateNeurode evalN = new EvaluateNeurode();
      Element CurrentNode = (Element) itNeurodes.next(); // get current NEURODE				
      evalN.NNetMap = Erudite_gui.NNetMap;
      evalN.neurode = CurrentNode; // get summed input for current NEURODE
      executor.execute(evalN);
    } while (itNeurodes.hasNext());

    try {
      executor.shutdown();
      executor.awaitTermination(2, TimeUnit.SECONDS);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 private void getErrorGradient(Element node) throws JDOMException {
   // if-then-else for output/hidden/input layers, if-then for transfer functions, compute error
   // gradient and update nnet map
   String layerType = node.getParentElement().getAttributeValue("LAYER_NAME").toString();
   int xfer = Integer.parseInt(node.getParentElement().getAttributeValue("TRANSFER_FUNCTION"));
   String nid = node.getAttributeValue("N_ID").toString();
   if (layerType.equals("OUTPUT")) { // if computing output neurode error gradient
     if (xfer == 1) { // if hyperbolic tangent transfer function
       for (int i = 0; i < Erudite_gui.outputNID.length; i++) {
         if (nid.equals(Erudite_gui.outputNID[i])) {
           double error = Erudite_gui.desiredOutput[i] - Erudite_gui.output[i];
           double errorGradient =
               error * ((1 - Erudite_gui.output[i]) * (1 + Erudite_gui.output[i]));
           node.setAttribute("NNET_V4", String.valueOf(errorGradient));
         }
       }
     } else if (xfer == 2) { // if sigmoid transfer function function
       for (int i = 0; i < Erudite_gui.outputNID.length; i++) {
         if (nid.equals(Erudite_gui.outputNID[i])) {
           double error = Erudite_gui.desiredOutput[i] - Erudite_gui.output[i];
           double errorGradient = error * (Erudite_gui.output[i] * (1 - Erudite_gui.output[i]));
           node.setAttribute("NNET_V4", String.valueOf(errorGradient));
         }
       }
     }
   } else if (layerType.equals("HIDDEN")
       || layerType.equals("INPUT")) { // if computing hidden or input neurode error gradient
     if (xfer == 1) {
       double sumWouts = 0.0;
       double nodeActivity = java.lang.Double.parseDouble(node.getAttributeValue("ACTIVITY"));
       java.util.List outputs =
           XPath.newInstance("//SYNAPSE[@ORG_NEURODE = '" + nid + "']")
               .selectNodes(Erudite_gui.NNetMap);
       Iterator synapseO = outputs.iterator();
       do {
         Element currentNode = (Element) synapseO.next();
         sumWouts +=
             java.lang.Double.parseDouble(currentNode.getAttributeValue("WEIGHT"))
                 * java.lang.Double.parseDouble(
                     currentNode.getParentElement().getAttributeValue("NNET_V4"));
       } while (synapseO.hasNext());
       double errorGradient = sumWouts * ((1 - nodeActivity) * (1 + nodeActivity));
       node.setAttribute("NNET_V4", String.valueOf(errorGradient));
     } else if (xfer == 2) {
       double sumWouts = 0.0;
       double nodeActivity = java.lang.Double.parseDouble(node.getAttributeValue("ACTIVITY"));
       java.util.List outputs =
           XPath.newInstance("//SYNAPSE[@ORG_NEURODE = '" + nid + "']")
               .selectNodes(Erudite_gui.NNetMap);
       Iterator synapseO = outputs.iterator();
       do {
         Element currentNode = (Element) synapseO.next();
         sumWouts +=
             java.lang.Double.parseDouble(currentNode.getAttributeValue("WEIGHT"))
                 * java.lang.Double.parseDouble(
                     currentNode.getParentElement().getAttributeValue("NNET_V4"));
       } while (synapseO.hasNext());
       double errorGradient = sumWouts * (nodeActivity * (1 - nodeActivity));
       node.setAttribute("NNET_V4", String.valueOf(errorGradient));
     }
   }
 }