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