Pattern prefixPattern) {
final PatternDescr patternDescr = (PatternDescr) descr;
if ( patternDescr.getObjectType() == null || patternDescr.getObjectType().equals( "" ) ) {
context.getErrors().add( new DescrBuildError( context.getParentDescr(),
patternDescr,
null,
"ObjectType not correctly defined" ) );
return null;
}
ObjectType objectType = null;
final FactTemplate factTemplate = context.getPkg().getFactTemplate( patternDescr.getObjectType() );
if ( factTemplate != null ) {
objectType = new FactTemplateObjectType( factTemplate );
} else {
try {
final Class userProvidedClass = context.getDialect().getTypeResolver().resolveType( patternDescr.getObjectType() );
final boolean isEvent = context.getPkg().isEvent( userProvidedClass );
objectType = new ClassObjectType( userProvidedClass,
isEvent );
} catch ( final ClassNotFoundException e ) {
// swallow as we'll do another check in a moment and then record the problem
}
}
// lets see if it maps to a query
if ( objectType == null ) {
Rule rule = context.getPkg().getRule( patternDescr.getObjectType() );
if ( rule != null && rule instanceof Query ) {
// it's a query so delegate to the QueryElementBuilder
QueryElementBuilder qeBuilder = new QueryElementBuilder();
return qeBuilder.build( context, descr, prefixPattern );
} else {
// this isn't a query either, so log an error
context.getErrors().add( new DescrBuildError( context.getParentDescr(),
patternDescr,
null,
"Unable to resolve ObjectType '" + patternDescr.getObjectType() + "'" ) );
return null;
}
}
Pattern pattern;
if ( patternDescr.getIdentifier() != null && !patternDescr.getIdentifier().equals( "" ) ) {
if ( context.getDeclarationResolver().isDuplicated( context.getRule(),
patternDescr.getIdentifier() ) ) {
// This declaration already exists, so throw an Exception
context.getErrors().add( new DescrBuildError( context.getParentDescr(),
patternDescr,
null,
"Duplicate declaration for variable '" + patternDescr.getIdentifier() + "' in the rule '" + context.getRule().getName() + "'" ) );
}
pattern = new Pattern( context.getNextPatternId(),
0, // offset is 0 by default
objectType,
patternDescr.getIdentifier(),
patternDescr.isInternalFact() );
if ( objectType instanceof ClassObjectType ) {
// make sure PatternExtractor is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().getClassObjectType( ((ClassObjectType) objectType),
(PatternExtractor) pattern.getDeclaration().getExtractor() );
}
} else {
pattern = new Pattern( context.getNextPatternId(),
0, // offset is 0 by default
objectType,
null );
}
if ( objectType instanceof ClassObjectType ) {
// make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
context.getPkg().getClassFieldAccessorStore().getClassObjectType( ((ClassObjectType) objectType),
pattern );
}
//context.getPkg().getClassFieldAccessorStore().get
// adding the newly created pattern to the build stack
// this is necessary in case of local declaration usage
context.getBuildStack().push( pattern );
for ( final Iterator it = patternDescr.getDescrs().iterator(); it.hasNext(); ) {
final Object object = it.next();
buildConstraint( context,
pattern,
object,
null );
}
if ( patternDescr.getSource() != null ) {
// we have a pattern source, so build it
RuleConditionBuilder builder = (RuleConditionBuilder) context.getDialect().getBuilder( patternDescr.getSource().getClass() );
PatternSource source = (PatternSource) builder.build( context,
patternDescr.getSource() );
pattern.setSource( source );
}
for ( BehaviorDescr behaviorDescr : patternDescr.getBehaviors() ) {
if( pattern.getObjectType().isEvent() ) {
if ( Behavior.BehaviorType.TIME_WINDOW.matches( behaviorDescr.getType() ) ) {
SlidingWindowDescr swd = (SlidingWindowDescr) behaviorDescr;
SlidingTimeWindow window = new SlidingTimeWindow( swd.getLength() );
pattern.addBehavior( window );
} else if ( Behavior.BehaviorType.LENGTH_WINDOW.matches( behaviorDescr.getType() ) ) {
SlidingWindowDescr swd = (SlidingWindowDescr) behaviorDescr;
SlidingLengthWindow window = new SlidingLengthWindow( (int) swd.getLength() );
pattern.addBehavior( window );
}
} else {
// Some behaviors can only be assigned to patterns declared as events
context.getErrors().add( new DescrBuildError( context.getParentDescr(),
patternDescr,
null,
"A Sliding Window behavior can only be assigned to patterns declared with @role( event ). The pattern '" + pattern.getObjectType() + "' in the rule '" + context.getRule().getName() + "' is not declared as an Event." ) );
}
}