/**
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.base.mvel;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Map;
import org.drools.rule.Declaration;
import org.drools.rule.MVELDialectRuntimeData;
import org.drools.rule.Package;
import org.drools.WorkingMemory;
import org.drools.spi.EvalExpression;
import org.drools.spi.Tuple;
import org.mvel2.MVEL;
public class MVELEvalExpression
implements
EvalExpression,
MVELCompileable,
Externalizable {
private static final long serialVersionUID = 510l;
private MVELCompilationUnit unit;
private String id;
private Serializable expr;
private DroolsMVELFactory prototype;
public MVELEvalExpression() {
}
public MVELEvalExpression(final MVELCompilationUnit unit,
final String id) {
this.unit = unit;
this.id = id;
}
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
id = in.readUTF();
unit = (MVELCompilationUnit) in.readObject();
// expr = (Serializable)in.readObject();
// prototype = (DroolsMVELFactory)in.readObject();
}
public void writeExternal(ObjectOutput out) throws IOException {
out.writeUTF( id );
out.writeObject( unit );
// out.writeObject(expr);
// out.writeObject(prototype);
}
public void compile(ClassLoader classLoader) {
expr = unit.getCompiledExpression( classLoader );
prototype = unit.getFactory();
}
public Object createContext() {
return this.prototype.clone();
}
public boolean evaluate(final Tuple tuple,
final Declaration[] requiredDeclarations,
final WorkingMemory workingMemory,
final Object context) throws Exception {
DroolsMVELFactory factory = (DroolsMVELFactory) context;
factory.setContext( tuple,
null,
null,
workingMemory,
null );
// do we have any functions for this namespace?
Package pkg = workingMemory.getRuleBase().getPackage( "MAIN" );
if ( pkg != null ) {
MVELDialectRuntimeData data = (MVELDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData( this.id );
factory.setNextFactory( data.getFunctionFactory() );
}
final Boolean result = (Boolean) MVEL.executeExpression( this.expr,
new Object(),
factory );
return result.booleanValue();
}
public String toString() {
return this.unit.getExpression();
}
@SuppressWarnings("unchecked")
public Declaration[] getRequiredDeclarations() {
Map previousDeclarations = this.unit.getFactory().getPreviousDeclarations();
return (Declaration[]) previousDeclarations.values().toArray( new Declaration[previousDeclarations.size()] );
}
public void replaceDeclaration(Declaration declaration,
Declaration resolved) {
this.unit.replaceDeclaration( declaration,
resolved );
// need to get a new prototype factory, since the declaration was updated
prototype = unit.getFactory();
}
public MVELEvalExpression clone() {
MVELEvalExpression clone = new MVELEvalExpression( unit.clone(),
id );
// expr should be stateless, so it should be fine to share the reference
clone.expr = expr;
clone.prototype = clone.unit.getFactory();
return clone;
}
}