[jboss-svn-commits] JBL Code SVN: r14810 - in labs/jbossrules/trunk/drools-decisiontables/src/main/resources: python-dt and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Sep 2 20:25:43 EDT 2007


Author: michael.neale at jboss.com
Date: 2007-09-02 20:25:43 -0400 (Sun, 02 Sep 2007)
New Revision: 14810

Added:
   labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/
   labs/jbossrules/trunk/drools-decisiontables/src/main/resources/python-dt/PyDT.py
Removed:
   labs/jbossrules/trunk/drools-decisiontables/src/main/resources/PyDT.py
Log:
refactored to be fully functional

Deleted: labs/jbossrules/trunk/drools-decisiontables/src/main/resources/PyDT.py
===================================================================
--- labs/jbossrules/trunk/drools-decisiontables/src/main/resources/PyDT.py	2007-09-02 17:15:43 UTC (rev 14809)
+++ labs/jbossrules/trunk/drools-decisiontables/src/main/resources/PyDT.py	2007-09-03 00:25:43 UTC (rev 14810)
@@ -1,92 +0,0 @@
-#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
-
-
-fact = { "Age" : 42, "Risk" : "'HIGH'", "PolicyType" : "'COMPREHENSIVE'" }
-
-table = {
-    "condition_headers" : [ ["A" , "Age"], ["B", "Risk =="], ["C", "PolicyType =="]],
-    "action_headers" : [ ["F","Premium"], ["G","Log"]],
-    
-
-    "data" : [ 
-        {"row" : 2, "A" : "> 2", "B" : "'HIGH'", "C": "'COMPREHENSIVE'",  "F" : "245"},
-        {"row" : 3, "A" : "< 25 ", "B" : "'LOW'", "F" : "390"}
-        ]
-
-}
-
-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'])
-
-    
-
-"""
-for row in table['data'] :
-    #go through all the conditions, evaluating
-    row_pass = True
-    for condition in headers :
-        col_index = condition[0]
-        cell_value = row[col_index]
-         
-        predicate = str(condition[1]) + str(cell_value)
-        
-        if not eval(predicate) :
-            #then failure due to negation
-            row_pass = False
-            break
-        #if they all pass
-        #then iterate through and apply the action (unless we finish on first match)
-        #thats it !
-    if row_pass :
-        for action in table['action_headers'] :
-            
-            col_label = action[0]
-            if (row.has_key(col_label)) :
-                fact[action[1]] = row[col_label]
-    
-
-
-
-print str(fact)
-"""
-
-
-#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'])
-
-print "And the result is: " + str(fact)

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-03 00:25:43 UTC (rev 14810)
@@ -0,0 +1,64 @@
+#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
+
+
+test_fact = { "Age" : 42, "Risk" : "'HIGH'", "PolicyType" : "'COMPREHENSIVE'" }
+
+test_table = {
+    "condition_headers" : [ ["A" , "Age"], ["B", "Risk =="], ["C", "PolicyType =="]],
+    "action_headers" : [ ["F","Premium"], ["G","Log"]],
+
+
+    "data" : [
+        {"row" : 2, "A" : "> 2", "B" : "'HIGH'", "C": "'COMPREHENSIVE'",  "F" : "245"},
+        {"row" : 3, "A" : "< 25 ", "B" : "'LOW'", "F" : "390"}
+        ]
+
+}
+
+#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'])
+
+#and now some crude test code
+process_dt(test_fact, test_table)
+print "RESULT: " + str(test_fact)
+if not test_fact.has_key("Premium") :
+	print("ERROR: no premium was calculated")
+premium = test_fact["Premium"]
+if premium == '245' :
+	print("PASSED")
+else :
+	print("FAILED: Premium was " + test_fact["Premium"])
+


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