Example usage for org.jdom2.xpath XPath newInstance

List of usage examples for org.jdom2.xpath XPath newInstance

Introduction

In this page you can find the example usage for org.jdom2.xpath XPath newInstance.

Prototype

public static XPath newInstance(String path) throws JDOMException 

Source Link

Document

Creates a new XPath wrapper object, compiling the specified XPath expression.

Usage

From source file:TrainNet.java

License:Apache License

public void run() { //throws JDOMException

    try {/*www.ja v  a2  s .  c o  m*/
        if (learningRate < 0) { //auto-adapting learning rate
            double numOuts;
            String nid = neurode.getAttributeValue("N_ID");
            java.util.List outList = XPath.newInstance("//SYNAPSE[@ORG_NEURODE='" + nid + "']")
                    .selectNodes(Erudite_gui.NNetMap);
            if (outList != null && outList.size() != 0) {
                numOuts = outList.size();
            } else {
                numOuts = 1 / neurode.getChildren("SYNAPSE").size();
            }
            double adptLearningRate = java.lang.Double
                    .parseDouble(neurode.getParentElement().getParentElement().getAttributeValue("NNET_V2"))
                    + Math.abs(java.lang.Double.parseDouble(neurode.getAttributeValue("NNET_V4")) * numOuts);
            learningRate = adptLearningRate;
        } else {
            learningRate = java.lang.Double
                    .parseDouble(neurode.getParentElement().getParentElement().getAttributeValue("NNET_V2"));
        }
        adjustBias(neurode);
        adjustWeights(neurode);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Erudite_SW_runCmd.java

License:Apache License

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 {// ww w . j a v  a  2  s  . c o m
        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();
    }
}

From source file:Erudite_SW_runCmd.java

License:Apache License

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 {/* w  w  w. j  a va2  s.c o m*/
        Element CurrentNode = (Element) itNeurodes.next(); //get current NEURODE   
        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();
    }

}

From source file:Erudite_SW_trainCmd.java

License:Apache License

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);/*from w  ww.ja  va 2 s  . co m*/

    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();
    }
}

From source file:Erudite_SW_trainCmd.java

License:Apache License

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));
                }/*from   ww w . j  a  va  2s .co  m*/
            }
        } 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));
        }

    }

}

From source file:Erudite_SW_trainCmd.java

License:Apache License

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 {// w  w w.jav a2  s .c o m
        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();
    }

}

From source file:AL_gui.java

License:Apache License

public void loadFile(String pathname, String filename) {

    try {/*from  w  ww . jav a2s .c  om*/
        // Build & creat the document with SAX, use XML schema validation
        URL path = ClassLoader.getSystemResource("ANNeML.xsd");
        if (path.getFile() == null) {
            jLabel2.setForeground(Color.RED);
            jLabel2.setText("error loading XML schema");
        } else {
            //File argylexsd = new File(path.toURI());
            //XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(argylexsd);
            XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory("ANNeML.xsd"); //***for .jar deployment
            SAXBuilder builder = new SAXBuilder(schemafac);
            AL_gui.NNetMap = builder.build(pathname);
            java.util.List subnets = XPath.newInstance("//SUBNET").selectNodes(AL_gui.NNetMap);
            java.util.List layers = XPath.newInstance("//LAYER").selectNodes(AL_gui.NNetMap);
            java.util.List inputNeurodes = XPath.newInstance("//NEURODE[SYNAPSE/@ORG_NEURODE='INPUT']")
                    .selectNodes(AL_gui.NNetMap);
            java.util.List hiddenNeurodes = XPath.newInstance("//LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                    .selectNodes(AL_gui.NNetMap);
            java.util.List outputNeurodes = XPath.newInstance("//LAYER[@LAYER_NAME='OUTPUT']/NEURODE")
                    .selectNodes(AL_gui.NNetMap);
            jLabel2.setForeground(Color.GREEN);
            jLabel2.setText("Valid ANNeML file.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(AL_gui.this, "There was an error parsing the file.\n" + e.toString(),
                "Warning", JOptionPane.WARNING_MESSAGE);
    }

}

From source file:AL_gui.java

License:Apache License

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    // TODO add your handling code here:
    try {/*w  ww . ja  v a2  s  .  co  m*/
        jLabel2.setForeground(Color.BLACK);
        jLabel2.setText("Randomizing synpase weights.");
        java.util.List synapses = XPath.newInstance("//SYNAPSE").selectNodes(AL_gui.NNetMap);

        for (Iterator SynIt = synapses.iterator(); SynIt.hasNext();) {
            Element synapse = (Element) SynIt.next();
            String layerType = synapse.getParentElement().getParentElement().getAttributeValue("LAYER_NAME")
                    .toString();
            if (layerType.equals("INPUT")) {
                synapse.setAttribute("WEIGHT", "1.00");
            } else {
                Random rnd = new Random();
                synapse.setAttribute("WEIGHT", getRandomValue(rnd, low, high, decpl));
            }
        }
        jLabel2.setForeground(Color.BLACK);
        jLabel2.setText("Randomizing neurode biases.");
        java.util.List nodes = XPath.newInstance("//NEURODE").selectNodes(AL_gui.NNetMap);
        for (Iterator It = nodes.iterator(); It.hasNext();) {
            Element node = (Element) It.next();
            String layerType = node.getParentElement().getAttributeValue("LAYER_NAME").toString();
            if (layerType.equals("INPUT")) {
                node.setAttribute("BIAS", "0.00");
            } else {
                Random rnd = new Random();
                node.setAttribute("BIAS", getRandomValue(rnd, low, high, decpl));
            }
        }
        jLabel2.setForeground(Color.BLACK);
        jLabel2.setText("All Done.");

    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(AL_gui.this, "Problem encountered while saving file\n" + e.toString(),
                "Warning", JOptionPane.WARNING_MESSAGE);
    }
}

From source file:AL_gui.java

License:Apache License

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    // TODO add your handling code here:
    try {/*w w w .  ja va2 s.c  o m*/
        String nnetName = JOptionPane.showInputDialog(jButton3, "Enter a filename, excluding extention.",
                "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
        if (nnetName == " ") {
            JOptionPane.showMessageDialog(null, "An input value must be entered.");
        }

        Element nnet = new Element("NNETWORK");
        nnet.setAttribute(new Attribute("noNamespaceSchemaLocation", "ANNeML.xsd",
                Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")));
        nnet.setAttribute(new Attribute("NNET_NAME", nnetName));
        Document doc = new Document(nnet);

        String subnnets = JOptionPane.showInputDialog(jButton3, "How many SUBNET(s)?", "ANNeML Wizard",
                JOptionPane.QUESTION_MESSAGE);
        if (subnnets == " ") {
            JOptionPane.showMessageDialog(null, "An input value must be entered.");
        }
        int numSubs = java.lang.Integer.parseInt(subnnets);
        int i = 0;
        do {
            Element subnet = new Element("SUBNET");
            String learningRate = JOptionPane.showInputDialog(jButton3, "SUBNET learning rate(eta)?",
                    "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
            if (learningRate == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            subnet.setAttribute(new Attribute("NNET_V2", learningRate));
            subnet.setAttribute(new Attribute("SNET_NAME", nnetName + "-subnet" + String.valueOf(i + 1)));
            subnet.setAttribute(new Attribute("ADJUST_LOCK", "0"));

            String input_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<INPUT>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (input_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numInLayers = java.lang.Integer.parseInt(input_layers);
            int x = 0;
            do {
                Element inLayer = new Element("LAYER");
                inLayer.setAttribute(new Attribute("LAYER_NAME", "INPUT"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                inLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String inNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<INPUT>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (inNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numInNodes = java.lang.Integer.parseInt(inNodes);
                int y = 0;
                do {
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "IN" + String.valueOf(x + 1) + String.valueOf(y + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", "0.0"));
                    node.setAttribute(new Attribute("CNAME", "Input node#" + String.valueOf(y + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    Element inSynapse = new Element("SYNAPSE");
                    inSynapse.setAttribute(new Attribute("WEIGHT", "1.00"));
                    inSynapse.setAttribute(new Attribute("ORG_NEURODE", "INPUT"));
                    node.addContent(inSynapse);
                    inLayer.addContent(node);
                    y++;
                } while (y < numInNodes);
                subnet.addContent(inLayer);
                x++;
            } while (x < numInLayers);

            String hidden_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<HIDDEN>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (hidden_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numHLayers = java.lang.Integer.parseInt(hidden_layers);
            int z = 0;
            do {
                Element hLayer = new Element("LAYER");
                hLayer.setAttribute(new Attribute("LAYER_NAME", "HIDDEN"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                hLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String hNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<HIDDEN>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (hNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numhNodes = java.lang.Integer.parseInt(hNodes);
                int a = 0;
                do {
                    Random rnd = new Random();
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "N" + String.valueOf(z + 1) + String.valueOf(a + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", getRandomValue(rnd, low, high, decpl)));
                    node.setAttribute(new Attribute("CNAME", "Hidden node#" + String.valueOf(a + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    hLayer.addContent(node);
                    a++;
                } while (a < numhNodes);
                subnet.addContent(hLayer);
                z++;
            } while (z < numHLayers);

            String output_layers = JOptionPane.showInputDialog(jButton3,
                    "How many <<OUTPUT>> LAYERS(s) in this subnet?", "ANNeML Wizard",
                    JOptionPane.QUESTION_MESSAGE);
            if (hidden_layers == " ") {
                JOptionPane.showMessageDialog(null, "An input value must be entered.");
            }
            int numOLayers = java.lang.Integer.parseInt(output_layers);
            int b = 0;
            do {
                Element oLayer = new Element("LAYER");
                oLayer.setAttribute(new Attribute("LAYER_NAME", "OUTPUT"));
                String transferFunc = JOptionPane.showInputDialog(jButton3,
                        "Which transfer function for this LAYER? 1(hyberbolic tangent) or 2(logarithmic sigmoid)",
                        "ANNeML Wizard", JOptionPane.QUESTION_MESSAGE);
                if (transferFunc == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                oLayer.setAttribute(new Attribute("TRANSFER_FUNCTION", transferFunc));
                String oNodes = JOptionPane.showInputDialog(jButton3,
                        "How many NEURODE(s) in this <<OUTPUT>> LAYER?", "ANNeML Wizard",
                        JOptionPane.QUESTION_MESSAGE);
                if (oNodes == " ") {
                    JOptionPane.showMessageDialog(null, "An input value must be entered.");
                }
                int numoNodes = java.lang.Integer.parseInt(oNodes);
                int d = 0;
                do {
                    Random rnd = new Random();
                    Element node = new Element("NEURODE");
                    node.setAttribute(
                            new Attribute("N_ID", "ON" + String.valueOf(b + 1) + String.valueOf(d + 1)));
                    node.setAttribute(new Attribute("ACTIVE", "-1"));
                    node.setAttribute(new Attribute("ACTIVITY", "0.0"));
                    node.setAttribute(new Attribute("BIAS", getRandomValue(rnd, low, high, decpl)));
                    node.setAttribute(new Attribute("CNAME", "Output node#" + String.valueOf(d + 1)));
                    node.setAttribute(new Attribute("NNET_V4", "0.0"));
                    oLayer.addContent(node);
                    d++;
                } while (d < numoNodes);
                subnet.addContent(oLayer);
                b++;
            } while (b < numOLayers);

            doc.getRootElement().addContent(subnet);
            i++;
        } while (i < numSubs);

        //generate fully interconnected SYNAPSE(s) for all NEURODE(s) within each SUBNET

        java.util.List subnets = XPath.newInstance("//SUBNET").selectNodes(doc);
        Iterator itSubslist = subnets.iterator();
        do {
            Element currentSnet = (Element) itSubslist.next();
            String snetName = currentSnet.getAttributeValue("SNET_NAME");
            //System.out.println(snetName);

            java.util.List Hnodes = XPath
                    .newInstance("//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                    .selectNodes(doc);
            Iterator itHNodelist = Hnodes.iterator();
            do {
                Element node = (Element) itHNodelist.next();
                //System.out.println(node.getAttributeValue("N_ID"));
                java.util.List Inodes = XPath
                        .newInstance(
                                "//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='INPUT']/NEURODE")
                        .selectNodes(doc);
                Iterator itNodelist = Inodes.iterator();
                do {
                    Element currentNode = (Element) itNodelist.next();
                    //System.out.println(currentNode.getAttributeValue("N_ID"));
                    Element hSynapse = new Element("SYNAPSE");
                    Random rnd = new Random();
                    hSynapse.setAttribute(new Attribute("WEIGHT", getRandomValue(rnd, low, high, decpl)));
                    hSynapse.setAttribute(new Attribute("ORG_NEURODE", currentNode.getAttributeValue("N_ID")));
                    node.addContent(hSynapse);
                } while (itNodelist.hasNext());
            } while (itHNodelist.hasNext());

            java.util.List Onodes = XPath
                    .newInstance("//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='OUTPUT']/NEURODE")
                    .selectNodes(doc);
            Iterator itONodelist = Onodes.iterator();
            do {
                Element node = (Element) itONodelist.next();
                //System.out.println(node.getAttributeValue("N_ID"));
                java.util.List hnodes = XPath
                        .newInstance(
                                "//SUBNET[@SNET_NAME='" + snetName + "']/LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                        .selectNodes(doc);
                Iterator itNodelist = hnodes.iterator();
                do {
                    Element currentNode = (Element) itNodelist.next();
                    //System.out.println(currentNode.getAttributeValue("N_ID"));
                    Element hSynapse = new Element("SYNAPSE");
                    Random rnd = new Random();
                    hSynapse.setAttribute(new Attribute("WEIGHT", getRandomValue(rnd, low, high, decpl)));
                    hSynapse.setAttribute(new Attribute("ORG_NEURODE", currentNode.getAttributeValue("N_ID")));
                    node.addContent(hSynapse);
                } while (itNodelist.hasNext());
            } while (itONodelist.hasNext());

        } while (itSubslist.hasNext());

        // new XMLOutputter().output(doc, System.out);
        XMLOutputter xmlOutput = new XMLOutputter();

        // display nice nice
        xmlOutput.setFormat(Format.getPrettyFormat());
        xmlOutput.output(doc, System.out);
        xmlOutput.output(doc, new FileWriter(nnetName + ".xml"));

        System.out.println("File Saved!");

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

}

From source file:Erudite_gui.java

License:Apache License

public void loadFile(String pathname, String filename) {
    jLabel6.setEnabled(true);/*from w w  w.  jav  a 2  s.  c o  m*/
    jLabel7.setEnabled(true);
    jButton3.setEnabled(true);
    jButton4.setEnabled(true);
    jCheckBox1.setSelected(false);
    jCheckBox1.setEnabled(true);
    jEditorPane1.setEnabled(true);
    DefaultTableModel tblmodel = (DefaultTableModel) jTable2.getModel();
    tblmodel.setRowCount(0);

    try {
        // Build & creat the document with SAX, use XML schema validation
        URL path = ClassLoader.getSystemResource("ANNeML.xsd");
        if (path.getFile() == null) {
            jLabel2.setForeground(Color.RED);
            jLabel2.setText("error loading XML schema");
        } else {
            //File argylexsd = new File(path.toURI());
            //XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory(argylexsd);
            XMLReaderJDOMFactory schemafac = new XMLReaderXSDFactory("ANNeML.xsd"); //***for .jar deployment
            SAXBuilder builder = new SAXBuilder(schemafac);
            Erudite_gui.NNetMap = builder.build(pathname);
            JDOMToTreeModelAdapter model = new JDOMToTreeModelAdapter(Erudite_gui.NNetMap);
            XMLTreeCellRenderer renderer = new XMLTreeCellRenderer();
            jTree1.setCellRenderer(renderer);
            jTree1.setModel(model);
            java.util.List subnets = XPath.newInstance("//SUBNET").selectNodes(Erudite_gui.NNetMap);
            java.util.List layers = XPath.newInstance("//LAYER").selectNodes(Erudite_gui.NNetMap);
            java.util.List inputNeurodes = XPath.newInstance("//NEURODE[SYNAPSE/@ORG_NEURODE='INPUT']")
                    .selectNodes(Erudite_gui.NNetMap);
            java.util.List hiddenNeurodes = XPath.newInstance("//LAYER[@LAYER_NAME='HIDDEN']/NEURODE")
                    .selectNodes(Erudite_gui.NNetMap);
            java.util.List outputNeurodes = XPath.newInstance("//LAYER[@LAYER_NAME='OUTPUT']/NEURODE")
                    .selectNodes(Erudite_gui.NNetMap);
            Color colr = new Color(0, 153, 255);
            jLabel2.setForeground(colr);
            jLabel2.setText("Valid ANNeML file.");
            HTMLEditorKit kit = (HTMLEditorKit) jEditorPane1.getEditorKit();
            HTMLDocument doc = (HTMLDocument) jEditorPane1.getDocument();
            kit.insertHTML(doc, jEditorPane1.getCaretPosition(), "<b>" + filename + " loaded...</b>", 0, 0,
                    null);
            jLabel3.setText("Subnet(s): " + subnets.size() + "  Layers: " + layers.size() + "  Inputs: "
                    + inputNeurodes.size() + "  Hidden: " + hiddenNeurodes.size() + "  Outputs: "
                    + outputNeurodes.size());
            jLabel7.setText(java.lang.String.valueOf(inputNeurodes.size()));
            Erudite_gui.inputNID = new String[inputNeurodes.size()];
            Erudite_gui.inputCNAME = new String[inputNeurodes.size()];
            Erudite_gui.outputNID = new String[outputNeurodes.size()];
            Erudite_gui.outputCNAME = new String[outputNeurodes.size()];
            int i = 0;
            for (Iterator it = inputNeurodes.iterator(); it.hasNext();) {
                Element InputNode = (Element) it.next();
                Erudite_gui.inputNID[i] = InputNode.getAttributeValue("N_ID");
                Erudite_gui.inputCNAME[i] = InputNode.getAttributeValue("CNAME");
                tblmodel.addRow(
                        new String[] { null, Erudite_gui.inputNID[i] + " '" + inputCNAME[i] + "' ", null });
                kit.insertHTML(doc, jEditorPane1.getCaretPosition(),
                        "<b>" + inputNID[i] + "</b> " + inputCNAME[i] + "<br>", 0, 0, null);
                i++;
            }
            int y = 0;
            for (Iterator it = outputNeurodes.iterator(); it.hasNext();) {
                Element OutputNode = (Element) it.next();
                Erudite_gui.outputCNAME[y] = OutputNode.getAttributeValue("CNAME");
                Erudite_gui.outputNID[y] = OutputNode.getAttributeValue("N_ID");
                kit.insertHTML(doc, jEditorPane1.getCaretPosition(),
                        "<b>" + outputNID[y] + "</b> " + outputCNAME[y] + "<br>", 0, 0, null);
                y++;
            }
            kit.insertHTML(doc, jEditorPane1.getCaretPosition(),
                    "Ready for input processing or training...<br><br>", 0, 0, null);
        }
    } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(jPanel1, "There was an error parsing the file.\n" + e.toString(),
                "Warning", JOptionPane.WARNING_MESSAGE);
    }

}