Coverage Report - org.boretti.drools.integration.drools4.implementation.DroolsInterfaceImplementation
 
Classes in this File Line Coverage Branch Coverage Complexity
DroolsInterfaceImplementation
73 %
48/65
29 %
30/102
9.111
 
 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  80
         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  56
                 return ruleBase;
 50  
         }
 51  
         
 52  80
         protected DroolsInterfaceImplementation(Class<T> clazz,RuleBase ruleBase) {
 53  80
                 this.clazz=clazz;
 54  80
                 this.ruleBase=ruleBase;
 55  80
         }
 56  
 
 57  
         public Class<T> getClazz() {
 58  0
                 return clazz;
 59  
         }
 60  
 
 61  
         @Override
 62  
         public StatefulSession getCurrentSession() {
 63  72
                 return session;
 64  
         }
 65  
         
 66  
         void setCurrentSession(StatefulSession session) {
 67  20
                 this.session=session;
 68  20
         }
 69  
         
 70  
         Iterator<?> runStatelessSession(List<Object> fact) {
 71  48
                 StatelessSession dss = getRuleBase().newStatelessSession();
 72  48
                 new DroolsWorkingMemoryLogger(dss);
 73  48
                 return dss.executeWithResults(fact).iterateObjects();
 74  
         }
 75  
         
 76  
         Iterator<?> runStatefulSession(List<Object> fact,boolean requiredExisting) {
 77  92
                 if (requiredExisting && session==null)  {
 78  12
                         String msg = "No session found when required";
 79  12
                         logger.error(msg);
 80  12
                         throw new IllegalArgumentException(msg);
 81  
                 }
 82  80
                 if (session==null) {
 83  20
                         session=ruleBase.newStatefulSession();
 84  20
                         new DroolsWorkingMemoryLogger(session);
 85  
                 }
 86  80
                 for(Object o:fact) session.insert(o);
 87  80
                 session.fireAllRules();
 88  80
                 return session.iterateObjects();
 89  
         }
 90  
         
 91  
         Object getResult(Class<?> returnType,Iterator<?> result) {
 92  128
                 if (returnType.equals(Void.TYPE)) return null;
 93  120
                 if (logger.isDebugEnabled()) logger.debug("Type is "+returnType);
 94  120
                 if (result!=null) {
 95  120
                         if (returnType.isArray()) {
 96  8
                                 Class<?> real = returnType.getComponentType();
 97  8
                                 if (logger.isDebugEnabled()) logger.debug("Type is array of "+real);
 98  8
                                 List<Object> lst = new ArrayList<Object>();
 99  8
                                 Iterator<?> i = result;
 100  32
                                 while(i.hasNext()) {
 101  24
                                         Object o = i.next();
 102  24
                                         if (logger.isDebugEnabled()) logger.debug("In evaluation object is "+o);
 103  24
                                         if (isTypeCompatible(real,o.getClass())) lst.add(o);
 104  24
                                 }
 105  8
                                 return lst.toArray((Object[])Array.newInstance(real,lst.size()));
 106  
                         } else {
 107  112
                                 Iterator<?> i = result;
 108  150
                                 while(i.hasNext()) {
 109  134
                                         Object o = i.next();
 110  134
                                         if (logger.isDebugEnabled()) logger.debug("In evaluation object is "+o);
 111  134
                                         if (isTypeCompatible(returnType,o.getClass())) return o;
 112  38
                                 }
 113  
                         }
 114  
                 }
 115  16
                 return null;
 116  
         }
 117  
         
 118  
         private boolean isTypeCompatible(Class<?> type,Class<?> src) {
 119  158
                 if (logger.isDebugEnabled()) logger.debug("type is "+type+" ; src is "+src);
 120  158
                 if (type.equals(src)) return true;
 121  46
                 if (type.isAssignableFrom(src)) return true;
 122  46
                 if (type.isPrimitive()) {
 123  0
                         if (type.equals(Boolean.TYPE) && src.equals(Boolean.class)) return true;
 124  0
                         if (src.equals(Boolean.TYPE) && type.equals(Boolean.class)) return true;
 125  0
                         if (type.equals(Integer.TYPE) && src.equals(Integer.class)) return true;
 126  0
                         if (src.equals(Integer.TYPE) && type.equals(Integer.class)) return true;
 127  0
                         if (type.equals(Long.TYPE) && src.equals(Long.class)) return true;
 128  0
                         if (src.equals(Long.TYPE) && type.equals(Long.class)) return true;
 129  0
                         if (type.equals(Short.TYPE) && src.equals(Short.class)) return true;
 130  0
                         if (src.equals(Short.TYPE) && type.equals(Short.class)) return true;
 131  0
                         if (type.equals(Byte.TYPE) && src.equals(Byte.class)) return true;
 132  0
                         if (src.equals(Byte.TYPE) && type.equals(Byte.class)) return true;
 133  0
                         if (type.equals(Float.TYPE) && src.equals(Float.class)) return true;
 134  0
                         if (src.equals(Float.TYPE) && type.equals(Float.class)) return true;
 135  0
                         if (type.equals(Double.TYPE) && src.equals(Double.class)) return true;
 136  0
                         if (src.equals(Double.TYPE) && type.equals(Double.class)) return true;
 137  0
                         if (type.equals(Character.TYPE) && src.equals(Character.class)) return true;
 138  0
                         if (src.equals(Character.TYPE) && type.equals(Character.class)) return true;
 139  
                 }
 140  46
                 return false;
 141  
         }
 142  
 
 143  
 }