Skip to content

Commit

Permalink
[KIE-1013] add convenient methods to retrieve objects of a specific c…
Browse files Browse the repository at this point in the history
…lass from an entry-point (#5789)
  • Loading branch information
mariofusco authored Mar 18, 2024
1 parent 6281abd commit 4004fe9
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.conf.AlphaNetworkCompilerOption;
Expand Down Expand Up @@ -157,7 +156,7 @@ protected KieModuleModel getDefaultKieModuleModel( KieServices ks ) {
}

public static <T> List<T> getObjectsIntoList(KieSession ksession, Class<T> clazz) {
return (List<T>) ksession.getObjects(new ClassObjectFilter(clazz)).stream().collect(Collectors.toList());
return ksession.getInstancesOf(clazz).stream().collect(Collectors.toList());
}

protected void createAndDeployJar( KieServices ks, ReleaseId releaseId, String... drls ) {
Expand Down
32 changes: 0 additions & 32 deletions drools-core/src/main/java/org/drools/core/ClassObjectFilter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.util.Collection;

import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieRuntime;
import org.kie.api.runtime.process.CaseAssignment;
import org.kie.api.runtime.process.CaseData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.builder.conf.AlphaNetworkCompilerOption;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.fail;
import static org.drools.model.codegen.execmodel.BaseModelTest.RUN_TYPE.PATTERN_DSL;
import static org.drools.model.codegen.execmodel.BaseModelTest.RUN_TYPE.PATTERN_WITH_ALPHA_NETWORK;
import static org.drools.model.codegen.execmodel.BaseModelTest.RUN_TYPE.STANDARD_WITH_ALPHA_NETWORK;
import static org.assertj.core.api.Assertions.fail;

@RunWith(Parameterized.class)
public abstract class BaseModelTest {
Expand Down Expand Up @@ -168,7 +167,7 @@ protected KieModuleModel getDefaultKieModuleModel( KieServices ks ) {
}

public static <T> List<T> getObjectsIntoList(KieSession ksession, Class<T> clazz) {
return (List<T>) ksession.getObjects(new ClassObjectFilter(clazz)).stream().collect(Collectors.toList());
return ksession.getInstancesOf(clazz).stream().collect(Collectors.toList());
}

protected void createAndDeployJar( KieServices ks, ReleaseId releaseId, String... drls ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ public void testQueryInRule() {

ksession.fireAllRules();

Collection<Result> results = (Collection<Result>) ksession.getObjects( new ClassObjectFilter( Result.class ) );
assertThat(results.size()).isEqualTo(1);
assertThat(results.iterator().next().getValue()).isEqualTo("Mario");
Result result = ksession.getSingleInstanceOf( Result.class );
assertThat(result.getValue()).isEqualTo("Mario");
}

@Test
Expand Down Expand Up @@ -1132,6 +1131,6 @@ public void test2AccsWithGetObjectsDeep() throws Exception {
}

public static <T> List<T> getObjectsIntoList(KieSession ksession, Class<T> clazz) {
return (List<T>) ksession.getObjects(new ClassObjectFilter(clazz)).stream().collect(Collectors.toList());
return ksession.getInstancesOf(clazz).stream().collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.kiesession.session.StatefulKnowledgeSessionImpl;
import org.drools.model.Model;
import org.drools.model.codegen.ExecutableModelProject;
Expand Down Expand Up @@ -431,7 +431,7 @@ protected Optional<Person> getPersonByName(String name) {
}

protected Optional<Person> getPersonByName(KieSession kieSession, String name) {
return kieSession.getObjects(new ClassObjectFilter(Person.class))
return kieSession.getInstancesOf( Person.class )
.stream()
.map(Person.class::cast)
.filter(p -> p.getName().equals(name) ).findFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
*/
package org.drools.serialization.protobuf;

import org.drools.core.ClassObjectFilter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.drools.core.common.EqualityKey;
import org.drools.core.common.InternalAgenda;
import org.drools.core.common.InternalFactHandle;
Expand All @@ -38,18 +43,13 @@
import org.kie.api.KieBase;
import org.kie.api.definition.KiePackage;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.rule.FactHandle;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.io.ResourceFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.drools.serialization.protobuf.SerializationHelper.getSerialisedStatefulKnowledgeSession;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.drools.commands.runtime.rule.FireAllRulesCommand;
import org.drools.compiler.kie.builder.impl.DrlProject;
import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.base.definitions.rule.impl.RuleImpl;
import org.drools.core.event.DefaultAgendaEventListener;
import org.drools.core.impl.InternalRuleBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.List;

import org.drools.core.BeliefSystemType;
import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.core.RuleSessionConfiguration;
import org.drools.core.common.EqualityKey;
import org.drools.core.common.InternalFactHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.core.ClockType;
import org.drools.core.SessionConfiguration;
import org.drools.core.impl.RuleBaseFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import org.drools.base.base.ValueResolver;
import org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl;
import org.drools.compiler.builder.impl.KnowledgeBuilderImpl;
import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.base.InitialFact;
import org.drools.base.base.ClassObjectType;
import org.drools.core.common.DefaultFactHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.util.Iterator;
import java.util.List;

import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.core.common.DefaultFactHandle;
import org.drools.mvel.compiler.Cheese;
import org.drools.mvel.compiler.CheeseEqual;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.kie.api.builder.Message;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

Expand Down Expand Up @@ -99,7 +98,7 @@ public static KieModuleModel getDefaultKieModuleModel( KieServices ks ) {
}

public static <T> List<T> getObjectsIntoList( KieSession ksession, Class<T> clazz) {
return (List<T>) ksession.getObjects(new ClassObjectFilter(clazz)).stream().collect( Collectors.toList());
return ksession.getInstancesOf(clazz).stream().collect( Collectors.toList());
}

public static void createAndDeployJar( KieServices ks, ReleaseId releaseId, String... drls ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
*/
package org.drools.testcoverage.regression;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.drools.core.ClassObjectFilter;
import org.drools.testcoverage.common.util.KieBaseTestConfiguration;
import org.drools.testcoverage.common.util.KieBaseUtil;
import org.drools.testcoverage.common.util.TestConstants;
Expand All @@ -34,6 +32,7 @@
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.kie.api.KieBase;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieSession;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.util.Map;

import org.drools.traits.compiler.CommonTraitTest;
import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.traits.core.factmodel.TraitableMap;
import org.drools.traits.core.factmodel.TraitFactoryImpl;
import org.drools.base.factmodel.traits.Traitable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import java.util.ArrayList;
import java.util.Collection;

import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.drools.verifier.components.EntryPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlElements;

import org.drools.core.ClassObjectFilter;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.core.ClassObjectSerializationFilter;
import org.drools.core.base.RuleNameEndsWithAgendaFilter;
import org.drools.core.base.RuleNameEqualsAgendaFilter;
Expand Down
31 changes: 30 additions & 1 deletion kie-api/src/main/java/org/kie/api/runtime/rule/EntryPoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.kie.api.runtime.rule;

import java.util.Collection;
import java.util.NoSuchElementException;

import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.ObjectFilter;

/**
Expand Down Expand Up @@ -152,6 +154,34 @@ void update(FactHandle handle,
*/
Collection<? extends Object> getObjects(ObjectFilter filter);

/**
* Returns a Collection of objects in this entry-point that are instances of the given class.
* @param clazz the class of objects to be retrieved
* @return all facts from this entry-point that are instance of the given class.
*/
default <T> Collection<T> getInstancesOf(Class<T> clazz) {
return (Collection<T>) getObjects(new ClassObjectFilter(clazz));
}

/**
* Returns the only object in this entry-point that is an instance of the given class.
* @param clazz the class of object to be retrieved
* @return the only object from this entry-point that is an instance of the given class.
* @throws NoSuchElementException if there isn't any object of the given class in this entry-point
* @throws IllegalStateException if there is more than one object of the given class in this entry-point
*/
default <T> T getSingleInstanceOf(Class<T> clazz) {
Collection<T> ts = (Collection<T>) getObjects(new ClassObjectFilter(clazz));
int size = ts.size();
if (size == 0) {
throw new NoSuchElementException();
}
if (size > 1) {
throw new IllegalStateException("Found " + size + " instances instead of the expected single one");
}
return ts.iterator().next();
}

/**
* @return all <code>FactHandle</code>s from the current session.
*/
Expand All @@ -168,5 +198,4 @@ void update(FactHandle handle,
* @return the total number of facts currently in this entry point
*/
long getFactCount();

}

0 comments on commit 4004fe9

Please sign in to comment.