[jboss-svn-commits] JBL Code SVN: r15322 - labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Sep 24 03:14:27 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-09-24 03:14:27 -0400 (Mon, 24 Sep 2007)
New Revision: 15322

Added:
   labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/pydt.py
Log:
refactored

Added: labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/pydt.py
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/pydt.py	                        (rev 0)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/pydt.py	2007-09-24 07:14:27 UTC (rev 15322)
@@ -0,0 +1,82 @@
+#this is PyDT - Python Decision Tables
+# (c) 2007 Michael Neale (michael at michaelneale.net)
+# Use entirely at your own risk !
+# Licenced under LGPL unless stated otherwise
+
+
+
+#this is the actual "engine" if you can call it that.
+def process_dt(fact, table) :
+	def make_header(hdr) :
+	    splut = hdr[1].split(' ')
+	    if len(splut) > 1 :
+	    #if hdr[1].contains(' ') :
+	        #itms = hdr[1].split(' ')
+	        return [hdr[0], fact[splut[0]] + ' ' + splut[1]]
+	    else :
+	        return [hdr[0], fact[hdr[1]]]
+	#calc the headers
+	headers = map(make_header, table['condition_headers'])
+	#lets try a map based approach
+	def eval_table(row) :
+	    #go through all the conditions, evaluating
+	    def check_condition(condition) :
+	    #for condition in headers :
+	        col_index = condition[0]
+	        if not row.has_key(col_index) :
+	            return False
+	        cell_value = row[col_index]
+	        predicate = str(condition[1]) + str(cell_value)
+	        return not eval(predicate)
+	    size = len(filter(check_condition,headers))
+	    if size == 0 :
+	        #for action in table['action_headers'] :
+	        def apply_actions(action) :
+	            col_label = action[0]
+	            if (row.has_key(col_label)) :
+	                fact[action[1]] = row[col_label]
+	        map(apply_actions, table['action_headers'])
+	map(eval_table, table['data'])
+
+
+
+
+# Load a XLS into a decision table structure for processing
+def load_xls(file_name) :
+	import xlrd
+	book = xlrd.open_workbook(file_name)
+	sh = book.sheet_by_index(0)	
+        condition_headers, action_headers, data = [],[],[]
+	for rx in range(sh.nrows):
+		if rx == 0 :		
+			divider = 0
+			for cx in range(sh.ncols):
+				cv = sh.cell_value(rowx=rx, colx=cx)				
+				if cv == "" : 
+					continue
+				if cv == "*" or cv == 'actions:' :
+					divider = cx
+				else:
+					if divider == 0 : #we are in conditions
+						condition_headers.append([cx, cv])
+					else: #we are in actions
+						action_headers.append([cx, cv])
+		else:	
+			data_row = {}
+			#print condition_headers
+			for cx in range(sh.ncols):
+				cv = sh.cell_value(rowx=rx, colx=cx)
+				if cv != "":
+					data_row[cx] = cv
+			if len(data_row) > 0 :
+				data.append(data_row)
+	return {
+		"condition_headers" : condition_headers,
+		"action_headers" : action_headers,
+		"data" : data
+		}
+
+
+
+
+


Property changes on: labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/pydt.py
___________________________________________________________________
Name: svn:eol-style
   + native




More information about the jboss-svn-commits mailing list