Skip to content

Commit

Permalink
KOGITO-590: Rules should "see" process variables (apache#269)
Browse files Browse the repository at this point in the history
* KOGITO-590: Rules should "see" process variables

* add test case

* fix

* rm comments

* fix npe in model tests

* address comments, cleanup imports

* disable test

* add support for additional operations on process variables from rule unit's data sources (#1)

Co-authored-by: Maciej Swiderski <[email protected]>
  • Loading branch information
evacchi and mswiderski committed Jan 13, 2020
1 parent bb8d51b commit bb232bd
Show file tree
Hide file tree
Showing 21 changed files with 678 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public FactHandle insert(DataHandle handle, T object) {

@Override
public void update(DataHandle handle, T object) {

consumer.accept(object);
}

@Override
public void delete(DataHandle handle) {

consumer.accept(null);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static RuleUnitComponentFactory get() {

RuleUnitDescription createRuleUnitDescription( KiePackage pkg, Class<?> ruleUnitClass );

RuleUnitDescription createRuleUnitDescription( KiePackage pkg, String ruleUnitSimpleName );

ApplyPmmlModelCommandExecutor newApplyPmmlModelCommandExecutor();

boolean isRuleUnitClass( Class<?> ruleUnitClass );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@
public interface RuleUnitDescription {

/**
* @deprecated this is only used by PMML
* @deprecated this is only used by PMML / it should not be used for other purposes
*
*/
@Deprecated
Class<?> getRuleUnitClass();

default String getRuleUnitName() {
return getRuleUnitClass().getName();
}
String getRuleUnitName();

default String getCanonicalName() {
return getRuleUnitClass().getCanonicalName();
}
String getCanonicalName();

String getSimpleName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.drools.core.reteoo.ObjectTypeNode;
import org.drools.core.rule.EntryPointId;
import org.drools.core.time.impl.PseudoClockScheduler;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.kie.api.KieBase;
import org.kie.api.conf.EventProcessingOption;
Expand All @@ -57,6 +58,7 @@
import org.kie.internal.conf.MultithreadEvaluationOption;
import org.kie.internal.utils.KieHelper;

@Disabled
public class ParallelEvaluationTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,21 @@ private RuleUnitDescription findDescription(final String ruleUnit) {
if (nonExistingUnits.contains(ruleUnit)) {
return null;
}
RuleUnitComponentFactory ruleUnitComponentFactory = RuleUnitComponentFactory.get();
// short-circuit if there is no support for units
if (ruleUnitComponentFactory == null) {
return null;
}
try {
return RuleUnitComponentFactory.get().createRuleUnitDescription( pkg, pkg.getTypeResolver().resolveType(ruleUnit) );
return ruleUnitComponentFactory.createRuleUnitDescription(pkg, pkg.getTypeResolver().resolveType(ruleUnit) );
} catch (final ClassNotFoundException e) {
nonExistingUnits.add(ruleUnit);
return null;
RuleUnitDescription ruleUnitDescription = ruleUnitComponentFactory.createRuleUnitDescription(pkg, ruleUnit);
if (ruleUnitDescription == null) {
nonExistingUnits.add(ruleUnit);
return null;
} else {
return ruleUnitDescription;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,13 @@
import java.util.Map;
import java.util.Optional;

import org.drools.core.rule.EntryPointId;
import org.kie.internal.ruleunit.RuleUnitDescription;
import org.kie.internal.ruleunit.RuleUnitVariable;

public abstract class AbstractRuleUnitDescription implements RuleUnitDescription {

private final Map<String, RuleUnitVariable> varDeclarations = new HashMap<>();

// @Override
// public Optional<EntryPointId> getEntryPointId(String name) {
// return Optional.ofNullable(varDeclarations.get(name))
// .filter(RuleUnitVariable::isDataSource)
// .map(ds -> new EntryPointId(name));
// }

@Override
public Optional<Class<?>> getDatasourceType(String name) {
return Optional.ofNullable(varDeclarations.get(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,15 @@
* limitations under the License.
*/

package org.drools.core.ruleunit;
package org.kie.kogito.rules.units;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

import org.drools.core.addon.TypeResolver;
import org.kie.kogito.rules.DataSource;
import org.kie.kogito.rules.RuleUnitData;
import org.kie.kogito.rules.units.AbstractRuleUnitDescription;
import org.kie.kogito.rules.units.SimpleRuleUnitVariable;

public class GeneratedRuleUnitDescription extends AbstractRuleUnitDescription {

Expand Down Expand Up @@ -59,6 +52,11 @@ public Class<?> getRuleUnitClass() {
return null;
}

@Override
public String getCanonicalName() {
return getPackageName() + '.' + getSimpleName();
}

@Override
public String getSimpleName() {
return simpleName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public Class<?> getRuleUnitClass() {
return ruleUnitClass;
}

@Override
public String getCanonicalName() {
return getRuleUnitClass().getCanonicalName();
}

@Override
public String getSimpleName() {
return ruleUnitClass.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kie.kogito.rules.units;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,37 @@

package org.kie.kogito.rules.units.impl;

import java.util.HashMap;
import java.util.Map;

import org.drools.core.definitions.InternalKnowledgePackage;
import org.kie.api.definition.KiePackage;
import org.kie.internal.ruleunit.ApplyPmmlModelCommandExecutor;
import org.kie.internal.ruleunit.RuleUnitComponentFactory;
import org.kie.internal.ruleunit.RuleUnitDescription;
import org.kie.kogito.rules.DataSource;
import org.kie.kogito.rules.RuleUnitData;
import org.kie.kogito.rules.units.GeneratedRuleUnitDescription;
import org.kie.kogito.rules.units.ReflectiveRuleUnitDescription;

public class RuleUnitComponentFactoryImpl implements RuleUnitComponentFactory {

private final Map<String, GeneratedRuleUnitDescription> generatedRuleUnitDescriptions = new HashMap<>();

public void registerRuleUnitDescription(GeneratedRuleUnitDescription ruleUnitDescription) {
generatedRuleUnitDescriptions.put(ruleUnitDescription.getCanonicalName(), ruleUnitDescription);
}

@Override
public RuleUnitDescription createRuleUnitDescription(KiePackage pkg, Class<?> ruleUnitClass ) {
return new ReflectiveRuleUnitDescription((InternalKnowledgePackage) pkg, (Class<? extends RuleUnitData>) ruleUnitClass );
}

@Override
public RuleUnitDescription createRuleUnitDescription(KiePackage pkg, String ruleUnitSimpleName ) {
return generatedRuleUnitDescriptions.get(pkg.getName() + '.' + ruleUnitSimpleName);
}

@Override
public ApplyPmmlModelCommandExecutor newApplyPmmlModelCommandExecutor() {
throw new UnsupportedOperationException();
Expand Down
4 changes: 4 additions & 0 deletions jbpm/jbpm-flow-builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<groupId>org.kie.kogito</groupId>
<artifactId>drools-core</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-ruleunits</artifactId>
</dependency>
<dependency>
<groupId>org.kie.kogito</groupId>
<artifactId>kogito-api</artifactId>
Expand Down
Loading

0 comments on commit bb232bd

Please sign in to comment.