Skip to content

Commit

Permalink
[#665] Add builder pattern for ExecutionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
opatrascoiu committed May 24, 2024
1 parent a40c11a commit b029863
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ public class ExecutionContext {
private final ExternalFunctionExecutor externalFunctionExecutor;
private final Cache cache;

/**
* @deprecated As of release 8.4.0, replaced by {@link ExecutionContextBuilder}
*/
@Deprecated
public ExecutionContext() {
this.annotations = new AnnotationSet();
this.eventListener = new LoggingEventListener(LOGGER);
this.externalFunctionExecutor = new DefaultExternalFunctionExecutor();
this.cache = new DefaultCache();
}

/**
* @deprecated As of release 8.4.0, replaced by {@link ExecutionContextBuilder}
*/
@Deprecated
public ExecutionContext(AnnotationSet annotations, EventListener eventListener, ExternalFunctionExecutor externalFunctionExecutor, Cache cache) {
this.annotations = annotations;
this.eventListener = eventListener;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2016 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.gs.dmn.runtime;

import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.Cache;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.external.ExternalFunctionExecutor;
import com.gs.dmn.runtime.listener.EventListener;
import com.gs.dmn.runtime.listener.NopEventListener;

public final class ExecutionContextBuilder {
private AnnotationSet annotations = new AnnotationSet();
private EventListener eventListener = new NopEventListener();
private ExternalFunctionExecutor externalFunctionExecutor = new DefaultExternalFunctionExecutor();
private Cache cache = new DefaultCache();

private ExecutionContextBuilder() {
}

public static ExecutionContextBuilder executionContext() {
return new ExecutionContextBuilder();
}

public ExecutionContextBuilder withAnnotations(AnnotationSet annotations) {
this.annotations = annotations;
return this;
}

public ExecutionContextBuilder withEventListener(EventListener eventListener) {
this.eventListener = eventListener;
return this;
}

public ExecutionContextBuilder withExternalFunctionExecutor(ExternalFunctionExecutor externalFunctionExecutor) {
this.externalFunctionExecutor = externalFunctionExecutor;
return this;
}

public ExecutionContextBuilder withCache(Cache cache) {
this.cache = cache;
return this;
}

public ExecutionContext build() {
return new ExecutionContext(annotations, eventListener, externalFunctionExecutor, cache);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016 Goldman Sachs.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.gs.dmn.runtime;

import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.Cache;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.external.ExternalFunctionExecutor;
import com.gs.dmn.runtime.listener.EventListener;
import com.gs.dmn.runtime.listener.NopEventListener;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ExecutionContextBuilderTest {
@Test
void testBuild() {
AnnotationSet annotations = new AnnotationSet();
EventListener eventListener = new NopEventListener();
ExternalFunctionExecutor externalFunctionExecutor = new DefaultExternalFunctionExecutor();
Cache cache = new DefaultCache();
ExecutionContext context = ExecutionContextBuilder.executionContext().
withAnnotations(annotations).
withEventListener(eventListener).
withExternalFunctionExecutor(externalFunctionExecutor).
withCache(cache).
build();

assertEquals(annotations, context.getAnnotations());
assertEquals(eventListener, context.getEventListener());
assertEquals(externalFunctionExecutor, context.getExternalFunctionExecutor());
assertEquals(cache, context.getCache());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.listener.NopEventListener;
import com.gs.dmn.serialization.JsonSerializer;
import org.junit.jupiter.api.BeforeEach;

Expand All @@ -37,7 +35,7 @@ protected String toJson(Object object) {

@BeforeEach
public void setUp() {
this.annotationSet = new AnnotationSet();
this.context = new ExecutionContext(annotationSet, new NopEventListener(), new DefaultExternalFunctionExecutor(), new DefaultCache());
this.context = ExecutionContextBuilder.executionContext().build();
this.annotationSet = context.getAnnotations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import com.gs.dmn.generated.example_credit_decision.type.ApplicantImpl;
import com.gs.dmn.runtime.Assert;
import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.listener.PostorderTraceEventListener;
import com.gs.dmn.runtime.listener.node.DRGElementNode;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,7 +66,7 @@ public void testListenerWithFilter() throws Exception {
}

private List<?> applyDecision(Applicant toApplicant, BigDecimal currentRiskAppetite, BigDecimal lendingThreshold, PostorderTraceEventListener listener) {
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

return decision.apply(toApplicant, currentRiskAppetite, lendingThreshold, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.gs.dmn.generated.example_credit_decision.type.Applicant;
import com.gs.dmn.generated.example_credit_decision.type.ApplicantImpl;
import com.gs.dmn.runtime.Assert;
import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.listener.TreeTraceEventListener;
import com.gs.dmn.runtime.listener.node.DRGElementNode;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,7 +82,7 @@ public void testPostorder() throws Exception {
}

private List<?> applyDecision(Applicant applicant, BigDecimal currentRiskAppetite, BigDecimal lendingThreshold, TreeTraceEventListener listener) {
return decision.apply(applicant, currentRiskAppetite, lendingThreshold, new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache()));
return decision.apply(applicant, currentRiskAppetite, lendingThreshold, ExecutionContextBuilder.executionContext().withEventListener(listener).build());
}

private String getExpectedPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public void testCase1() {
assertEquals("compareAgainstLendingThreshold", annotation.getDecisionName());
assertEquals(1, annotation.getRuleIndex());
assertEquals("Raw issue score is -7.50, Age-weighted score is 60, Acceptance threshold is 25", annotation.getAnnotation());

assertEquals(1, annotationSet.size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.listener.NopEventListener;
import com.gs.dmn.serialization.JsonSerializer;
import org.junit.jupiter.api.BeforeEach;

Expand All @@ -37,7 +35,7 @@ protected String toJson(Object object) {

@BeforeEach
public void setUp() {
this.annotationSet = new AnnotationSet();
this.context = new ExecutionContext(annotationSet, new NopEventListener(), new DefaultExternalFunctionExecutor(), new DefaultCache());
this.context = ExecutionContextBuilder.executionContext().build();
this.annotationSet = context.getAnnotations();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import com.gs.dmn.generated.example_credit_decision.type.ApplicantImpl;
import com.gs.dmn.runtime.Assert;
import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.listener.PostorderTraceEventListener;
import com.gs.dmn.runtime.listener.node.DRGElementNode;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -68,7 +66,7 @@ public void testListenerWithFilter() throws Exception {
}

private List<?> applyDecision(Applicant toApplicant, BigDecimal currentRiskAppetite, BigDecimal lendingThreshold, PostorderTraceEventListener listener) {
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

return decision.apply(toApplicant, currentRiskAppetite, lendingThreshold, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import com.gs.dmn.generated.example_credit_decision.type.Applicant;
import com.gs.dmn.generated.example_credit_decision.type.ApplicantImpl;
import com.gs.dmn.runtime.Assert;
import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.listener.TreeTraceEventListener;
import com.gs.dmn.runtime.listener.node.DRGElementNode;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -85,7 +82,7 @@ public void testPostorder() throws Exception {
}

private List<?> applyDecision(Applicant applicant, BigDecimal currentRiskAppetite, BigDecimal lendingThreshold, TreeTraceEventListener listener) {
return decision.apply(applicant, currentRiskAppetite, lendingThreshold, new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache()));
return decision.apply(applicant, currentRiskAppetite, lendingThreshold, ExecutionContextBuilder.executionContext().withEventListener(listener).build());
}

private String getExpectedPath() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,13 @@
package com.gs.dmn.generated.tck;

import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.Cache;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.external.ExternalFunctionExecutor;
import com.gs.dmn.runtime.listener.EventListener;
import com.gs.dmn.runtime.listener.NopEventListener;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

public abstract class AbstractHandwrittenDecisionTest {
protected AnnotationSet annotationSet;
protected EventListener eventListener;
protected ExternalFunctionExecutor externalFunctionExecutor;
protected Cache cache;
protected ExecutionContext context;

protected abstract void applyDecision();
Expand All @@ -44,10 +34,6 @@ public void testPerformance() {

@BeforeEach
public void setUp() {
this.annotationSet = new AnnotationSet();
this.eventListener = new NopEventListener();
this.externalFunctionExecutor = new DefaultExternalFunctionExecutor();
this.cache = new DefaultCache();
this.context = new ExecutionContext(annotationSet, eventListener, externalFunctionExecutor, cache);
this.context = ExecutionContextBuilder.executionContext().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
package com.gs.dmn.generated.tck.cl3_0020_vacation_days;

import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.listener.PostorderTraceEventListener;
import com.gs.dmn.runtime.listener.node.DRGElementNode;
import org.junit.jupiter.api.Test;
Expand All @@ -35,7 +33,7 @@ public class PostorderTraceListenerTest extends AbstractTraceListenerTest {
@Test
public void testListener() throws Exception {
PostorderTraceEventListener listener = new PostorderTraceEventListener();
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

String expectedResult = "27";
String age = "16";
Expand All @@ -52,7 +50,7 @@ public void testListener() throws Exception {
@Test
public void testListenerWithFilter() throws Exception {
PostorderTraceEventListener listener = new PostorderTraceEventListener(Arrays.asList("Extra days case 1", "Extra days case 2"));
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

String expectedResult = "27";
String age = "16";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
package com.gs.dmn.generated.tck.cl3_0020_vacation_days;

import com.gs.dmn.runtime.ExecutionContext;
import com.gs.dmn.runtime.annotation.AnnotationSet;
import com.gs.dmn.runtime.cache.DefaultCache;
import com.gs.dmn.runtime.external.DefaultExternalFunctionExecutor;
import com.gs.dmn.runtime.ExecutionContextBuilder;
import com.gs.dmn.runtime.listener.TreeTraceEventListener;
import com.gs.dmn.runtime.listener.node.DRGElementNode;
import org.junit.jupiter.api.Test;
Expand All @@ -34,7 +32,7 @@ public class TreeTraceListenerTest extends AbstractTraceListenerTest {
@Test
public void testTree() throws Exception {
TreeTraceEventListener listener = new TreeTraceEventListener();
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

String expectedResult = "27";
String age = "16";
Expand All @@ -51,7 +49,7 @@ public void testTree() throws Exception {
@Test
public void testPreorder() throws Exception {
TreeTraceEventListener listener = new TreeTraceEventListener();
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

String expectedResult = "27";
String age = "16";
Expand All @@ -68,7 +66,7 @@ public void testPreorder() throws Exception {
@Test
public void testPostorder() throws Exception {
TreeTraceEventListener listener = new TreeTraceEventListener();
ExecutionContext context = new ExecutionContext(new AnnotationSet(), listener, new DefaultExternalFunctionExecutor(), new DefaultCache());
ExecutionContext context = ExecutionContextBuilder.executionContext().withEventListener(listener).build();

String expectedResult = "27";
String age = "16";
Expand Down
Loading

0 comments on commit b029863

Please sign in to comment.