use of org.drools.compiler.lang.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.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclaration.
@Test
public void testTypeDeclaration() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("StockTick");
typeDescr.addAnnotation(Role.class.getCanonicalName(), "Event");
typeDescr.addAnnotation(TypeSafe.class.getCanonicalName(), "true");
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
if (builder.hasErrors()) {
fail(builder.getErrors().toString());
}
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("StockTick");
assertTrue(type.isTypesafe());
assertEquals(Role.Type.EVENT, type.getRole());
assertEquals(StockTick.class, type.getTypeClass());
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationWithFieldMetadata.
@Test
public void testTypeDeclarationWithFieldMetadata() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("TypeWithFieldMeta");
TypeFieldDescr f1 = new TypeFieldDescr("field", new PatternDescr("String"));
f1.addAnnotation("custom", null);
typeDescr.addField(f1);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
final FactType factType = bp.getFactType("org.drools.compiler.test.TypeWithFieldMeta");
assertNotNull(factType);
final FactField field = factType.getField("field");
assertNotNull(field);
final Map<String, Object> fieldMetaData = field.getMetaData();
assertNotNull("No field-level custom metadata got compiled", fieldMetaData);
assertTrue("Field metadata does not include expected value", fieldMetaData.containsKey("custom"));
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class DrlParserTest method testDeclaredSuperType.
@Test
public void testDeclaredSuperType() throws Exception {
String drl = "package foo \n" + "declare Bean1 \n" + "age: int \n" + "name : String \n" + "end \n" + "declare Bean2 extends Bean1\n" + "cheese : String \n" + "end";
DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
PackageDescr pkgDescr = parser.parse(null, drl);
TypeDeclarationDescr bean1Type = pkgDescr.getTypeDeclarations().get(0);
assertNull(bean1Type.getSuperTypeName());
TypeDeclarationDescr bean2Type = pkgDescr.getTypeDeclarations().get(1);
assertEquals("Bean1", bean2Type.getSuperTypeName());
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationFactory method processAnnotations.
public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
Role role = typeDescr.getTypedAnnotation(Role.class);
if (role != null) {
type.setRole(role.value());
}
TypeSafe typeSafe = typeDescr.getTypedAnnotation(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));
}
Aggregations