Skip to content

Commit

Permalink
JBRULES-3119 Timestamp and Duration no longer work with complex expre…
Browse files Browse the repository at this point in the history
…ssions
  • Loading branch information
mdproctor committed Jul 5, 2011
1 parent 9e4baf2 commit 64f3ba9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,8 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
if ( cls != null ) {
String str = ClassUtils.getPackage( cls );
typeDescr.setNamespace( str );
typeDescr.setTypeName( cls.getSimpleName() );
dotPos = cls.getName().lastIndexOf( '.' ); // reget dotPos, incase there were nested classes
typeDescr.setTypeName( cls.getName().substring( dotPos + 1 ) );
} else {
typeDescr.setNamespace( typeDescr.getTypeName().substring( 0,
dotPos ) );
Expand All @@ -1417,7 +1418,9 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
try {
Class< ? > cls = defaultRegistry.getTypeResolver().resolveType( typeDescr.getTypeName() );
typeDescr.setNamespace( ClassUtils.getPackage( cls ) );
typeDescr.setTypeName( cls.getSimpleName() );
String name = cls.getName();
dotPos = name.lastIndexOf( '.' );
typeDescr.setTypeName( name.substring( dotPos + 1 ) );
} catch ( ClassNotFoundException e ) {
// swallow, as this isn't a mistake, it just means the type declaration is intended for the default namespace
typeDescr.setNamespace( packageDescr.getNamespace() ); // set the default namespace
Expand Down Expand Up @@ -1562,7 +1565,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
type.setTimestampAttribute( timestamp );
ClassDefinition cd = type.getTypeClassDef();
Package pkg = pkgRegistry.getPackage();
InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader( pkg.getName(),
InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader( ClassUtils.getPackage( type.getTypeClass() ),
type.getTypeClass().getName(),
timestamp,
type.isTypesafe() );
Expand All @@ -1578,7 +1581,7 @@ private void processTypeDeclarations(final PackageDescr packageDescr) {
type.setDurationAttribute( duration );
ClassDefinition cd = type.getTypeClassDef();
Package pkg = pkgRegistry.getPackage();
InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader( pkg.getName(),
InternalReadAccessor reader = pkg.getClassFieldAccessorStore().getMVELReader( ClassUtils.getPackage( type.getTypeClass() ),
type.getTypeClass().getName(),
duration,
type.isTypesafe() );
Expand Down Expand Up @@ -1749,7 +1752,6 @@ private void generateDeclaredBean(TypeDeclarationDescr typeDescr,
d );

} catch ( Exception e ) {
e.printStackTrace();
this.results.add( new TypeDeclarationError( "Unable to create a class for declared type " + fullName + ": " + e.getMessage() + ";",
typeDescr.getLine() ) );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import org.drools.time.SessionPseudoClock;
import org.drools.time.impl.DurationTimer;
import org.drools.time.impl.PseudoClockScheduler;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.ArgumentCaptor;

Expand Down Expand Up @@ -169,10 +170,9 @@ public void testComplexTimestamp() {
rule += "package " + Message.class.getPackage().getName() + "\n" +
"declare " + Message.class.getCanonicalName() + "\n" +
" @role( event ) \n" +
" @timestamp( getProperties().get( 'timestamp' ) ) \n" +
" @timestamp( getProperties().get( 'timestamp' )-1 ) \n" +
" @duration( getProperties().get( 'duration' )+1 ) \n" +
"end\n";

System.out.println( rule );

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newReaderResource( new StringReader( rule ) ),
Expand All @@ -191,10 +191,12 @@ public void testComplexTimestamp() {
Message msg = new Message();
Properties props = new Properties();
props.put( "timestamp", new Integer( 99 ) );
props.put( "duration", new Integer( 52 ) );
msg.setProperties( props );

EventFactHandle efh = ( EventFactHandle ) ksession.insert( msg );
assertEquals( 99, efh.getStartTimestamp() );
assertEquals( 98, efh.getStartTimestamp() );
assertEquals( 53, efh.getDuration() );

}

Expand Down
10 changes: 9 additions & 1 deletion drools-core/src/main/java/org/drools/core/util/ClassUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,15 @@ public static String getPackage(Class<?> cls) {
// cls.getPackage() sometimes returns null, in which case fall back to string massaging.
java.lang.Package pkg = cls.isArray() ? cls.getComponentType().getPackage() : cls.getPackage();
if ( pkg == null ) {
int dotPos = cls.getName().lastIndexOf( '.' );
int dotPos;
int dolPos = cls.getName().indexOf( '$' );
if ( dolPos > 0 ) {
// we have nested classes, so adjust dotpos to before first $
dotPos = cls.getName().substring( 0, dolPos ).lastIndexOf( '.' );
} else {
dotPos = cls.getName().lastIndexOf( '.' );
}

if ( dotPos > 0 ) {
return cls.getName().substring( 0,
dotPos - 1 );
Expand Down

0 comments on commit 64f3ba9

Please sign in to comment.