use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class ClassDefinitionFactory method populateDefinitionFromClass.
public static void populateDefinitionFromClass(ClassDefinition def, Resource resource, Class<?> concrete, boolean asTrait) {
try {
def.setClassName(concrete.getName());
if (concrete.getSuperclass() != null) {
def.setSuperClass(concrete.getSuperclass().getName());
}
ClassFieldInspector inspector = CoreComponentsBuilder.get().createClassFieldInspector(concrete);
Map<String, Method> methods = inspector.getGetterMethods();
Map<String, Method> setters = inspector.getSetterMethods();
Map<String, TypeFieldDescr> fields = new HashMap<>();
for (String fieldName : methods.keySet()) {
if (asTrait && ("core".equals(fieldName) || "fields".equals(fieldName))) {
continue;
}
if (!inspector.isNonGetter(fieldName) && setters.containsKey(fieldName)) {
Position position = null;
if (!concrete.isInterface()) {
try {
Field fld = concrete.getDeclaredField(fieldName);
position = fld.getAnnotation(Position.class);
} catch (NoSuchFieldException nsfe) {
// @Position can only annotate fields. This x means that a getter/setter pair was found with no field
}
}
Class ret = methods.get(fieldName).getReturnType();
TypeFieldDescr field = new TypeFieldDescr();
field.setResource(resource);
field.setFieldName(fieldName);
field.setPattern(new PatternDescr(ret.getName()));
field.setIndex(position != null ? position.value() : -1);
fields.put(fieldName, field);
}
}
if (!fields.isEmpty()) {
List<FieldDefinition> fieldDefs = sortFields(fields, null, null);
int i = 0;
for (FieldDefinition fieldDef : fieldDefs) {
fieldDef.setIndex(i++);
def.addField(fieldDef);
}
}
Set<String> interfaces = new HashSet<>();
Collections.addAll(interfaces, def.getInterfaces());
for (Class iKlass : ClassUtils.getAllImplementedInterfaceNames(concrete)) {
interfaces.add(iKlass.getName());
}
def.setInterfaces(interfaces.toArray(new String[interfaces.size()]));
def.setDefinedClass(concrete);
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class ClassHierarchyManager method buildInheritedFieldDescrFromDefinition.
protected TypeFieldDescr buildInheritedFieldDescrFromDefinition(org.kie.api.definition.type.FactField fld, TypeDeclarationDescr typeDescr) {
TypeFieldDescr inheritedFldDescr = new TypeFieldDescr();
inheritedFldDescr.setFieldName(fld.getName());
inheritedFldDescr.setResource(typeDescr.getResource());
PatternDescr fldType = new PatternDescr();
fldType.setObjectType(((FieldDefinition) fld).getTypeName());
// also sets resource for PatternDescr fldType
inheritedFldDescr.setPattern(fldType);
if (fld.isKey()) {
AnnotationDescr keyAnnotation = new AnnotationDescr(Key.class.getCanonicalName());
keyAnnotation.setFullyQualifiedName(Key.class.getCanonicalName());
keyAnnotation.setResource(typeDescr.getResource());
inheritedFldDescr.addAnnotation(keyAnnotation);
}
inheritedFldDescr.setIndex(((FieldDefinition) fld).getDeclIndex());
inheritedFldDescr.setInherited(true);
String initExprOverride = ((FieldDefinition) fld).getInitExpr();
int overrideCount = 0;
// only @aliasing local fields may override defaults.
for (TypeFieldDescr localField : typeDescr.getFields().values()) {
Alias alias = getTypedAnnotation(localField, Alias.class);
if (alias != null && fld.getName().equals(alias.value().replaceAll("\"", "")) && localField.getInitExpr() != null) {
overrideCount++;
initExprOverride = localField.getInitExpr();
}
}
if (overrideCount > 1) {
// however, only one is allowed
initExprOverride = null;
}
inheritedFldDescr.setInitExpr(initExprOverride);
return inheritedFldDescr;
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class WindowReferenceGenerator method addField.
private void addField(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, WindowDeclarationDescr descr) {
final String windowName = toVar(descr.getName());
final MethodCallExpr initializer = createDslTopLevelMethod(WINDOW_CALL);
final PatternDescr pattern = descr.getPattern();
ParsedBehavior behavior = pattern.getBehaviors().stream().map(this::parseTypeFromBehavior).findFirst().orElseThrow(RuntimeException::new);
final WindowDefinition.Type windowType = behavior.windowType;
initializer.addArgument(new NameExpr(windowType.getDeclaringClass().getCanonicalName() + "." + windowType.toString()));
initializer.addArgument(new IntegerLiteralExpr(behavior.duration.getValue()));
final TimeUnit timeUnit = behavior.duration.getTimeUnit();
initializer.addArgument(new NameExpr(timeUnit.getDeclaringClass().getCanonicalName() + "." + timeUnit.name()));
final Class<?> initClass = DrlxParseUtil.getClassFromContext(typeResolver, pattern.getObjectType());
initializer.addArgument(new ClassExpr(toJavaParserType(initClass)));
if (pattern.getSource() != null) {
String epName = ((EntryPointDescr) pattern.getSource()).getEntryId();
MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
entryPointCall.addArgument(toStringLiteral(epName));
initializer.addArgument(entryPointCall);
}
parseConditions(kbuilder, packageModel, pattern, initClass).forEach(initializer::addArgument);
packageModel.addAllWindowReferences(windowName, initializer);
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsXor.
@Test
public void declareConstraintsXor() {
String temperatureField = "TEMPERATURE";
final List<KiePMMLFieldOperatorValue> xorConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsXor(xorConstraints);
assertNotNull(lhsBuilder.getDescr());
assertNotNull(lhsBuilder.getDescr().getDescrs());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
AndDescr rootAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
assertEquals(2, rootAndDescr.getDescrs().size());
assertTrue(rootAndDescr.getDescrs().get(0) instanceof NotDescr);
assertTrue(rootAndDescr.getDescrs().get(1) instanceof ExistsDescr);
// "Not" construct
NotDescr notDescr = (NotDescr) rootAndDescr.getDescrs().get(0);
assertEquals(1, notDescr.getDescrs().size());
assertTrue(notDescr.getDescrs().get(0) instanceof AndDescr);
AndDescr notAndDescr = (AndDescr) notDescr.getDescrs().get(0);
assertTrue(notAndDescr.getDescrs().get(0) instanceof PatternDescr);
assertTrue(notAndDescr.getDescrs().get(1) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) notAndDescr.getDescrs().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());
patternDescr = (PatternDescr) notAndDescr.getDescrs().get(1);
assertEquals(temperatureField, 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());
// "Exists" construct
ExistsDescr existsDescr = (ExistsDescr) rootAndDescr.getDescrs().get(1);
assertEquals(1, existsDescr.getDescrs().size());
assertTrue(existsDescr.getDescrs().get(0) instanceof OrDescr);
OrDescr existsOrDescr = (OrDescr) existsDescr.getDescrs().get(0);
assertEquals(2, existsOrDescr.getDescrs().size());
assertTrue(existsOrDescr.getDescrs().get(0) instanceof PatternDescr);
assertTrue(existsOrDescr.getDescrs().get(1) instanceof OrDescr);
patternDescr = (PatternDescr) existsOrDescr.getDescrs().get(0);
assertEquals(temperatureField, 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 < 35";
assertEquals(expected, exprConstraintDescr.getExpression());
OrDescr nestedOrDescr = (OrDescr) existsOrDescr.getDescrs().get(1);
assertEquals(1, nestedOrDescr.getDescrs().size());
assertTrue(nestedOrDescr.getDescrs().get(0) instanceof PatternDescr);
patternDescr = (PatternDescr) nestedOrDescr.getDescrs().get(0);
assertEquals(temperatureField, 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 declareConstraintNotIn.
@Test
public void declareConstraintNotIn() {
List<Object> values = Arrays.asList("3", "8.5");
String patternType = "INPUT2";
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintNotIn(patternType, values);
final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
assertNotNull(descrs);
assertEquals(1, descrs.size());
assertTrue(descrs.get(0) instanceof NotDescr);
NotDescr notDescr = (NotDescr) descrs.get(0);
assertEquals(1, notDescr.getDescrs().size());
assertTrue(notDescr.getDescrs().get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().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 (3, 8.5)";
assertEquals(expected, exprConstraintDescr.getExpression());
}
Aggregations