Copilot commented on code in PR #7186:
URL: https://github.com/apache/incubator-kie/pull/7186#discussion_r4267937529


##########
drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationRuntimeTest.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.model.codegen.execmodel;
+
+import java.util.concurrent.TimeUnit;
+
+import org.drools.core.ClockType;
+import org.drools.core.common.DefaultEventHandle;
+import org.drools.model.codegen.execmodel.domain.AnnotatedEvent;
+import org.drools.model.codegen.execmodel.domain.PropertyChangeSupportFact;
+import org.drools.model.codegen.execmodel.domain.PropertyReactiveFact;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.kie.api.KieServices;
+import org.kie.api.builder.model.KieModuleModel;
+import org.kie.api.conf.EventProcessingOption;
+import org.kie.api.runtime.KieSession;
+import org.kie.api.runtime.conf.ClockTypeOption;
+import org.kie.api.runtime.rule.FactHandle;
+import org.kie.api.time.SessionPseudoClock;
+import org.kie.internal.builder.conf.PropertySpecificOption;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Verifies that Java annotations preserved through an empty DRL {@code 
declare} block
+ * produce the correct runtime behavior in both classic and executable models.
+ *
+ * @see AlignAnnotationTest
+ * @see <a 
href="https://github.com/apache/incubator-kie/issues/7185";>incubator-kie#7185</a>
+ */
+public class AlignAnnotationRuntimeTest extends BaseModelTest {
+
+    private static KieModuleModel getCepKieModuleModel() {
+        KieModuleModel model = KieServices.get().newKieModuleModel();
+        model.newKieBaseModel("kb")
+                .setDefault(true)
+                .setEventProcessingMode(EventProcessingOption.STREAM)
+                .newKieSessionModel("ks")
+                
.setDefault(true).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
+        return model;
+    }
+
+    // --- @Role(EVENT): fact should be treated as an event ---
+
+    @ParameterizedTest
+    @MethodSource("parameters")
+    public void testRoleEventRuntimeWithEmptyDeclare(RUN_TYPE runType) {
+        String drl =
+                "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+                "declare AnnotatedEvent end\n" +
+                "rule R when AnnotatedEvent() then end\n";
+
+        KieSession ksession = getKieSession(runType, getCepKieModuleModel(), 
drl);
+        FactHandle handle = ksession.insert(new AnnotatedEvent());
+        assertThat(handle).isInstanceOf(DefaultEventHandle.class);

Review Comment:
   The runtime tests create `KieSession` instances without disposing them. 
Since these tests use STREAM + pseudo clock in some cases (and are 
parameterized), explicitly calling `ksession.dispose()` in a `finally` block 
will prevent resource build-up and test interference.



##########
drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationTest.java:
##########
@@ -0,0 +1,199 @@
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.model.codegen.execmodel;
+
+import org.drools.base.rule.TypeDeclaration;
+import org.drools.core.impl.InternalRuleBase;
+import org.drools.model.codegen.execmodel.domain.AnnotatedEvent;
+import org.drools.model.codegen.execmodel.domain.PropertyChangeSupportFact;
+import org.drools.model.codegen.execmodel.domain.PropertyReactiveFact;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.kie.api.KieServices;
+import org.kie.api.builder.model.KieModuleModel;
+import org.kie.api.definition.type.Role;
+import org.kie.api.runtime.KieSession;
+import org.kie.internal.builder.conf.PropertySpecificOption;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Reproduces the annotation-precedence discrepancy between classic and 
executable
+ * models when an empty DRL {@code declare} block exists for a Java class.
+ *
+ * <p>{@code @TypeSafe} is not covered here: neither model reads it from Java 
annotations
+ * (only from DRL {@code @typesafe}), so there is no inheritance discrepancy 
to test.
+ *
+ * @see <a 
href="https://github.com/apache/incubator-kie/issues/7185";>incubator-kie#7185</a>
+ */
+public class AlignAnnotationTest extends BaseModelTest {
+
+    private static final String ANNOTATED_EVENT_NO_DECLARE_DRL =
+            "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+            "rule R when AnnotatedEvent() then end\n";
+
+    private static final String ANNOTATED_EVENT_EMPTY_DECLARE_DRL =
+            "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+            "declare AnnotatedEvent end\n" +
+            "rule R when AnnotatedEvent() then end\n";
+
+    private static final String ANNOTATED_EVENT_OVERRIDE_DRL =
+            "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+            "declare AnnotatedEvent\n" +
+            "  @role(FACT)\n" +
+            "  @expires(5s)\n" +
+            "  @propertyReactive\n" +
+            "end\n" +
+            "rule R when AnnotatedEvent() then end\n";
+
+    private TypeDeclaration getTypeDeclaration(RUN_TYPE runType, String drl, 
Class<?> clazz) {
+        return getTypeDeclaration(runType, null, drl, clazz);
+    }
+
+    private TypeDeclaration getTypeDeclaration(RUN_TYPE runType, 
KieModuleModel model, String drl, Class<?> clazz) {
+        KieSession ksession = getKieSession(runType, model, drl);
+        return ((InternalRuleBase) 
ksession.getKieBase()).getTypeDeclaration(clazz);

Review Comment:
   The `KieSession` created here is never disposed, which can leak resources 
across parameterized runs (especially when these new tests create many 
sessions). Wrap session usage in a `try/finally` and call `ksession.dispose()` 
in the `finally` block (or use an existing BaseModelTest helper if available) 
before returning the `TypeDeclaration` result.



##########
drools-model/drools-model-codegen/src/test/java/org/drools/model/codegen/execmodel/AlignAnnotationRuntimeTest.java:
##########
@@ -0,0 +1,216 @@
+/*
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.model.codegen.execmodel;
+
+import java.util.concurrent.TimeUnit;
+
+import org.drools.core.ClockType;
+import org.drools.core.common.DefaultEventHandle;
+import org.drools.model.codegen.execmodel.domain.AnnotatedEvent;
+import org.drools.model.codegen.execmodel.domain.PropertyChangeSupportFact;
+import org.drools.model.codegen.execmodel.domain.PropertyReactiveFact;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.kie.api.KieServices;
+import org.kie.api.builder.model.KieModuleModel;
+import org.kie.api.conf.EventProcessingOption;
+import org.kie.api.runtime.KieSession;
+import org.kie.api.runtime.conf.ClockTypeOption;
+import org.kie.api.runtime.rule.FactHandle;
+import org.kie.api.time.SessionPseudoClock;
+import org.kie.internal.builder.conf.PropertySpecificOption;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Verifies that Java annotations preserved through an empty DRL {@code 
declare} block
+ * produce the correct runtime behavior in both classic and executable models.
+ *
+ * @see AlignAnnotationTest
+ * @see <a 
href="https://github.com/apache/incubator-kie/issues/7185";>incubator-kie#7185</a>
+ */
+public class AlignAnnotationRuntimeTest extends BaseModelTest {
+
+    private static KieModuleModel getCepKieModuleModel() {
+        KieModuleModel model = KieServices.get().newKieModuleModel();
+        model.newKieBaseModel("kb")
+                .setDefault(true)
+                .setEventProcessingMode(EventProcessingOption.STREAM)
+                .newKieSessionModel("ks")
+                
.setDefault(true).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
+        return model;
+    }
+
+    // --- @Role(EVENT): fact should be treated as an event ---
+
+    @ParameterizedTest
+    @MethodSource("parameters")
+    public void testRoleEventRuntimeWithEmptyDeclare(RUN_TYPE runType) {
+        String drl =
+                "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+                "declare AnnotatedEvent end\n" +
+                "rule R when AnnotatedEvent() then end\n";
+
+        KieSession ksession = getKieSession(runType, getCepKieModuleModel(), 
drl);
+        FactHandle handle = ksession.insert(new AnnotatedEvent());
+        assertThat(handle).isInstanceOf(DefaultEventHandle.class);
+    }
+
+    // --- @Expires("10s"): event should expire after 10 seconds ---
+
+    @ParameterizedTest
+    @MethodSource("parameters")
+    public void testExpiresRuntimeWithEmptyDeclare(RUN_TYPE runType) {
+        String drl =
+                "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+                "declare AnnotatedEvent end\n" +
+                "rule R when AnnotatedEvent() then end\n";
+
+        KieSession ksession = getKieSession(runType, getCepKieModuleModel(), 
drl);
+        SessionPseudoClock clock = ksession.getSessionClock();
+
+        ksession.insert(new AnnotatedEvent());
+        ksession.fireAllRules();
+        assertThat(ksession.getObjects()).hasSize(1);
+
+        clock.advanceTime(11, TimeUnit.SECONDS);
+        ksession.fireAllRules();
+        assertThat(ksession.getObjects()).isEmpty();
+    }
+
+    // --- @Timestamp("start"): temporal operators should use the start field 
---
+
+    @ParameterizedTest
+    @MethodSource("parameters")
+    public void testTimestampRuntimeWithEmptyDeclare(RUN_TYPE runType) {
+        String drl =
+                "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+                "declare AnnotatedEvent end\n" +
+                "rule R when\n" +
+                "    $a: AnnotatedEvent(length == 0)\n" +
+                "    $b: AnnotatedEvent(length == 1, this after[1ms, 1000ms] 
$a)\n" +
+                "then end\n";
+
+        KieSession ksession = getKieSession(runType, getCepKieModuleModel(), 
drl);
+
+        // Both inserted at the same pseudo clock time; @Timestamp("start") 
should
+        // make the engine use the start field for temporal ordering.
+        ksession.insert(new AnnotatedEvent(0, 0));
+        ksession.insert(new AnnotatedEvent(500, 1));
+        assertThat(ksession.fireAllRules()).isEqualTo(1);
+    }
+
+    // --- @Duration("length"): event duration should span [start, 
start+length] ---
+
+    @ParameterizedTest
+    @MethodSource("parameters")
+    public void testDurationRuntimeWithEmptyDeclare(RUN_TYPE runType) {
+        String drl =
+                "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+                "declare AnnotatedEvent end\n" +
+                "rule R when\n" +
+                "    $a: AnnotatedEvent(length > 0)\n" +
+                "    $b: AnnotatedEvent(length == 0, this during $a)\n" +
+                "then end\n";
+
+        KieSession ksession = getKieSession(runType, getCepKieModuleModel(), 
drl);
+
+        // $a spans [0, 2000ms], $b at 500ms is during $a
+        ksession.insert(new AnnotatedEvent(0, 2000));
+        ksession.insert(new AnnotatedEvent(500, 0));
+        assertThat(ksession.fireAllRules()).isEqualTo(1);
+    }
+
+    // --- @ClassReactive: modifying any property should re-trigger evaluation 
---
+
+    @ParameterizedTest
+    @MethodSource("parameters")
+    public void testClassReactiveRuntimeWithEmptyDeclare(RUN_TYPE runType) {
+        String drl =
+                "import " + AnnotatedEvent.class.getCanonicalName() + ";\n" +
+                "declare AnnotatedEvent end\n" +
+                "rule R when\n" +
+                "    $e: AnnotatedEvent(value == null)\n" +
+                "then\n" +
+                // Modify a property NOT in the condition. With 
@ClassReactive, this
+                // re-triggers evaluation. With property-reactive, it would 
not.
+                "    modify($e) { setStart($e.getStart() + 1) }\n" +
+                "end\n";
+
+        KieSession ksession = getKieSession(runType, drl);

Review Comment:
   `testClassReactiveRuntimeWithEmptyDeclare` aims to prove Java 
`@ClassReactive` is preserved through an empty DRL `declare`, but without 
explicitly setting `PropertySpecificOption.ALWAYS` the test can become 
non-discriminating (it may pass even if `@ClassReactive` isn’t actually read, 
depending on the default property-specific configuration). To make the 
assertion meaningful and stable, create/pass a `KieModuleModel` that sets 
`PropertySpecificOption.PROPERTY_NAME` to `ALWAYS`, so the observed “re-trigger 
on unused property change” truly depends on `@ClassReactive`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to