Clover coverage report - Services for Dentaku - 0.6
Coverage timestamp: Wed Jun 30 2004 19:54:58 EDT
file stats: LOC: 108   Methods: 6
NCLOC: 66   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
DefaultDroolsNativeRulesProcessor.java 33.3% 53.3% 33.3% 47.6%
coverage coverage
 1   
 /*
 2   
  * DefaultDroolsNativeRulesProcessor.java
 3   
  * Copyright 2002-2004 Bill2, Inc.
 4   
  *
 5   
  * Licensed under the Apache License, Version 2.0 (the "License");
 6   
  * you may not use this file except in compliance with the License.
 7   
  * You may obtain a copy of the License at
 8   
  *
 9   
  *     http://www.apache.org/licenses/LICENSE-2.0
 10   
  *
 11   
  * Unless required by applicable law or agreed to in writing, software
 12   
  * distributed under the License is distributed on an "AS IS" BASIS,
 13   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14   
  * See the License for the specific language governing permissions and
 15   
  * limitations under the License.
 16   
  */
 17   
 package org.dentaku.services.rules;
 18   
 
 19   
 import org.codehaus.plexus.logging.LogEnabled;
 20   
 import org.codehaus.plexus.logging.Logger;
 21   
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 22   
 import org.codehaus.plexus.util.DirectoryScanner;
 23   
 import org.drools.RuleBase;
 24   
 import org.drools.WorkingMemory;
 25   
 import org.drools.io.RuleBaseBuilder;
 26   
 
 27   
 import java.io.InputStream;
 28   
 import java.util.HashMap;
 29   
 import java.util.List;
 30   
 
 31   
 public class DefaultDroolsNativeRulesProcessor
 32   
         implements DroolsNativeRulesProcessor, Initializable, LogEnabled {
 33   
     private static Logger log;
 34   
     /* The store that keeps a reference to all ruleBases */
 35   
     private HashMap ruleBaseStore;
 36   
     String ruleFilesDirectory;
 37   
     String ruleFilesIncludes;
 38   
 
 39  3
     public void enableLogging(Logger logger) {
 40  3
         log = logger;
 41   
     }
 42   
     // ----------------------------------------------------------------------
 43   
     // Lifecylce Management
 44   
     // ----------------------------------------------------------------------
 45   
 
 46  3
     public void initialize() throws Exception {
 47  3
         String identifier = null;
 48   
 
 49  3
         DirectoryScanner scanner = new DirectoryScanner();
 50  3
         scanner.setBasedir( ruleFilesDirectory );
 51  3
         scanner.setIncludes( new String[]{ ruleFilesIncludes } );
 52  3
         scanner.scan();
 53   
 
 54  3
         String[] fileNames = scanner.getIncludedFiles();
 55  3
         for (int i = 0; i < fileNames.length; i++) {
 56  3
             String fileName = fileNames[i];
 57   
             // read rules and register with administrator
 58   
             // save way, expect filename including .drl extension,
 59   
             // but in case not, then add it
 60  3
             if (fileName.indexOf(".drl") > 0) {
 61  3
                 identifier = fileName.substring(0, fileName.length() - 4);
 62   
             } else {
 63  0
                 identifier = fileName;
 64  0
                 fileName = fileName + ".drl";
 65   
             }
 66  3
             identifier = identifier.substring(identifier.lastIndexOf("/")+1);
 67  3
             log.debug("Found rulebase filename " + fileName + ", identified as " + identifier);
 68   
 
 69  3
             InputStream resourceAsStream = this.getClass().getResourceAsStream(fileName);
 70   
 
 71  3
             try {
 72  3
                 RuleBase ruleBase = RuleBaseBuilder.buildFromInputStream(resourceAsStream);
 73  0
                 setRuleBase(identifier, ruleBase);
 74   
             } catch (Exception e) {
 75  0
                 log.error("RuleBase " + identifier + " could not be loaded", e);
 76  0
                 setRuleBase(identifier, null);
 77   
             }
 78   
         }
 79   
     }
 80   
 
 81   
     /*
 82   
      * Makes a working memory available from this rulebase
 83   
      */
 84  0
     public WorkingMemory getWorkingMemory(String ruleBaseIdentifier)
 85   
             throws Exception {
 86  0
         log.debug("Create working memory for rulebase " + ruleBaseIdentifier);
 87  0
         RuleBase ruleBase = getRuleBase(ruleBaseIdentifier);
 88  0
         WorkingMemory workingMemory = ruleBase.newWorkingMemory();
 89  0
         return workingMemory;
 90   
     }
 91   
 
 92   
 
 93  0
     public RuleBase getRuleBase(String identifier) {
 94  0
         return (RuleBase) getRuleBaseStore().get(identifier);
 95   
     }
 96   
 
 97  0
     public void setRuleBase(String identifier, RuleBase ruleBase) {
 98  0
         getRuleBaseStore().put(identifier, ruleBase);
 99   
     }
 100   
 
 101  0
     public HashMap getRuleBaseStore() {
 102  0
         if (this.ruleBaseStore == null) {
 103  0
             this.ruleBaseStore = new HashMap();
 104   
         }
 105  0
         return this.ruleBaseStore;
 106   
     }
 107   
 
 108   
 }