use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintIn.
@Test
public void declareConstraintIn() {
List<Object> values = Arrays.asList("-5", "0.5", "1", "10");
String patternType = "INPUT1";
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintIn(patternType, values);
final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
assertNotNull(descrs);
assertEquals(1, descrs.size());
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.get(0);
assertEquals(patternType, patternDescr.getObjectType());
assertNull(patternDescr.getIdentifier());
assertTrue(patternDescr.getConstraint() instanceof AndDescr);
AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
assertEquals(1, andDescr.getDescrs().size());
assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
assertFalse(exprConstraintDescr.isNegated());
assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
String expected = "value in (-5, 0.5, 1, 10)";
assertEquals(expected, exprConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class DRL5Parser method fromAccumulate.
/**
* fromAccumulate := ACCUMULATE LEFT_PAREN lhsAnd COMMA
* ( INIT chunk_(_) COMMA ACTION chunk_(_) COMMA
* ( REVERSE chunk_(_) COMMA)? RESULT chunk_(_)
* | accumulateFunction
* ) RIGHT_PAREN
*
* @param pattern
* @throws RecognitionException
*/
private void fromAccumulate(PatternDescrBuilder<?> pattern) throws RecognitionException {
AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
try {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
}
match(input, DRL5Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
CEDescrBuilder<?, AndDescr> source = accumulate.source();
try {
helper.start(source, CEDescrBuilder.class, null);
lhsAnd(source, false);
if (state.failed)
return;
if (source.getDescr() != null && source.getDescr() instanceof ConditionalElementDescr) {
ConditionalElementDescr root = (ConditionalElementDescr) source.getDescr();
BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
root.getDescrs().clear();
for (int i = 0; i < descrs.length; i++) {
root.addOrMerge(descrs[i]);
}
}
} finally {
helper.end(CEDescrBuilder.class, source);
}
if (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
if (helper.validateIdentifierKey(DroolsSoftKeywords.INIT)) {
// custom code, inline accumulate
// initBlock
match(input, DRL5Lexer.ID, DroolsSoftKeywords.INIT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_INIT);
}
String init = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_INIT_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.init(init);
if (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
// actionBlock
match(input, DRL5Lexer.ID, DroolsSoftKeywords.ACTION, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION);
}
String action = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_ACTION_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.action(action);
if (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
// reverseBlock
if (helper.validateIdentifierKey(DroolsSoftKeywords.REVERSE)) {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.REVERSE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE);
}
String reverse = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_REVERSE_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.reverse(reverse);
if (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
}
}
// resultBlock
match(input, DRL5Lexer.ID, DroolsSoftKeywords.RESULT, null, DroolsEditorType.KEYWORD);
if (state.failed)
return;
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT);
}
String result = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.RIGHT_PAREN, Location.LOCATION_LHS_FROM_ACCUMULATE_RESULT_INSIDE);
if (state.failed)
return;
if (state.backtracking == 0)
accumulate.result(result);
} else {
// accumulate functions
accumulateFunction(accumulate, false, null);
if (state.failed)
return;
}
match(input, DRL5Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return;
} finally {
helper.end(AccumulateDescrBuilder.class, accumulate);
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
}
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class DRL5Parser method lhsAccumulate.
/**
* lhsAccumulate := ACCUMULATE LEFT_PAREN lhsAnd (COMMA|SEMICOLON)
* accumulateFunctionBinding (COMMA accumulateFunctionBinding)*
* (SEMICOLON constraints)?
* RIGHT_PAREN SEMICOLON?
*
* @param ce
* @return
* @throws RecognitionException
*/
private BaseDescr lhsAccumulate(PatternContainerDescrBuilder<?, ?> ce) throws RecognitionException {
PatternDescrBuilder<?> pattern = null;
BaseDescr result = null;
pattern = helper.start((DescrBuilder<?, ?>) ce, PatternDescrBuilder.class, null);
if (pattern != null) {
result = pattern.getDescr();
}
try {
if (state.backtracking == 0) {
pattern.type("Object[]");
pattern.isQuery(false);
// might have to add the implicit bindings as well
}
AccumulateDescrBuilder<?> accumulate = helper.start(pattern, AccumulateDescrBuilder.class, null);
try {
match(input, DRL5Lexer.ID, DroolsSoftKeywords.ACCUMULATE, null, DroolsEditorType.KEYWORD);
if (state.failed)
return null;
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_FROM_ACCUMULATE);
}
match(input, DRL5Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
CEDescrBuilder<?, AndDescr> source = accumulate.source();
try {
helper.start(source, CEDescrBuilder.class, null);
lhsAnd(source, false);
if (state.failed)
return null;
if (source.getDescr() != null && source.getDescr() instanceof ConditionalElementDescr) {
ConditionalElementDescr root = (ConditionalElementDescr) source.getDescr();
BaseDescr[] descrs = root.getDescrs().toArray(new BaseDescr[root.getDescrs().size()]);
root.getDescrs().clear();
for (int i = 0; i < descrs.length; i++) {
root.addOrMerge(descrs[i]);
}
}
} finally {
helper.end(CEDescrBuilder.class, source);
}
if (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} else if (input.LA(-1) != DRL5Lexer.SEMICOLON) {
// lhsUnary will consume an optional SEMICOLON, so we need to check if it was consumed already
// or if we must fail consuming it now
match(input, DRL5Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
}
// accumulate functions
accumulateFunctionBinding(accumulate);
if (state.failed)
return null;
while (input.LA(1) == DRL5Lexer.COMMA) {
match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
accumulateFunctionBinding(accumulate);
if (state.failed)
return null;
}
if (input.LA(1) == DRL5Lexer.SEMICOLON) {
match(input, DRL5Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
constraints(pattern);
}
match(input, DRL5Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
} finally {
helper.end(AccumulateDescrBuilder.class, accumulate);
if (state.backtracking == 0 && input.LA(1) != DRL5Lexer.EOF) {
helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
}
}
} finally {
helper.end(PatternDescrBuilder.class, pattern);
}
if (input.LA(1) == DRL5Lexer.SEMICOLON) {
match(input, DRL5Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
if (state.failed)
return null;
}
return result;
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class QueryDescrBuilderImpl method lhs.
public CEDescrBuilder<QueryDescrBuilder, AndDescr> lhs() {
CEDescrBuilder<QueryDescrBuilder, AndDescr> ce = new CEDescrBuilderImpl<QueryDescrBuilder, AndDescr>(this, new AndDescr());
descr.setLhs(ce.getDescr());
return ce;
}
use of org.drools.drl.ast.descr.AndDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testReload.
@Test
public void testReload() 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);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addGlobal("map", Map.class);
final KieSession workingMemory = kBase.newKieSession();
final HashMap map = new HashMap();
workingMemory.setGlobal("map", map);
final LeftTupleImpl tuple = new MockTuple(new HashMap());
tuple.setLeftTupleSink(new RuleTerminalNode(1, new MockBetaNode(), rule, rule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
pkg.removeRule(rule);
// Make sure the compiled classes are also removed
assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
builder.addPackage(packageDescr);
pkg = builder.getPackage(packageDescr.getName());
rule = pkg.getRule("rule-1");
knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(2), map.get("value"));
}
Aggregations