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


##########
drools-base/src/main/java/org/drools/base/reteoo/sequencing/signalprocessors/LogicGate.java:
##########
@@ -139,6 +169,11 @@ public void clearInputBit(int signalBitIndex, 
SequenceMemory memory, ValueResolv
 
     public void propagate(SequenceMemory memory, ValueResolver valueResolver) {
         resetPrior(memory, valueResolver);
+        // Arm before consuming the output: the output may complete the step, 
whose deactivation
+        // must then also remove the adapters armed here.
+        for (LogicGate gate : armOnMatch) {
+            gate.arm(memory);
+        }
         output.consume(memory, valueResolver);
     }

Review Comment:
   Deferred gates are armed when the owning gate propagates, but a gate can 
also transition to MATCHED during `activate(...)` (e.g., when facts already 
satisfy it at step activation). If the MATCHED transition happens via 
`activate(...)` without going through `propagate(...)`, `armOnMatch` gates may 
never be armed, breaking the intended window semantics. Consider ensuring 
arming occurs on any MATCHED transition (e.g., factor an `onMatched(...)` 
helper called from both `activate(...)` and the normal propagation path).



##########
drools-base/src/main/java/org/drools/base/reteoo/sequencing/signalprocessors/LogicGate.java:
##########
@@ -75,6 +86,25 @@ public int[] getSignalAdapterIndexes() {
         return signalAdapterIndexes;
     }
 
+    public void setDeferredActivation(boolean deferredActivation) {
+        this.deferredActivation = deferredActivation;
+    }
+
+    public void addArmOnMatch(LogicGate gate) {
+        LogicGate[] extended = java.util.Arrays.copyOf(armOnMatch, 
armOnMatch.length + 1);
+        extended[armOnMatch.length] = gate;
+        armOnMatch = extended;
+    }

Review Comment:
   `addArmOnMatch` grows an array via `Arrays.copyOf` on each addition, which 
is O(n²) if multiple deferred gates are attached. Since this structure can be 
built with multiple absence leaves, consider collecting into a 
`List<LogicGate>` during construction (or pre-sizing once when the number of 
leaves is known) and only converting to an array at the end.



##########
drools-model/drools-model-compiler/src/main/java/org/drools/modelcompiler/KiePackagesBuilder.java:
##########
@@ -731,10 +739,27 @@ private LogicGate buildStepGate(RuleContext ctx, 
GroupElement group,
             throw new IllegalStateException(
                     "SEQUENCE composite node has no children: " + type);
         }
-        LogicGate[] inputs = new LogicGate[children.size()];
-        for (int i = 0; i < children.size(); i++) {
-            inputs[i] = buildStepGate(ctx, group, children.get(i), filters, 
stepGates, gateCounter, seqIdx, signalAdapterCounter);
+        List<LogicGate> inputList = new ArrayList<>(children.size());
+        for (Condition child : children) {
+            LogicGate previous = inputList.isEmpty() ? null : 
inputList.get(inputList.size() - 1);
+            if ((child.getType() == Condition.Type.NOT || child.getType() == 
Condition.Type.NOR)
+                    && type == Condition.Type.AND && previous != null && 
previous.isStatusCanRevert()) {
+                // absent() written inside a folded oneOf()/noneOrAll() run, 
i.e. directly after a
+                // revertible gate. It is a veto side-channel, not an input of 
this composite. Its
+                // window opens once that gate is MATCHED, so a blocker that 
arrived earlier is
+                // invisible (arrival order governs). Any other NOT/NOR child 
falls through to
+                // buildStepGate() and is rejected as an unsupported composite 
step gate.
+                for (LogicGate leaf : buildAbsenceLeaves(ctx, group, 
child.getSubConditions(), filters,
+                                                          gateCounter, 
signalAdapterCounter)) {
+                    leaf.setDeferredActivation(true);
+                    previous.addArmOnMatch(leaf);
+                    stepGates.add(leaf);
+                }
+                continue;
+            }
+            inputList.add(buildStepGate(ctx, group, child, filters, stepGates, 
gateCounter, seqIdx, signalAdapterCounter));
         }
+        LogicGate[] inputs = inputList.toArray(new LogicGate[0]);

Review Comment:
   Minor: `inputList.toArray(new LogicGate[0])` can allocate an extra array in 
some cases. Using `inputList.toArray(new LogicGate[inputList.size()])` avoids 
that and is a common micro-optimization in hot code paths.



-- 
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