Skip to content

Commit

Permalink
Merge pull request #664 from goldmansachs/native-visitor
Browse files Browse the repository at this point in the history
Refactor native transformer to use the vistor patttern
  • Loading branch information
opatrascoiu authored May 24, 2024
2 parents a40c11a + e4713f9 commit 9d3a0f1
Show file tree
Hide file tree
Showing 38 changed files with 559 additions and 439 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@

import com.gs.dmn.ast.Visitor;
import com.gs.dmn.error.ErrorHandler;
import com.gs.dmn.log.BuildLogger;

public abstract class AbstractVisitor<C, R> implements Visitor<C, R> {
protected final BuildLogger logger;
protected final ErrorHandler errorHandler;

public AbstractVisitor(ErrorHandler errorHandler) {
public AbstractVisitor(BuildLogger logger, ErrorHandler errorHandler) {
this.logger = logger;
this.errorHandler = errorHandler;
}
}
5 changes: 3 additions & 2 deletions dmn-core/src/main/java/com/gs/dmn/ast/visitor/NopVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import com.gs.dmn.ast.*;
import com.gs.dmn.ast.dmndi.*;
import com.gs.dmn.error.ErrorHandler;
import com.gs.dmn.log.BuildLogger;

public class NopVisitor<C, R> extends AbstractVisitor<C, R> {
public NopVisitor(ErrorHandler errorHandler) {
super(errorHandler);
public NopVisitor(BuildLogger logger, ErrorHandler errorHandler) {
super(logger, errorHandler);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
import com.gs.dmn.ast.*;
import com.gs.dmn.ast.dmndi.*;
import com.gs.dmn.error.ErrorHandler;
import com.gs.dmn.log.BuildLogger;

import javax.xml.namespace.QName;

public class TraversalVisitor<C> extends AbstractVisitor<C, Object> {
public TraversalVisitor(ErrorHandler errorHandler) {
super(errorHandler);
public TraversalVisitor(BuildLogger logger, ErrorHandler errorHandler) {
super(logger, errorHandler);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.gs.dmn.feel.analysis.syntax.ast.expression.textual.FilterExpression;
import com.gs.dmn.feel.analysis.syntax.ast.test.UnaryTests;
import com.gs.dmn.feel.lib.FEELLib;
import com.gs.dmn.log.BuildLogger;
import com.gs.dmn.log.Slf4jBuildLogger;
import com.gs.dmn.runtime.*;
import com.gs.dmn.runtime.annotation.HitPolicy;
import com.gs.dmn.runtime.function.DMNFunction;
Expand Down Expand Up @@ -59,6 +61,8 @@ public static void setEventListener(EventListener eventListener) {

private final DMNModelRepository repository;
private final EnvironmentFactory environmentFactory;

protected final BuildLogger logger;
protected final ErrorHandler errorHandler;

protected final BasicDMNToNativeTransformer<Type, DMNContext> dmnTransformer;
Expand All @@ -69,12 +73,13 @@ public static void setEventListener(EventListener eventListener) {
protected InterpreterVisitor visitor;

public AbstractDMNInterpreter(BasicDMNToNativeTransformer<Type, DMNContext> dmnTransformer, FEELLib<NUMBER, DATE, TIME, DATE_TIME, DURATION> feelLib) {
this.logger = new Slf4jBuildLogger(LOGGER);
this.errorHandler = new LogErrorHandler(LOGGER);
this.dmnTransformer = dmnTransformer;
this.repository = dmnTransformer.getDMNModelRepository();
this.environmentFactory = dmnTransformer.getEnvironmentFactory();
this.feelLib = feelLib;
this.visitor = new InterpreterVisitor(this.errorHandler);
this.visitor = new InterpreterVisitor(logger, this.errorHandler);
}

@Override
Expand Down Expand Up @@ -529,8 +534,8 @@ protected Result evaluateLiteralExpression(String text, DMNContext context) {
}

protected class InterpreterVisitor extends NopVisitor<EvaluationContext, Result> implements ReferenceVisitor<DMNContext, Result> {
public InterpreterVisitor(ErrorHandler errorHandler) {
super(errorHandler);
public InterpreterVisitor(BuildLogger logger, ErrorHandler errorHandler) {
super(logger, errorHandler);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
public abstract class SimpleDMNDialectTransformer {
protected static final Logger LOGGER = LoggerFactory.getLogger(SimpleDMNDialectTransformer.class);

protected final ErrorHandler errorHandler = new LogErrorHandler(LOGGER);
protected final BuildLogger logger;
protected final ErrorHandler errorHandler;
protected final DMNVersion sourceVersion;
protected final DMNVersion targetVersion;
protected final DMNVersionTransformerVisitor visitor;

public SimpleDMNDialectTransformer(BuildLogger logger, DMNVersion sourceVersion, DMNVersion targetVersion) {
this.logger = logger;
this.errorHandler = new LogErrorHandler(LOGGER);
this.sourceVersion = sourceVersion;
this.targetVersion = targetVersion;
this.visitor = new DMNVersionTransformerVisitor(this.errorHandler, sourceVersion, targetVersion);
this.visitor = new DMNVersionTransformerVisitor(logger, this.errorHandler, sourceVersion, targetVersion);
}

public TDefinitions transformDefinitions(TDefinitions sourceDefinitions) {
Expand All @@ -54,8 +55,8 @@ class DMNVersionTransformerVisitor<C> extends TraversalVisitor<C> {
private final DMNVersion targetVersion;
private TDefinitions definitions;

public DMNVersionTransformerVisitor(ErrorHandler errorHandler, DMNVersion sourceVersion, DMNVersion targetVersion) {
super(errorHandler);
public DMNVersionTransformerVisitor(BuildLogger logger, ErrorHandler errorHandler, DMNVersion sourceVersion, DMNVersion targetVersion) {
super(logger, errorHandler);
this.sourceVersion = sourceVersion;
this.targetVersion = targetVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected void transformFile(File file, File root, Path outputPath) {
}
testCasesList = dmnTransformer.transform(basicTransformer.getDMNModelRepository(), testCasesList).getRight();

TemplateProvider templateProvider = templateProcessor.getTemplateProvider();
for (TestCases testCases: testCasesList) {
String javaClassName = testClassName(testCases, basicTransformer);
processTemplate(testCases, templateProvider.testBaseTemplatePath(), templateProvider.testTemplateName(), basicTransformer, outputPath, javaClassName);
Expand All @@ -96,7 +97,7 @@ protected void transformFile(File file, File root, Path outputPath) {
generateExtra(basicTransformer, basicTransformer.getDMNModelRepository(), outputPath);

watch.stop();
logger.info("TCK processing time: " + watch.toString());
logger.info("TCK processing time: " + watch);
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Error during transforming %s.", file.getName()), e);
}
Expand All @@ -111,16 +112,16 @@ protected void processTemplate(TestCases testCases, String baseTemplatePath, Str
String javaPackageName = dmnTransformer.nativeModelPackageName(testCases.getModelName());
String relativeFilePath = javaPackageName.replace('.', '/');
String fileExtension = getFileExtension();
File outputFile = makeOutputFile(outputPath, relativeFilePath, testClassName, fileExtension);
File outputFile = this.templateProcessor.makeOutputFile(outputPath, relativeFilePath, testClassName, fileExtension);

// Make parameters
Map<String, Object> params = makeTemplateParams(testCases, dmnTransformer);
Map<String, Object> params = makeTemplateParams(testCases);
params.put("packageName", javaPackageName);
params.put("testClassName", testClassName);
params.put("decisionBaseClass", decisionBaseClass);

// Process template
processTemplate(baseTemplatePath, templateName, params, outputFile, true);
this.templateProcessor.processTemplate(baseTemplatePath, templateName, params, outputFile, true);
} catch (Exception e) {
throw new DMNRuntimeException(String.format("Cannot process template '%s' for testCases of '%s'", templateName, testCases.getModelName()), e);
}
Expand All @@ -139,7 +140,7 @@ protected String testClassName(TestCases testCases, BasicDMNToNativeTransformer<
}
}

private Map<String, Object> makeTemplateParams(TestCases testCases, BasicDMNToNativeTransformer<Type, DMNContext> dmnTransformer) {
private Map<String, Object> makeTemplateParams(TestCases testCases) {
Map<String, Object> params = new HashMap<>();
params.put("testCases", testCases);
params.put("tckUtil", tckUtil);
Expand Down
Loading

0 comments on commit 9d3a0f1

Please sign in to comment.