Example usage for org.jdom2.filter Filters fdouble

List of usage examples for org.jdom2.filter Filters fdouble

Introduction

In this page you can find the example usage for org.jdom2.filter Filters fdouble.

Prototype

Filter fdouble

To view the source code for org.jdom2.filter Filters fdouble.

Click Source Link

Usage

From source file:com.c4om.autoconf.ulysses.extra.svrlmultipathinterpreter.PartialNodeGenerator.java

License:Apache License

/**
 * Given a selectFromPath, it queries the runtime configuration to get the selectable values pointed by that path
 * and generates the met:possibleValues element that describes it.
 * @param selectFromPath the path to select from (including a document function, relative to the runtime configuration)
 * @param metamodelDoc the metamodel document, to extract probabilities from.
 * @param originalPath the path of the original element at the document
 * @return the desired met:possibleValues
 * @throws IOException If there are I/O errors while loading documents to select values from.
 * @throws JDOMException If there are problems at XML parsing while loading documents to select values from.
 *//*  w w  w  .j a v  a  2  s.c  o m*/
private Element getPossibleValuesElement(String selectFromPath, Document metamodelDoc, String originalPath)
        throws JDOMException, IOException {
    Element possibleValuesElement = new Element("possibleValues",
            AutoconfXMLConstants.NAMESPACE_AUTOCONF_METADATA);

    List<String> gatheredPossibleValues = new ArrayList<>();

    if (selectFromPath.contains("|")) {
        String[] singlePaths = selectFromPath.split("\\|");
        for (int i = 0; i < singlePaths.length; i++) {
            String singlePath = singlePaths[i];
            gatheredPossibleValues.addAll(getSelectableValuesFromSinglePath(singlePath));
        }
    } else {
        gatheredPossibleValues = getSelectableValuesFromSinglePath(selectFromPath);
    }

    for (String possibleValue : gatheredPossibleValues) {
        Element currentPosibleValueElement = new Element("possibleValue",
                AutoconfXMLConstants.NAMESPACE_AUTOCONF_METADATA);
        String queryFrequencyStr = "sum(" + originalPath + "/" + metamodelAttributesPrefix + "Value[./@"
                + metamodelAttributesPrefix + "ValueAttr='" + possibleValue + "']/@" + metamodelAttributesPrefix
                + "Frequency)";
        String queryParentFrequencyStr = "sum(" + originalPath + "/@" + metamodelAttributesPrefix
                + "Frequency)";
        List<Double> queryFrequencyResults;
        List<Double> queryParentFrequencyResults;
        if (metamodelDoc != null) {
            queryFrequencyResults = performJAXENXPath(queryFrequencyStr, metamodelDoc, Filters.fdouble());
            queryParentFrequencyResults = performJAXENXPath(queryParentFrequencyStr, metamodelDoc,
                    Filters.fdouble());

        } else {
            queryFrequencyResults = Collections.emptyList();
            queryParentFrequencyResults = Collections.emptyList();
        }

        if (queryParentFrequencyResults.size() > 0 && queryFrequencyResults.size() > 0) {
            double myFrequency = queryFrequencyResults.get(0);
            double myParentFrequency = queryParentFrequencyResults.get(0);
            double probability = myFrequency / myParentFrequency;
            String formattedProbability = String.format(Locale.US, "%.2f", probability);
            if (!formattedProbability.equalsIgnoreCase("nan")) {
                Attribute probabilityAttribute = new Attribute("probability", formattedProbability);
                currentPosibleValueElement.setAttribute(probabilityAttribute);
            }
        }

        currentPosibleValueElement.setText(possibleValue);
        possibleValuesElement.addContent(currentPosibleValueElement);
    }

    return possibleValuesElement;
}