Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync main branch with Apache main branch #52

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -453,12 +453,9 @@ public void testAlphaConstraintNagate() {
"then\n" +
"end";

KieSession ksession = getKieSession(str);
try {
try (KieSession ksession = getKieSession(str)) {
ksession.insert(new Person("Mario", 45));
assertThat(ksession.fireAllRules()).isEqualTo(0);
} finally {
ksession.dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,18 @@ public void testLargeCompiledAlphaNetwork() {
rule.append(ruleWithIndex(i));
}

KieSession ksession = getKieSession(rule.toString());
ArrayList<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);
Person a = new Person("a", 1);
Person b = new Person("b", 0);
Person c = new Person("a", 7);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);
try (KieSession ksession = getKieSession(rule.toString())) {
ArrayList<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);
Person a = new Person("a", 1);
Person b = new Person("b", 0);
Person c = new Person("a", 7);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);

try {
ksession.fireAllRules();
assertThat(results).contains(a, b, c);
} finally {
ksession.dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,18 @@ public void testMixedConstraints() {
rule.append(ruleWithIndex(i));
}

KieSession ksession = getKieSession(rule.toString());
ArrayList<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);
Person a = new Person("a", 1);
Person b = new Person("b", 0);
Person c = new Person("a", 7);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);
try (KieSession ksession = getKieSession(rule.toString())) {
ArrayList<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);
Person a = new Person("a", 1);
Person b = new Person("b", 0);
Person c = new Person("a", 7);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);

try {
ksession.fireAllRules();
assertThat(results).contains(a, b, c);
} finally {
ksession.dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,18 @@ public void testMultipleIndexedConstraintTest() {
rule.append(ruleWithIndex(i));
}

KieSession ksession = getKieSession(rule.toString());
ArrayList<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);
Person a = new Person("a", 1);
Person b = new Person("b", 0);
Person c = new Person("a", 7);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);
try (KieSession ksession = getKieSession(rule.toString())) {
ArrayList<Object> results = new ArrayList<>();
ksession.setGlobal("results", results);
Person a = new Person("a", 1);
Person b = new Person("b", 0);
Person c = new Person("a", 7);
ksession.insert(a);
ksession.insert(b);
ksession.insert(c);

try {
ksession.fireAllRules();
assertThat(results).contains(a, b, c);
} finally {
ksession.dispose();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
import org.drools.compiler.lang.DescrDumper;
import org.drools.compiler.lang.DumperContext;
import org.drools.compiler.rule.builder.EvaluatorDefinition.Target;
import org.drools.compiler.rule.builder.XpathAnalysis.XpathPart;
import org.drools.drl.parser.lang.XpathAnalysis;
import org.drools.drl.parser.lang.XpathAnalysis.XpathPart;
import org.drools.compiler.rule.builder.util.ConstraintUtil;
import org.drools.core.base.FieldNameSupplier;
import org.drools.core.rule.BehaviorRuntime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.compiler.rule.builder;
package org.drools.drl.parser.lang;

import java.util.ArrayList;
import java.util.Iterator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
name: mypkg
dialect: java
imports:
- java.lang.Number
- java.lang.Long
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This file was automatically generated from a YAML representation.
package mypkg;
dialect "java";
import java.lang.Number;
import java.lang.Long;
rule "R1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ public BaseDeserializer(Class<?> vc) {
@Override
public Base deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
JsonNode node = jp.getCodec().readTree(jp);
if (node.get("given") != null) { // TODO maybe enforce XOR checks.
if (node.get("given") != null || node.get("datasource") != null) {
return jp.getCodec().treeToValue(node, Pattern.class);
} else if (node.get("exists") != null) {
}
if (node.get("exists") != null) {
return jp.getCodec().treeToValue(node, Exists.class);
} else if (node.get("all") != null) {
}
if (node.get("all") != null) {
return jp.getCodec().treeToValue(node, All.class);
} else if (node.get("not") != null) {
}
if (node.get("not") != null) {
return jp.getCodec().treeToValue(node, Not.class);
} else {
throw new UnsupportedOperationException();
}
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,53 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonPropertyOrder({"name", "dialect", "imports", "globals", "rules", "functions"})
@JsonPropertyOrder({"name", "unit", "dialect", "imports", "globals", "rules", "functions"})
public class DrlPackage {

@JsonInclude(Include.NON_EMPTY)
private String name = ""; // default empty, consistent with DRL parser.

@JsonInclude(Include.NON_EMPTY)
private String dialect = "java";
private String unit = "";

@JsonInclude(Include.NON_EMPTY)
private String dialect = "";

@JsonInclude(Include.NON_EMPTY)
private List<Import> imports = new ArrayList<>();

@JsonInclude(Include.NON_EMPTY)
private List<Global> globals = new ArrayList<>();

private List<Rule> rules = new ArrayList<>();

@JsonInclude(Include.NON_EMPTY)
private List<Function> functions = new ArrayList<>();

public static DrlPackage from(PackageDescr o) {
Objects.requireNonNull(o);
public static DrlPackage from(PackageDescr pkg) {
Objects.requireNonNull(pkg);
DrlPackage result = new DrlPackage();
result.name = o.getName();
AttributeDescr dialectAttr = o.getAttribute("dialect");
result.name = pkg.getName();

if (pkg.getUnit() != null) {
result.unit = pkg.getUnit().getTarget();
}

AttributeDescr dialectAttr = pkg.getAttribute("dialect");
if (dialectAttr != null) {
result.dialect = dialectAttr.getValue();
}
for (ImportDescr i : o.getImports()) {

for (ImportDescr i : pkg.getImports()) {
result.imports.add(Import.from(i));
}
for (GlobalDescr g : o.getGlobals()) {
for (GlobalDescr g : pkg.getGlobals()) {
result.globals.add(Global.from(g));
}
for (RuleDescr r : o.getRules()) {
for (RuleDescr r : pkg.getRules()) {
result.rules.add(Rule.from(r));
}
for (FunctionDescr f : o.getFunctions()) {
for (FunctionDescr f : pkg.getFunctions()) {
result.functions.add(Function.from(f));
}
return result;
Expand All @@ -74,6 +89,10 @@ public String getName() {
return name;
}

public String getUnit() {
return unit;
}

public String getDialect() {
return dialect;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,79 @@
import java.util.List;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.drools.drl.ast.descr.BaseDescr;
import org.drools.drl.ast.descr.ExprConstraintDescr;
import org.drools.drl.ast.descr.FromDescr;
import org.drools.drl.ast.descr.MVELExprDescr;
import org.drools.drl.ast.descr.PatternDescr;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.drools.drl.parser.lang.XpathAnalysis;

@JsonDeserialize(as = Pattern.class) // see https://stackoverflow.com/a/34128468/893991 TODO maybe enforce this check somehow
public class Pattern implements Base {
@JsonProperty(required = true)

@JsonInclude(Include.NON_EMPTY)
private String given;

@JsonInclude(Include.NON_EMPTY)
private String datasource;

@JsonInclude(Include.NON_EMPTY)
private String as;

@JsonInclude(Include.NON_EMPTY)
private List<String> having = new ArrayList<>();

@JsonInclude(Include.NON_EMPTY)
private String from;
public static Pattern from(PatternDescr o) {
Objects.requireNonNull(o);

public static Pattern from(PatternDescr pattern) {
Objects.requireNonNull(pattern);
Pattern result = new Pattern();
result.given = o.getObjectType();
if (o.getAllBoundIdentifiers().isEmpty()) {
// do nothing, as expected.
} else if (o.getAllBoundIdentifiers().size() == 1) {
result.as = o.getAllBoundIdentifiers().get(0);

if (pattern.getObjectType() != null) {
result.given = pattern.getObjectType();

for (BaseDescr c: pattern.getConstraint().getDescrs()) {
if (c instanceof MVELExprDescr) {
result.having.add(((MVELExprDescr) c).getExpression());
} if (c instanceof ExprConstraintDescr) {
result.having.add(((ExprConstraintDescr) c).getExpression());
} else {
throw new UnsupportedOperationException();
}
}

if (pattern.getSource() != null) {
if (pattern.getSource() instanceof FromDescr) {
result.from = ((FromDescr) pattern.getSource()).getDataSource().getText();
} else {
throw new UnsupportedOperationException("unknown patternSourceDescr");
}
}

} else {
result.as = o.getAllBoundIdentifiers().get(0); // TODO check the index=0 is always the pattern one
}
for (BaseDescr c: o.getConstraint().getDescrs()) {
if (c instanceof MVELExprDescr) {
result.having.add(((MVELExprDescr) c).getExpression());
} if (c instanceof ExprConstraintDescr) {
result.having.add(((ExprConstraintDescr) c).getExpression());
} else {

String oopathExpr = pattern.getDescrs().get(0).getText();
XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(oopathExpr);
if (!xpathAnalysis.isSinglePart()) {
throw new UnsupportedOperationException();
}
XpathAnalysis.XpathPart xpathPart = xpathAnalysis.getPart(0);
result.datasource = xpathPart.getField();
result.having.addAll( xpathPart.getConstraints() );
}
if (o.getSource() != null) {
if (o.getSource() instanceof FromDescr) {
result.from = ((FromDescr) o.getSource()).getDataSource().getText();
} else {
throw new UnsupportedOperationException("unknown patternSourceDescr");
}

if (pattern.getAllBoundIdentifiers().isEmpty()) {
// do nothing, as expected.
} else if (pattern.getAllBoundIdentifiers().size() == 1) {
result.as = pattern.getAllBoundIdentifiers().get(0);
} else {
result.as = pattern.getAllBoundIdentifiers().get(0); // TODO check the index=0 is always the pattern one
}

return result;
}

Expand All @@ -89,4 +113,8 @@ public List<String> getHaving() {
public String getFrom() {
return from;
}

public String getDatasource() {
return datasource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@

@JsonPropertyOrder({"name", "when", "then"})
public class Rule {

@JsonProperty(required = true)
private String name;

@JsonProperty(required = true)
private List<Base> when = new ArrayList<>();

@JsonProperty(required = true)
private AbstractThen then;

Expand All @@ -42,7 +45,7 @@ public static Rule from(RuleDescr r) {
Rule result = new Rule();
result.name = r.getName();
for (BaseDescr dd: r.getLhs().getDescrs()) {
result.when.add((Base) Utils.from(dd));
result.when.add(Utils.from(dd));
}
result.then = StringThen.from(r.getConsequence().toString());
return result;
Expand Down
Loading
Loading