Search in sources :

Example 76 with PackageDescr

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

Example 77 with PackageDescr

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

Example 78 with PackageDescr

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

Example 79 with PackageDescr

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

Example 80 with PackageDescr

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;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompiledNetwork(org.drools.core.reteoo.compiled.CompiledNetwork) JavaDialect(org.drools.compiler.rule.builder.dialect.java.JavaDialect)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)239 Test (org.junit.Test)192 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)44 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InputStreamReader (java.io.InputStreamReader)25 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14