This is an automated email from the ASF dual-hosted git repository.

mariofusco pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git


The following commit(s) were added to refs/heads/main by this push:
     new 0abd1fcbb2 [DROOLS-7578] Evaluate Impact analysis for ansible 
integration rules (#5594)
0abd1fcbb2 is described below

commit 0abd1fcbb2c126d4a6b5ad67764bce9c894c4b10
Author: Toshiya Kobayashi <[email protected]>
AuthorDate: Thu Dec 7 19:08:42 2023 +0900

    [DROOLS-7578] Evaluate Impact analysis for ansible integration rules (#5594)
    
    - Introduced DeleteSpecificFactAction for retract_fact
    - Add a test case
---
 .../drools-impact-analysis-graph-common/pom.xml    |  4 --
 .../analysis/graph/ModelToGraphConverter.java      | 30 +++++++++
 .../DeleteSpecificFactActionTest.java              | 78 ++++++++++++++++++++++
 .../model/right/DeleteSpecificFactAction.java      | 53 +++++++++++++++
 .../analysis/model/right/SpecificProperty.java     | 38 +++++++++++
 5 files changed, 199 insertions(+), 4 deletions(-)

diff --git 
a/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/pom.xml
 
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/pom.xml
index 0f27e9eb2d..9c40c10fe9 100644
--- 
a/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/pom.xml
+++ 
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/pom.xml
@@ -47,10 +47,6 @@
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
     </dependency>
-    <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-classic</artifactId>
-    </dependency>
 
     <dependency>
       <groupId>junit</groupId>
diff --git 
a/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/src/main/java/org/drools/impact/analysis/graph/ModelToGraphConverter.java
 
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/src/main/java/org/drools/impact/analysis/graph/ModelToGraphConverter.java
index ae740a7dfc..c8c048c64d 100644
--- 
a/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/src/main/java/org/drools/impact/analysis/graph/ModelToGraphConverter.java
+++ 
b/drools-impact-analysis/drools-impact-analysis-graph/drools-impact-analysis-graph-common/src/main/java/org/drools/impact/analysis/graph/ModelToGraphConverter.java
@@ -30,12 +30,14 @@ import org.drools.impact.analysis.model.left.LeftHandSide;
 import org.drools.impact.analysis.model.left.MapConstraint;
 import org.drools.impact.analysis.model.left.Pattern;
 import org.drools.impact.analysis.model.right.ConsequenceAction;
+import org.drools.impact.analysis.model.right.DeleteSpecificFactAction;
 import org.drools.impact.analysis.model.right.InsertAction;
 import org.drools.impact.analysis.model.right.InsertedProperty;
 import org.drools.impact.analysis.model.right.ModifiedMapProperty;
 import org.drools.impact.analysis.model.right.ModifiedProperty;
 import org.drools.impact.analysis.model.right.ModifyAction;
 import org.drools.impact.analysis.model.right.RightHandSide;
+import org.drools.impact.analysis.model.right.SpecificProperty;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -189,6 +191,14 @@ public class ModelToGraphConverter {
     }
 
     private void processDelete(GraphAnalysis graphAnalysis, String pkgName, 
String ruleName, ConsequenceAction action) {
+        if (action instanceof DeleteSpecificFactAction) {
+            processDeleteSpecificFact(graphAnalysis, pkgName, ruleName, 
(DeleteSpecificFactAction) action);
+        } else {
+            processDeleteAnyFact(graphAnalysis, pkgName, ruleName, action);
+        }
+    }
+
+    private void processDeleteAnyFact(GraphAnalysis graphAnalysis, String 
pkgName, String ruleName, ConsequenceAction action) {
         Class<?> deletedClass = action.getActionClass();
         // all rules which react to the fact
         Node source = graphAnalysis.getNode(fqdn(pkgName, ruleName));
@@ -198,6 +208,26 @@ public class ModelToGraphConverter {
         }
     }
 
+    private void processDeleteSpecificFact(GraphAnalysis graphAnalysis, String 
pkgName, String ruleName, DeleteSpecificFactAction action) {
+        Node source = graphAnalysis.getNode(fqdn(pkgName, ruleName));
+
+        Class<?> deletedClass = action.getActionClass();
+        if (!graphAnalysis.isRegisteredClass(deletedClass)) {
+            // Not likely happen but not invalid
+            logger.warn("Not found {} in reactiveMap", deletedClass);
+            return;
+        }
+        List<SpecificProperty> specificProperties = 
action.getSpecificProperties();
+        for (SpecificProperty specificProperty : specificProperties) {
+            String property = specificProperty.getProperty();
+            for (AnalyzedRule reactedRule : 
graphAnalysis.getRulesReactiveTo(deletedClass, property)) {
+                // Reactive to this class+property. Deleting this fact means 
NEGATIVE impact.
+                Node target = graphAnalysis.getNode(fqdn(pkgName, 
reactedRule.getRule().getName()));
+                linkNodesIfExpected(source, target, ReactivityType.NEGATIVE);
+            }
+        }
+    }
+
     private void processModify(GraphAnalysis graphAnalysis, String pkgName, 
String ruleName, ModifyAction action) {
         Node source = graphAnalysis.getNode(fqdn(pkgName, ruleName));
 
diff --git 
a/drools-impact-analysis/drools-impact-analysis-itests/src/test/java/org/drools/impact/analysis/integrationtests/DeleteSpecificFactActionTest.java
 
b/drools-impact-analysis/drools-impact-analysis-itests/src/test/java/org/drools/impact/analysis/integrationtests/DeleteSpecificFactActionTest.java
new file mode 100644
index 0000000000..4458cbc3d5
--- /dev/null
+++ 
b/drools-impact-analysis/drools-impact-analysis-itests/src/test/java/org/drools/impact/analysis/integrationtests/DeleteSpecificFactActionTest.java
@@ -0,0 +1,78 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.drools.impact.analysis.integrationtests;
+
+import java.util.List;
+
+import org.drools.impact.analysis.graph.Graph;
+import org.drools.impact.analysis.graph.ModelToGraphConverter;
+import org.drools.impact.analysis.graph.ReactivityType;
+import org.drools.impact.analysis.integrationtests.domain.Address;
+import org.drools.impact.analysis.integrationtests.domain.Person;
+import org.drools.impact.analysis.model.AnalysisModel;
+import org.drools.impact.analysis.model.right.ConsequenceAction;
+import org.drools.impact.analysis.model.right.DeleteSpecificFactAction;
+import org.drools.impact.analysis.model.right.SpecificProperty;
+import org.drools.impact.analysis.parser.ModelBuilder;
+import org.junit.Test;
+
+/**
+ * This test is to verify that DeleteSpecificFactAction can be handled 
correctly.
+ * DeleteSpecificFactAction cannot be created by ModelBuilder from DRL, so we 
programmatically add it to the model.
+ */
+public class DeleteSpecificFactActionTest extends AbstractGraphTest {
+
+    @Test
+    public void insertDeleteSpecific() {
+        String str =
+                "package mypkg;\n" +
+                        "import " + Person.class.getCanonicalName() + ";" +
+                        "import " + Address.class.getCanonicalName() + ";" +
+                        "rule R1 when\n" +
+                        "  $p : Person(name == \"John\")\n" +
+                        "then\n" +
+                        "  Address address = new Address();" +
+                        "  address.setStreet(\"ABC\");" +
+                        "  insert(address);" +
+                        "end\n" +
+                        "rule R2 when\n" +
+                        "  $p : Person(name == \"Paul\")\n" +
+                        "then\n" +
+                        // Here, delete a fact with street == "ABC" 
(ansible-rulebook can do it)
+                        "end\n" +
+                        "rule R3 when\n" +
+                        "  $a : Address(street == \"ABC\")\n" +
+                        "then\n" +
+                        "end\n";
+
+        AnalysisModel analysisModel = new ModelBuilder().build(str);
+
+        // Tweak analysisModel because DeleteSpecificFactAction cannot be 
created by ModelBuilder
+        List<ConsequenceAction> actions = 
analysisModel.getPackages().get(0).getRules().get(1).getRhs().getActions();
+        DeleteSpecificFactAction deleteSpecificFactAction = new 
DeleteSpecificFactAction(Address.class);
+        deleteSpecificFactAction.addSpecificProperty(new 
SpecificProperty("street", "ABC"));
+        actions.add(deleteSpecificFactAction);
+
+        ModelToGraphConverter converter = new ModelToGraphConverter();
+        Graph graph = converter.toGraph(analysisModel);
+
+        assertLink(graph, "mypkg.R1", "mypkg.R3", ReactivityType.POSITIVE);
+        assertLink(graph, "mypkg.R2", "mypkg.R3", ReactivityType.NEGATIVE);
+    }
+}
diff --git 
a/drools-impact-analysis/drools-impact-analysis-model/src/main/java/org/drools/impact/analysis/model/right/DeleteSpecificFactAction.java
 
b/drools-impact-analysis/drools-impact-analysis-model/src/main/java/org/drools/impact/analysis/model/right/DeleteSpecificFactAction.java
new file mode 100644
index 0000000000..d2a969d113
--- /dev/null
+++ 
b/drools-impact-analysis/drools-impact-analysis-model/src/main/java/org/drools/impact/analysis/model/right/DeleteSpecificFactAction.java
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.drools.impact.analysis.model.right;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * This class represents a delete action for a specific fact.
+ * Use this action when you know the fact's properties to be deleted, assuming 
other facts of the same class still
+ * exist in the working memory. (Usually, you don't know the fact's properties)
+ * This action is introduced to support retract_fact action in 
drools-ansible-rulebook-integration-visualization.
+ */
+public class DeleteSpecificFactAction extends ConsequenceAction {
+
+    private final List<SpecificProperty> specificProperties = new 
ArrayList<>();
+
+    public DeleteSpecificFactAction(Class<?> actionClass) {
+        super(Type.DELETE, actionClass);
+    }
+
+    public List<SpecificProperty> getSpecificProperties() {
+        return specificProperties;
+    }
+
+    public void addSpecificProperty(SpecificProperty specificProperty) {
+        specificProperties.add(specificProperty);
+    }
+
+    @Override
+    public String toString() {
+        return "DeleteSpecificFactAction{" +
+                "actionClass=" + actionClass +
+                ", specificProperties=" + specificProperties +
+                '}';
+    }
+}
diff --git 
a/drools-impact-analysis/drools-impact-analysis-model/src/main/java/org/drools/impact/analysis/model/right/SpecificProperty.java
 
b/drools-impact-analysis/drools-impact-analysis-model/src/main/java/org/drools/impact/analysis/model/right/SpecificProperty.java
new file mode 100644
index 0000000000..049ab3821e
--- /dev/null
+++ 
b/drools-impact-analysis/drools-impact-analysis-model/src/main/java/org/drools/impact/analysis/model/right/SpecificProperty.java
@@ -0,0 +1,38 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.drools.impact.analysis.model.right;
+
+public class SpecificProperty extends ModifiedProperty {
+
+    public SpecificProperty(String property) {
+        this(property, null);
+    }
+
+    public SpecificProperty(String property, Object value) {
+        super(property, value);
+    }
+
+    @Override
+    public String toString() {
+        return "SpecificProperty{" +
+                "property='" + property + '\'' +
+                ", value=" + value +
+                '}';
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to