Search in sources :

Example 31 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class QueryBuilderTest method testQuery.

@Test
public void testQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$type");
    packageDescr.addRule(queryDescr);
    final AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == $type"));
    // Another query, no parameters
    QueryDescr queryDescr2 = new QueryDescr("query2");
    packageDescr.addRule(queryDescr2);
    AndDescr lhs2 = new AndDescr();
    queryDescr2.setLhs(lhs2);
    PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
    lhs2.addDescr(pattern2);
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
    final KieSession session = kbase.newKieSession();
    session.insert(new Cheese("stilton", 15));
    QueryResults results = session.getQueryResults("query1", "stilton");
    assertEquals(1, results.size());
    Object object = results.iterator().next().get("stilton");
    assertEquals(new Cheese("stilton", 15), object);
    results = session.getQueryResults("query1", new Object[] { "cheddar" });
    assertEquals(0, results.size());
    session.insert(new Cheese("dolcelatte", 20));
    results = session.getQueryResults("query2", new Object[] {});
    assertEquals(2, results.size());
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 32 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class RuleErrorTest method testNewLineInMessage.

@Test
public void testNewLineInMessage() {
    CompilationProblem[] probs = new CompilationProblem[3];
    probs[0] = new MockCompilationProblem();
    probs[1] = new MockCompilationProblem();
    probs[2] = new MockCompilationProblem();
    DescrBuildError err = new DescrBuildError(new RuleDescr("ruleName"), new AndDescr(), probs, "IM IN YR EROR");
    assertNotNull(err.toString());
    String msg = err.getMessage();
    assertTrue(msg.indexOf("IM IN YR EROR") != -1);
    System.err.println(msg);
    assertEquals("IM IN YR EROR problem\nproblem\nproblem", msg);
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) Test(org.junit.Test)

Example 33 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class ModelGenerator method getExtendedLhs.

private static AndDescr getExtendedLhs(PackageDescr packageDescr, RuleDescr ruleDescr) {
    if (ruleDescr.getParentName() == null) {
        return ruleDescr.getLhs();
    }
    RuleDescr parent = packageDescr.getRules().stream().filter(r -> r.getName().equals(ruleDescr.getParentName())).findFirst().orElseThrow(() -> new RuntimeException("Rule " + ruleDescr.getName() + " extends an unknown rule " + ruleDescr.getParentName()));
    AndDescr extendedLhs = new AndDescr();
    getExtendedLhs(packageDescr, parent).getDescrs().forEach(extendedLhs::addDescr);
    ruleDescr.getLhs().getDescrs().forEach(extendedLhs::addDescr);
    return extendedLhs;
}
Also used : AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr)

Example 34 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method parseBaseDescr.

private IPattern parseBaseDescr(final RuleModel m, final BaseDescr descr, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
    if (descr instanceof PatternDescr) {
        return parsePatternDescr(m, (PatternDescr) descr, isJavaDialect, boundParams, dmo);
    } else if (descr instanceof AndDescr) {
        AndDescr andDescr = (AndDescr) descr;
        return parseBaseDescr(m, andDescr.getDescrs().get(0), isJavaDialect, boundParams, dmo);
    } else if (descr instanceof EvalDescr) {
        FreeFormLine freeFormLine = new FreeFormLine();
        freeFormLine.setText("eval( " + ((EvalDescr) descr).getContent() + " )");
        return freeFormLine;
    } else if (descr instanceof ConditionalElementDescr) {
        return parseExistentialElementDescr(m, (ConditionalElementDescr) descr, isJavaDialect, boundParams, dmo);
    }
    return null;
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 35 with AndDescr

use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.

the class VerifierDataMapsTest method testSaveVerifierComponentAndGetForAllComponentTypes.

@Test
public void testSaveVerifierComponentAndGetForAllComponentTypes() {
    RulePackage rulePackage = VerifierComponentMockFactory.createPackage1();
    saveVerifierComponentAndGet(rulePackage);
    VerifierRule rule = VerifierComponentMockFactory.createRule1();
    saveVerifierComponentAndGet(rule);
    Pattern pattern = VerifierComponentMockFactory.createPattern1();
    saveVerifierComponentAndGet(pattern);
    saveVerifierComponentAndGet(new InlineEvalDescr(pattern));
    saveVerifierComponentAndGet(new ObjectType(new PackageDescr("testPackage1")));
    saveVerifierComponentAndGet(new RuleOperatorDescr(new AndDescr(), rule, OperatorDescrType.AND));
    saveVerifierComponentAndGet(new PatternOperatorDescr(pattern, OperatorDescrType.AND));
    saveVerifierComponentAndGet(new SubPattern(pattern, 0));
    saveVerifierComponentAndGet(new ReturnValueFieldDescr(pattern));
    saveVerifierComponentAndGet(new SubRule(rule, 0));
    saveVerifierComponentAndGet(new TextConsequence(rule));
    saveVerifierComponentAndGet(new PatternVariable(rule));
    saveVerifierComponentAndGet(new VerifierAccessorDescr(rule));
    saveVerifierComponentAndGet(new VerifierAccumulateDescr(pattern));
    saveVerifierComponentAndGet(new VerifierCollectDescr(pattern));
    saveVerifierComponentAndGet(new RuleEval(rule));
    saveVerifierComponentAndGet(new VerifierFieldAccessDescr(rule));
    saveVerifierComponentAndGet(new VerifierFromDescr(pattern));
    saveVerifierComponentAndGet(new VerifierMethodAccessDescr(rule));
    saveVerifierComponentAndGet(new PatternEval(pattern));
}
Also used : Pattern(org.drools.verifier.components.Pattern) SubPattern(org.drools.verifier.components.SubPattern) TextConsequence(org.drools.verifier.components.TextConsequence) PatternVariable(org.drools.verifier.components.PatternVariable) VerifierMethodAccessDescr(org.drools.verifier.components.VerifierMethodAccessDescr) ReturnValueFieldDescr(org.drools.verifier.components.ReturnValueFieldDescr) SubRule(org.drools.verifier.components.SubRule) VerifierFieldAccessDescr(org.drools.verifier.components.VerifierFieldAccessDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) VerifierAccumulateDescr(org.drools.verifier.components.VerifierAccumulateDescr) VerifierRule(org.drools.verifier.components.VerifierRule) VerifierFromDescr(org.drools.verifier.components.VerifierFromDescr) SubPattern(org.drools.verifier.components.SubPattern) RuleEval(org.drools.verifier.components.RuleEval) RulePackage(org.drools.verifier.components.RulePackage) PatternEval(org.drools.verifier.components.PatternEval) ObjectType(org.drools.verifier.components.ObjectType) PatternOperatorDescr(org.drools.verifier.components.PatternOperatorDescr) RuleOperatorDescr(org.drools.verifier.components.RuleOperatorDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) VerifierCollectDescr(org.drools.verifier.components.VerifierCollectDescr) VerifierAccessorDescr(org.drools.verifier.components.VerifierAccessorDescr) InlineEvalDescr(org.drools.verifier.components.InlineEvalDescr) Test(org.junit.Test)

Aggregations

AndDescr (org.drools.compiler.lang.descr.AndDescr)79 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)54 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)52 Test (org.junit.Test)52 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)39 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)27 Cheese (org.drools.compiler.Cheese)13 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)12 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)12 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)11 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)8 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)7 NotDescr (org.drools.compiler.lang.descr.NotDescr)6 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 List (java.util.List)4 EvalDescr (org.drools.compiler.lang.descr.EvalDescr)4 QueryDescr (org.drools.compiler.lang.descr.QueryDescr)4