diff options
Diffstat (limited to 'policy-management/src/main/java')
3 files changed, 71 insertions, 1 deletions
diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java index 344725ff..78a2f4e8 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java @@ -22,7 +22,7 @@ package org.onap.policy.drools.controller; import java.util.List; import java.util.Map; - +import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.policy.common.capabilities.Lockable; import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.endpoints.event.comm.TopicSink; @@ -215,6 +215,11 @@ public interface DroolsController extends Startable, Lockable { List<Object> facts(String sessionName, String className, boolean delete); /** + * Gets facts. + */ + <T> List<T> facts(@NonNull String sessionName, @NonNull Class<T> clazz); + + /** * gets the facts associated with a query for a give session for a given queried entity. * * @param sessionName the session @@ -228,6 +233,16 @@ public interface DroolsController extends Startable, Lockable { Object... queryParams); /** + * Deletes a fact from a session. + */ + <T> boolean delete(@NonNull String sessionName, @NonNull T fact); + + /** + * Delete a fact object of type T. + */ + <T> boolean delete(@NonNull T fact); + + /** * halts and permanently releases all resources. * */ diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java index 95b053fb..a9ab212b 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java @@ -27,7 +27,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; import org.apache.commons.collections4.queue.CircularFifoQueue; +import org.checkerframework.checker.nullness.qual.NonNull; import org.drools.core.ClassObjectFilter; import org.kie.api.definition.KiePackage; import org.kie.api.definition.rule.Query; @@ -833,6 +836,15 @@ public class MavenDroolsController implements DroolsController { } @Override + public <T> List<T> facts(@NonNull String sessionName, @NonNull Class<T> clazz) { + return facts(sessionName, clazz.getCanonicalName(), false) + .stream() + .filter(clazz::isInstance) + .map(clazz::cast) + .collect(Collectors.toList()); + } + + @Override public List<Object> factQuery(String sessionName, String queryName, String queriedEntity, boolean delete, Object... queryParams) { if (sessionName == null || sessionName.isEmpty()) { @@ -881,6 +893,33 @@ public class MavenDroolsController implements DroolsController { } @Override + public <T> boolean delete(@NonNull String sessionName, @NonNull T fact) { + String factClassName = fact.getClass().getName(); + + PolicySession session = getSession(sessionName); + KieSession kieSession = session.getKieSession(); + + Collection<FactHandle> factHandles = kieSession.getFactHandles(new ClassObjectFilter(fact.getClass())); + for (FactHandle factHandle : factHandles) { + try { + if (Objects.equals(fact, kieSession.getObject(factHandle))) { + logger.info("Deleting {} from {}", factClassName, sessionName); + kieSession.delete(factHandle); + return true; + } + } catch (Exception e) { + logger.warn("Object cannot be retrieved from fact {}", factHandle, e); + } + } + return false; + } + + @Override + public <T> boolean delete(@NonNull T fact) { + return this.getSessionNames().stream().map((ss) -> delete(ss, fact)).reduce(false, Boolean::logicalOr); + } + + @Override public Class<?> fetchModelClass(String className) { return ReflectionUtil.fetchClass(this.policyContainer.getClassLoader(), className); } diff --git a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java index 815aaabb..5bf51efa 100644 --- a/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java +++ b/policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.checkerframework.checker.nullness.qual.NonNull; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.drools.controller.DroolsController; import org.onap.policy.drools.core.PolicyContainer; @@ -181,12 +182,27 @@ public class NullDroolsController implements DroolsController { } @Override + public <T> List<T> facts(@NonNull String sessionName, @NonNull Class<T> clazz) { + return new ArrayList<>(); + } + + @Override public List<Object> factQuery(String sessionName, String queryName, String queriedEntity, boolean delete, Object... queryParams) { return new ArrayList<>(); } + @Override + public <T> boolean delete(@NonNull String sessionName, @NonNull T fact) { + return false; + } + + @Override + public <T> boolean delete(@NonNull T fact) { + return false; + } + private String makeInvokeMsg() { return this.getClass().getCanonicalName() + " invoked"; } |