Search in sources :

Example 6 with GlobalDescr

use of org.drools.drl.ast.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.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 7 with GlobalDescr

use of org.drools.drl.ast.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.drl.ast.descr.GlobalDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) AccumulateImportDescr(org.drools.drl.ast.descr.AccumulateImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 8 with GlobalDescr

use of org.drools.drl.ast.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.drl.ast.descr.GlobalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 9 with GlobalDescr

use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.

the class PackageHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final PackageDescr packageDescr = (PackageDescr) parser.getData();
    final Element element = parser.endElementBuilder();
    NodeList imports = element.getElementsByTagName("import");
    for (int i = 0, length = imports.getLength(); i < length; i++) {
        final String importEntry = ((Element) imports.item(i)).getAttribute("name");
        if (importEntry == null || importEntry.trim().equals("")) {
            throw new SAXParseException("<import> cannot be blank", parser.getLocator());
        }
        packageDescr.addImport(new ImportDescr(importEntry));
    }
    NodeList importfunctions = element.getElementsByTagName("importfunction");
    for (int i = 0, length = importfunctions.getLength(); i < length; i++) {
        final String importfunctionEntry = ((Element) importfunctions.item(i)).getAttribute("name");
        if (importfunctionEntry == null || importfunctionEntry.trim().equals("")) {
            throw new SAXParseException("<importfunction> cannot be blank", parser.getLocator());
        }
        FunctionImportDescr funcdescr = new FunctionImportDescr();
        funcdescr.setTarget(importfunctionEntry);
        packageDescr.addFunctionImport(funcdescr);
    }
    NodeList globals = element.getElementsByTagName("global");
    for (int i = 0, length = globals.getLength(); i < length; i++) {
        final String identifier = ((Element) globals.item(i)).getAttribute("identifier");
        if (identifier == null || identifier.trim().equals("")) {
            throw new SAXParseException("<global> must have an identifier", parser.getLocator());
        }
        final String type = ((Element) globals.item(i)).getAttribute("type");
        if (type == null || type.trim().equals("")) {
            throw new SAXParseException("<global> must have specify a type", parser.getLocator());
        }
        final GlobalDescr global = new GlobalDescr(identifier, type);
        packageDescr.addGlobal(global);
    }
    return packageDescr;
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 10 with GlobalDescr

use of org.drools.drl.ast.descr.GlobalDescr in project drools by kiegroup.

the class KnowledgeBuilderImpl method processGlobals.

protected void processGlobals(PackageRegistry pkgRegistry, PackageDescr packageDescr) {
    InternalKnowledgePackage pkg = pkgRegistry.getPackage();
    Set<String> existingGlobals = new HashSet<>(pkg.getGlobals().keySet());
    for (final GlobalDescr global : packageDescr.getGlobals()) {
        final String identifier = global.getIdentifier();
        existingGlobals.remove(identifier);
        String className = global.getType();
        // JBRULES-3039: can't handle type name with generic params
        while (className.indexOf('<') >= 0) {
            className = className.replaceAll("<[^<>]+?>", "");
        }
        try {
            Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(className);
            if (clazz.isPrimitive()) {
                addBuilderResult(new GlobalError(global, " Primitive types are not allowed in globals : " + className));
                return;
            }
            pkg.addGlobal(identifier, clazz);
            addGlobal(identifier, clazz);
            if (kBase != null) {
                kBase.addGlobal(identifier, clazz);
            }
        } catch (final ClassNotFoundException e) {
            addBuilderResult(new GlobalError(global, e.getMessage()));
            logger.warn("ClassNotFoundException occured!", e);
        }
    }
    for (String toBeRemoved : existingGlobals) {
        if (filterAcceptsRemoval(ResourceChange.Type.GLOBAL, pkg.getName(), toBeRemoved)) {
            pkg.removeGlobal(toBeRemoved);
            if (kBase != null) {
                kBase.removeGlobal(toBeRemoved);
            }
        }
    }
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) GlobalError(org.drools.compiler.compiler.GlobalError) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) HashSet(java.util.HashSet)

Aggregations

GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)24 PackageDescr (org.drools.drl.ast.descr.PackageDescr)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)13 Test (org.junit.Test)12 AndDescr (org.drools.drl.ast.descr.AndDescr)10 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 PatternDescr (org.drools.drl.ast.descr.PatternDescr)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)6 BindingDescr (org.drools.drl.ast.descr.BindingDescr)6 Cheese (org.drools.mvel.compiler.Cheese)6 List (java.util.List)5 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)5 FunctionDescr (org.drools.drl.ast.descr.FunctionDescr)5 InputStreamReader (java.io.InputStreamReader)4 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)4 ImportDescr (org.drools.drl.ast.descr.ImportDescr)4 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Iterator (java.util.Iterator)2