Skip to content

Commit

Permalink
[DROOLS-3367] Created DMNRuntimeFluent and related APIs (apache#2179)
Browse files Browse the repository at this point in the history
* [DROOLS-3401] Moved Fluent API to kie-internal

* [DROOLS-3367] Implemented DMNRuntimeFluent APIs

* [DROOLS-3367] Implemented DMNRuntimeFluent APIs

* [DROOLS-3367] Fixed bug

* [DROOLS-3367] Added copyright

* [DROOLS-3367] Use DMNRuntime.newContext() instead of DMNFactory.newContext()
  • Loading branch information
danielezonca authored and mariofusco committed Dec 10, 2018
1 parent 354e200 commit b668b12
Show file tree
Hide file tree
Showing 22 changed files with 913 additions and 6 deletions.
Expand Up @@ -22,15 +22,16 @@
import org.drools.core.command.OutCommand; import org.drools.core.command.OutCommand;
import org.drools.core.command.StartConversationCommand; import org.drools.core.command.StartConversationCommand;
import org.kie.api.command.ExecutableCommand; import org.kie.api.command.ExecutableCommand;
import org.kie.api.runtime.Executable;
import org.kie.internal.builder.fluent.ContextFluent; import org.kie.internal.builder.fluent.ContextFluent;
import org.kie.internal.builder.fluent.Scope; import org.kie.internal.builder.fluent.Scope;


public class BaseBatchFluent<T, E> implements ContextFluent<T, E> { public class BaseBatchFluent<T, E> implements ContextFluent<T, E> {


protected ExecutableImpl fluentCtx; protected ExecutableImpl fluentCtx;


public BaseBatchFluent(ExecutableImpl fluentCtx) { public BaseBatchFluent(Executable fluentCtx) {
this.fluentCtx = fluentCtx; this.fluentCtx = (ExecutableImpl) fluentCtx;
} }


public ExecutableImpl getFluentContext() { public ExecutableImpl getFluentContext() {
Expand Down
Expand Up @@ -20,9 +20,11 @@
import java.util.List; import java.util.List;


import org.kie.api.command.ExecutableCommand; import org.kie.api.command.ExecutableCommand;
import org.kie.internal.builder.fluent.CommandBasedExecutable;
import org.kie.internal.builder.fluent.ExecutableBuilder; import org.kie.internal.builder.fluent.ExecutableBuilder;


public class ExecutableImpl implements InternalExecutable { public class ExecutableImpl implements InternalExecutable,
CommandBasedExecutable {


private FluentComponentFactory factory; private FluentComponentFactory factory;
private ExecutableBuilder executableBuilder; private ExecutableBuilder executableBuilder;
Expand Down Expand Up @@ -54,6 +56,7 @@ public void setFactory(FluentComponentFactory factory) {
this.factory = factory; this.factory = factory;
} }


@Override
public void addCommand(ExecutableCommand cmd) { public void addCommand(ExecutableCommand cmd) {
if (batch == null) { if (batch == null) {
batch = new BatchImpl(); batch = new BatchImpl();
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.drools.core.command.NewKieSessionCommand; import org.drools.core.command.NewKieSessionCommand;
import org.drools.core.command.NewRuleUnitExecutorCommand; import org.drools.core.command.NewRuleUnitExecutorCommand;
import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieContainer;
import org.kie.internal.builder.fluent.DMNRuntimeFluent;
import org.kie.internal.builder.fluent.ExecutableBuilder; import org.kie.internal.builder.fluent.ExecutableBuilder;
import org.kie.internal.builder.fluent.KieContainerFluent; import org.kie.internal.builder.fluent.KieContainerFluent;
import org.kie.internal.builder.fluent.KieSessionFluent; import org.kie.internal.builder.fluent.KieSessionFluent;
Expand Down Expand Up @@ -66,4 +67,9 @@ public RuleUnitExecutorFluent newRuleUnitExecutor(String sessionName) {
ctx.addCommand(cmd); ctx.addCommand(cmd);
return new RuleUnitExecutorFluentImpl(ctx); return new RuleUnitExecutorFluentImpl(ctx);
} }

@Override
public DMNRuntimeFluent newDMNRuntime() {
return DMNRuntimeFluent.create(ctx);
}
} }
Expand Up @@ -26,8 +26,6 @@
import java.util.stream.IntStream; import java.util.stream.IntStream;


import org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration; import org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration;
import org.drools.model.functions.Block3;
import org.drools.model.functions.Function2;
import org.kie.dmn.api.core.DMNMessage; import org.kie.dmn.api.core.DMNMessage;
import org.kie.dmn.api.feel.runtime.events.FEELEvent; import org.kie.dmn.api.feel.runtime.events.FEELEvent;
import org.kie.dmn.core.compiler.DMNCompilerContext; import org.kie.dmn.core.compiler.DMNCompilerContext;
Expand Down
@@ -0,0 +1,32 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import org.kie.dmn.api.core.DMNResult;
import org.kie.internal.command.RegistryContext;

public abstract class AbstractDMNResultCommand {

protected DMNResult extractDMNResult(RegistryContext context) {
RegistryContext registryContext = context;
DMNResult dmnResult = registryContext.lookup(DMNResult.class);
if (dmnResult == null) {
throw new IllegalStateException("There is no DMNResult available");
}
return dmnResult;
}
}
@@ -0,0 +1,81 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import org.drools.core.fluent.impl.BaseBatchFluent;
import org.kie.api.io.Resource;
import org.kie.internal.builder.fluent.CommandBasedExecutable;
import org.kie.internal.builder.fluent.DMNRuntimeFluent;
import org.kie.internal.builder.fluent.ExecutableBuilder;

public class DMNRuntimeFluentImpl extends BaseBatchFluent<DMNRuntimeFluent, ExecutableBuilder> implements DMNRuntimeFluent {

private final CommandBasedExecutable ctx;

public DMNRuntimeFluentImpl(CommandBasedExecutable ctx) {
super(ctx);
this.ctx = ctx;
newDMNRuntime();
}

protected DMNRuntimeFluent newDMNRuntime() {
ctx.addCommand(new NewDMNRuntimeCommand());
return this;
}

@Override
public DMNRuntimeFluent setInput(String name, Object value) {
ctx.addCommand(new SetDMNInputCommand(name, value));
return this;
}

@Override
public DMNRuntimeFluent setActiveModel(String namespace, String modelName) {
ctx.addCommand(new SetDMNActiveModelCommand(namespace, modelName));
return this;
}

@Override
public DMNRuntimeFluent setActiveModel(Resource resource) {
ctx.addCommand(new SetDMNActiveModelCommand(resource));
return this;
}

@Override
public DMNRuntimeFluent evaluateModel() {
ctx.addCommand(new ExecuteDMNCommand());
return this;
}

@Override
public DMNRuntimeFluent getAllContext() {
ctx.addCommand(new GetAllDMNContextCommand());
return this;
}

@Override
public DMNRuntimeFluent getDecisionResults() {
ctx.addCommand(new GetDMNDecisionResultsCommand());
return this;
}

@Override
public DMNRuntimeFluent getMessages() {
ctx.addCommand(new GetDMNMessagesCommand());
return this;
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import org.kie.api.command.ExecutableCommand;
import org.kie.api.runtime.Context;
import org.kie.dmn.api.core.DMNContext;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNResult;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.dmn.core.api.DMNFactory;
import org.kie.internal.command.RegistryContext;

public class ExecuteDMNCommand implements ExecutableCommand<DMNResult> {

@Override
public DMNResult execute(Context context) {
RegistryContext registryContext = (RegistryContext) context;
DMNModel activeModel = registryContext.lookup(DMNModel.class);
DMNRuntime dmnRuntime = registryContext.lookup(DMNRuntime.class);
DMNContext dmnContext = registryContext.lookup(DMNContext.class);
if (activeModel == null) {
throw new IllegalStateException("No DMN active model defined");
}
if (dmnRuntime == null) {
throw new IllegalStateException("No DMNRuntime available");
}
if (dmnContext == null) {
dmnContext = dmnRuntime.newContext();
}
DMNResult dmnResult = dmnRuntime.evaluateAll(activeModel, dmnContext);
registryContext.register(DMNResult.class, dmnResult);

// reset context
registryContext.register(DMNContext.class, dmnRuntime.newContext());

return dmnResult;
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import java.util.Map;

import org.kie.api.command.ExecutableCommand;
import org.kie.api.runtime.Context;
import org.kie.dmn.api.core.DMNResult;
import org.kie.internal.command.RegistryContext;

public class GetAllDMNContextCommand implements ExecutableCommand<Map<String, Object>> {

@Override
public Map<String, Object> execute(Context context) {
RegistryContext registryContext = (RegistryContext) context;
DMNResult dmnResult = registryContext.lookup(DMNResult.class);
if(dmnResult == null) {
throw new IllegalStateException("There is not DMNResult available");
}

return dmnResult.getContext().getAll();
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import java.util.List;

import org.kie.api.command.ExecutableCommand;
import org.kie.api.runtime.Context;
import org.kie.dmn.api.core.DMNDecisionResult;
import org.kie.internal.command.RegistryContext;

public class GetDMNDecisionResultsCommand extends AbstractDMNResultCommand implements ExecutableCommand<List<DMNDecisionResult>> {

@Override
public List<DMNDecisionResult> execute(Context context) {
return extractDMNResult((RegistryContext) context).getDecisionResults();
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import java.util.List;

import org.kie.api.command.ExecutableCommand;
import org.kie.api.runtime.Context;
import org.kie.dmn.api.core.DMNMessage;
import org.kie.internal.command.RegistryContext;

public class GetDMNMessagesCommand extends AbstractDMNResultCommand implements ExecutableCommand<List<DMNMessage>> {

@Override
public List<DMNMessage> execute(Context context) {
return extractDMNResult((RegistryContext) context).getMessages();
}
}
@@ -0,0 +1,39 @@
/*
* Copyright 2018 Red Hat, Inc. and/or its affiliates.
*
* 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 org.kie.dmn.core.fluent;

import org.kie.api.command.ExecutableCommand;
import org.kie.api.runtime.Context;
import org.kie.api.runtime.KieContainer;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.internal.command.RegistryContext;

public class NewDMNRuntimeCommand implements ExecutableCommand<DMNRuntime> {

@Override
public DMNRuntime execute(Context context) {
RegistryContext registryContext = (RegistryContext) context;
KieContainer kieContainer = registryContext.lookup(KieContainer.class);
if (kieContainer == null) {
throw new IllegalStateException("There is no existing KieContainer assigned to the Registry");
}
DMNRuntime dmnRuntime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);

registryContext.register(DMNRuntime.class, dmnRuntime);
return dmnRuntime;
}
}

0 comments on commit b668b12

Please sign in to comment.