use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.
the class RuleParserTest method testGlobal1.
@Test
public void testGlobal1() throws Exception {
final String source = "package foo.bar.baz\n" + "import com.foo.Bar\n" + "global java.util.List<java.util.Map<String,Integer>> aList;\n" + "global Integer aNumber";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
assertEquals("foo.bar.baz", pkg.getName());
assertEquals(1, pkg.getImports().size());
ImportDescr impdescr = pkg.getImports().get(0);
assertEquals("com.foo.Bar", impdescr.getTarget());
assertEquals(source.indexOf("import " + impdescr.getTarget()), impdescr.getStartCharacter());
assertEquals(source.indexOf("import " + impdescr.getTarget()) + ("import " + impdescr.getTarget()).length(), impdescr.getEndCharacter());
assertEquals(2, pkg.getGlobals().size());
GlobalDescr global = pkg.getGlobals().get(0);
assertEquals("java.util.List<java.util.Map<String,Integer>>", global.getType());
assertEquals("aList", global.getIdentifier());
assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
global = pkg.getGlobals().get(1);
assertEquals("Integer", global.getType());
assertEquals("aNumber", global.getIdentifier());
assertEquals(source.indexOf("global " + global.getType()), global.getStartCharacter());
assertEquals(source.indexOf("global " + global.getType() + " " + global.getIdentifier()) + ("global " + global.getType() + " " + global.getIdentifier()).length(), global.getEndCharacter());
}
use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.
the class RuleParserTest method testGlobal.
@Test
public void testGlobal() throws Exception {
PackageDescr pack = (PackageDescr) parseResource("compilationUnit", "globals.drl");
assertEquals(1, pack.getRules().size());
final RuleDescr rule = (RuleDescr) pack.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
assertEquals(1, pack.getImports().size());
assertEquals(2, pack.getGlobals().size());
final GlobalDescr foo = (GlobalDescr) pack.getGlobals().get(0);
assertEquals("java.lang.String", foo.getType());
assertEquals("foo", foo.getIdentifier());
final GlobalDescr bar = (GlobalDescr) pack.getGlobals().get(1);
assertEquals("java.lang.Integer", bar.getType());
assertEquals("bar", bar.getIdentifier());
}
use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testPredicate.
@Test
public void testPredicate() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
final BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
final BindingDescr fieldBindingDescr2 = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr2);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final ExprConstraintDescr predicate = new ExprConstraintDescr("eval(( ( Integer )map.get( new Integer(x) )).intValue() == y)");
pattern.addConstraint(predicate);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testReturnValue.
@Test
public void testReturnValue() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
pattern.addConstraint(new ExprConstraintDescr("price == (( (Integer) map.get( new Integer( x )) ).intValue() * y)"));
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertEquals("Should not have any errors", 0, builder.getErrors().getErrors().length);
}
use of org.drools.compiler.lang.descr.GlobalDescr in project drools by kiegroup.
the class ChangeSetBuilder method diffResource.
private static ResourceChangeSet diffResource(String file, byte[] ob, byte[] cb) {
ResourceChangeSet pkgcs = new ResourceChangeSet(file, ChangeType.UPDATED);
ResourceType type = ResourceType.determineResourceType(file);
if (ResourceType.DRL.equals(type) || ResourceType.GDRL.equals(type) || ResourceType.RDRL.equals(type) || ResourceType.TDRL.equals(type)) {
try {
PackageDescr opkg = new DrlParser().parse(new ByteArrayResource(ob));
PackageDescr cpkg = new DrlParser().parse(new ByteArrayResource(cb));
String pkgName = isEmpty(cpkg.getName()) ? getDefaultPackageName() : cpkg.getName();
String oldPkgName = isEmpty(opkg.getName()) ? getDefaultPackageName() : opkg.getName();
if (!oldPkgName.equals(pkgName)) {
// so it is useless to further investigate other changes
return pkgcs;
}
for (RuleDescr crd : cpkg.getRules()) {
pkgcs.getLoadOrder().add(new ResourceChangeSet.RuleLoadOrder(pkgName, crd.getName(), crd.getLoadOrder()));
}
// needs to be cloned
List<RuleDescr> orules = new ArrayList<>(opkg.getRules());
diffDescrs(ob, cb, pkgcs, orules, cpkg.getRules(), ResourceChange.Type.RULE, RULE_CONVERTER);
// needs to be cloned
List<FunctionDescr> ofuncs = new ArrayList<>(opkg.getFunctions());
diffDescrs(ob, cb, pkgcs, ofuncs, cpkg.getFunctions(), ResourceChange.Type.FUNCTION, FUNC_CONVERTER);
// needs to be cloned
List<GlobalDescr> oglobals = new ArrayList<>(opkg.getGlobals());
diffDescrs(ob, cb, pkgcs, oglobals, cpkg.getGlobals(), ResourceChange.Type.GLOBAL, GLOBAL_CONVERTER);
} catch (Exception e) {
logger.error("Error analyzing the contents of " + file + ". Skipping.", e);
}
}
pkgcs.getChanges().sort(Comparator.comparingInt(r -> r.getChangeType().ordinal()));
return pkgcs;
}
Aggregations