- java.lang.Object
-
- com.pervasive.datarush.analytics.pmml.PMML
-
public final class PMML extends Object
Various utility methods for PMML-Dom manipulation
-
-
Field Summary
Fields Modifier and Type Field Description static String
ATT_EXTENSION_NAME
Constant for thename
attribute of the extension element.static String
ATT_EXTENSION_VALUE
Constant for thevalue
attribute of the extension element.static String
ELEM_EXTENSION
Constant for theExtension
element
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Element
addChildElement(Element element, String name)
Creates a new child element and adds it to the given element.static Element
addChildTextElement(Element element, String name, String text)
Adds a child element consisting of purely text.static Element
addExtensionElement(Element element, String name, String value)
Adds a PMML Extension element to the specified element.static Element
findExtensionElement(Element element, String name)
Finds a PMML extension element whose name attribute matches that specified or null if not found.static String
findExtensionValue(Element element, String name)
Finds a PMML extension element whose name attribute matches that specified or null if not found.static BigDecimal
findExtensionValueAsBigDecimal(Element element, String name)
Finds a PMML extension element whose name attribute matches that specified or null if not found.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.static String
getAttribute(Element element, String attrName)
Gets the value of the given attribute from the given element.static BigDecimal
getAttributeAsBigDecimal(Element element, String attrName, BigDecimal dflt)
Gets the value of the given attribute from the given element as a BigDecimal.static boolean
getAttributeAsBoolean(Element element, String attrName, boolean dflt)
Gets the value of the given attribute from the given element as a boolean.static double
getAttributeAsDouble(Element element, String attrName, double dflt)
Gets the value of the given attribute from the given element as a double.static <T extends Enum<T>>
TgetAttributeAsEnum(Element element, String attrName, Class<T> enumClass, T dflt)
Gets the value of the given attribute from the given element as an enum.static int
getAttributeAsInt(Element element, String attrName, int dflt)
Gets the value of the given attribute from the given element as a int.static long
getAttributeAsLong(Element element, String attrName, long dflt)
Gets the value of the given attribute from the given element as a long.static BigDecimal
getAttributeAsOptionalBigDecimal(Element element, String attrName)
Gets the value of the given attribute from the given element as a BigDecimal.static Double
getAttributeAsOptionalDouble(Element element, String attrName)
Gets the value of the given attribute from the given element as a double.static Integer
getAttributeAsOptionalInt(Element element, String attrName)
Gets the value of the given attribute from the given element as a int.static Long
getAttributeAsOptionalLong(Element element, String attrName)
Gets the value of the given attribute from the given element as a long.static List<Element>
getChildElements(Element element, String... filterTagNames)
Returns all of the children elements of the given element.static Document
getDocument(Element element)
Return the Document associated with the given element.static Element
getElementByTagName(Element element, String tagName)
Returns the single child element of the given tag namestatic List<Element>
getElementsByTagName(Element element, String tagName)
Returns the child elements of the given tag name.static String
getElementText(Element element)
Parses the text from an elementstatic BigDecimal
getElementTextAsBigDecimal(Element element, BigDecimal dflt)
Parses the text of the given element as a BigDecimal valuestatic double
getElementTextAsDouble(Element element, double dflt)
Parses the text of the given element as a double valuestatic Element
getSingleChildElement(Element element)
Returns the single child element of the given elementstatic PMMLVersion
getVersion(Document document)
Returns the PMML version of the PMML documentstatic Document
parsePMML(FileClient client, String filePath)
Validates the PMML against the latest schema and parsesstatic void
setAttribute(Element element, String name, double value)
Sets the given attribute to the given value.static void
setAttribute(Element element, String name, long value)
Sets the given attribute to the given value.static void
setAttribute(Element element, String name, Boolean value)
Sets the given attribute to the given value if the value is non null.static void
setAttribute(Element element, String name, Double value)
Sets the given attribute to the given value if the value is non null.static void
setAttribute(Element element, String name, Long value)
Sets the given attribute to the given value if the value is non null.static void
setAttribute(Element element, String name, String value)
Sets the given attribute to the given value if the value is non null.static void
setAttribute(Element element, String name, BigDecimal value)
Sets the given attribute to the given value if the value is non null.static <T extends Enum<T>>
voidsetAttribute(Element element, String name, T value)
Sets the given attribute to the given value if the value is non null.static void
setPercentageNumberAttribute(Element element, String name, Double value)
Sets the given attribute to the given value if the value is non null.static void
setProbNumberAttribute(Element element, String name, Double value)
Sets the given attribute to the given value if the value is non null.
-
-
-
Field Detail
-
ELEM_EXTENSION
public static final String ELEM_EXTENSION
Constant for theExtension
element- See Also:
- Constant Field Values
-
ATT_EXTENSION_NAME
public static final String ATT_EXTENSION_NAME
Constant for thename
attribute of the extension element.- See Also:
- Constant Field Values
-
ATT_EXTENSION_VALUE
public static final String ATT_EXTENSION_VALUE
Constant for thevalue
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 elementtagName
- 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 elementtagName
- 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 elementfilterTagNames
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 addedname
- the name of the attributevalue
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- The name of the attributedflt
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- 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 elementattrName
- The name of the attribute.enumClass
- the type of the enumdflt
- 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 elementname
- the name of the child elementtext
- 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 elementdflt
- 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 elementdflt
- 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 clientfilePath
- the path to the file- Returns:
- the PMML document
-
-