All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.rule.builder.dialect.java.javaInvokers.mvel Maven / Gradle / Ivy

There is a newer version: 10.0.0
Show newest version
@declare{"getMethodBytecode"}
    public java.util.List getMethodBytecode() {
        org.drools.util.asm.MethodComparator.Tracer visit = new org.drools.util.asm.MethodComparator.Tracer("@{methodName}");

        java.io.InputStream is = getClass().getClassLoader().getResourceAsStream( "@{package}.@{ruleClassName}".replace( '.', '/' ) + ".class" );

        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int byteCount;
        try {
            while ( (byteCount = is.read( data,
                                 0,
                                 1024 )) > -1 )
            {
                bos.write(data, 0, byteCount);
            }
        } catch ( java.io.IOException e ) {
            throw new org.drools.RuntimeDroolsException("Unable getResourceAsStream for Class '@{ruleClassName}' ");
        }

        org.drools.asm.ClassReader classReader = new org.drools.asm.ClassReader( bos.toByteArray() );
        classReader.accept( visit, true );
        org.drools.asm.util.TraceMethodVisitor trace = visit.getTrace();
        return trace.getText();
    }
@end{}


@declare{"equals"}
    public boolean equals(Object object) {
        if (object == null ) {
            return false;
        } else if ( object == this ){
            return true;
        }

        if ( ! (object instanceof org.drools.spi.CompiledInvoker) ) {
            return false;
        }

        org.drools.spi.CompiledInvoker other = ( org.drools.spi.CompiledInvoker ) object;

        return org.drools.util.asm.MethodComparator.compareBytecode( getMethodBytecode(), other.getMethodBytecode() );
    }
@end{}

@declare{"hashCode"}
    public int hashCode() {
        return @{hashCode};
    }
@end{}

@declare{"returnValueInvoker"}
package @{package};

public class @{invokerClassName} implements org.drools.spi.ReturnValueExpression, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    public Object createContext() { return null; }

    public org.drools.spi.FieldValue evaluate(java.lang.Object object,
                            org.drools.spi.Tuple tuple,
                            org.drools.rule.Declaration[] previousDeclarations,
                            org.drools.rule.Declaration[] localDeclarations,
                            org.drools.WorkingMemory workingMemory,
                            Object context ) throws Exception {

        @code{i0=0}@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) previousDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
        @code{i0++}
        @end{}
        @if{readLocalsFromTuple}
          @code{i0=0}@foreach{type : localDeclarationTypes, declr : localDeclarations} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( localDeclarations[@{i0}] ) ).getObject() );
          @code{i0++}
          @end{}
        @else{}
          @code{i0=0}@foreach{type : localDeclarationTypes, declr : localDeclarations} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
          @code{i0++}
          @end{}
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        return @{ruleClassName}.@{methodName}(
            @foreach{declr : declarations} @{declr.identifier}
            @end{","}  @if{localDeclarations != empty && declarations != empty},@end{}
            @foreach{declr :localDeclarations} @{declr.identifier}
            @end{","}@if{globals != empty && (localDeclarations != empty || declarations != empty)},@end{}
            @foreach{identifier : globals}@{identifier}
            @end{","} );
    }

    @includeNamed{"hashCode"}

    @includeNamed{"getMethodBytecode"}

    @includeNamed{"equals"}
}
@end{}

@declare{"predicateInvoker"}
package @{package};

public class @{invokerClassName} implements org.drools.spi.PredicateExpression, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    public Object createContext() { return null; }

    public boolean evaluate(java.lang.Object object,
                            org.drools.spi.Tuple tuple,
                            org.drools.rule.Declaration[] previousDeclarations,
                            org.drools.rule.Declaration[] localDeclarations,
                            org.drools.WorkingMemory workingMemory,
                            Object context ) throws Exception {

        @code{i0=0}@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) previousDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( previousDeclarations[@{i0}] ) ).getObject() );
          @code{i0++}
        @end{}
        @code{i0=0}@foreach{type : localDeclarationTypes, declr : localDeclarations} @{type} @{declr.identifier} = ( @{type} ) localDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, object );
          @code{i0++}
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        return @{ruleClassName}.@{methodName}(
            @foreach{declr : declarations} @{declr.identifier}
            @end{","}  @if{localDeclarations != empty && declarations != empty},@end{}
            @foreach{declr : localDeclarations} @{declr.identifier}
            @end{","}@if{globals != empty && (localDeclarations != empty || declarations != empty)},@end{}
            @foreach{identifier : globals}@{identifier}
            @end{","} );
    }


    @includeNamed{"hashCode"}

    @includeNamed{"getMethodBytecode"}

    @includeNamed{"equals"}
}
@end{}

@declare{"evalInvoker"}
package @{package};

public class @{invokerClassName} implements org.drools.spi.EvalExpression, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    // no need for context
    public Object createContext() { return null; }

    public boolean evaluate(org.drools.spi.Tuple tuple,
                            org.drools.rule.Declaration[] declarations,
                            org.drools.WorkingMemory workingMemory,
                            Object context ) throws Exception {

        @code{i0=0}@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) tuple.get( declarations[@{i0}] ) ).getObject() );
            @code{i0++}
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        return @{ruleClassName}.@{methodName}(
            @foreach{declr : declarations} @{declr.identifier}
            @end{","} @if{globals != empty && declarations != empty},@end{}
            @foreach{identifier : globals}@{identifier}
            @end{","} );
    }

    @includeNamed{"hashCode"}

    @includeNamed{"getMethodBytecode"}

    @includeNamed{"equals"}
}
@end{}


@declare{"accumulateInvoker"}
package @{package};

import org.drools.asm.ClassReader;
import org.drools.asm.util.TraceMethodVisitor;
import org.drools.util.asm.MethodComparator.Tracer;
import java.util.Collections;
public class @{invokerClassName} implements org.drools.spi.Accumulator, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    public java.io.Serializable createContext() {
        return new @{ruleClassName}.@{className}();
    }

    public void init(java.lang.Object workingMemoryContext,
                     java.lang.Object context,
                     org.drools.spi.Tuple leftTuple,
                     org.drools.rule.Declaration[] declarations,
                     org.drools.WorkingMemory workingMemory) throws Exception {
        @code{i0=0}@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
            @code{i0++}
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        ((@{ruleClassName}.@{className})context).init(
            @foreach{declr : declarations} @{declr.identifier}@end{","}@if{globals != empty && declarations != empty},@end{}
	        @foreach{identifier : globals} @{identifier}@end{","} );

    }

    public void accumulate(java.lang.Object workingMemoryContext,
                           java.lang.Object context,
                           org.drools.spi.Tuple leftTuple,
                           org.drools.common.InternalFactHandle handle,
                           org.drools.rule.Declaration[] declarations,
                           org.drools.rule.Declaration[] innerDeclarations,
                           org.drools.WorkingMemory workingMemory) throws Exception {
        @code{ i0 = 0 }@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
            @code{ i0++ }
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}
        @if{isMultiPattern}
          @code{ i0 = 0 }@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( innerDeclarations[@{i0}] ) ).getObject() );
            @code{ i0++ }
          @end{}
        @else{}
          @code{ i0 = 0 }@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, handle.getObject() );
            @code{ i0++ }
          @end{}
        @end{}
        ((@{ruleClassName}.@{className})context).accumulate(
            workingMemory,
            handle,
            innerDeclarations,
            handle.getObject()@if{declarations != empty},@end{}
            @foreach{declr : declarations} @{declr.identifier}@end{","}@if{globals != empty},@end{}
	        @foreach{identifier: globals} @{identifier}@end{","}@if{innerDeclarations != empty},@end{}
            @foreach{declr : innerDeclarations} @{declr.identifier}@end{","});
    }

    public void reverse(java.lang.Object workingMemoryContext,
                           java.lang.Object context,
                           org.drools.spi.Tuple leftTuple,
                           org.drools.common.InternalFactHandle handle,
                           org.drools.rule.Declaration[] declarations,
                           org.drools.rule.Declaration[] innerDeclarations,
                           org.drools.WorkingMemory workingMemory) throws Exception {
        @code{ i0 = 0 }@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
            @code{ i0++ }
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}
        @if{isMultiPattern}
          @code{ i0 = 0}@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( innerDeclarations[@{i0}] ) ).getObject() );
            @code{ i0++ }
          @end{}
        @else{}
          @code{ i0 = 0 }@foreach{declr : innerDeclarations} @{declr.extractor.extractToClassName} @{declr.identifier} = (@{declr.extractor.extractToClassName}) innerDeclarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, handle.getObject() );
            @code{ i0++ }
          @end{}
        @end{}

        ((@{ruleClassName}.@{className})context).reverse(
            workingMemory,
            handle,
            handle.getObject()@if{globals != empty},@end{}
	        @foreach{identifier : globals} @{identifier}@end{","});
    }

    public Object getResult(java.lang.Object workingMemoryContext,
                            java.lang.Object context,
                            org.drools.spi.Tuple leftTuple,
                            org.drools.rule.Declaration[] declarations,
                            org.drools.WorkingMemory workingMemory) throws Exception {
        @code{ i0 = 0 }@foreach{type : declarationTypes, declr : declarations} @{type} @{declr.identifier} = ( @{type} ) declarations[@{i0}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, ( (org.drools.common.InternalFactHandle) leftTuple.get( declarations[@{i0}] ) ).getObject() );
            @code{ i0++ }
        @end{}
        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        return ((@{ruleClassName}.@{className})context).getResult(
            @foreach{declr : declarations} @{declr.identifier}@end{","}@if{globals != empty && declarations != empty},@end{}
	        @foreach{identifier : globals} @{identifier}@end{","} );
    }

    public boolean supportsReverse() {
        return @{supportsReverse};
    }

    public Object createWorkingMemoryContext() {
        return null;
    }

    @includeNamed{"hashCode"}

    @includeNamed{"equals"}

    public java.util.List getMethodBytecode() {
        java.io.InputStream is = @{ruleClassName}.class.getClassLoader().getResourceAsStream( "@{package}.@{ruleClassName}".replace( '.', '/' ) + "$@{className}" + ".class" );

        java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int byteCount;
        try {
            while ( (byteCount = is.read( data,
                                 0,
                                 1024 )) > -1 )
            {
                bos.write(data, 0, byteCount);
            }
        } catch ( java.io.IOException e ) {
            throw new org.drools.RuntimeDroolsException("Unable getResourceAsStream for Class '@{ruleClassName}$@{className}' ");
        }
        return Collections.singletonList( bos );
    }
}
@end{}

@declare{"consequenceInvoker"}
package @{package};

public class @{invokerClassName} implements org.drools.spi.Consequence, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    public void evaluate(org.drools.spi.KnowledgeHelper knowledgeHelper,
                         org.drools.WorkingMemory workingMemory) throws Exception {

        org.drools.spi.Tuple tuple = knowledgeHelper.getTuple();
        org.drools.rule.Declaration[] declarations = knowledgeHelper.getRule().getDeclarations();

        @foreach{type : declarationTypes, declr : declarations, index : indexes, notPattern : notPatterns}
          org.drools.common.InternalFactHandle @{declr.identifier}__Handle__ = ( org.drools.common.InternalFactHandle ) tuple.get( knowledgeHelper.getDeclaration( declarations[@{index}].getIdentifier() ) );
          @{type} @{declr.identifier} = ( @{type} )  declarations[@{index}].@{declr.nativeReadMethod.name}( (org.drools.common.InternalWorkingMemory) workingMemory, @{declr.identifier}__Handle__.getObject() );
          @if{notPattern}@{declr.identifier}__Handle__ =  (org.drools.common.InternalFactHandle) knowledgeHelper.getWorkingMemory().getFactHandle( @{declr.identifier} );@end{}
        @end{}

        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        @{ruleClassName}.@{methodName} (
            knowledgeHelper@if{declarations != empty},@end{}
            @foreach{declr : declarations} @{declr.identifier}, @{declr.identifier}__Handle__
            @end{","}@if{globals != empty},@end{}
	        @foreach{identifier : globals} @{identifier}
	        @end{","} );
    }
    
    public java.util.List getMethodBytecode() {
        return null;
    }    
}
@end{}


@declare{"actionInvoker"}
package @{package};

public class @{invokerClassName} implements org.drools.spi.Action, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    public void execute(org.drools.spi.KnowledgeHelper knowledgeHelper,
                        org.drools.WorkingMemory workingMemory) throws Exception {
        execute(knowledgeHelper, workingMemory, null);
    }

    public void execute(org.drools.spi.KnowledgeHelper knowledgeHelper,
                        org.drools.WorkingMemory workingMemory, org.drools.spi.ProcessContext context ) throws Exception {

        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        @{processClassName}.@{methodName} (
	        knowledgeHelper@if{globals != empty},@end{}
	        @foreach{identifier : globals} @{identifier}
	        @end{","}, context );
    }
    
    public java.util.List getMethodBytecode() {
        return null;
    }    
}
@end{}

@declare{"returnValueEvaluatorInvoker"}
package @{package};

public class @{invokerClassName} implements org.drools.spi.ReturnValueEvaluator, org.drools.spi.CompiledInvoker
{
    private static final long serialVersionUID  = 400L;

    public Object evaluate(org.drools.WorkingMemory workingMemory, org.drools.spi.ProcessContext processContext) throws Exception {

        @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
        @end{}

        return @{processClassName}.@{methodName} (
	        @foreach{identifier : globals} @{identifier}
	        @end{","} );
    }
    
    public java.util.List getMethodBytecode() {
        return null;
    }
}
@end{}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy