Search in sources :

Example 1 with GlobalDescr

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());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.compiler.lang.descr.AccumulateImportDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 2 with GlobalDescr

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());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 3 with GlobalDescr

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());
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 4 with GlobalDescr

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);
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 5 with GlobalDescr

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;
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Arrays(java.util.Arrays) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) ResourceChange(org.kie.internal.builder.ResourceChange) ChangeType(org.kie.internal.builder.ChangeType) LoggerFactory(org.slf4j.LoggerFactory) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceType(org.kie.api.io.ResourceType) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) StringUtils.isEmpty(org.drools.core.util.StringUtils.isEmpty) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) Collection(java.util.Collection) Set(java.util.Set) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) List(java.util.List) DrlParser(org.drools.compiler.compiler.DrlParser) Comparator(java.util.Comparator) StringUtils(org.drools.core.util.StringUtils) ArrayList(java.util.ArrayList) ResourceType(org.kie.api.io.ResourceType) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet) FunctionDescr(org.drools.compiler.lang.descr.FunctionDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr)

Aggregations

GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)16 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)13 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)12 Test (org.junit.Test)12 AndDescr (org.drools.compiler.lang.descr.AndDescr)10 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)7 List (java.util.List)6 Cheese (org.drools.compiler.Cheese)6 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)6 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)6 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)5 InputStreamReader (java.io.InputStreamReader)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)4 FunctionDescr (org.drools.compiler.lang.descr.FunctionDescr)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 HashMap (java.util.HashMap)2 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)2 DefaultKnowledgeHelper (org.drools.core.base.DefaultKnowledgeHelper)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2