use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsOr.
@Test
public void declareConstraintsOr() {
String temperatureField = "TEMPERATURE";
String humidityField = "HUMIDITY";
List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.or());
assertNotNull(lhsBuilder.getDescr());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof OrDescr);
OrDescr baseOrDescr = (OrDescr) lhsBuilder.getDescr().getDescrs().get(0);
final List<BaseDescr> descrs = baseOrDescr.getDescrs();
assertNotNull(descrs);
assertEquals(2, descrs.size());
// First KiePMMLFieldOperatorValue
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.get(0);
assertEquals(temperatureField, 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 < 35";
assertEquals(expected, exprConstraintDescr.getExpression());
// Second KiePMMLFieldOperatorValue
assertTrue(descrs.get(1) instanceof PatternDescr);
patternDescr = (PatternDescr) descrs.get(1);
assertEquals(humidityField, patternDescr.getObjectType());
assertNull(patternDescr.getIdentifier());
assertTrue(patternDescr.getConstraint() instanceof AndDescr);
andDescr = (AndDescr) patternDescr.getConstraint();
assertEquals(1, andDescr.getDescrs().size());
assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
assertFalse(exprConstraintDescr.isNegated());
assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
expected = "value > 85";
assertEquals(expected, exprConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.PatternDescr 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.PatternDescr in project drools by kiegroup.
the class ClassHierarchyManager method buildDescrsFromFields.
private static void buildDescrsFromFields(Class klass, TypeDeclarationDescr typeDescr, Map<String, TypeFieldDescr> fieldMap) {
ClassFieldInspector inspector;
try {
inspector = CoreComponentsBuilder.get().createClassFieldInspector(klass);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (String name : inspector.getGetterMethods().keySet()) {
// classFieldAccessor requires both getter and setter
if (inspector.getSetterMethods().containsKey(name)) {
if (!inspector.isNonGetter(name) && !"class".equals(name)) {
Resource resource = typeDescr.getResource();
PatternDescr patternDescr = new PatternDescr(inspector.getFieldType(name).getName());
patternDescr.setResource(resource);
TypeFieldDescr inheritedFlDescr = new TypeFieldDescr(name, patternDescr);
inheritedFlDescr.setResource(resource);
inheritedFlDescr.setInherited(!Modifier.isAbstract(inspector.getGetterMethods().get(name).getModifiers()));
if (!fieldMap.containsKey(inheritedFlDescr.getFieldName()))
fieldMap.put(inheritedFlDescr.getFieldName(), inheritedFlDescr);
}
}
}
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class JavaAccumulateBuilderTest method testBuildRuleBuildContextBaseDescr.
@Test
public void testBuildRuleBuildContextBaseDescr() {
// $total : Integer() from accumulate( Cheese( $price : price ) init( int x = 0; ) action( x += $price ) result( new Integer( x ) ) )
AccumulateDescr accumDescr = new AccumulateDescr();
BindingDescr price = new BindingDescr("$price", "price");
PatternDescr cheeseDescr = new PatternDescr("org.drools.mvel.compiler.Cheese");
cheeseDescr.addConstraint(price);
accumDescr.setInputPattern(cheeseDescr);
accumDescr.setInitCode("int x = 0; int y = 0;");
accumDescr.setActionCode("x += $price;");
accumDescr.setResultCode("new Integer( x )");
// org.drools.core.rule.Package pkg = new org.kie.rule.Package( "org.kie" );
final KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
pkgBuilder.addPackage(new PackageDescr("org.drools"));
final KnowledgeBuilderConfigurationImpl conf = pkgBuilder.getBuilderConfiguration();
PackageRegistry pkgReg = pkgBuilder.getPackageRegistry("org.drools");
InternalKnowledgePackage pkg = pkgReg.getPackage();
DialectCompiletimeRegistry dialectRegistry = pkgReg.getDialectCompiletimeRegistry();
Dialect dialect = dialectRegistry.getDialect("java");
RuleDescr ruleDescr = new RuleDescr("test rule");
RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, dialect);
Accumulate accumulate = (Accumulate) builder.build(context, accumDescr);
String generatedCode = (String) context.getMethods().get(0);
assertTrue(generatedCode.contains("private int x;"));
assertTrue(generatedCode.contains("private int y;"));
assertTrue(generatedCode.contains("x = 0;y = 0;"));
// System.out.println( context.getInvokers() );
// System.out.println( context.getMethods() );
}
use of org.drools.drl.ast.descr.PatternDescr 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());
}
Aggregations