[jboss-svn-commits] JBL Code SVN: r18784 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/brms/server/util and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Sun Mar 9 21:45:56 EDT 2008


Author: michael.neale at jboss.com
Date: 2008-03-09 21:45:56 -0400 (Sun, 09 Mar 2008)
New Revision: 18784

Added:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTDRLPersistence.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTDRLPersistenceTest.java
Removed:
   labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTBRLPersistence.java
   labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTBRLPersistenceTest.java
Log:
JBRULES-1494 Decision table model

Deleted: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTBRLPersistence.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTBRLPersistence.java	2008-03-09 19:24:16 UTC (rev 18783)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTBRLPersistence.java	2008-03-10 01:45:56 UTC (rev 18784)
@@ -1,125 +0,0 @@
-package org.drools.brms.server.util;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.drools.brms.client.modeldriven.brl.FactPattern;
-import org.drools.brms.client.modeldriven.brl.IPattern;
-import org.drools.brms.client.modeldriven.brl.ISingleFieldConstraint;
-import org.drools.brms.client.modeldriven.brl.RuleAttribute;
-import org.drools.brms.client.modeldriven.brl.RuleModel;
-import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
-import org.drools.brms.client.modeldriven.dt.AttributeCol;
-import org.drools.brms.client.modeldriven.dt.ConditionCol;
-import org.drools.brms.client.modeldriven.dt.GuidedDecisionTable;
-
-/**
- * This takes care of converting GuidedDT object to DRL (via the RuleModel).
- * @author Michael Neale
- *
- */
-public class GuidedDTBRLPersistence {
-
-
-	public String marshal(GuidedDecisionTable dt) {
-
-		StringBuilder sb = new StringBuilder();
-
-		for (int i = 0; i < dt.data.length; i++) {
-			String[] row = dt.data[i];
-			String num = row[0];
-			String desc = row[1];
-
-			RuleModel rm = new RuleModel();
-			rm.name = getName(dt.tableName, num, desc);
-
-			doAttribs(dt.attributeCols, row, rm);
-			doConditions(dt.attributeCols.size(), dt.conditionCols, row, rm);
-			doActions(dt.attributeCols.size() + dt.conditionCols.size(), dt.actionCols, row, rm);
-		}
-
-
-		return sb.toString();
-
-	}
-
-	void doActions(int condAndAttrs, List actionCols, String[] row, RuleModel rm) {
-
-
-	}
-
-	void doConditions(int numOfAttributes, List<ConditionCol> conditionCols, String[] row, RuleModel rm) {
-
-		List<FactPattern> patterns = new ArrayList<FactPattern>();
-
-		for (int i = 0; i < conditionCols.size(); i++) {
-			ConditionCol c = (ConditionCol) conditionCols.get(i);
-			String cell = row[i + 2 + numOfAttributes];
-			if (validCell(cell)) {
-
-				//get or create the pattern it belongs too
-				FactPattern fp = find(patterns, c.boundName);
-				if (fp == null) {
-					fp = new FactPattern(c.factType);
-					fp.boundName = c.boundName;
-					patterns.add(fp);
-				}
-
-				//now add the constraint from this cell
-				switch (c.constraintValueType) {
-					case ISingleFieldConstraint.TYPE_LITERAL:
-					case ISingleFieldConstraint.TYPE_RET_VALUE:
-						SingleFieldConstraint sfc = new SingleFieldConstraint(c.factField);
-						sfc.operator = c.operator;
-						sfc.constraintValueType = c.constraintValueType;
-						sfc.value = cell;
-						fp.addConstraint(sfc);
-						break;
-					case ISingleFieldConstraint.TYPE_PREDICATE:
-						SingleFieldConstraint pred = new SingleFieldConstraint();
-						pred.constraintValueType = c.constraintValueType;
-						pred.value = cell;
-						fp.addConstraint(pred);
-						break;
-				default:
-					throw new IllegalArgumentException("Unknown constraintValueType: " + c.constraintValueType);
-				}
-			}
-		}
-		rm.lhs = patterns.toArray(new IPattern[patterns.size()]);
-	}
-
-	private FactPattern find(List<FactPattern> patterns, String boundName) {
-		for (FactPattern factPattern : patterns) {
-			if (factPattern.boundName.equals(boundName)) {
-				return factPattern;
-			}
-		}
-		return null;
-	}
-
-	void doAttribs(List attributeCols, String[] row, RuleModel rm) {
-		List<RuleAttribute> attribs = new ArrayList<RuleAttribute>();
-		for (int j = 0; j < attributeCols.size(); j++) {
-			AttributeCol at = (AttributeCol) attributeCols.get(j);
-			String cell = row[j + 2];
-			if (validCell(cell)) {
-				attribs.add(new RuleAttribute(at.attr, cell));
-			}
-		}
-		if (attribs.size() > 0) {
-			rm.attributes = attribs.toArray(new RuleAttribute[attribs.size()]);
-		}
-	}
-
-	String getName(String tableName, String num, String desc) {
-		return (validCell(desc)) ? num + "_" + desc : num + "_" + tableName;
-	}
-
-	boolean validCell(String c) {
-		return c !=null && !c.trim().equals("");
-	}
-
-}

Copied: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTDRLPersistence.java (from rev 18750, labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTBRLPersistence.java)
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTDRLPersistence.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/brms/server/util/GuidedDTDRLPersistence.java	2008-03-10 01:45:56 UTC (rev 18784)
@@ -0,0 +1,195 @@
+package org.drools.brms.server.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.drools.brms.client.modeldriven.brl.ActionFieldValue;
+import org.drools.brms.client.modeldriven.brl.ActionInsertFact;
+import org.drools.brms.client.modeldriven.brl.ActionRetractFact;
+import org.drools.brms.client.modeldriven.brl.ActionSetField;
+import org.drools.brms.client.modeldriven.brl.FactPattern;
+import org.drools.brms.client.modeldriven.brl.IAction;
+import org.drools.brms.client.modeldriven.brl.IPattern;
+import org.drools.brms.client.modeldriven.brl.ISingleFieldConstraint;
+import org.drools.brms.client.modeldriven.brl.RuleAttribute;
+import org.drools.brms.client.modeldriven.brl.RuleModel;
+import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
+import org.drools.brms.client.modeldriven.dt.ActionCol;
+import org.drools.brms.client.modeldriven.dt.ActionInsertFactCol;
+import org.drools.brms.client.modeldriven.dt.ActionRetractFactCol;
+import org.drools.brms.client.modeldriven.dt.ActionSetFieldCol;
+import org.drools.brms.client.modeldriven.dt.AttributeCol;
+import org.drools.brms.client.modeldriven.dt.ConditionCol;
+import org.drools.brms.client.modeldriven.dt.GuidedDecisionTable;
+
+/**
+ * This takes care of converting GuidedDT object to DRL (via the RuleModel).
+ * @author Michael Neale
+ *
+ */
+public class GuidedDTDRLPersistence {
+
+
+	public String marshal(GuidedDecisionTable dt) {
+
+		StringBuilder sb = new StringBuilder();
+
+		for (int i = 0; i < dt.data.length; i++) {
+			String[] row = dt.data[i];
+			String num = row[0];
+			String desc = row[1];
+
+			RuleModel rm = new RuleModel();
+			rm.name = getName(dt.tableName, num, desc);
+
+			doAttribs(dt.attributeCols, row, rm);
+			doConditions(dt.attributeCols.size(), dt.conditionCols, row, rm);
+			doActions(dt.attributeCols.size() + dt.conditionCols.size(), dt.actionCols, row, rm);
+
+			String rule = BRDRLPersistence.getInstance().marshal(rm);
+			sb.append(rule);
+			sb.append("\n");
+		}
+
+
+		return sb.toString();
+
+	}
+
+	void doActions(int condAndAttrs, List<ActionCol> actionCols, String[] row, RuleModel rm) {
+		List<LabelledAction> actions = new ArrayList<LabelledAction>();
+		for (int i = 0; i < actionCols.size(); i++) {
+			ActionCol c = actionCols.get(i);
+			String cell = row[condAndAttrs + i + 2];
+			if (validCell(cell)) {
+				if (c instanceof ActionInsertFactCol) {
+					ActionInsertFactCol ac = (ActionInsertFactCol)c;
+					LabelledAction a = find(actions, ac.boundName);
+					if (a == null) {
+						a = new LabelledAction();
+						a.boundName  = ac.boundName;
+						ActionInsertFact ins = new ActionInsertFact(ac.factType);
+						a.action = ins;
+						actions.add(a);
+					}
+					ActionInsertFact ins = (ActionInsertFact) a.action;
+					ActionFieldValue val = new ActionFieldValue(ac.factField, cell, ac.type);
+					ins.addFieldValue(val);
+				} else if (c instanceof ActionRetractFactCol) {
+					ActionRetractFactCol rf = (ActionRetractFactCol)c;
+					LabelledAction a = find(actions, rf.boundName);
+					if (a == null) {
+						a = new LabelledAction();
+						a.action = new ActionRetractFact(rf.boundName);
+						a.boundName = rf.boundName;
+						actions.add(a);
+					}
+				} else if (c instanceof ActionSetFieldCol) {
+					ActionSetFieldCol sf = (ActionSetFieldCol)c;
+					LabelledAction a = find(actions, sf.boundName);
+					if (a == null) {
+						a = new LabelledAction();
+						a.boundName = sf.boundName;
+						a.action = new ActionSetField(sf.boundName);
+						actions.add(a);
+					}
+					ActionSetField asf = (ActionSetField) a.action;
+					ActionFieldValue val = new ActionFieldValue(sf.factField, cell, sf.type);
+					asf.addFieldValue(val);
+				}
+			}
+		}
+
+		rm.rhs = new IAction[actions.size()];
+		for (int i = 0; i < rm.rhs.length; i++) {
+			rm.rhs[i] = actions.get(i).action;
+		}
+	}
+
+	private LabelledAction find(List<LabelledAction> actions, String boundName) {
+		for (LabelledAction labelledAction : actions) {
+			if (labelledAction.boundName.equals(boundName)) {
+				return labelledAction;
+			}
+		}
+		return null;
+	}
+
+	void doConditions(int numOfAttributes, List<ConditionCol> conditionCols, String[] row, RuleModel rm) {
+
+		List<FactPattern> patterns = new ArrayList<FactPattern>();
+
+		for (int i = 0; i < conditionCols.size(); i++) {
+			ConditionCol c = (ConditionCol) conditionCols.get(i);
+			String cell = row[i + 2 + numOfAttributes];
+			if (validCell(cell)) {
+
+				//get or create the pattern it belongs too
+				FactPattern fp = find(patterns, c.boundName);
+				if (fp == null) {
+					fp = new FactPattern(c.factType);
+					fp.boundName = c.boundName;
+					patterns.add(fp);
+				}
+
+				//now add the constraint from this cell
+				switch (c.constraintValueType) {
+					case ISingleFieldConstraint.TYPE_LITERAL:
+					case ISingleFieldConstraint.TYPE_RET_VALUE:
+						SingleFieldConstraint sfc = new SingleFieldConstraint(c.factField);
+						sfc.operator = c.operator;
+						sfc.constraintValueType = c.constraintValueType;
+						sfc.value = cell;
+						fp.addConstraint(sfc);
+						break;
+					case ISingleFieldConstraint.TYPE_PREDICATE:
+						SingleFieldConstraint pred = new SingleFieldConstraint();
+						pred.constraintValueType = c.constraintValueType;
+						pred.value = cell;
+						fp.addConstraint(pred);
+						break;
+				default:
+					throw new IllegalArgumentException("Unknown constraintValueType: " + c.constraintValueType);
+				}
+			}
+		}
+		rm.lhs = patterns.toArray(new IPattern[patterns.size()]);
+	}
+
+	private FactPattern find(List<FactPattern> patterns, String boundName) {
+		for (FactPattern factPattern : patterns) {
+			if (factPattern.boundName.equals(boundName)) {
+				return factPattern;
+			}
+		}
+		return null;
+	}
+
+	void doAttribs(List<AttributeCol> attributeCols, String[] row, RuleModel rm) {
+		List<RuleAttribute> attribs = new ArrayList<RuleAttribute>();
+		for (int j = 0; j < attributeCols.size(); j++) {
+			AttributeCol at = attributeCols.get(j);
+			String cell = row[j + 2];
+			if (validCell(cell)) {
+				attribs.add(new RuleAttribute(at.attr, cell));
+			}
+		}
+		if (attribs.size() > 0) {
+			rm.attributes = attribs.toArray(new RuleAttribute[attribs.size()]);
+		}
+	}
+
+	String getName(String tableName, String num, String desc) {
+		return (validCell(desc)) ? num + "_" + desc : num + "_" + tableName;
+	}
+
+	boolean validCell(String c) {
+		return c !=null && !c.trim().equals("");
+	}
+
+	private class LabelledAction {
+		String boundName;
+		IAction action;
+	}
+
+}

Deleted: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTBRLPersistenceTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTBRLPersistenceTest.java	2008-03-09 19:24:16 UTC (rev 18783)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTBRLPersistenceTest.java	2008-03-10 01:45:56 UTC (rev 18784)
@@ -1,231 +0,0 @@
-package org.drools.brms.server.util;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
-import org.drools.brms.client.modeldriven.brl.FactPattern;
-import org.drools.brms.client.modeldriven.brl.ISingleFieldConstraint;
-import org.drools.brms.client.modeldriven.brl.RuleAttribute;
-import org.drools.brms.client.modeldriven.brl.RuleModel;
-import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
-import org.drools.brms.client.modeldriven.dt.ActionInsertFactCol;
-import org.drools.brms.client.modeldriven.dt.ActionRetractFactCol;
-import org.drools.brms.client.modeldriven.dt.ActionSetFieldCol;
-import org.drools.brms.client.modeldriven.dt.AttributeCol;
-import org.drools.brms.client.modeldriven.dt.ConditionCol;
-import org.drools.brms.client.modeldriven.dt.GuidedDecisionTable;
-
-public class GuidedDTBRLPersistenceTest extends TestCase {
-
-
-	public void testOneRule() throws Exception {
-		GuidedDecisionTable dt = new GuidedDecisionTable();
-		dt.tableName = "michael";
-
-		AttributeCol attr = new AttributeCol();
-		attr.attr = "salience";
-		dt.attributeCols.add(attr);
-
-		ConditionCol con = new ConditionCol();
-		con.boundName = "f1";
-		con.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
-		con.factField = "age";
-		con.factType = "Driver";
-		con.header = "Driver f1 age";
-		con.operator = "==";
-		dt.conditionCols.add(con);
-
-		ConditionCol con2 = new ConditionCol();
-		con2.boundName = "f1";
-		con2.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
-		con2.factField = "name";
-		con2.factType = "Driver";
-		con2.header = "Driver f1 name";
-		con2.operator = "==";
-		dt.conditionCols.add(con2);
-
-		ConditionCol con3 = new ConditionCol();
-		con3.boundName = "f1";
-		con3.constraintValueType = ISingleFieldConstraint.TYPE_RET_VALUE;
-		con3.factField = "rating";
-		con3.factType = "Driver";
-		con3.header = "Driver rating";
-		con3.operator = "==";
-		dt.conditionCols.add(con3);
-
-
-		ConditionCol con4 = new ConditionCol();
-		con4.boundName = "f2";
-		con4.constraintValueType = ISingleFieldConstraint.TYPE_PREDICATE;
-		con4.factType = "Driver";
-		con4.header = "Driver 2 pimp";
-		dt.conditionCols.add(con4);
-
-
-		ActionInsertFactCol ins = new ActionInsertFactCol();
-		ins.boundName = "ins";
-		ins.type = "Cheese";
-		ins.factField = "price";
-		ins.type = SuggestionCompletionEngine.TYPE_NUMERIC;
-		dt.actionCols.add(ins);
-
-		ActionRetractFactCol ret = new ActionRetractFactCol();
-		ret.boundName = "f2";
-		dt.actionCols.add(ret);
-
-		ActionSetFieldCol set = new ActionSetFieldCol();
-		set.boundName = "f1";
-		set.factField = "goo1";
-		set.type = SuggestionCompletionEngine.TYPE_STRING;
-		dt.actionCols.add(set);
-
-		ActionSetFieldCol set2 = new ActionSetFieldCol();
-		set2.boundName = "f1";
-		set2.factField = "goo2";
-		set2.type = SuggestionCompletionEngine.TYPE_STRING;
-		dt.actionCols.add(set2);
-
-
-		dt.data = new String[][] {
-				new String[] {"1", "desc", "42", "33", "michael", "age * 0.2", "age > 7", "6.60", "true", "gooVal1", "gooVal2"},
-				new String[] {"2", "desc", "", "39", "bob", "age * 0.3", "age > 7", "6.60", "", "gooVal1", "gooVal2"}
-		};
-
-
-
-
-
-
-
-
-	}
-
-	public void testCellVal() {
-		GuidedDTBRLPersistence p = new GuidedDTBRLPersistence();
-		assertFalse(p.validCell(null));
-		assertFalse(p.validCell(""));
-		assertFalse(p.validCell("  "));
-
-	}
-
-	public void testName() {
-		GuidedDTBRLPersistence p = new GuidedDTBRLPersistence();
-		assertEquals("42_hey", p.getName("XXX", "42", "hey"));
-		assertEquals("42_XXX", p.getName("XXX", "42", ""));
-	}
-
-	public void testAttribs() {
-		GuidedDTBRLPersistence p = new GuidedDTBRLPersistence();
-		String[] row = new String[] {"1", "desc", "a", ""};
-
-		List<AttributeCol> attributeCols = new ArrayList<AttributeCol>();
-		RuleModel rm = new RuleModel();
-		RuleAttribute[] orig = rm.attributes;
-		p.doAttribs(attributeCols, row, rm);
-
-		assertSame(orig, rm.attributes);
-
-		AttributeCol col1 = new AttributeCol();
-		col1.attr = "salience";
-		AttributeCol col2 = new AttributeCol();
-		col2.attr = "agenda-group";
-		attributeCols.add(col1);
-		attributeCols.add(col2);
-
-		p.doAttribs(attributeCols, row, rm);
-
-		assertEquals(1, rm.attributes.length);
-		assertEquals("salience", rm.attributes[0].attributeName);
-		assertEquals("a", rm.attributes[0].value);
-
-		row = new String[] {"1", "desc", "a", "b"};
-		p.doAttribs(attributeCols, row, rm);
-		assertEquals(2, rm.attributes.length);
-		assertEquals("salience", rm.attributes[0].attributeName);
-		assertEquals("a", rm.attributes[0].value);
-		assertEquals("agenda-group", rm.attributes[1].attributeName);
-		assertEquals("b", rm.attributes[1].value);
-
-	}
-
-	public void testLHS() {
-		GuidedDTBRLPersistence p = new GuidedDTBRLPersistence();
-		String[] row = new String[] {"1", "desc", "a", "mike", "33 + 1", "age > 6", "stilton"};
-
-		List<ConditionCol> cols = new ArrayList<ConditionCol>();
-		ConditionCol col = new ConditionCol();
-		col.boundName = "p1";
-		col.factType = "Person";
-		col.factField = "name";
-		col.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
-		col.operator = "==";
-		cols.add(col);
-
-		ConditionCol col2 = new ConditionCol();
-		col2.boundName = "p1";
-		col2.factType = "Person";
-		col2.factField = "age";
-		col2.constraintValueType = ISingleFieldConstraint.TYPE_RET_VALUE;
-		col2.operator = "<";
-		cols.add(col2);
-
-		ConditionCol col3 = new ConditionCol();
-		col3.boundName = "p1";
-		col3.factType = "Person";
-		col3.constraintValueType = ISingleFieldConstraint.TYPE_PREDICATE;
-		cols.add(col3);
-
-		ConditionCol col4 = new ConditionCol();
-		col4.boundName = "c";
-		col4.factType = "Cheese";
-		col4.factField = "type";
-		col4.operator = "==";
-		col4.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
-		cols.add(col4);
-
-		RuleModel rm = new RuleModel();
-
-		p.doConditions(1, cols, row, rm);
-		assertEquals(2, rm.lhs.length);
-
-		assertEquals("Person", ((FactPattern)rm.lhs[0]).factType);
-		assertEquals("p1", ((FactPattern)rm.lhs[0]).boundName);
-
-		assertEquals("Cheese", ((FactPattern)rm.lhs[1]).factType);
-		assertEquals("c", ((FactPattern)rm.lhs[1]).boundName);
-
-		//examine the first pattern
-		FactPattern person = (FactPattern) rm.lhs[0];
-		assertEquals(3, person.constraintList.constraints.length);
-		SingleFieldConstraint cons = (SingleFieldConstraint) person.constraintList.constraints[0];
-		assertEquals(ISingleFieldConstraint.TYPE_LITERAL, cons.constraintValueType);
-		assertEquals("name", cons.fieldName);
-		assertEquals("==", cons.operator);
-		assertEquals("mike", cons.value);
-
-		cons = (SingleFieldConstraint) person.constraintList.constraints[1];
-		assertEquals(ISingleFieldConstraint.TYPE_RET_VALUE, cons.constraintValueType);
-		assertEquals("age", cons.fieldName);
-		assertEquals("<", cons.operator);
-		assertEquals("33 + 1", cons.value);
-
-		cons = (SingleFieldConstraint) person.constraintList.constraints[2];
-		assertEquals(ISingleFieldConstraint.TYPE_PREDICATE, cons.constraintValueType);
-		assertEquals("age > 6", cons.value);
-
-
-		//examine the second pattern
-		FactPattern cheese = (FactPattern) rm.lhs[1];
-		assertEquals(1, cheese.constraintList.constraints.length);
-		cons = (SingleFieldConstraint) cheese.constraintList.constraints[0];
-		assertEquals("type", cons.fieldName);
-		assertEquals("==", cons.operator);
-		assertEquals("stilton", cons.value);
-		assertEquals(ISingleFieldConstraint.TYPE_LITERAL, cons.constraintValueType);
-	}
-
-
-}

Copied: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTDRLPersistenceTest.java (from rev 18750, labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTBRLPersistenceTest.java)
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTDRLPersistenceTest.java	                        (rev 0)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/brms/server/util/GuidedDTDRLPersistenceTest.java	2008-03-10 01:45:56 UTC (rev 18784)
@@ -0,0 +1,313 @@
+package org.drools.brms.server.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.drools.brms.client.modeldriven.SuggestionCompletionEngine;
+import org.drools.brms.client.modeldriven.brl.ActionInsertFact;
+import org.drools.brms.client.modeldriven.brl.ActionRetractFact;
+import org.drools.brms.client.modeldriven.brl.ActionSetField;
+import org.drools.brms.client.modeldriven.brl.FactPattern;
+import org.drools.brms.client.modeldriven.brl.ISingleFieldConstraint;
+import org.drools.brms.client.modeldriven.brl.RuleAttribute;
+import org.drools.brms.client.modeldriven.brl.RuleModel;
+import org.drools.brms.client.modeldriven.brl.SingleFieldConstraint;
+import org.drools.brms.client.modeldriven.dt.ActionCol;
+import org.drools.brms.client.modeldriven.dt.ActionInsertFactCol;
+import org.drools.brms.client.modeldriven.dt.ActionRetractFactCol;
+import org.drools.brms.client.modeldriven.dt.ActionSetFieldCol;
+import org.drools.brms.client.modeldriven.dt.AttributeCol;
+import org.drools.brms.client.modeldriven.dt.ConditionCol;
+import org.drools.brms.client.modeldriven.dt.GuidedDecisionTable;
+
+public class GuidedDTDRLPersistenceTest extends TestCase {
+
+
+	public void test2Rules() throws Exception {
+		GuidedDecisionTable dt = new GuidedDecisionTable();
+		dt.tableName = "michael";
+
+		AttributeCol attr = new AttributeCol();
+		attr.attr = "salience";
+		dt.attributeCols.add(attr);
+
+		ConditionCol con = new ConditionCol();
+		con.boundName = "f1";
+		con.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
+		con.factField = "age";
+		con.factType = "Driver";
+		con.header = "Driver f1 age";
+		con.operator = "==";
+		dt.conditionCols.add(con);
+
+		ConditionCol con2 = new ConditionCol();
+		con2.boundName = "f1";
+		con2.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
+		con2.factField = "name";
+		con2.factType = "Driver";
+		con2.header = "Driver f1 name";
+		con2.operator = "==";
+		dt.conditionCols.add(con2);
+
+		ConditionCol con3 = new ConditionCol();
+		con3.boundName = "f1";
+		con3.constraintValueType = ISingleFieldConstraint.TYPE_RET_VALUE;
+		con3.factField = "rating";
+		con3.factType = "Driver";
+		con3.header = "Driver rating";
+		con3.operator = "==";
+		dt.conditionCols.add(con3);
+
+
+		ConditionCol con4 = new ConditionCol();
+		con4.boundName = "f2";
+		con4.constraintValueType = ISingleFieldConstraint.TYPE_PREDICATE;
+		con4.factType = "Driver";
+		con4.header = "Driver 2 pimp";
+		dt.conditionCols.add(con4);
+
+
+		ActionInsertFactCol ins = new ActionInsertFactCol();
+		ins.boundName = "ins";
+		ins.factType = "Cheese";
+		ins.factField = "price";
+		ins.type = SuggestionCompletionEngine.TYPE_NUMERIC;
+		dt.actionCols.add(ins);
+
+		ActionRetractFactCol ret = new ActionRetractFactCol();
+		ret.boundName = "f2";
+		dt.actionCols.add(ret);
+
+		ActionSetFieldCol set = new ActionSetFieldCol();
+		set.boundName = "f1";
+		set.factField = "goo1";
+		set.type = SuggestionCompletionEngine.TYPE_STRING;
+		dt.actionCols.add(set);
+
+		ActionSetFieldCol set2 = new ActionSetFieldCol();
+		set2.boundName = "f1";
+		set2.factField = "goo2";
+		set2.type = SuggestionCompletionEngine.TYPE_STRING;
+		dt.actionCols.add(set2);
+
+
+		dt.data = new String[][] {
+				new String[] {"1", "desc", "42", "33", "michael", "age * 0.2", "age > 7", "6.60", "true", "gooVal1", "gooVal2"},
+				new String[] {"2", "desc", "", "39", "bob", "age * 0.3", "age > 7", "6.60", "", "gooVal1", "gooVal2"}
+		};
+
+
+
+
+
+		GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
+		String drl = p.marshal(dt);
+		System.err.println(drl);
+
+
+	}
+
+	public void testCellVal() {
+		GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
+		assertFalse(p.validCell(null));
+		assertFalse(p.validCell(""));
+		assertFalse(p.validCell("  "));
+
+	}
+
+	public void testName() {
+		GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
+		assertEquals("42_hey", p.getName("XXX", "42", "hey"));
+		assertEquals("42_XXX", p.getName("XXX", "42", ""));
+	}
+
+	public void testAttribs() {
+		GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
+		String[] row = new String[] {"1", "desc", "a", ""};
+
+		List<AttributeCol> attributeCols = new ArrayList<AttributeCol>();
+		RuleModel rm = new RuleModel();
+		RuleAttribute[] orig = rm.attributes;
+		p.doAttribs(attributeCols, row, rm);
+
+		assertSame(orig, rm.attributes);
+
+		AttributeCol col1 = new AttributeCol();
+		col1.attr = "salience";
+		AttributeCol col2 = new AttributeCol();
+		col2.attr = "agenda-group";
+		attributeCols.add(col1);
+		attributeCols.add(col2);
+
+		p.doAttribs(attributeCols, row, rm);
+
+		assertEquals(1, rm.attributes.length);
+		assertEquals("salience", rm.attributes[0].attributeName);
+		assertEquals("a", rm.attributes[0].value);
+
+		row = new String[] {"1", "desc", "a", "b"};
+		p.doAttribs(attributeCols, row, rm);
+		assertEquals(2, rm.attributes.length);
+		assertEquals("salience", rm.attributes[0].attributeName);
+		assertEquals("a", rm.attributes[0].value);
+		assertEquals("agenda-group", rm.attributes[1].attributeName);
+		assertEquals("b", rm.attributes[1].value);
+
+	}
+
+	public void testLHS() {
+		GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
+		String[] row = new String[] {"1", "desc", "a", "mike", "33 + 1", "age > 6", "stilton"};
+
+		List<ConditionCol> cols = new ArrayList<ConditionCol>();
+		ConditionCol col = new ConditionCol();
+		col.boundName = "p1";
+		col.factType = "Person";
+		col.factField = "name";
+		col.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
+		col.operator = "==";
+		cols.add(col);
+
+		ConditionCol col2 = new ConditionCol();
+		col2.boundName = "p1";
+		col2.factType = "Person";
+		col2.factField = "age";
+		col2.constraintValueType = ISingleFieldConstraint.TYPE_RET_VALUE;
+		col2.operator = "<";
+		cols.add(col2);
+
+		ConditionCol col3 = new ConditionCol();
+		col3.boundName = "p1";
+		col3.factType = "Person";
+		col3.constraintValueType = ISingleFieldConstraint.TYPE_PREDICATE;
+		cols.add(col3);
+
+		ConditionCol col4 = new ConditionCol();
+		col4.boundName = "c";
+		col4.factType = "Cheese";
+		col4.factField = "type";
+		col4.operator = "==";
+		col4.constraintValueType = ISingleFieldConstraint.TYPE_LITERAL;
+		cols.add(col4);
+
+		RuleModel rm = new RuleModel();
+
+		p.doConditions(1, cols, row, rm);
+		assertEquals(2, rm.lhs.length);
+
+		assertEquals("Person", ((FactPattern)rm.lhs[0]).factType);
+		assertEquals("p1", ((FactPattern)rm.lhs[0]).boundName);
+
+		assertEquals("Cheese", ((FactPattern)rm.lhs[1]).factType);
+		assertEquals("c", ((FactPattern)rm.lhs[1]).boundName);
+
+		//examine the first pattern
+		FactPattern person = (FactPattern) rm.lhs[0];
+		assertEquals(3, person.constraintList.constraints.length);
+		SingleFieldConstraint cons = (SingleFieldConstraint) person.constraintList.constraints[0];
+		assertEquals(ISingleFieldConstraint.TYPE_LITERAL, cons.constraintValueType);
+		assertEquals("name", cons.fieldName);
+		assertEquals("==", cons.operator);
+		assertEquals("mike", cons.value);
+
+		cons = (SingleFieldConstraint) person.constraintList.constraints[1];
+		assertEquals(ISingleFieldConstraint.TYPE_RET_VALUE, cons.constraintValueType);
+		assertEquals("age", cons.fieldName);
+		assertEquals("<", cons.operator);
+		assertEquals("33 + 1", cons.value);
+
+		cons = (SingleFieldConstraint) person.constraintList.constraints[2];
+		assertEquals(ISingleFieldConstraint.TYPE_PREDICATE, cons.constraintValueType);
+		assertEquals("age > 6", cons.value);
+
+
+		//examine the second pattern
+		FactPattern cheese = (FactPattern) rm.lhs[1];
+		assertEquals(1, cheese.constraintList.constraints.length);
+		cons = (SingleFieldConstraint) cheese.constraintList.constraints[0];
+		assertEquals("type", cons.fieldName);
+		assertEquals("==", cons.operator);
+		assertEquals("stilton", cons.value);
+		assertEquals(ISingleFieldConstraint.TYPE_LITERAL, cons.constraintValueType);
+	}
+
+	public void testRHS() {
+		GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
+		String[] row = new String[] {"1", "desc", "a", "a condition", "actionsetfield1", "actionsetfield2", "retract", "actioninsertfact1", "actioninsertfact2"};
+
+		List<ActionCol> cols = new ArrayList<ActionCol>();
+		ActionSetFieldCol asf1 = new ActionSetFieldCol();
+		asf1.boundName = "a";
+		asf1.factField = "field1";
+
+		asf1.type = SuggestionCompletionEngine.TYPE_STRING;
+		cols.add(asf1);
+
+		ActionSetFieldCol asf2 = new ActionSetFieldCol();
+		asf2.boundName = "a";
+		asf2.factField = "field2";
+		asf2.type = SuggestionCompletionEngine.TYPE_NUMERIC;
+		cols.add(asf2);
+
+		ActionRetractFactCol ret = new ActionRetractFactCol();
+		ret.boundName = "ret";
+		cols.add(ret);
+
+		ActionInsertFactCol ins1 = new ActionInsertFactCol();
+		ins1.boundName = "ins";
+		ins1.factType = "Cheese";
+		ins1.factField = "price";
+		ins1.type = SuggestionCompletionEngine.TYPE_NUMERIC;
+		cols.add(ins1);
+
+		ActionInsertFactCol ins2 = new ActionInsertFactCol();
+		ins2.boundName = "ins";
+		ins2.factType = "Cheese";
+		ins2.factField = "type";
+		ins2.type = SuggestionCompletionEngine.TYPE_NUMERIC;
+		cols.add(ins2);
+
+
+		RuleModel rm = new RuleModel();
+		p.doActions(2, cols, row, rm);
+		assertEquals(3, rm.rhs.length);
+
+		//examine the set field action that is produced
+		ActionSetField a1 = (ActionSetField) rm.rhs[0];
+		assertEquals("a", a1.variable);
+		assertEquals(2, a1.fieldValues.length);
+
+		assertEquals("field1", a1.fieldValues[0].field);
+		assertEquals("actionsetfield1", a1.fieldValues[0].value);
+		assertEquals(SuggestionCompletionEngine.TYPE_STRING, a1.fieldValues[0].type);
+
+		assertEquals("field2", a1.fieldValues[1].field);
+		assertEquals("actionsetfield2", a1.fieldValues[1].value);
+		assertEquals(SuggestionCompletionEngine.TYPE_NUMERIC, a1.fieldValues[1].type);
+
+
+		//examine the retract
+		ActionRetractFact a2 = (ActionRetractFact) rm.rhs[1];
+		assertEquals("ret", a2.variableName);
+
+		//examine the insert
+		ActionInsertFact a3 = (ActionInsertFact) rm.rhs[2];
+		assertEquals("Cheese", a3.factType);
+		assertEquals(2, a3.fieldValues.length);
+
+		assertEquals("price", a3.fieldValues[0].field);
+		assertEquals("actioninsertfact1", a3.fieldValues[0].value);
+		assertEquals(SuggestionCompletionEngine.TYPE_NUMERIC, a3.fieldValues[0].type);
+
+		assertEquals("type", a3.fieldValues[1].field);
+		assertEquals("actioninsertfact2", a3.fieldValues[1].value);
+		assertEquals(SuggestionCompletionEngine.TYPE_NUMERIC, a3.fieldValues[1].type);
+
+
+	}
+
+
+
+}




More information about the jboss-svn-commits mailing list