Example usage for org.jdom2 Element getContentSize

List of usage examples for org.jdom2 Element getContentSize

Introduction

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

Prototype

@Override
    public int getContentSize() 

Source Link

Usage

From source file:utils.RandomTopo.java

License:Open Source License

/** Generates an XCSP representation of a graph coloring problem
 * @param publicInteragentConstraints    whether inter-agent constraints should be public
 * @param soft                      whether the output should be a Max-DisCSP
 * @param intensional                whether the output should be intensional
 * @return An XCSP-formatted Document/*  www . j  a  v a 2 s .co m*/
 */
public Document toXCSP(final boolean publicInteragentConstraints, final boolean soft,
        final boolean intensional) {

    // Create the root element
    Element probElement = new Element("instance");
    probElement.setAttribute("noNamespaceSchemaLocation",
            "src/frodo2/algorithms/XCSPschema" + (soft || !intensional ? "" : "JaCoP") + ".xsd",
            Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance"));

    // Create the "presentation" element
    Element presElmt = new Element("presentation");
    probElement.addContent(presElmt);
    presElmt.setAttribute("name", this.instanceName);
    presElmt.setAttribute("maxConstraintArity", "2");
    presElmt.setAttribute("maximize", "false");
    presElmt.setAttribute("format", "XCSP 2.1_FRODO");

    // Create the "agents" element
    Element elmt = new Element("agents");
    probElement.addContent(elmt);
    final int nbrNodes = graph.nodes.size();
    elmt.setAttribute("nbAgents", Integer.toString(nbrNodes - this.stochNodes.size()));
    for (String varID : this.graph.nodes) {

        // Skip the stoch nodes
        if (this.stochNodes.contains(varID))
            continue;

        Element subElmt = new Element("agent");
        elmt.addContent(subElmt);
        subElmt.setAttribute("name", "a" + varID);
    }

    // Create the "domains" element
    elmt = new Element("domains");
    probElement.addContent(elmt);
    elmt.setAttribute("nbDomains", "1");
    Element subElmt = new Element("domain");
    elmt.addContent(subElmt);
    subElmt.setAttribute("name", "colors");
    subElmt.setAttribute("nbValues", Integer.toString(nbrColors));
    subElmt.addContent("1.." + Integer.toString(nbrColors));

    // Create the "variables" element
    elmt = new Element("variables");
    probElement.addContent(elmt);
    elmt.setAttribute("nbVariables", Integer.toString(graph.nodes.size()));
    for (String varID : this.graph.nodes) {
        subElmt = new Element("variable");
        elmt.addContent(subElmt);
        subElmt.setAttribute("name", "n" + varID);
        subElmt.setAttribute("domain", "colors");
        if (this.stochNodes.contains(varID))
            subElmt.setAttribute("type", "random");
        else
            subElmt.setAttribute("agent", "a" + varID);
    }

    // Create the "relations" element
    Element relElmt = new Element("relations");
    if (soft || !intensional || !this.unaryCons.isEmpty()) {
        probElement.addContent(relElmt);
    }

    if (soft || !intensional) { // extensional

        subElmt = new Element("relation");
        relElmt.addContent(subElmt);
        subElmt.setAttribute("name", "neq");
        subElmt.setAttribute("semantics", "soft");
        subElmt.setAttribute("arity", "2");
        subElmt.setAttribute("defaultCost", "0");
        subElmt.setAttribute("nbTuples", Integer.toString(nbrColors));

        StringBuilder builder = new StringBuilder(soft ? "1: " : "infinity: ");
        for (int i = 1; i < nbrColors; i++)
            builder.append(Integer.toString(i) + " " + i + " | ");
        builder.append(Integer.toString(nbrColors) + " " + nbrColors);
        subElmt.setText(builder.toString());

    } else { // pure satisfaction, intensional

        // Create the "predicates" element
        elmt = new Element("predicates");
        probElement.addContent(elmt);
        elmt.setAttribute("nbPredicates", "1");

        subElmt = new Element("predicate");
        elmt.addContent(subElmt);
        subElmt.setAttribute("name", "neq");

        elmt = new Element("parameters");
        subElmt.addContent(elmt);
        elmt.setText("int X int Y");

        elmt = new Element("expression");
        subElmt.addContent(elmt);

        subElmt = new Element("functional");
        elmt.addContent(subElmt);
        subElmt.setText("ne(X, Y)");
    }

    if (!this.stochNodes.isEmpty()) {

        // Create the "probabilities" element
        elmt = new Element("probabilities");
        probElement.addContent(elmt);
        elmt.setAttribute("nbProbabilities", Integer.toString(this.stochNodes.size()));

        for (String varID : this.stochNodes) {

            subElmt = new Element("probability");
            elmt.addContent(subElmt);
            subElmt.setAttribute("name", "n" + varID + "proba");
            subElmt.setAttribute("semantics", "soft");
            subElmt.setAttribute("arity", "1");
            subElmt.setAttribute("nbTuples", Integer.toString(nbrColors));

            // Choose a random probability distribution
            StringBuilder builder = new StringBuilder();
            double[] probas = new double[nbrColors];
            double sum = 0.0;
            for (int i = 0; i < nbrColors; i++) {
                probas[i] = Math.random();
                sum += probas[i];
            }
            for (int i = 0; i < nbrColors - 1; i++)
                builder.append(Double.toString(probas[i] / sum) + ": " + (i + 1) + " | ");
            builder.append(Double.toString(probas[nbrColors - 1] / sum) + ": " + nbrColors);
            subElmt.setText(builder.toString());
        }
    }

    // Create the "constraints" element
    Element conElmt = new Element("constraints");
    probElement.addContent(conElmt);

    // Create the unary constraints
    double tightness = 0.0;
    final double weight = 1.0 / (this.nbrColors * (this.graph.nodes.size() - this.stochNodes.size()));
    for (Map.Entry<String, ArrayList<Integer>> entry : this.unaryCons.entrySet()) {
        String n = entry.getKey();
        ArrayList<Integer> colors = entry.getValue();
        tightness += colors.size() * weight;

        // Create the relation
        subElmt = new Element("relation");
        relElmt.addContent(subElmt);
        subElmt.setAttribute("name", "unaryRel_" + n);
        subElmt.setAttribute("semantics", soft || !intensional ? "soft" : "conflicts");
        subElmt.setAttribute("arity", "1");
        if (soft || !intensional)
            subElmt.setAttribute("defaultCost", "0");
        subElmt.setAttribute("nbTuples", Integer.toString(colors.size()));

        StringBuilder builder = new StringBuilder(soft ? "1: " : !intensional ? "infinity: " : "");
        for (int i = colors.size() - 1; i > 0; i--)
            builder.append(colors.get(i)).append("|");
        builder.append(colors.get(0));
        subElmt.setText(builder.toString());

        // Create the constraint
        elmt = new Element("constraint");
        conElmt.addContent(elmt);
        elmt.setAttribute("name", "unaryCons_" + n);
        elmt.setAttribute("scope", "n" + n);
        elmt.setAttribute("arity", "1");
        elmt.setAttribute("reference", "unaryRel_" + n);
        elmt.setAttribute("agent", "a" + n);
        //random volatility
        int vol;
        do {
            vol = (int) Math.round(gaussian ? ((new Random().nextGaussian() + 50)) : ((Math.random() * 100)));
        } while (vol < 0 || vol > 100);
        elmt.setAttribute("volatility", String.valueOf(vol));
    }

    // Go through all edges in the graph
    for (Edge edge : graph.edges) {

        // Skip this constraint if it involves two random variables
        if (this.stochNodes.contains(edge.source) && this.stochNodes.contains(edge.dest))
            continue;

        elmt = new Element("constraint");
        conElmt.addContent(elmt);

        final String n1 = "n" + edge.source;
        final String n2 = "n" + edge.dest;

        elmt.setAttribute("name", n1 + "_neq_" + n2);
        elmt.setAttribute("scope", n1 + " " + n2);
        elmt.setAttribute("arity", "2");
        elmt.setAttribute("reference", "neq");
        if (publicInteragentConstraints)
            elmt.setAttribute("agent", "PUBLIC");

        if (!soft && intensional) {
            subElmt = new Element("parameters");
            elmt.addContent(subElmt);
            subElmt.setText(n1 + " " + n2);
        }
        //random volatility
        int vol;
        do {
            vol = (int) Math
                    .round(gaussian ? ((new Random().nextGaussian() * 100 + 50)) : ((Math.random() * 100)));
        } while (vol < 0 || vol > 100);
        elmt.setAttribute("volatility", String.valueOf(vol));
    }

    // Add the probability distributions
    for (String varID : this.stochNodes) {
        final String varName = "n" + varID;

        elmt = new Element("constraint");
        conElmt.addContent(elmt);
        elmt.setAttribute("name", varName + "dist");
        elmt.setAttribute("scope", varName);
        elmt.setAttribute("arity", "1");
        elmt.setAttribute("reference", varName + "proba");
    }

    relElmt.setAttribute("nbRelations", Integer.toString(relElmt.getContentSize()));
    conElmt.setAttribute("nbConstraints", Integer.toString(conElmt.getContentSize()));

    // Write the stats
    presElmt.addContent(createStats("number of nodes", Integer.toString(this.graph.nodes.size())));
    presElmt.addContent(createStats("target density", Double.toString(this.targetDensity)));
    presElmt.addContent(createStats("true average density", Double.toString(this.graph.computeDensity())));
    presElmt.addContent(createStats("target unary tightness", Double.toString(this.targetTightness)));
    presElmt.addContent(createStats("true average unary tightness", Double.toString(tightness)));
    presElmt.addContent(createStats("number of colors", Integer.toString(this.nbrColors)));
    presElmt.addContent(
            createStats("number of uncontrollable nodes", Integer.toString(this.stochNodes.size())));
    presElmt.addContent(
            createStats("number of disconnected components", Integer.toString(graph.components.size())));
    presElmt.addContent(createStats("max degree", Integer.toString(graph.computeMaxDeg())));

    return new Document(probElement);
}

From source file:wasr.DocNode.java

License:Apache License

private void updateLabel() {
    String oldLabel = getLabel();
    String newLabel = null;//from  w  ww. j  a v  a 2s  .  c  o  m

    String labelFieldID = getTemplateAttribute(ReportTemplate.LABELFIELD_ATTR, null);

    if (labelFieldID != null) {
        Element labelField = element.getChild(labelFieldID);
        if (labelField != null) {
            newLabel = labelField.getContentSize() == 0 ? null : labelField.getText(); // todo: check for CData
        }
    }

    if (newLabel == null || newLabel.equals("")) {
        try {
            newLabel = UserSettings.getSelectedTemplateBundle().getString(id);
        } catch (MissingResourceException e) {
            newLabel = id;
        }
    }

    if (!newLabel.equals(oldLabel)) {
        setUserObject(newLabel);
        DocTreePanel.getInstance().getTreeModel().nodeChanged(this);

    }
}