Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 220   Methods: 11
NCLOC: 91   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
GroovyCondition.java 25% 64% 63.6% 56.8%
coverage coverage
 1    package org.drools.semantics.groovy;
 2   
 3    /*
 4    * $Id: GroovyCondition.java,v 1.5 2005/02/04 02:13:38 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 groovy.lang.Binding;
 45   
 46    import java.util.Iterator;
 47    import java.util.NoSuchElementException;
 48   
 49    import org.drools.rule.Declaration;
 50    import org.drools.rule.Rule;
 51    import org.drools.spi.Condition;
 52    import org.drools.spi.ConditionException;
 53    import org.drools.spi.Tuple;
 54   
 55    /**
 56    * Groovy expression semantics <code>Condition</code>.
 57    *
 58    * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a>
 59    * @author <a href="mailto:james@coredevelopers.net">James Strachan </a>
 60    */
 61    public class GroovyCondition extends GroovyInterp implements Condition
 62    {
 63    // ------------------------------------------------------------
 64    // Instance members
 65    // ------------------------------------------------------------
 66   
 67    /** Required declarations. */
 68    private Declaration[] requiredDeclarations;
 69   
 70    // ------------------------------------------------------------
 71    // Constructors
 72    // ------------------------------------------------------------
 73   
 74    /**
 75    * Construct.
 76    */
 77  21 public GroovyCondition( String text,
 78    Rule rule ) throws Exception
 79    {
 80  21 super( text,
 81    rule );
 82   
 83  21 GroovyExprAnalyzer analyzer = new GroovyExprAnalyzer( );
 84   
 85  21 this.requiredDeclarations = analyzer.analyze( getText( ),
 86    rule.getParameterDeclarations( ) );
 87    }
 88   
 89    // ------------------------------------------------------------
 90    // Instance methods
 91    // ------------------------------------------------------------
 92   
 93    /**
 94    * Evaluate.
 95    *
 96    * @param tuple Tuple containing variable bindings.
 97    * @return The result of evaluation.
 98    */
 99  21 public Object evaluate( Tuple tuple )
 100    {
 101  21 Binding dict = setUpDictionary( tuple, declarationIterator( ) );
 102   
 103  21 return evaluate( dict );
 104    }
 105   
 106    /**
 107    * Evaluate.
 108    *
 109    * @param locals The evaluation dictionary.
 110    * @return The result of evaluation.
 111    */
 112  21 protected Object evaluate( Binding locals )
 113    {
 114    //ScriptContext globals = new ScriptContext();
 115  21 getCode( ).setBinding( locals );
 116  21 return getCode( ).run( );
 117    }
 118   
 119    /**
 120    * Retrieve the array of <code>Declaration</code> s required by this condition to perform its duties.
 121    *
 122    * @return The array of <code>Declarations</code> expected on incoming <code>Tuples</code>.
 123    */
 124  0 public Declaration[] getRequiredTupleMembers()
 125    {
 126  0 return this.requiredDeclarations;
 127    }
 128   
 129    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 130    // org.drools.spi.Condition
 131    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 132   
 133    /**
 134    * Determine if the supplied <code>Tuple</code> is allowed by this
 135    * condition.
 136    *
 137    * @param tuple The <code>Tuple</code> to test.
 138    *
 139    * @return <code>true</code> if the <code>Tuple</code> passes this
 140    * condition, else <code>false</code>.
 141    *
 142    * @throws ConditionException if an error occurs during filtering.
 143    */
 144  21 public boolean isAllowed( Tuple tuple ) throws ConditionException
 145    {
 146  21 try
 147    {
 148  21 Object answer = evaluate( tuple );
 149   
 150  20 if ( !( answer instanceof Boolean ) )
 151    {
 152  0 throw new NonBooleanExprException( getText( ) );
 153    }
 154   
 155  20 return ( ( Boolean ) answer ).booleanValue( );
 156    }
 157    catch ( RuntimeException e )
 158    {
 159  1 throw new ConditionException( e,
 160    getRule( ),
 161    getText( ) );
 162    }
 163    }
 164   
 165    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 166   
 167  0 public int hashCode()
 168    {
 169  0 return this.getText( ).hashCode( );
 170    }
 171   
 172  0 public boolean equals( Object object )
 173    {
 174  0 if ( this == object )
 175    {
 176  0 return true;
 177    }
 178   
 179  0 if ( object == null || getClass( ) != object.getClass( ) )
 180    {
 181  0 return false;
 182    }
 183   
 184  0 return this.getText().equals( ( ( GroovyInterp ) object ).getText( ) );
 185    }
 186   
 187    /**
 188    * GroovyInterp needs a declaration iterator.
 189    * BlockConsequence uses the Iterator from Set.
 190    * So we emulate Iterator here so GroovyInterp can be used for both.
 191    * @return
 192    */
 193  21 public Iterator declarationIterator( )
 194    {
 195  21 return new Iterator()
 196    {
 197    private int index=0;
 198   
 199  0 public void remove()
 200    {
 201    //null;
 202    }
 203   
 204  59 public boolean hasNext()
 205    {
 206  59 return (index < requiredDeclarations.length);
 207    }
 208   
 209  19 public Object next()
 210    {
 211  19 if ( !hasNext( ) )
 212    {
 213  0 throw new NoSuchElementException( );
 214    }
 215  19 return requiredDeclarations[this.index++];
 216    }
 217   
 218    };
 219    }
 220    }