use of org.omg.dmn.tck.marshaller._20160719.ValueType.Component in project tck by dmn-tck.
the class CamundaTCKTest method getValue.
private Object getValue(ValueType valueType) {
final JAXBElement<Object> value = valueType.getValue();
final JAXBElement<ValueType.List> listValue = valueType.getList();
final List<Component> componentValue = valueType.getComponent();
if (value == null && listValue == null && componentValue == null) {
return null;
}
if (listValue != null) {
final List<Object> list = new ArrayList<>();
for (ValueType item : listValue.getValue().getItem()) {
list.add(getValue(item));
}
return list;
}
if (componentValue != null && !componentValue.isEmpty()) {
final Map<String, Object> context = new HashMap<>();
for (Component component : componentValue) {
final Object compValue = getValue(component);
context.put(component.getName(), compValue);
}
return context;
}
if (value instanceof Node) {
final Node node = (Node) value;
final String text = node.getFirstChild().getTextContent();
if ("true".equalsIgnoreCase(text) || "false".equalsIgnoreCase(text)) {
return Boolean.valueOf(text);
}
try {
return Long.valueOf(text);
} catch (NumberFormatException e) {
}
try {
return Double.valueOf(text);
} catch (NumberFormatException e) {
}
return text;
}
if (value != null) {
final Object singleValue = value.getValue();
if (singleValue instanceof XMLGregorianCalendar) {
return transformDateTime((XMLGregorianCalendar) singleValue);
}
if (singleValue instanceof Duration) {
return transformDuration((Duration) singleValue);
}
if (singleValue instanceof String[]) {
String[] array = (String[]) singleValue;
return Arrays.asList(array);
}
return singleValue;
}
throw new RuntimeException(String.format("Unexpected value: '%s'", valueType));
}
use of org.omg.dmn.tck.marshaller._20160719.ValueType.Component in project tck by dmn-tck.
the class DroolsTCKTest method parseType.
private Object parseType(ValueType value, DMNType dmnType) {
if (value.getList() != null && !value.getList().isNil()) {
List<Object> result = new ArrayList<>();
ValueType.List list = value.getList().getValue();
for (ValueType vt : list.getItem()) {
result.add(parseType(vt, (dmnType.isCollection()) ? dmnType : REGISTRY.unknown()));
}
return result;
} else if (isDMNSimpleType(dmnType) || (isDMNAny(dmnType) && isJAXBValue(value) && !isJAXBComponent(value))) {
String text = null;
Object val = value.getValue();
if (val != null && !value.getValue().isNil() && val instanceof JAXBElement<?> && ((JAXBElement<?>) val).getValue() instanceof Node && !isDateTimeOrDuration(((JAXBElement<?>) val).getValue())) {
Node nodeVal = (Node) ((JAXBElement<?>) val).getValue();
if (nodeVal.getFirstChild() != null) {
text = nodeVal.getFirstChild().getTextContent();
}
if (text != null) {
if (isDMNSimpleType(dmnType)) {
return recurseSimpleDMNTypeToFindBuiltInFEELType((BaseDMNTypeImpl) dmnType).fromString(text);
} else {
// no information from DMN; try to use the xml test case file.
Class<?> xmlClass = value.getValue().getDeclaredType();
if (!xmlClass.equals(Object.class) && !xmlClass.equals(String.class)) {
try {
Method m = xmlClass.getMethod("valueOf", String.class);
Object valueOfResult = m.invoke(null, text);
return valueOfResult;
} catch (Exception e) {
return text;
}
} else {
return text;
}
}
} else {
return null;
}
} else if (val instanceof JAXBElement<?> && !(((JAXBElement<?>) val).getValue() instanceof Node) && !isDateTimeOrDuration(((JAXBElement<?>) val).getValue())) {
return ((JAXBElement<?>) val).getValue();
} else {
try {
Object dateTimeOrDurationValue = (val != null) ? ((JAXBElement<?>) val).getValue() : null;
if (dateTimeOrDurationValue instanceof Duration || dateTimeOrDurationValue instanceof XMLGregorianCalendar) {
if (!isDMNAny(dmnType)) {
// need to convert to java.time.* equivalent
text = dateTimeOrDurationValue.toString();
return text != null ? recurseSimpleDMNTypeToFindBuiltInFEELType((BaseDMNTypeImpl) dmnType).fromString(text) : null;
} else {
// no DMN type information from the DMN model
if (dateTimeOrDurationValue instanceof Duration) {
try {
return java.time.Duration.parse(dateTimeOrDurationValue.toString());
} catch (DateTimeParseException e) {
return ComparablePeriod.parse(dateTimeOrDurationValue.toString());
}
} else if (dateTimeOrDurationValue instanceof XMLGregorianCalendar) {
XMLGregorianCalendar xmlCal = (XMLGregorianCalendar) dateTimeOrDurationValue;
if (xmlCal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED && xmlCal.getXMLSchemaType() == DatatypeConstants.DATETIME) {
return ZonedDateTime.parse(xmlCal.toXMLFormat());
} else if (xmlCal.getTimezone() != DatatypeConstants.FIELD_UNDEFINED && xmlCal.getXMLSchemaType() == DatatypeConstants.TIME) {
return OffsetTime.parse(xmlCal.toXMLFormat());
} else if (xmlCal.getXMLSchemaType() == DatatypeConstants.DATETIME) {
return LocalDateTime.of(LocalDate.of(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay()), LocalTime.of(xmlCal.getHour(), xmlCal.getMinute(), xmlCal.getSecond()));
} else if (xmlCal.getXMLSchemaType() == DatatypeConstants.DATE) {
return LocalDate.of(xmlCal.getYear(), xmlCal.getMonth(), xmlCal.getDay());
} else if (xmlCal.getXMLSchemaType() == DatatypeConstants.TIME) {
return LocalTime.parse(xmlCal.toXMLFormat());
}
return xmlCal.toGregorianCalendar();
} else {
return dateTimeOrDurationValue;
}
}
}
} catch (Exception e) {
logger.error("Error trying to coerce JAXB type " + val.getClass().getName() + " with value '" + val.toString() + "': " + e.getMessage());
}
return val;
}
} else if (isDMNCompositeType(dmnType)) {
Map<String, Object> result = new HashMap<>();
if (value.getComponent().size() == 0) {
return null;
}
for (ValueType.Component component : value.getComponent()) {
DMNType fieldType = dmnType.getFields().get(component.getName());
if (!dmnType.getFields().containsKey(component.getName())) {
logger.warn("Error TCK Runner type check: unknown field '" + component.getName() + "' for type '" + dmnType.getName() + "'. This usually happens when a TCK test leverages coercion.");
fieldType = REGISTRY.unknown();
}
if (fieldType == null) {
throw new RuntimeException("Error parsing input: unknown type for field '" + component.getName() + "' on type " + dmnType.getName() + "'");
}
Object fieldValue = parseType(component, fieldType);
result.put(component.getName(), fieldValue);
}
return result;
} else if (isDMNAny(dmnType) && !isJAXBValue(value) && isJAXBComponent(value)) {
Map<String, Object> result = new HashMap<>();
for (ValueType.Component component : value.getComponent()) {
Object fieldValue = parseType(component, REGISTRY.unknown());
result.put(component.getName(), fieldValue);
}
return result;
} else {
throw new RuntimeException("Unable to infer information from JAXB type");
}
}
use of org.omg.dmn.tck.marshaller._20160719.ValueType.Component in project tck by dmn-tck.
the class DmnScalaTCKTest method getValue.
private Object getValue(ValueType valueType) {
final JAXBElement<Object> value = valueType.getValue();
final JAXBElement<ValueType.List> listValue = valueType.getList();
final List<Component> componentValue = valueType.getComponent();
if (value == null && listValue == null && componentValue == null) {
return null;
}
if (listValue != null) {
final List<Object> list = new ArrayList<>();
for (ValueType item : listValue.getValue().getItem()) {
list.add(getValue(item));
}
return list;
}
if (componentValue != null && !componentValue.isEmpty()) {
final Map<String, Object> context = new HashMap<>();
for (Component component : componentValue) {
final Object compValue = getValue(component);
context.put(component.getName(), compValue);
}
return context;
}
if (value instanceof Node) {
final Node node = (Node) value;
final String text = node.getFirstChild().getTextContent();
if ("true".equalsIgnoreCase(text) || "false".equalsIgnoreCase(text)) {
return Boolean.valueOf(text);
}
try {
return Long.valueOf(text);
} catch (NumberFormatException e) {
}
try {
return Double.valueOf(text);
} catch (NumberFormatException e) {
}
return text;
}
if (value != null) {
final Object singleValue = value.getValue();
if (singleValue instanceof XMLGregorianCalendar) {
return transformDateTime((XMLGregorianCalendar) singleValue);
}
if (singleValue instanceof Duration) {
return transformDuration((Duration) singleValue);
}
if (singleValue instanceof String[]) {
String[] array = (String[]) singleValue;
return Arrays.asList(array);
}
return singleValue;
}
throw new RuntimeException(String.format("Unexpected value: '%s'", valueType));
}
Aggregations