add methods to check if a fact exists. 55/116855/3
authorjhh <jorge.hernandez-herrero@att.com>
Wed, 13 Jan 2021 22:34:13 +0000 (16:34 -0600)
committerjhh <jorge.hernandez-herrero@att.com>
Thu, 14 Jan 2021 20:04:07 +0000 (14:04 -0600)
Issue-ID: POLICY-2762
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: Ic00d2a548dbc904e044d2310c7d5439a1cb708a5
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
policy-management/src/main/java/org/onap/policy/drools/controller/DroolsController.java
policy-management/src/main/java/org/onap/policy/drools/controller/internal/MavenDroolsController.java
policy-management/src/main/java/org/onap/policy/drools/controller/internal/NullDroolsController.java
policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController2Test.java
policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
policy-management/src/test/java/org/onap/policy/drools/controller/internal/NullDroolsControllerTest.java
policy-management/src/test/resources/echo.drl

index 8e78b07..36a4d5b 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2019, 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -232,6 +232,17 @@ public interface DroolsController extends Startable, Lockable {
      */
     <T> boolean delete(@NonNull Class<T> fact);
 
+    /**
+     * Checks is a fact exists in a given session.
+     */
+    <T> boolean exists(@NonNull String sessionName, @NonNull T fact);
+
+    /**
+     * Checks if a fact exists in any session.
+     */
+    <T> boolean exists(@NonNull T fact);
+
+
     /**
      * halts and permanently releases all resources.
      *
index b8edb55..c1eb9f3 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -870,24 +870,31 @@ public class MavenDroolsController implements DroolsController {
     }
 
     @Override
-    public <T> boolean delete(@NonNull String sessionName, @NonNull T fact) {
-        String factClassName = fact.getClass().getName();
+    public <T> boolean delete(@NonNull String sessionName, @NonNull T objFact) {
+        KieSession kieSession = getSession(sessionName).getKieSession();
 
-        PolicySession session = getSession(sessionName);
-        KieSession kieSession = session.getKieSession();
+        // try first to get the object to delete first by reference
+
+        FactHandle quickFact = kieSession.getFactHandle(objFact);
+        if (quickFact != null) {
+            logger.info("Fast delete of {} from {}", objFact, sessionName);
+            kieSession.delete(quickFact);
+            return true;
+        }
+
+        // otherwise, try to the delete the fact associated with the object by scanning all
+        // facts from the same type and performing object equality.   The set of facts
+        // is restricted to those of the same type
 
-        Collection<FactHandle> factHandles = kieSession.getFactHandles(new ClassObjectFilter(fact.getClass()));
+        Collection<FactHandle> factHandles = kieSession.getFactHandles(new ClassObjectFilter(objFact.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(FACT_RETRIEVE_ERROR, factHandle, e);
+            if (Objects.equals(objFact, kieSession.getObject(factHandle))) {
+                logger.info("Slow delete of {} of type {} from {}", objFact, sessionName);
+                kieSession.delete(factHandle);
+                return true;
             }
         }
+
         return false;
     }
 
@@ -919,6 +926,30 @@ public class MavenDroolsController implements DroolsController {
         return this.getSessionNames().stream().map(ss -> delete(ss, fact)).reduce(false, Boolean::logicalOr);
     }
 
+    @Override
+    public <T> boolean exists(@NonNull String sessionName, @NonNull T objFact) {
+        KieSession kieSession = getSession(sessionName).getKieSession();
+        if (kieSession.getFactHandle(objFact) != null) {
+            return true;
+        }
+
+        // try to find the object by equality comparison instead if it could not be
+        // found by reference
+
+        Collection<FactHandle> factHandles = kieSession.getFactHandles(new ClassObjectFilter(objFact.getClass()));
+        for (FactHandle factHandle : factHandles) {
+            if (Objects.equals(objFact, kieSession.getObject(factHandle))) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
+    @Override
+    public <T> boolean exists(@NonNull T fact) {
+        return this.getSessionNames().stream().anyMatch(ss -> exists(ss, fact));
+    }
 
     @Override
     public Class<?> fetchModelClass(String className) {
index 4373b35..e20d177 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -214,6 +214,16 @@ public class NullDroolsController implements DroolsController {
         return false;
     }
 
+    @Override
+    public <T> boolean exists(@NonNull String sessionName, @NonNull T fact) {
+        return false;
+    }
+
+    @Override
+    public <T> boolean exists(@NonNull T fact) {
+        return false;
+    }
+
     private String makeInvokeMsg() {
         return this.getClass().getName() + " invoked";
     }
index 26103aa..8628830 100644 (file)
@@ -202,6 +202,9 @@ public class MavenDroolsController2Test {
         when(kieSess.getKieBase()).thenReturn(kieBase);
         when(kieSess.getQueryResults(QUERY, PARM1, PARM2)).thenReturn(queryResults);
 
+        when(kieSess.getFactHandle(FACT1_OBJECT)).thenReturn(fact1);
+        when(kieSess.getFactHandle(FACT3_OBJECT)).thenReturn(fact3);
+
         when(kieSess.getObject(fact1)).thenReturn(FACT1_OBJECT);
         when(kieSess.getObject(fact2)).thenReturn("");
         when(kieSess.getObject(fact3)).thenReturn(FACT3_OBJECT);
index b39276d..a2f41bc 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -93,6 +93,35 @@ public class MavenDroolsControllerTest {
         new GsonTestUtils().compareGson(controller, MavenDroolsControllerTest.class);
     }
 
+    @Test
+    public void testFact() throws InterruptedException {
+        DroolsController controller = createDroolsController(30000L);
+
+        Integer one = 1;
+        Integer two = 2;
+
+        Assert.assertTrue(controller.offer(one));
+        Assert.assertTrue(controller.exists(one));
+        Assert.assertFalse(controller.exists(two));
+
+        Assert.assertTrue(controller.offer(two));
+        Assert.assertTrue(controller.exists(one));
+        Assert.assertTrue(controller.exists(two));
+
+        Integer three = 3;
+        Assert.assertFalse(controller.delete(three));
+        Assert.assertTrue(controller.exists(one));
+        Assert.assertTrue(controller.exists(two));
+
+        Assert.assertTrue(controller.delete(two));
+        Assert.assertTrue(controller.exists(one));
+        Assert.assertFalse(controller.exists(two));
+
+        Assert.assertTrue(controller.delete(one));
+        Assert.assertFalse(controller.exists(one));
+        Assert.assertFalse(controller.exists(two));
+    }
+
     private DroolsController createDroolsController(long courtesyStartTimeMs) throws InterruptedException {
         if (releaseId == null) {
             throw new IllegalStateException("no prereq artifact installed in maven repository");
index 743de8f..010056b 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -157,4 +157,11 @@ public class NullDroolsControllerTest {
     public void factQuery() {
         Assert.assertTrue(new NullDroolsController().factQuery(null, null, null, false).isEmpty());
     }
+
+    @Test
+    public void exists() {
+        Object o1 = new Object();
+        Assert.assertFalse(new NullDroolsController().exists("blah", o1));
+        Assert.assertFalse(new NullDroolsController().exists(o1));
+    }
 }
index bd26f95..03716d9 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2019, 2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -35,5 +35,4 @@ when
     $o : Object();
 then
     System.out.println("ECHO: " + $o.toString());
-       retract($o);
 end