use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testExpanderMultipleConstraintsFlush.
@Test
public void testExpanderMultipleConstraintsFlush() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
// this is similar to the other test, but it requires a flush to add the
// constraints
final PackageDescr pkg = parser.parse(this.getReader("expander_multiple_constraints_flush.dslr"), this.getReader("multiple_constraints.dsl"));
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals(1, rule.getLhs().getDescrs().size());
final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
assertEquals("age < 42", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0)).getExpression());
assertEquals("location==atlanta", ((ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1)).getExpression());
assertNotNull((String) rule.getConsequence());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class RuleParserTest method testRuleOldSyntax1.
@Test
public void testRuleOldSyntax1() throws Exception {
final String source = "rule \"Test\" when ( not $r :LiteralRestriction( operator == Operator.EQUAL ) ) then end";
PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
assertFalse(parser.getErrors().toString(), parser.hasErrors());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertEquals("Test", rule.getName());
assertEquals(1, rule.getLhs().getDescrs().size());
assertEquals(1, ((NotDescr) rule.getLhs().getDescrs().get(0)).getDescrs().size());
NotDescr notDescr = (NotDescr) rule.getLhs().getDescrs().get(0);
PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
assertEquals("$r", patternDescr.getIdentifier());
assertEquals(1, patternDescr.getDescrs().size());
ExprConstraintDescr fieldConstraintDescr = (ExprConstraintDescr) patternDescr.getDescrs().get(0);
assertEquals("operator == Operator.EQUAL", fieldConstraintDescr.getExpression());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class Misc2Test method testEvalConstraintWithMvelOperator.
@Test
public void testEvalConstraintWithMvelOperator() {
String drl = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Foo( eval( field soundslike \"water\" ) )" + " then " + "end";
DrlParser drlParser = new DrlParser();
PackageDescr packageDescr;
try {
packageDescr = drlParser.parse(true, drl);
} catch (DroolsParserException e) {
throw new RuntimeException(e);
}
RuleDescr r = packageDescr.getRules().get(0);
PatternDescr pd = (PatternDescr) r.getLhs().getDescrs().get(0);
assertEquals(1, pd.getConstraint().getDescrs().size());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class DialectUtil method lookupFunction.
private static FunctionDescr lookupFunction(RuleBuildContext context, String functionName) {
String packageName = context.getRule().getPackageName();
List<PackageDescr> pkgDescrs = context.getKnowledgeBuilder().getPackageDescrs(packageName);
for (PackageDescr pkgDescr : pkgDescrs) {
for (FunctionDescr function : pkgDescr.getFunctions()) {
if (function.getName().equals(functionName)) {
return function;
}
}
}
return null;
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class ObjectTypeNodeCompiler method compile.
/**
* Creates a {@link CompiledNetwork} for the specified {@link ObjectTypeNode}. The {@link PackageBuilder} is used
* to compile the generated source and load the class.
*
* @param kBuilder builder used to compile and load class
* @param objectTypeNode OTN we are generating a compiled network for
* @return CompiledNetwork
*/
public static CompiledNetwork compile(KnowledgeBuilderImpl kBuilder, ObjectTypeNode objectTypeNode) {
if (objectTypeNode == null) {
throw new IllegalArgumentException("ObjectTypeNode cannot be null!");
}
if (kBuilder == null) {
throw new IllegalArgumentException("PackageBuilder cannot be null!");
}
ObjectTypeNodeCompiler compiler = new ObjectTypeNodeCompiler(objectTypeNode);
String packageName = compiler.getPackageName();
PackageRegistry pkgReg = kBuilder.getPackageRegistry(packageName);
if (pkgReg == null) {
kBuilder.addPackage(new PackageDescr(packageName));
pkgReg = kBuilder.getPackageRegistry(packageName);
}
String source = compiler.generateSource();
String generatedSourceName = compiler.getName();
JavaDialect dialect = (JavaDialect) pkgReg.getDialectCompiletimeRegistry().getDialect("java");
dialect.addSrc(compiler.getBinaryName(), source.getBytes(IoUtils.UTF8_CHARSET));
kBuilder.compileAll();
kBuilder.updateResults();
CompiledNetwork network;
try {
network = (CompiledNetwork) Class.forName(generatedSourceName, true, kBuilder.getRootClassLoader()).newInstance();
} catch (ClassNotFoundException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
} catch (IllegalAccessException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
} catch (InstantiationException e) {
throw new RuntimeException("This is a bug. Please contact the development team", e);
}
return network;
}
Aggregations