Clover coverage report - Drools - 2.0-rc2
Coverage timestamp: Wed May 11 2005 07:12:26 BST
file stats: LOC: 194   Methods: 5
NCLOC: 92   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JavaFunctions.java 41.7% 66.7% 40% 56.8%
coverage coverage
 1    package org.drools.semantics.java;
 2   
 3    /*
 4    * $Id: JavaFunctions.java,v 1.4.2.7 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    import java.io.IOException;
 44    import java.util.HashMap;
 45    import java.util.HashSet;
 46    import java.util.Iterator;
 47    import java.util.Set;
 48   
 49    import org.codehaus.janino.ByteArrayClassLoader;
 50    import org.codehaus.janino.Scanner;
 51    import org.codehaus.janino.Java.CompileException;
 52    import org.codehaus.janino.Parser.ParseException;
 53    import org.codehaus.janino.Scanner.ScanException;
 54    import org.drools.rule.RuleSet;
 55    import org.drools.spi.Functions;
 56    import org.drools.spi.ImportEntry;
 57    import org.drools.spi.Importer;
 58    import org.drools.spi.RuleBaseContext;
 59   
 60    /**
 61    * Python block semantics <code>Consequence</code>.
 62    *
 63    * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a>
 64    *
 65    * @version $Id: JavaFunctions.java,v 1.4.2.7 2005/05/08 00:57:36 mproctor Exp $
 66    */
 67    public class JavaFunctions
 68    implements
 69    Functions
 70    {
 71   
 72    private String text;
 73   
 74    private transient Class functionsClass;
 75   
 76    private RuleSet ruleSet;
 77   
 78    private String className;
 79   
 80    // private
 81   
 82    /** The line separator system property ("\n" on UNIX). */
 83    private static final String LINE_SEPARATOR = System.getProperty( "line.separator" );
 84    // ------------------------------------------------------------
 85    // Constructors
 86    // ------------------------------------------------------------
 87   
 88    /**
 89    * Construct.
 90    *
 91    * @param classLoader
 92    *
 93    * @param text
 94    * The block text.
 95    * @throws ClassNotFoundException
 96    * @throws IOException
 97    * @throws CompilationException
 98    * @throws IOException
 99    * @throws ScanException
 100    * @throws ParseException
 101    * @throws CompileException
 102    */
 103  1 public JavaFunctions(RuleSet ruleSet,
 104    int id,
 105    String text) throws CompilationException, IOException, ClassNotFoundException
 106    {
 107  1 this.text = text;
 108  1 this.ruleSet = ruleSet;
 109   
 110  1 this.className = "Function_" + id;
 111   
 112  1 compile();
 113    }
 114   
 115  1 private void compile() throws IOException, CompilationException, ClassNotFoundException
 116    {
 117  1 RuleBaseContext ruleBaseContext = ruleSet.getRuleBaseContext( );
 118  1 ClassLoader classLoader = (ClassLoader) ruleBaseContext.get( "java-classLoader" );
 119   
 120  1 if ( classLoader == null )
 121    {
 122  1 ClassLoader cl = (ClassLoader) ruleBaseContext.get( "smf-classLoader" );
 123   
 124  1 if ( cl == null )
 125    {
 126  1 cl = Thread.currentThread( ).getContextClassLoader( );
 127  1 ruleBaseContext.put( "smf-classLoader",
 128    cl );
 129    }
 130   
 131  1 if ( cl == null )
 132    {
 133  0 cl = JavaCompiler.class.getClassLoader( );
 134  0 ruleBaseContext.put( "smf-classLoader",
 135    cl );
 136    }
 137   
 138  1 classLoader = new ByteArrayClassLoader( new HashMap( ),
 139    cl );
 140    }
 141   
 142   
 143  1 Set imports = new HashSet();
 144  1 Importer importer = ruleSet.getImporter();
 145   
 146  1 try
 147    {
 148  1 ImporterClassBodyEvaluator classBody = new ImporterClassBodyEvaluator(importer, this.className, new Scanner(null, new java.io.StringReader(this.text)), classLoader);
 149  0 this.functionsClass = classBody.evaluate();
 150    }
 151    catch ( Scanner.LocatedException e )
 152    {
 153  1 throw new CompilationException( ruleSet,
 154    null,
 155    this.text,
 156  1 e.getLocation( ) != null ? e.getLocation( ).getLineNumber( ) : -1,
 157  1 e.getLocation( ) != null ? e.getLocation( ).getColumnNumber( ) : -1,
 158    e.getMessage( ) );
 159    }
 160   
 161  0 ruleBaseContext.put( "java-classLoader",
 162    this.functionsClass.getClassLoader() );
 163    }
 164   
 165    /*
 166    * (non-Javadoc)
 167    *
 168    * @see org.drools.spi.Functions#getText()
 169    */
 170  0 public String getText()
 171    {
 172  0 return this.text;
 173    }
 174   
 175  0 public Class getFunctionsClass() throws CompilationException, IOException, ClassNotFoundException
 176    {
 177  0 if (functionsClass == null)
 178    {
 179  0 compile();
 180    }
 181  0 return functionsClass;
 182    }
 183   
 184    /*
 185    * (non-Javadoc)
 186    *
 187    * @see org.drools.spi.Functions#getSemantic()
 188    */
 189  0 public String getSemantic()
 190    {
 191  0 return "java";
 192    }
 193   
 194    }