use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationFactory method processAnnotations.
public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
Role role = getTypedAnnotation(typeDescr, Role.class);
if (role != null) {
type.setRole(role.value());
}
TypeSafe typeSafe = getTypedAnnotation(typeDescr, TypeSafe.class);
if (typeSafe != null) {
type.setTypesafe(typeSafe.value());
}
if (typeDescr instanceof EnumDeclarationDescr) {
type.setKind(TypeDeclaration.Kind.ENUM);
} else if (typeDescr instanceof TypeDeclarationDescr && ((TypeDeclarationDescr) typeDescr).isTrait()) {
type.setKind(TypeDeclaration.Kind.TRAIT);
}
type.setDynamic(typeDescr.hasAnnotation(PropertyChangeSupport.class));
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationBuilder method mergeTypeDescriptors.
private boolean mergeTypeDescriptors(AbstractClassTypeDeclarationDescr prev, AbstractClassTypeDeclarationDescr descr) {
boolean isDef1 = isDefinition(prev);
boolean isDef2 = isDefinition(descr);
if (isDef1 && isDef2) {
return false;
}
if (!prev.getSuperTypes().isEmpty() && !descr.getSuperTypes().isEmpty() && prev.getSuperTypes().size() != descr.getSuperTypes().size()) {
return false;
}
if (prev.getSuperTypes().isEmpty()) {
for (QualifiedName qn : descr.getSuperTypes()) {
((TypeDeclarationDescr) prev).addSuperType(qn);
}
}
if (prev.getFields().isEmpty()) {
for (String fieldName : descr.getFields().keySet()) {
prev.addField(descr.getFields().get(fieldName));
}
}
for (AnnotationDescr ad : descr.getAnnotations()) {
prev.addQualifiedAnnotation(ad);
}
for (AnnotationDescr ad : prev.getAnnotations()) {
if (!descr.getAnnotations().contains(ad)) {
descr.addQualifiedAnnotation(ad);
}
}
return true;
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTimeWindowBehavior.
@Test
public void testTimeWindowBehavior() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
typeDeclDescr.addAnnotation("role", "event");
packageDescr.addTypeDeclaration(typeDeclDescr);
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
final BehaviorDescr windowDescr = new BehaviorDescr("window");
windowDescr.setSubType("time");
windowDescr.setParameters(Collections.singletonList("60000"));
patternDescr.addBehavior(windowDescr);
lhs.addDescr(patternDescr);
ruleDescr.setConsequence("System.out.println( $tick );");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
final RuleImpl rule = pkg.getRule("rule-1");
assertNotNull(rule);
final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
final Behavior window = pattern.getBehaviors().get(0);
assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class RuleParserTest method testTypeDeclarationWithFields.
@Test
public void testTypeDeclarationWithFields() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "declare_type_with_fields.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
List<TypeDeclarationDescr> td = pkg.getTypeDeclarations();
assertEquals(3, td.size());
TypeDeclarationDescr d = td.get(0);
assertEquals("SomeFact", d.getTypeName());
assertEquals(2, d.getFields().size());
assertTrue(d.getFields().containsKey("name"));
assertTrue(d.getFields().containsKey("age"));
TypeFieldDescr f = d.getFields().get("name");
assertEquals("String", f.getPattern().getObjectType());
f = d.getFields().get("age");
assertEquals("Integer", f.getPattern().getObjectType());
d = td.get(1);
assertEquals("AnotherFact", d.getTypeName());
TypeDeclarationDescr type = td.get(2);
assertEquals("Person", type.getTypeName());
assertEquals("fact", type.getAnnotation("role").getValue());
assertEquals("\"Models a person\"", type.getAnnotation("doc").getValue("descr"));
assertEquals("\"Bob\"", type.getAnnotation("doc").getValue("author"));
assertEquals("Calendar.getInstance().getDate()", type.getAnnotation("doc").getValue("date"));
assertEquals(2, type.getFields().size());
TypeFieldDescr field = type.getFields().get("name");
assertEquals("name", field.getFieldName());
assertEquals("String", field.getPattern().getObjectType());
assertEquals("\"John Doe\"", field.getInitExpr());
assertEquals("50", field.getAnnotation("length").getValue("max"));
assertNotNull(field.getAnnotation("key"));
field = type.getFields().get("age");
assertEquals("age", field.getFieldName());
assertEquals("int", field.getPattern().getObjectType());
assertEquals("-1", field.getInitExpr());
assertEquals("0", field.getAnnotation("ranged").getValue("min"));
assertEquals("150", field.getAnnotation("ranged").getValue("max"));
assertEquals("-1", field.getAnnotation("ranged").getValue("unknown"));
}
use of org.drools.drl.ast.descr.TypeDeclarationDescr in project drools by kiegroup.
the class RuleParserTest method testTypeWithMetaData.
@Test
public void testTypeWithMetaData() throws Exception {
PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "type_with_meta.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations();
assertEquals(3, declarations.size());
}
Aggregations