Skip to content

Commit

Permalink
Merge pull request #560 from opatrascoiu/master
Browse files Browse the repository at this point in the history
Enhance support for Python
  • Loading branch information
opatrascoiu authored Oct 20, 2022
2 parents db1a058 + 81c684d commit eb4851b
Show file tree
Hide file tree
Showing 198 changed files with 3,512 additions and 1,437 deletions.
19 changes: 19 additions & 0 deletions dmn-core/src/main/java/com/gs/dmn/DMNModelRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static com.gs.dmn.ast.TBuiltinAggregator.COUNT;
Expand All @@ -32,6 +34,7 @@
public class DMNModelRepository {
public static final String FREE_TEXT_LANGUAGE = "free_text";
protected static final ObjectFactory OBJECT_FACTORY = new ObjectFactory();
private static Pattern WORD = Pattern.compile("\\w+");

protected static final Logger LOGGER = LoggerFactory.getLogger(DMNModelRepository.class);

Expand Down Expand Up @@ -277,6 +280,10 @@ private List<TItemDefinition> findAllItemDefinitions() {
return this.itemDefinitions;
}

public boolean isRecursiveBKM(TDRGElement element) {
return element instanceof TBusinessKnowledgeModel && isRecursive(((TBusinessKnowledgeModel) element).getEncapsulatedLogic().getExpression(), element.getName());
}

public List<TDRGElement> findDRGElements(TDefinitions definitions) {
List<TDRGElement> result = this.drgElementsByModel.get(definitions);
if (result == null) {
Expand Down Expand Up @@ -1237,4 +1244,16 @@ private void collectCompositeItemDefinitions(List<TItemDefinition> itemDefinitio
}
}

private boolean isRecursive(TExpression exp, String bkm) {
if (exp instanceof TLiteralExpression) {
String text = ((TLiteralExpression) exp).getText();
Matcher matcher = WORD.matcher(text);
while (matcher.find()) {
if (bkm.equals(matcher.group())) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@
import com.gs.dmn.transformation.template.TemplateProvider;
import com.gs.dmn.validation.DMNValidator;

import javax.xml.datatype.Duration;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.gs.dmn.transformation.template.TemplateProvider;
import com.gs.dmn.validation.DMNValidator;

import javax.xml.datatype.Duration;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@
import com.gs.dmn.transformation.template.TemplateProvider;
import com.gs.dmn.validation.DMNValidator;

import javax.xml.datatype.Duration;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetTime;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.OffsetTime;
import java.time.ZonedDateTime;

public class StandardDMNDialectDefinition extends AbstractStandardDMNDialectDefinition<BigDecimal, XMLGregorianCalendar, XMLGregorianCalendar, XMLGregorianCalendar, Duration> {
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import static com.gs.dmn.el.analysis.semantics.type.AnyType.ANY;
import static com.gs.dmn.feel.analysis.semantics.type.BooleanType.BOOLEAN;
import static com.gs.dmn.feel.analysis.semantics.type.ComparableDataType.COMPARABLE;
import static com.gs.dmn.feel.analysis.semantics.type.DateTimeType.DATE_AND_TIME;
import static com.gs.dmn.feel.analysis.semantics.type.DateType.DATE;
import static com.gs.dmn.feel.analysis.semantics.type.DurationType.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ public boolean conformsTo(Type other) {
return false;
}

@Override
public List<FormalParameter<Type, DMNContext>> getParameters() {
throw new SemanticError("Not supported yet");
}

@Override
public List<Type> getParameterTypes() {
throw new SemanticError("Not supported yet");
}

@Override
public Type getReturnType() {
throw new SemanticError("Not supported yet");
}

@Override
public void setReturnType(Type returnType) {
throw new SemanticError("Not supported yet");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Conversion(ConversionKind kind, T elementType) {
this.targetType = elementType;
}

@Override
public ConversionKind getKind() {
return this.kind;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public Expression<T, C> getEndpoint() {
return this.endpoint;
}

@Override
public T getEndpointType() {
return this.endpoint.getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ protected String makeNavigation(Expression<Type, DMNContext> element, Type sourc
String filter = makeNavigation(element, ((ListType) sourceType).getElementType(), "x", memberName, memberVariableName);
return this.nativeFactory.makeCollectionMap(source, filter);
} else if (sourceType instanceof DateType) {
return String.format("%s(%s)", javaMemberFunctionName(memberName), source);
return this.nativeFactory.makeBuiltinFunctionInvocation(propertyFunctionName(memberName), source);
} else if (sourceType instanceof TimeType) {
return String.format("%s(%s)", javaMemberFunctionName(memberName), source);
return this.nativeFactory.makeBuiltinFunctionInvocation(propertyFunctionName(memberName), source);
} else if (sourceType instanceof DateTimeType) {
return String.format("%s(%s)", javaMemberFunctionName(memberName), source);
return this.nativeFactory.makeBuiltinFunctionInvocation(propertyFunctionName(memberName), source);
} else if (sourceType instanceof DurationType) {
return String.format("%s(%s)", javaMemberFunctionName(memberName), source);
return this.nativeFactory.makeBuiltinFunctionInvocation(propertyFunctionName(memberName), source);
} else if (sourceType instanceof RangeType) {
return String.format("%s.%s", source, javaRangeGetter(memberName));
return String.format("%s.%s", source, rangeGetter(memberName));
} else if (sourceType instanceof AnyType) {
// source is Context
return this.nativeFactory.makeContextSelectExpression(this.dmnTransformer.contextClassName(), source, memberName);
Expand All @@ -126,7 +126,7 @@ protected String makeNavigation(Expression<Type, DMNContext> element, Type sourc
}
}

protected String javaMemberFunctionName(String memberName) {
protected String propertyFunctionName(String memberName) {
memberName = NameUtils.removeSingleQuotes(memberName);
if ("time offset".equalsIgnoreCase(memberName)) {
return "timeOffset";
Expand All @@ -135,7 +135,7 @@ protected String javaMemberFunctionName(String memberName) {
}
}

private String javaRangeGetter(String memberName) {
private String rangeGetter(String memberName) {
memberName = NameUtils.removeSingleQuotes(memberName);
if ("start included".equalsIgnoreCase(memberName)) {
return "isStartIncluded()";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
import com.gs.dmn.feel.lib.StringEscapeUtil;
import com.gs.dmn.runtime.DMNRuntimeException;
import com.gs.dmn.runtime.Pair;
import com.gs.dmn.runtime.Range;
import com.gs.dmn.transformation.DMNToJavaTransformer;
import com.gs.dmn.transformation.basic.BasicDMNToNativeTransformer;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -130,12 +128,12 @@ public Object visit(EndpointsRange<Type, DMNContext> element, DMNContext context
Expression<Type, DMNContext> endEndpoint = element.getEnd();
if (context.isExpressionContext()) {
// Evaluate as range
boolean startIncluded = !element.isOpenStart();
boolean endIncluded = !element.isOpenEnd();
String startIncluded = this.nativeFactory.booleanValueLiteral("" + !element.isOpenStart());
String endIncluded = this.nativeFactory.booleanValueLiteral("" + !element.isOpenEnd());
String start = startEndpoint == null ? this.nativeFactory.nullLiteral() : (String) startEndpoint.accept(this, context);
String end = endEndpoint == null ? this.nativeFactory.nullLiteral() : (String) endEndpoint.accept(this, context);

String clsName = this.dmnTransformer.qualifiedName(Range.class);
String clsName = this.dmnTransformer.rangeClassName();
String args = String.format("%s, %s, %s, %s", startIncluded, start, endIncluded, end);
return this.dmnTransformer.constructor(clsName, args);
} else {
Expand Down Expand Up @@ -199,7 +197,7 @@ public Object visit(FormalParameter<Type, DMNContext> element, DMNContext contex
@Override
public Object visit(Context<Type, DMNContext> element, DMNContext context) {
String addMethods = element.getEntries().stream().map(e -> (String) e.accept(this, context)).collect(Collectors.joining(""));
return this.nativeFactory.fluentConstructor(DMNToJavaTransformer.CONTEXT_CLASS_NAME, addMethods);
return this.nativeFactory.fluentConstructor(this.dmnTransformer.contextClassName(), addMethods);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package com.gs.dmn.feel.synthesis.type;

import com.gs.dmn.runtime.DMNRuntimeException;

public abstract class PythonTypeFactory implements NativeTypeFactory {
@Override
public String nullableType(String type) {
Expand All @@ -26,11 +24,11 @@ public String nullableType(String type) {

@Override
public String constructorOfGenericType(String typeName, String... typeParameters) {
throw new DMNRuntimeException("Generic types are not supported for Python");
return typeName;
}

@Override
public String javaClass(String className) {
throw new DMNRuntimeException("Class metadata is not supported for Python");
return String.format("%s.__class__", className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StandardNativeTypeToPythonFactory extends PythonTypeFactory {
private static final Map<String, String> FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE = new LinkedHashMap<>();
static {
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(ENUMERATION.getName(), "str");
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(YEARS_AND_MONTHS_DURATION.getName(), "datetime.timedelta");
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(YEARS_AND_MONTHS_DURATION.getName(), "isodate.Duration");
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(DAYS_AND_TIME_DURATION.getName(), "datetime.timedelta");
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(DATE_AND_TIME.getName(), "datetime.datetime");
FEEL_TYPE_TO_QUALIFIED_JAVA_TYPE.put(TIME.getName(), "datetime.time");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public TFunctionDefinition getFunctionDefinition() {
return functionDefinition;
}

@Override
public Type getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public TInvocable getInvocable() {
return invocable;
}

@Override
public Type getType() {
return type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static DMNVersion inferDMNVersion(Reader from) {
public TDefinitions unmarshal(String input, boolean validateSchema) {
try (Reader firstStringReader = new StringReader(input); Reader secondStringReader = new StringReader(input)) {
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
if (validateSchema) {
if (validateSchema && dmnVersion != null) {
try (StringReader reader = new StringReader(input)) {
validateXMLSchema(new StreamSource(reader), dmnVersion.getSchemaLocation());
}
Expand All @@ -133,7 +133,7 @@ public TDefinitions unmarshal(String input, boolean validateSchema) {
public TDefinitions unmarshal(File input, boolean validateSchema) {
try (Reader firstStringReader = new FileReader(input); Reader secondStringReader = new FileReader(input)) {
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
if (validateSchema) {
if (validateSchema && dmnVersion != null) {
try (FileInputStream inputStream = new FileInputStream(input)) {
validateXMLSchema(new StreamSource(inputStream), dmnVersion.getSchemaLocation());
}
Expand All @@ -149,7 +149,7 @@ public TDefinitions unmarshal(File input, boolean validateSchema) {
public TDefinitions unmarshal(URL input, boolean validateSchema) {
try (Reader firstStringReader = new InputStreamReader(input.openStream()); Reader secondStringReader = new InputStreamReader(input.openStream())) {
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
if (validateSchema) {
if (validateSchema && dmnVersion != null) {
try (InputStreamReader reader = new InputStreamReader(input.openStream())) {
validateXMLSchema(new StreamSource(reader), dmnVersion.getSchemaLocation());
}
Expand All @@ -165,7 +165,7 @@ public TDefinitions unmarshal(URL input, boolean validateSchema) {
public TDefinitions unmarshal(InputStream input, boolean validateSchema) {
try (Reader firstStringReader = new InputStreamReader(input); Reader secondStringReader = new InputStreamReader(input)) {
DMNVersion dmnVersion = inferDMNVersion(firstStringReader);
if (validateSchema) {
if (validateSchema && dmnVersion != null) {
try (InputStreamReader reader = new InputStreamReader(input)) {
validateXMLSchema(new StreamSource(reader), dmnVersion.getSchemaLocation());
}
Expand Down Expand Up @@ -265,6 +265,9 @@ private boolean validateXMLSchema(Source source, String schemaPath) {
try {
URL schemaURL = this.getClass().getClassLoader().getResource(schemaPath).toURI().toURL();
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
// Prohibit the use of all protocols by external entities:
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
Schema schema = factory.newSchema(schemaURL);
Validator validator = schema.newValidator();
validator.validate(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
import java.util.Set;

public class DomConverter implements Converter {
@Override
public boolean canConvert(Class clazz) {
return Document.class.isAssignableFrom(clazz) || Element.class.isAssignableFrom(clazz);
}

@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext unmarshallingContext) {
// Read DOM
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Expand All @@ -61,6 +63,7 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext un
return new NSElement(documentElement, namespace, prefix);
}

@Override
public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) {
// Calculate all element names
NSElement element = (NSElement) object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
writer.addAttribute(TYPE_LANGUAGE, id.getTypeLanguage());
}
if (id.getIsCollectionField() != null) {
writer.addAttribute(IS_COLLECTION, Boolean.valueOf(id.getIsCollectionField()).toString());
writer.addAttribute(IS_COLLECTION, id.getIsCollectionField().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,6 @@ public static String formatQName(QName qname) {
}
}

public static String formatQName(String str) {
QName qname = parseQNameString(str);
if (!XMLConstants.DEFAULT_NS_PREFIX.equals(qname.getPrefix())) {
return qname.getPrefix() + ":" + qname.getLocalPart();
} else {
return qname.toString();
}
}

public static String defineExpressionNodeName(TExpression e) {
String nodeName = "expression";
if (e instanceof TContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
writer.addAttribute("isListedInputData", shape.isIsListedInputData().toString());
}
if (shape.getIsCollapsedField() != null) {
writer.addAttribute("isCollapsed", Boolean.valueOf(shape.getIsCollapsedField()).toString());
writer.addAttribute("isCollapsed", shape.getIsCollapsedField().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
writer.addAttribute(TYPE_LANGUAGE, id.getTypeLanguage());
}
if (id.getIsCollectionField() != null) {
writer.addAttribute(IS_COLLECTION, Boolean.valueOf(id.getIsCollectionField()).toString());
writer.addAttribute(IS_COLLECTION, id.getIsCollectionField().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
writer.addAttribute("isListedInputData", shape.isIsListedInputData().toString());
}
if (shape.getIsCollapsedField() != null) {
writer.addAttribute("isCollapsed", Boolean.valueOf(shape.getIsCollapsedField()).toString());
writer.addAttribute("isCollapsed", shape.getIsCollapsedField().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected void writeAttributes(HierarchicalStreamWriter writer, Object parent) {
writer.addAttribute(TYPE_LANGUAGE, id.getTypeLanguage());
}
if (id.getIsCollectionField() != null) {
writer.addAttribute(IS_COLLECTION, Boolean.valueOf(id.getIsCollectionField()).toString());
writer.addAttribute(IS_COLLECTION, id.getIsCollectionField().toString());
}
}
}
Loading

0 comments on commit eb4851b

Please sign in to comment.