Created
February 11, 2016 10:17
-
-
Save patope/ea9eb97b8d45e75ccf7c to your computer and use it in GitHub Desktop.
After failed mvel function call kiesession could get into infinite loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>io.resys.test</groupId> | |
<artifactId>drools-bug</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.drools</groupId> | |
<artifactId>drools-compiler</artifactId> | |
<version>6.4.0.Beta1</version> | |
</dependency> | |
<dependency> | |
<groupId>ch.qos.logback</groupId> | |
<artifactId>logback-classic</artifactId> | |
<version>1.1.3</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import org.kie.api.KieBase; | |
import org.kie.api.KieServices; | |
import org.kie.api.builder.KieBuilder; | |
import org.kie.api.builder.KieFileSystem; | |
import org.kie.api.builder.KieModule; | |
import org.kie.api.io.KieResources; | |
import org.kie.api.io.Resource; | |
import org.kie.api.io.ResourceType; | |
import org.kie.api.runtime.KieSession; | |
public class Application { | |
public static class Parameter { | |
private String id; | |
private Integer value; | |
public Parameter(String id) { | |
this.id = id; | |
} | |
public Integer getValue() { | |
return value; | |
} | |
public void setValue(Integer value) { | |
this.value = value; | |
} | |
public String getId() { | |
return id; | |
} | |
} | |
public static class Setting { | |
private String id; | |
private Integer value; | |
public Setting(String id, Integer value) { | |
this.id = id; | |
this.value = value; | |
} | |
public String getId() { | |
return id; | |
} | |
public Integer getValue() { | |
return value; | |
} | |
} | |
static String rule = "package test\n" + | |
"import test.Application.Parameter\n" + | |
"import test.Application.Setting\n" + | |
"function int index(int weight, int height) {\n" + | |
" return 0;\n" + | |
"}\n" + | |
"\n" + | |
"rule \"set\"\n" + | |
"when\n" + | |
" settings : Setting($id : id)\n" + | |
" parameter : Parameter(id == $id)\n" + | |
"then\n" + | |
" retract(settings);\n" + | |
" parameter.setValue(settings.getValue());\n" + | |
" update(parameter);\n" + | |
"end\n" + | |
"rule \"check index\"\n" + | |
"when\n" + | |
" Parameter(id == \"attribute1\", value != null, $value1: value)\n" + | |
" Parameter(id == \"attribute2\", index($value1, value) > 0)\n" + | |
"then\n" + | |
"end"; | |
public static void main(String[] args) { | |
KieSession kieSession = createKieSession(); | |
kieSession.insert(new Parameter("attribute1")); | |
kieSession.insert(new Parameter("attribute2")); | |
kieSession.fireAllRules(); | |
kieSession.insert(new Setting("attribute1", 1)); | |
fireRules(kieSession); | |
kieSession.insert(new Setting("attribute2", 1)); | |
fireRules(kieSession); | |
kieSession.insert(new Setting("attribute2", 12)); | |
fireRules(kieSession); // Drools goes into infinite loop here | |
} | |
private static void fireRules(KieSession kieSession) { | |
try { | |
kieSession.fireAllRules(); | |
} catch (Exception e2) { | |
e2.printStackTrace(); | |
} | |
} | |
private static KieSession createKieSession() { | |
KieServices kieServices = KieServices.Factory.get(); | |
KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); | |
KieResources kieResources = kieServices.getResources(); | |
final Resource ruleResource = kieResources.newByteArrayResource(rule.getBytes()); | |
ruleResource.setResourceType(ResourceType.DRL); | |
ruleResource.setSourcePath("rule"); | |
kieFileSystem.write(ruleResource); | |
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem); | |
kieBuilder.buildAll(); | |
KieModule kieModule = kieBuilder.getKieModule(); | |
KieBase kieBase = kieServices.newKieContainer(kieModule.getReleaseId()).getKieBase(); | |
return kieBase.newKieSession(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment