Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 227   Methods: 7
NCLOC: 112   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
JavaCondition.java 50% 75% 85.7% 73%
coverage coverage
 1    package org.drools.semantics.java;
 2   
 3    /*
 4    * $Id: JavaCondition.java,v 1.11.2.2 2005/05/08 00:57:36 mproctor Exp $
 5    *
 6    * Copyright 2002 (C) The Werken Company. All Rights Reserved.
 7    *
 8    * Redistribution and use of this software and associated documentation
 9    * ("Software"), with or without modification, are permitted provided that the
 10    * following conditions are met:
 11    *
 12    * 1. Redistributions of source code must retain copyright statements and
 13    * notices. Redistributions must also contain a copy of this document.
 14    *
 15    * 2. Redistributions in binary form must reproduce the above copyright notice,
 16    * this list of conditions and the following disclaimer in the documentation
 17    * and/or other materials provided with the distribution.
 18    *
 19    * 3. The name "drools" must not be used to endorse or promote products derived
 20    * from this Software without prior written permission of The Werken Company.
 21    * For written permission, please contact bob@werken.com.
 22    *
 23    * 4. Products derived from this Software may not be called "drools" nor may
 24    * "drools" appear in their names without prior written permission of The Werken
 25    * Company. "drools" is a registered trademark of The Werken Company.
 26    *
 27    * 5. Due credit should be given to The Werken Company.
 28    * (http://drools.werken.com/).
 29    *
 30    * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
 31    * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 32    * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 33    * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
 34    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 35    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 36    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 37    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 38    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 39    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 40    * POSSIBILITY OF SUCH DAMAGE.
 41    *
 42    */
 43   
 44    import java.io.Serializable;
 45    import java.util.List;
 46    import java.util.Map;
 47   
 48    import javax.naming.ConfigurationException;
 49   
 50    import org.codehaus.janino.Scanner;
 51    import org.drools.rule.Declaration;
 52    import org.drools.rule.Rule;
 53    import org.drools.spi.Condition;
 54    import org.drools.spi.ConditionException;
 55    import org.drools.spi.DefaultKnowledgeHelper;
 56    import org.drools.spi.KnowledgeHelper;
 57    import org.drools.spi.Tuple;
 58   
 59    /**
 60    * Java expression semantics <code>Condition</code>.
 61    *
 62    * @author <a href="mailto:bob@werken.com">bob@werken.com </a>
 63    */
 64    public class JavaCondition
 65    implements
 66    Serializable,
 67    Condition
 68    {
 69    // ------------------------------------------------------------
 70    // Instance members
 71    // ------------------------------------------------------------
 72   
 73    private final String originalExpression;
 74   
 75    private final Rule rule;
 76   
 77    private final String expression;
 78   
 79    private final Declaration[] requiredDeclarations;
 80   
 81    private final String className;
 82   
 83    private transient Script script;
 84   
 85    // ------------------------------------------------------------
 86    // Constructors
 87    // ------------------------------------------------------------
 88   
 89    /**
 90    * Construct.
 91    *
 92    * @param expression
 93    * The expression.
 94    * @param rule
 95    * The rule.
 96    * @throws ConfigurationException
 97    * If an error occurs while attempting to perform configuration.
 98    */
 99  126 protected JavaCondition(Rule rule,
 100    int id,
 101    String expression) throws Exception
 102    {
 103  126 this.originalExpression = expression;
 104  126 this.expression = "return (" + expression + ");";
 105  126 this.rule = rule;
 106   
 107  126 JavaExprAnalyzer analyzer = new JavaExprAnalyzer( );
 108  126 List requiredDecls = analyzer.analyze( expression,
 109    rule.getParameterDeclarations( ) );
 110   
 111  126 this.requiredDeclarations = (Declaration[]) requiredDecls.toArray( new Declaration[requiredDecls.size( )] );
 112   
 113  126 this.className = "Condition_" + id;
 114  126 this.script = compile( );
 115    }
 116   
 117    // ------------------------------------------------------------
 118    // Instance methods
 119    // ------------------------------------------------------------
 120   
 121    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 122    // org.drools.spi.Condition
 123    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 124   
 125    /**
 126    * Determine if the supplied <code>Tuple</code> is allowed by this
 127    * condition.
 128    *
 129    * @param tuple
 130    * The <code>Tuple</code> to test.
 131    *
 132    * @return <code>true</code> if the <code>Tuple</code> passes this
 133    * condition, else <code>false</code>.
 134    *
 135    * @throws ConditionException
 136    * if an error occurs during filtering.
 137    */
 138  39253 public boolean isAllowed(Tuple tuple) throws ConditionException
 139    {
 140  39253 try
 141    {
 142  39253 if ( this.script == null )
 143    {
 144  0 this.script = compile( );
 145    }
 146  39253 return this.script.invoke( tuple,
 147    requiredDeclarations,
 148    new DefaultKnowledgeHelper( rule,
 149    tuple ),
 150    tuple.getWorkingMemory( ).getApplicationDataMap( ) );
 151    }
 152    catch ( Scanner.LocatedException e )
 153    {
 154  0 throw new ConditionException( e,
 155    this.rule,
 156    this.originalExpression );
 157    }
 158    catch ( CompilationException e )
 159    {
 160  0 throw new ConditionException( e.getMessage( ),
 161    e.getRule( ),
 162    e.getText( ) );
 163    }
 164    catch ( Exception e )
 165    {
 166  1 throw new ConditionException( e,
 167    this.rule,
 168    this.originalExpression );
 169    }
 170    }
 171   
 172    /**
 173    * Retrieve the <code>Declaration</code> s required for evaluating the
 174    * expression.
 175    *
 176    * @return The required declarations.
 177    */
 178  319 public Declaration[] getRequiredTupleMembers()
 179    {
 180  319 return this.requiredDeclarations;
 181    }
 182   
 183  126 private Script compile() throws Exception
 184    {
 185  126 return (Script) JavaCompiler.compile( this.rule,
 186    this.className,
 187    Script.class,
 188    this.expression,
 189    this.originalExpression,
 190    this.requiredDeclarations );
 191    }
 192   
 193    // ------------------------------------------------------------
 194   
 195  3818 public int hashCode()
 196    {
 197  3818 return this.originalExpression.hashCode( );
 198    }
 199   
 200  16 public boolean equals(Object object)
 201    {
 202  16 if ( this == object )
 203    {
 204  0 return true;
 205    }
 206   
 207  16 if ( object == null || getClass( ) != object.getClass( ) )
 208    {
 209  0 return false;
 210    }
 211   
 212  16 return this.originalExpression.equals( ((JavaCondition) object).originalExpression );
 213    }
 214   
 215  0 public String toString()
 216    {
 217  0 return "[Condition: " + this.originalExpression + "]";
 218    }
 219   
 220    public static interface Script
 221    {
 222    public boolean invoke(Tuple tuple,
 223    Declaration[] decls,
 224    KnowledgeHelper drools,
 225    Map applicationData) throws Exception;
 226    }
 227    }