use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class RuleMetadataTest method testModify.
@Test
public void testModify() {
String rule1 = "modify( $a ) { setA( 20 ), setB( $bb ) }";
KieBase kbase = getKnowledgeBase(rule1);
RuleImpl rule = getRule(kbase, "R0");
ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
assertEquals(1, consequenceMetaData.getStatements().size());
ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
assertEquals(ConsequenceMetaData.Statement.Type.MODIFY, statment.getType());
assertEquals("org.drools.A", statment.getFactClassName());
assertEquals(2, statment.getFields().size());
ConsequenceMetaData.Field field1 = statment.getFields().get(0);
assertEquals("a", field1.getName());
assertEquals("20", field1.getValue());
assertTrue(field1.isLiteral());
ConsequenceMetaData.Field field2 = statment.getFields().get(1);
assertEquals("b", field2.getName());
assertEquals("$bb", field2.getValue());
assertFalse(field2.isLiteral());
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class RuleMetadataTest method testRetractWithFunction.
@Test
public void testRetractWithFunction() {
String rule1 = "retract( getA($a) );";
KieBase kbase = getKnowledgeBase(rule1);
RuleImpl rule = getRule(kbase, "R0");
ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
assertEquals(1, consequenceMetaData.getStatements().size());
ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
assertEquals(ConsequenceMetaData.Statement.Type.RETRACT, statment.getType());
assertEquals("org.drools.A", statment.getFactClassName());
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KnowledgeBuilderImpl method preProcessRules.
private void preProcessRules(PackageDescr packageDescr, PackageRegistry pkgRegistry) {
if (this.kBase == null) {
return;
}
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
boolean needsRemoval = false;
// first, check if any rules no longer exist
for (org.kie.api.definition.rule.Rule rule : pkg.getRules()) {
if (filterAcceptsRemoval(ResourceChange.Type.RULE, rule.getPackageName(), rule.getName())) {
needsRemoval = true;
break;
}
}
if (!needsRemoval) {
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
if (pkg.getRule(ruleDescr.getName()) != null) {
needsRemoval = true;
break;
}
}
}
}
if (needsRemoval) {
kBase.enqueueModification(() -> {
Collection<RuleImpl> rulesToBeRemoved = new HashSet<>();
for (org.kie.api.definition.rule.Rule rule : pkg.getRules()) {
if (filterAcceptsRemoval(ResourceChange.Type.RULE, rule.getPackageName(), rule.getName())) {
rulesToBeRemoved.add(((RuleImpl) rule));
}
}
rulesToBeRemoved.forEach(pkg::removeRule);
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
RuleImpl rule = pkg.getRule(ruleDescr.getName());
if (rule != null) {
rulesToBeRemoved.add(rule);
}
}
}
if (!rulesToBeRemoved.isEmpty()) {
kBase.removeRules(rulesToBeRemoved);
}
});
}
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class KieBaseUpdater method updateResourcesIncrementally.
private int updateResourcesIncrementally(KnowledgeBuilderImpl kbuilder, CompositeKnowledgeBuilder ckbuilder) {
int fileCount = ctx.modifiedClasses.size();
for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
if (rcs.getChangeType() != ChangeType.REMOVED) {
String resourceName = rcs.getResourceName();
if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.newKM, ctx.newKieBaseModel, resourceName)) {
List<ResourceChange> changes = rcs.getChanges();
if (!changes.isEmpty()) {
// we need to deal with individual parts of the resource
fileCount += AbstractKieModule.updateResource(ckbuilder, ctx.newKM, resourceName, rcs) ? 1 : 0;
} else {
// the whole resource has to handled
if (rcs.getChangeType() == ChangeType.UPDATED) {
Resource resource = ctx.currentKM.getResource(resourceName);
kbuilder.removeObjectsGeneratedFromResource(resource);
}
fileCount += ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, resourceName, rcs) ? 1 : 0;
}
}
}
for (ResourceChangeSet.RuleLoadOrder loadOrder : rcs.getLoadOrder()) {
KnowledgePackageImpl pkg = (KnowledgePackageImpl) ctx.kBase.getKiePackage(loadOrder.getPkgName());
if (pkg != null) {
RuleImpl rule = pkg.getRule(loadOrder.getRuleName());
if (rule != null) {
// rule can be null, if it didn't exist before
rule.setLoadOrder(loadOrder.getLoadOrder());
}
}
}
}
return fileCount;
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class RuleBuilderTest method testBuildDurationExpression.
@Test
public void testBuildDurationExpression() throws Exception {
// creates mock objects
final RuleBuildContext context = mock(RuleBuildContext.class);
final RuleImpl rule = mock(RuleImpl.class);
// creates input object
final RuleDescr ruleDescr = new RuleDescr("my rule");
ruleDescr.addAttribute(new AttributeDescr("duration", "( 1h30m )"));
ruleDescr.addAttribute(new AttributeDescr("calendars", "[\"cal1\", \"cal2\"]"));
// defining expectations on the mock object
when(context.getRule()).thenReturn(rule);
when(context.getRuleDescr()).thenReturn(ruleDescr);
// calling the build method
RuleBuilder.buildAttributes(context);
// check expectations
verify(rule).setTimer(new IntervalTimer(null, null, -1, TimeUtils.parseTimeString("1h30m"), 0));
verify(rule).setCalendars(new String[] { "cal1", "cal2" });
}
Aggregations