Example usage for org.jdom2 Element setAttribute

List of usage examples for org.jdom2 Element setAttribute

Introduction

In this page you can find the example usage for org.jdom2 Element setAttribute.

Prototype

public Element setAttribute(final String name, final String value) 

Source Link

Document

This sets an attribute value for this element.

Usage

From source file:TrainNet.java

License:Apache License

public void adjustWeights(Element node) throws JDOMException {
    //each backward connection's weight to equal current weight + LEARNING_RATE * output of neuron at other end of connection * this neuron's errorGradient
    int lock = java.lang.Integer
            .parseInt(node.getParentElement().getParentElement().getAttributeValue("ADJUST_LOCK"));
    Iterator synapses = node.getChildren().iterator(); //iterate through incoming synapses and adjust weights
    do {//from  w w w  . j  ava  2s .co m
        Element currentSynapse = (Element) synapses.next();
        String OrgNodeID = currentSynapse.getAttributeValue("ORG_NEURODE");
        //if OrgNode !=input then continue else abort/return
        if (!"INPUT".equals(OrgNodeID) && lock != 1) {
            Element OrgNode = (Element) XPath.selectSingleNode(Erudite_gui.NNetMap,
                    "/NNETWORK/SUBNET/LAYER/NEURODE[@N_ID='" + OrgNodeID + "']");
            double weight = java.lang.Double.parseDouble(currentSynapse.getAttributeValue("WEIGHT"))
                    + (learningRate * (java.lang.Double.parseDouble(OrgNode.getAttributeValue("ACTIVITY"))
                            * java.lang.Double.parseDouble(node.getAttributeValue("NNET_V4"))));
            currentSynapse.setAttribute("WEIGHT", String.valueOf(weight));
        }
    } while (synapses.hasNext());
}

From source file:TrainNet.java

License:Apache License

public void adjustBias(Element node) throws JDOMException {
    //adjusted bias = current bias + LEARNING_RATE * errorGradient
    int lock = java.lang.Integer
            .parseInt(node.getParentElement().getParentElement().getAttributeValue("ADJUST_LOCK"));
    Element currentSynapse = (Element) node.getChild("SYNAPSE");
    String OrgNodeID = currentSynapse.getAttributeValue("ORG_NEURODE");
    if (!"INPUT".equals(OrgNodeID) && lock != 1) {
        double bias = java.lang.Double.parseDouble(node.getAttributeValue("BIAS"))
                + (learningRate * java.lang.Double.parseDouble(node.getAttributeValue("NNET_V4"))); //original ver
        node.setAttribute("BIAS", String.valueOf(bias));
    }/*  w ww . java 2 s .  c om*/
}

From source file:Erudite_SW_runCmd.java

License:Apache License

private void loadInputs(Document NNetMap) {
    try {//ww  w.j  av a  2 s.c om
        //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();
    }

}

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   w  w w  .  java 2 s  .  c  o  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:EvaluateNeurode.java

License:Apache License

public synchronized void output(Element node, double summedInputs) throws JDOMException {
    int xfer = Integer.parseInt(node.getParentElement().getAttributeValue("TRANSFER_FUNCTION"));
    if (xfer == 1) {
        double output = transferFunction(summedInputs);
        node.setAttribute("ACTIVITY", String.valueOf(output));
        node.setAttribute("ACTIVE", "1");
    } else if (xfer == 2) {
        double output = transferFunction2(summedInputs);
        node.setAttribute("ACTIVITY", String.valueOf(output));
        node.setAttribute("ACTIVE", "1");
    }//from ww  w . ja v a 2s .  c o m

}

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 {/*from  w  ww  . j a v a2s  .c o 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:apps.configurexml.SystemConsoleConfigPanelXml.java

License:Open Source License

/**
 * Arrange for console settings to be stored
 *
 * @param o Object to store, of type SystemConsole
 * @return Element containing the complete info
 *//* www. jav  a 2 s  .c  o m*/
@Override
public Element store(Object o) {

    Element e = new Element("console");
    e.setAttribute("class", this.getClass().getName());
    e.setAttribute("scheme", "" + SystemConsole.getInstance().getScheme());
    e.setAttribute("fontfamily", "" + SystemConsole.getInstance().getFontFamily());
    e.setAttribute("fontsize", "" + SystemConsole.getInstance().getFontSize());
    e.setAttribute("fontstyle", "" + SystemConsole.getInstance().getFontStyle());
    e.setAttribute("wrapstyle", "" + SystemConsole.getInstance().getWrapStyle());

    return e;
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.DOMFormulaConverter.java

License:Open Source License

@Override
public Formula parse(int id, String latexFormula) {
    Formula formula = super.parseToMathML(id, latexFormula);

    // Generate output
    Element div = new Element("div");

    Element html = new Element("div");
    html.setAttribute("class", "math");

    if (formula.isInvalid() == false) {
        SAXBuilder builder = new SAXBuilder();

        try {//  w ww . j  a  v a2s . c  o m
            Document mathml = builder.build(new StringReader(formula.getMathMl()));

            Element root = mathml.getRootElement();
            if (root.getChildren().isEmpty()) {
                return null;
            }

            Iterator<Element> it = root.getChildren().iterator();

            while (it.hasNext()) {
                Element cur = it.next();
                FormulaElement formulaElement = renderElement(cur);
                if (formulaElement != null) {
                    Element resultHtml = formulaElement.render(null, null);
                    if (resultHtml != null) {
                        html.addContent(resultHtml);
                    } else {
                        logger.debug("HTML is NULL: " + cur.getName());
                    }
                }
            }

        } catch (JDOMException e) {
            logger.error("Error parsing generated MathML:");
            logger.error(formula.getMathMl());
            logger.error(e.getMessage(), e);

        } catch (IOException e) {
            logger.error("Error reading generated MathML:");
            logger.error(formula.getMathMl());
            logger.error(e.getMessage(), e);
        }
    } else {
        html.addContent(renderInvalidFormulaSource(formula));
    }
    div.addContent(html);
    formula.setHtml(div);

    return formula;
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.elements.layout.Mfenced.java

License:Open Source License

@Override
public Element render(FormulaElement parent, List<FormulaElement> siblings) {
    Element fencedSpan = new Element("span");
    fencedSpan.setAttribute("class", "mfenced");

    // Opening fence
    Element openFenceSpan = new Element("span");
    openFenceSpan.setAttribute("class", "mfenced-open");
    openFenceSpan.setText(opened);//from  www .j  a  v a 2 s  . c  o m
    fencedSpan.addContent(openFenceSpan);

    // Content
    if (content.isEmpty() == false) {

        // if this is a fenced table or matrix, just render the content element
        // pass information about the fence via parent reference
        if (content.size() == 1 && content.get(0) instanceof Mtable) {
            Mtable fencedTableOrMatrix = (Mtable) content.get(0);
            return fencedTableOrMatrix.render(this, null);
        }

        String tempSeparators = separators.replaceAll(" ", "");

        for (int i = 0; i < content.size(); i++) {
            FormulaElement element = content.get(i);

            Element contentSpan = new Element("span");
            contentSpan.setAttribute("class", "mfenced-content");
            contentSpan.addContent(element.render(null, null));
            fencedSpan.addContent(contentSpan);

            // Separators
            if (content.size() > 1) {
                Mo separatorElement = new Mo();
                separatorElement.setSeparator(true);

                String separator = SEPARATOR;
                if (tempSeparators.length() == 1) {
                    separator = tempSeparators;
                } else if (i < tempSeparators.length()) {
                    separator = Character.toString(tempSeparators.charAt(i));

                    // Entity lookup
                    if (separator.length() > 1) {
                        String entityName = separator.substring(1, separator.length() - 1);
                        if (MathmlCharacterDictionary.entityMapByName.containsKey(entityName)) {
                            separator = MathmlCharacterDictionary.entityMapByName.get(entityName);
                        }
                    }
                }

                separatorElement.setValue(separator);

                Element mo = separatorElement.render(this, null);
                mo.setAttribute("class", mo.getAttributeValue("class") + " mfenced-separator");
                fencedSpan.addContent(mo);
            }
        }
    }

    // Closing fence
    Element closeFenceSpan = new Element("span");
    closeFenceSpan.setAttribute("class", "mfenced-close");
    closeFenceSpan.setText(closed);
    fencedSpan.addContent(closeFenceSpan);

    return fencedSpan;
}

From source file:at.ac.tuwien.ims.latex2mobiformulaconv.converter.mathml2html.elements.layout.Mfrac.java

License:Open Source License

@Override
public Element render(FormulaElement parent, List<FormulaElement> siblings) {
    Element fraction = new Element("span");
    fraction.setAttribute("class", "mfrac");

    Element numeratorSpan = new Element("span");
    numeratorSpan.setAttribute("class", "numerator");

    List<FormulaElement> siblingsList = new ArrayList<>();
    siblingsList.add(numerator);//from www  . j av a  2 s  .  co m
    siblingsList.add(denominator);

    numeratorSpan.addContent(numerator.render(this, siblingsList));

    Element denominatorSpan = new Element("span");
    denominatorSpan.setAttribute("class", "denominator");

    // style for linethickness

    //  medium (same as 1), thin (thinner than 1, otherwise up to the renderer), or thick (thicker than 1, otherwise up to the renderer).
    switch (linethickness) {
    case "medium":
        linethickness = "1";
        break;
    case "thin":
        linethickness = "0.75";
        break;
    case "thick":
        linethickness = "2";
        break;
    default:
        try {
            Double.valueOf(linethickness);
        } catch (NumberFormatException e) {
            linethickness = "1";
        }
    }

    denominatorSpan.setAttribute("style", "border-top: " + linethickness + "px solid black;");

    denominatorSpan.addContent(this.denominator.render(this, siblingsList));

    fraction.addContent(numeratorSpan);
    fraction.addContent(denominatorSpan);

    return fraction;
}