Skip to content

Commit

Permalink
[DROOLS-7416] Invalidate RuleUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Jul 21, 2023
1 parent 948a448 commit a362111
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ default <T extends RuleUnitData> RuleUnitInstance<T> createRuleUnitInstance(T ru
return ruleUnit.createInstance(ruleUnitData, ruleConfig);
}

/**
* Invalidates all {@link RuleUnit}s generated from the given class.
* @return The number of invalidated ruleunits.
*/
<T extends RuleUnitData> int invalidateRuleUnits(Class<T> ruleUnitDataClass);

/**
* Creates a new RuleConfig instance.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.ruleunits.dsl;

import java.util.ArrayList;
import java.util.List;

import org.drools.ruleunits.api.DataSource;
import org.drools.ruleunits.api.DataStore;
import org.drools.ruleunits.impl.NamedRuleUnitData;

import static org.drools.model.Index.ConstraintType.EQUAL;

public class DynamicHelloWorldUnit implements RuleUnitDefinition {

private final DataStore<String> strings;

private final List<String> results = new ArrayList<>();

private final String expectedMessage; // this is the dynamic part

public DynamicHelloWorldUnit(String expectedMessage) {
this.strings = DataSource.createStore();
this.expectedMessage = expectedMessage;
}

public DataStore<String> getStrings() {
return strings;
}


public List<String> getResults() {
return results;
}

@Override
public void defineRules(RulesFactory rulesFactory) {
// /strings[ this == expectedMessage ]
rulesFactory.rule()
.on(strings)
.filter(EQUAL, expectedMessage) // when no extractor is provided "this" is implicit
.execute(results, r -> r.add("it worked!")); // the consequence can ignore the matched facts
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2023 Red Hat, Inc. and/or its affiliates.
*
* Licensed 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.ruleunits.dsl;

import org.drools.ruleunits.api.RuleUnitInstance;
import org.drools.ruleunits.api.RuleUnitProvider;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class RuleUnitRebuildTest {

@Test
void dynamicHelloWorld() {
DynamicHelloWorldUnit unit = new DynamicHelloWorldUnit("Hello World");
unit.getStrings().add("Hello World");

try (RuleUnitInstance<DynamicHelloWorldUnit> unitInstance = RuleUnitProvider.get().createRuleUnitInstance(unit)) {
assertThat(unitInstance.fire()).isEqualTo(1);
assertThat(unit.getResults()).containsExactly("it worked!");
}

int invalidated = RuleUnitProvider.get().invalidateRuleUnits(DynamicHelloWorldUnit.class);
assertThat(invalidated).as("Invalidate 1 rule unit").isEqualTo(1);

DynamicHelloWorldUnit newUnit = new DynamicHelloWorldUnit("Goodbye World");
newUnit.getStrings().add("Hello World");

try (RuleUnitInstance<DynamicHelloWorldUnit> newUnitInstance = RuleUnitProvider.get().createRuleUnitInstance(newUnit)) {
assertThat(newUnitInstance.fire()).isZero();
assertThat(newUnit.getResults()).isEmpty();

newUnit.getStrings().add("Goodbye World");
assertThat(newUnitInstance.fire()).isEqualTo(1);
assertThat(newUnit.getResults()).containsExactly("it worked!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,10 @@ public RuleConfig newRuleConfig() {
return new RuleConfigImpl();
}

@Override
public <T extends RuleUnitData> int invalidateRuleUnits(Class<T> ruleUnitDataClass) {
String ruleUnitName = getRuleUnitName(ruleUnitDataClass);
RuleUnit remove = ruleUnitMap.remove(ruleUnitName);
return remove == null ? 0 : 1;
}
}

0 comments on commit a362111

Please sign in to comment.