Class PMML


  • public final class PMML
    extends Object
    Various utility methods for PMML-Dom manipulation
    • Field Detail

      • ATT_EXTENSION_NAME

        public static final String ATT_EXTENSION_NAME
        Constant for the name attribute of the extension element.
        See Also:
        Constant Field Values
      • ATT_EXTENSION_VALUE

        public static final String ATT_EXTENSION_VALUE
        Constant for the value attribute of the extension element.
        See Also:
        Constant Field Values
    • Method Detail

      • getDocument

        public static Document getDocument​(Element element)
        Return the Document associated with the given element.
        Parameters:
        element - the element
        Returns:
        the owning document
      • addChildElement

        public static Element addChildElement​(Element element,
                                              String name)
        Creates a new child element and adds it to the given element.
        Parameters:
        element - The element to add to.
        name - The name of the element to add.
        Returns:
        The child element.
      • getElementsByTagName

        public static List<Element> getElementsByTagName​(Element element,
                                                         String tagName)
        Returns the child elements of the given tag name. Note that only immediate children are returned
        Parameters:
        element - the parent element
        tagName - the tag name to search for
        Returns:
        the child elements of the given tag name
      • getElementByTagName

        public static Element getElementByTagName​(Element element,
                                                  String tagName)
        Returns the single child element of the given tag name
        Parameters:
        element - the parent element
        tagName - the tag name
        Returns:
        the single child element or null if there are non
        Throws:
        InvalidPMMLException - if there are multiple children of the given name
      • getChildElements

        public static List<Element> getChildElements​(Element element,
                                                     String... filterTagNames)
        Returns all of the children elements of the given element. If a set of filter tag names are provided, any nodes with the given names are skipped.
        Parameters:
        element - the parent element
        filterTagNames - names of nodes to skip (useful for skipping Extension tags)
        Returns:
        all of the children elements
      • getSingleChildElement

        public static Element getSingleChildElement​(Element element)
        Returns the single child element of the given element
        Parameters:
        element - the parent element
        Returns:
        the single single element or null if there are no children
        Throws:
        InvalidPMMLException - if there is more than one child element
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        String value)
        Sets the given attribute to the given value if the value is non null.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setAttribute

        public static <T extends Enum<T>> void setAttribute​(Element element,
                                                            String name,
                                                            T value)
        Sets the given attribute to the given value if the value is non null.
        Type Parameters:
        T - the type of the enum
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        Long value)
        Sets the given attribute to the given value if the value is non null.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        Double value)
        Sets the given attribute to the given value if the value is non null.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        BigDecimal value)
        Sets the given attribute to the given value if the value is non null.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        Boolean value)
        Sets the given attribute to the given value if the value is non null.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setProbNumberAttribute

        public static void setProbNumberAttribute​(Element element,
                                                  String name,
                                                  Double value)
        Sets the given attribute to the given value if the value is non null. The number will be formatted as a PMML "PROB-NUMBER".
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setPercentageNumberAttribute

        public static void setPercentageNumberAttribute​(Element element,
                                                        String name,
                                                        Double value)
        Sets the given attribute to the given value if the value is non null. The number will be formatted as a PMML "PERCENTAGE-NUMBER"
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute. May be null, in which case this call is ignored.
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        long value)
        Sets the given attribute to the given value.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute.
      • setAttribute

        public static void setAttribute​(Element element,
                                        String name,
                                        double value)
        Sets the given attribute to the given value.
        Parameters:
        element - the element to which the given attribute is being added
        name - the name of the attribute
        value - the value of the attribute.
      • getAttribute

        public static String getAttribute​(Element element,
                                          String attrName)
        Gets the value of the given attribute from the given element. Use in preference to element.getAttribute() since this returns null rather than empty string if it doesn't exist.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        Returns:
        The attribute value or null if it doesn't exist.
      • getAttributeAsDouble

        public static double getAttributeAsDouble​(Element element,
                                                  String attrName,
                                                  double dflt)
        Gets the value of the given attribute from the given element as a double.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        dflt - The default value to use if it doesn't exist
        Returns:
        The value as a double or dflt if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid double
      • getAttributeAsBigDecimal

        public static BigDecimal getAttributeAsBigDecimal​(Element element,
                                                          String attrName,
                                                          BigDecimal dflt)
        Gets the value of the given attribute from the given element as a BigDecimal.
        Parameters:
        element - The element
        attrName - The name of the attribute
        dflt - The default value to use if it doesn't exist
        Returns:
        The value as a BigDecimal or dflt if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid BigDecimal
      • getAttributeAsInt

        public static int getAttributeAsInt​(Element element,
                                            String attrName,
                                            int dflt)
        Gets the value of the given attribute from the given element as a int.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        dflt - The default value to use if it doesn't exist
        Returns:
        The value as a int or dflt if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid int
      • getAttributeAsLong

        public static long getAttributeAsLong​(Element element,
                                              String attrName,
                                              long dflt)
        Gets the value of the given attribute from the given element as a long.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        dflt - The default value to use if it doesn't exist
        Returns:
        The value as a long or dflt if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid long
      • getAttributeAsBoolean

        public static boolean getAttributeAsBoolean​(Element element,
                                                    String attrName,
                                                    boolean dflt)
        Gets the value of the given attribute from the given element as a boolean.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        dflt - The default value to use if it doesn't exist
        Returns:
        The value as a boolean or dflt if it doesn't exist.
      • getAttributeAsOptionalDouble

        public static Double getAttributeAsOptionalDouble​(Element element,
                                                          String attrName)
        Gets the value of the given attribute from the given element as a double.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        Returns:
        The value as a double or null if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid double
      • getAttributeAsOptionalBigDecimal

        public static BigDecimal getAttributeAsOptionalBigDecimal​(Element element,
                                                                  String attrName)
        Gets the value of the given attribute from the given element as a BigDecimal.
        Parameters:
        element - The element
        attrName - The name of the attribute
        Returns:
        The value as a BigDecimal or null if it doesn't exist.
      • getAttributeAsOptionalInt

        public static Integer getAttributeAsOptionalInt​(Element element,
                                                        String attrName)
        Gets the value of the given attribute from the given element as a int.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        Returns:
        The value as a long or null if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid int
      • getAttributeAsOptionalLong

        public static Long getAttributeAsOptionalLong​(Element element,
                                                      String attrName)
        Gets the value of the given attribute from the given element as a long.
        Parameters:
        element - The element
        attrName - The name of the attribute.
        Returns:
        The value as a long or null if it doesn't exist.
        Throws:
        NumberFormatException - If the given attribute is not a valid long
      • getAttributeAsEnum

        public static <T extends Enum<T>> T getAttributeAsEnum​(Element element,
                                                               String attrName,
                                                               Class<T> enumClass,
                                                               T dflt)
        Gets the value of the given attribute from the given element as an enum.
        Type Parameters:
        T - the type of the enum
        Parameters:
        element - The element
        attrName - The name of the attribute.
        enumClass - the type of the enum
        dflt - The default value to use if it doesn't exist
        Returns:
        The value as an enum or dflt if it doesn't exist.
        Throws:
        IllegalArgumentException - If the given attribute is not a valid value of the enum
      • addChildTextElement

        public static Element addChildTextElement​(Element element,
                                                  String name,
                                                  String text)
        Adds a child element consisting of purely text.
        Parameters:
        element - the parent element
        name - the name of the child element
        text - the text of the child element
        Returns:
        a child element
      • getElementText

        public static String getElementText​(Element element)
        Parses the text from an element
        Parameters:
        element - the element
        Returns:
        the text of the element or null if the element does not exist. If the element does exist but has empty text, an empty string will be returned.
      • getElementTextAsDouble

        public static double getElementTextAsDouble​(Element element,
                                                    double dflt)
        Parses the text of the given element as a double value
        Parameters:
        element - the element
        dflt - the default value to return if the element is null
        Returns:
        the element text
      • getElementTextAsBigDecimal

        public static BigDecimal getElementTextAsBigDecimal​(Element element,
                                                            BigDecimal dflt)
        Parses the text of the given element as a BigDecimal value
        Parameters:
        element - the element
        dflt - the default value to return if the element is null
        Returns:
        the element text
      • addExtensionElement

        public static Element addExtensionElement​(Element element,
                                                  String name,
                                                  String value)
        Adds a PMML Extension element to the specified element.
        Parameters:
        element - The element to add to.
        name - The "name" attribute of the extension. May be null.
        value - The "value" attribute of the extension. May be null.
        Returns:
        The Extension element.
      • findExtensionValue

        public static String findExtensionValue​(Element element,
                                                String name)
        Finds a PMML extension element whose name attribute matches that specified or null if not found.
        Parameters:
        element - The parent element to search.
        name - The "name" attribute to match.
        Returns:
        The value attribute of the extension.
      • findExtensionValueAsDouble

        public static Double findExtensionValueAsDouble​(Element element,
                                                        String name,
                                                        Double defaultValue)
        Finds a PMML extension element whose name attribute matches that specified or null if not found.
        Parameters:
        element - The parent element to search.
        name - The "name" attribute to match.
        defaultValue - Returns this value if the element is not found.
        Returns:
        The value attribute of the extension as a double.
      • findExtensionValueAsBigDecimal

        public static BigDecimal findExtensionValueAsBigDecimal​(Element element,
                                                                String name)
        Finds a PMML extension element whose name attribute matches that specified or null if not found.
        Parameters:
        element - The parent element to search.
        name - The "name" attribute to match.
        Returns:
        The value attribute of the extension as a BigDecimal.
      • findExtensionElement

        public static Element findExtensionElement​(Element element,
                                                   String name)
        Finds a PMML extension element whose name attribute matches that specified or null if not found.
        Parameters:
        element - The parent element to search.
        name - The "name" attribute to match.
        Returns:
        The PMML extension element or null if not found.
      • getVersion

        public static PMMLVersion getVersion​(Document document)
        Returns the PMML version of the PMML document
        Parameters:
        document - the PMML document
        Returns:
        the PMML version
      • parsePMML

        public static Document parsePMML​(FileClient client,
                                         String filePath)
        Validates the PMML against the latest schema and parses
        Parameters:
        client - the file client
        filePath - the path to the file
        Returns:
        the PMML document