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);
}
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());
}
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());
}
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;
}
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);
}
}
}
}
Aggregations