-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[incubator-kie-issues#1478] Register DRG callbacks to assigned models from DMNCompilerImpl #6105
base: main
Are you sure you want to change the base?
Changes from 5 commits
a713f41
41cb9a1
b497d72
7ebf79b
c4bf372
723eff3
099528a
1bdab9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,9 +41,7 @@ | |
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.xml.namespace.QName; | ||
|
||
import org.drools.io.FileSystemResource; | ||
import org.kie.api.io.Resource; | ||
import org.kie.dmn.api.core.DMNCompiler; | ||
|
@@ -113,8 +111,8 @@ public class DMNCompilerImpl implements DMNCompiler { | |
private static final Logger logger = LoggerFactory.getLogger( DMNCompilerImpl.class ); | ||
|
||
private final DMNDecisionLogicCompiler evaluatorCompiler; | ||
private DMNCompilerConfiguration dmnCompilerConfig; | ||
private Deque<DRGElementCompiler> drgCompilers = new LinkedList<>(); | ||
private final DMNCompilerConfiguration dmnCompilerConfig; | ||
private final Deque<DRGElementCompiler> drgCompilers = new LinkedList<>(); | ||
{ | ||
drgCompilers.add( new InputDataCompiler() ); | ||
drgCompilers.add( new BusinessKnowledgeModelCompiler() ); | ||
|
@@ -123,6 +121,7 @@ public class DMNCompilerImpl implements DMNCompiler { | |
drgCompilers.add( new KnowledgeSourceCompiler() ); // keep last as it's a void compiler | ||
} | ||
private final List<AfterProcessDrgElements> afterDRGcallbacks = new ArrayList<>(); | ||
private final Map<DMNModel, List<AfterProcessDrgElements>> afterDRGcallbacksForModel = new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @samuel-beniamin |
||
private final static Pattern QNAME_PAT = Pattern.compile("(\\{([^\\}]*)\\})?(([^:]*):)?(.*)"); | ||
|
||
public DMNCompilerImpl() { | ||
|
@@ -477,6 +476,9 @@ private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Defi | |
} | ||
} | ||
|
||
afterDRGcallbacksForModel.getOrDefault(model, Collections.emptyList()). | ||
forEach(processDrgElements -> processDrgElements.callback(this, ctx, model)); | ||
|
||
for (AfterProcessDrgElements callback : afterDRGcallbacks) { | ||
logger.debug("About to invoke callback: {}", callback); | ||
callback.callback(this, ctx, model); | ||
|
@@ -491,9 +493,30 @@ private void processDrgElements(DMNCompilerContext ctx, DMNModelImpl model, Defi | |
public interface AfterProcessDrgElements { | ||
void callback(DMNCompilerImpl compiler, DMNCompilerContext ctx, DMNModelImpl model); | ||
} | ||
|
||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @samuel-beniamin |
||
* Adds a callback that will be invoked after the DRG elements have been processed, for all the following models that are compiled with this compiler instance. | ||
* Which may lead to the callback being called multiple times, and for unrelated models. Unless, the callback is registered to a specific model using: | ||
* {@link #addCallbackForModel(AfterProcessDrgElements, DMNModel)}. | ||
*/ | ||
public void addCallback(AfterProcessDrgElements callback) { | ||
this.afterDRGcallbacks.add(callback); | ||
afterDRGcallbacks.add(callback); | ||
} | ||
|
||
|
||
/** | ||
* Adds a callback that will be invoked after the DRG elements have been processed, for a given model. | ||
*/ | ||
public void addCallbackForModel(AfterProcessDrgElements callback, DMNModel model) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @samuel-beniamin |
||
this.afterDRGcallbacksForModel.compute(model, (k, v) -> { | ||
if (v == null) { | ||
v = new ArrayList<>(); | ||
} | ||
if (callback != null) { | ||
v.add(callback); | ||
} | ||
return v; | ||
}); | ||
} | ||
|
||
private void detectCycles( DMNModelImpl model ) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,29 +18,51 @@ | |
*/ | ||
package org.kie.dmn.core.compiler; | ||
|
||
import java.io.StringReader; | ||
import java.util.Collections; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.kie.dmn.api.core.DMNCompilerConfiguration; | ||
import org.kie.dmn.api.core.DMNModel; | ||
import org.kie.dmn.core.api.DMNFactory; | ||
import org.kie.dmn.core.impl.DMNModelImpl; | ||
import org.kie.dmn.model.api.DMNElementReference; | ||
import org.kie.dmn.model.api.Definitions; | ||
import org.kie.dmn.model.api.InformationRequirement; | ||
import org.kie.dmn.model.v1_5.TDMNElementReference; | ||
import org.kie.dmn.model.v1_5.TDefinitions; | ||
import org.kie.dmn.model.v1_5.TInformationRequirement; | ||
import org.mockito.Mockito; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
|
||
class DMNCompilerImplTest { | ||
|
||
private static final String nameSpace = "http://www.montera.com.au/spec/DMN/local-hrefs"; | ||
private static Definitions parent; | ||
private static DMNCompilerImpl dmnCompiler; | ||
private static DMNCompilerImpl.AfterProcessDrgElements mockCallback; | ||
private static DMNCompilerImpl.AfterProcessDrgElements mockCallbackForModel; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
String modelName = "LocalHrefs"; | ||
parent = new TDefinitions(); | ||
parent.setName(modelName); | ||
parent.setNamespace(nameSpace); | ||
|
||
DMNCompilerConfiguration config = DMNFactory.newCompilerConfiguration(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @samuel-beniamin |
||
dmnCompiler = new DMNCompilerImpl(config); | ||
mockCallback = Mockito.mock(DMNCompilerImpl.AfterProcessDrgElements.class); | ||
dmnCompiler.addCallback(mockCallback); | ||
mockCallbackForModel = Mockito.mock(DMNCompilerImpl.AfterProcessDrgElements.class); | ||
} | ||
|
||
@Test | ||
|
@@ -76,4 +98,27 @@ void getRootElement() { | |
retrieved = DMNCompilerImpl.getRootElement(elementReference); | ||
assertThat(retrieved).isNotNull().isEqualTo(parent); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @samuel-beniamin |
||
@Test | ||
void testCompileWithCallback() { | ||
String dmnXml = """ | ||
<definitions xmlns="http://www.omg.org/spec/DMN/20151101/dmn.xsd" id="definitions" name="definitions" namespace="http://www.trisotech.com/definitions/_f52ca843-504b-4c3b-a6bc-4d377bffef7a"> | ||
<decision id="decision" name="Decision"> | ||
<variable id="variable" name="Decision" typeRef="string"/> | ||
</decision> | ||
</definitions>"""; | ||
DMNModel mockModel = mock(DMNModel.class); | ||
dmnCompiler.addCallbackForModel(mockCallbackForModel, mockModel); | ||
|
||
|
||
Definitions definitions = dmnCompiler.getMarshaller().unmarshal(new StringReader(dmnXml)); | ||
DMNModel model = dmnCompiler.compile(definitions, Collections.emptyList()); | ||
dmnCompiler.addCallbackForModel(mockCallbackForModel, model); | ||
dmnCompiler.compile(definitions, Collections.emptyList()); | ||
|
||
|
||
assertThat(model).isNotNull(); | ||
verify(mockCallback, times(2)).callback(eq(dmnCompiler), any(), any(DMNModelImpl.class)); | ||
verify(mockCallbackForModel, never()).callback(any(), any(), any()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @samuel-beniamin
IINW this is signavio-only behaviour that get to the core code: if so, could you please remove and move to signavio module ? Thanks!