Coverage Report - org.boretti.drools.integration.drools4.implementation.DroolsInterfaceImplementation
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsInterfaceImplementation
97 %
39/40
95 %
21/22
2.875
 
 1  
 /*
 2  
     Drools4 Integration Helper
 3  
     Copyright (C) 2009  Mathieu Boretti mathieu.boretti@gmail.com
 4  
 
 5  
     This program is free software: you can redistribute it and/or modify
 6  
     it under the terms of the GNU General Public License as published by
 7  
     the Free Software Foundation, either version 3 of the License, or
 8  
     (at your option) any later version.
 9  
 
 10  
     This program is distributed in the hope that it will be useful,
 11  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
     GNU General Public License for more details.
 14  
 
 15  
     You should have received a copy of the GNU General Public License
 16  
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17  
 
 18  
  */
 19  
 package org.boretti.drools.integration.drools4.implementation;
 20  
 
 21  
 import java.lang.reflect.Array;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Iterator;
 24  
 import java.util.List;
 25  
 
 26  
 import org.apache.log4j.Logger;
 27  
 import org.boretti.drools.integration.drools4.DroolsInterface;
 28  
 import org.drools.RuleBase;
 29  
 import org.drools.StatefulSession;
 30  
 import org.drools.StatelessSession;
 31  
 
 32  
 /**
 33  
  * 
 34  
  * @author mbo
 35  
  *
 36  
  */
 37  
 abstract class DroolsInterfaceImplementation<T> implements DroolsInterface{
 38  
         
 39  19
         private final Logger logger = Logger.getLogger(this.getClass());
 40  
         
 41  
         private Class<T> clazz; 
 42  
                 
 43  
         private RuleBase ruleBase;
 44  
         
 45  
         private StatefulSession session;
 46  
 
 47  
         @Override
 48  
         public RuleBase getRuleBase() {
 49  13
                 return ruleBase;
 50  
         }
 51  
         
 52  19
         protected DroolsInterfaceImplementation(Class<T> clazz,RuleBase ruleBase) {
 53  19
                 this.clazz=clazz;
 54  19
                 this.ruleBase=ruleBase;
 55  19
         }
 56  
 
 57  
         public Class<T> getClazz() {
 58  0
                 return clazz;
 59  
         }
 60  
 
 61  
         @Override
 62  
         public StatefulSession getCurrentSession() {
 63  18
                 return session;
 64  
         }
 65  
         
 66  
         void setCurrentSession(StatefulSession session) {
 67  5
                 this.session=session;
 68  5
         }
 69  
         
 70  
         Iterator<?> runStatelessSession(List<Object> fact) {
 71  12
                 StatelessSession dss = getRuleBase().newStatelessSession();
 72  12
                 new DroolsWorkingMemoryLogger(dss);
 73  12
                 return dss.executeWithResults(fact).iterateObjects();
 74  
         }
 75  
         
 76  
         Iterator<?> runStatefulSession(List<Object> fact,boolean requiredExisting) {
 77  23
                 if (requiredExisting && session==null)  {
 78  3
                         String msg = "No session found when required";
 79  3
                         logger.error(msg);
 80  3
                         throw new IllegalArgumentException(msg);
 81  
                 }
 82  20
                 if (session==null) {
 83  5
                         session=ruleBase.newStatefulSession();
 84  5
                         new DroolsWorkingMemoryLogger(session);
 85  
                 }
 86  20
                 for(Object o:fact) session.insert(o);
 87  20
                 session.fireAllRules();
 88  20
                 return session.iterateObjects();
 89  
         }
 90  
         
 91  
         Object getResult(Class<?> returnType,Iterator<?> result) {
 92  32
                 if (returnType.equals(Void.TYPE)) return null;                        
 93  30
                 if (result!=null) {
 94  30
                         if (returnType.isArray()) {
 95  2
                                 Class<?> real = returnType.getComponentType();
 96  2
                                 List<Object> lst = new ArrayList<Object>();
 97  2
                                 Iterator<?> i = result;
 98  8
                                 while(i.hasNext()) {
 99  6
                                         Object o = i.next();
 100  6
                                         if (real.isAssignableFrom(o.getClass())) lst.add(o);
 101  6
                                 }
 102  2
                                 return lst.toArray((Object[])Array.newInstance(real,lst.size()));
 103  
                         } else {
 104  28
                                 Iterator<?> i = result;
 105  33
                                 while(i.hasNext()) {
 106  29
                                         Object o = i.next();
 107  29
                                         if (returnType.isAssignableFrom(o.getClass())) return o;
 108  5
                                 }
 109  
                         }
 110  
                 }
 111  4
                 return null;
 112  
         }
 113  
 
 114  
 }