[jboss-svn-commits] JBL Code SVN: r24072 - in labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor: server/util and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Nov 24 14:42:57 EST 2008
Author: jamesdeanturner
Date: 2008-11-24 14:42:56 -0500 (Mon, 24 Nov 2008)
New Revision: 24072
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/dt/GuidedDecisionTable.java
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/server/util/GuidedDTDRLPersistence.java
Log:
added convenience methods for manipulating metadata on decision tables
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/dt/GuidedDecisionTable.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/dt/GuidedDecisionTable.java 2008-11-24 17:38:46 UTC (rev 24071)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/client/modeldriven/dt/GuidedDecisionTable.java 2008-11-24 19:42:56 UTC (rev 24072)
@@ -7,28 +7,39 @@
import org.drools.guvnor.client.modeldriven.SuggestionCompletionEngine;
import org.drools.guvnor.client.modeldriven.brl.ISingleFieldConstraint;
import org.drools.guvnor.client.modeldriven.brl.PortableObject;
+import org.drools.guvnor.client.modeldriven.brl.RuleModel;
+import org.drools.guvnor.server.util.BRDRLPersistence;
/**
- * This is a decision table model for a guided editor. It is not template or XLS based.
- * (template could be done relatively easily by taking a template, as a String, and then String[][] data and driving the SheetListener
- * interface in the decision tables module).
- *
- * This works by taking the column definitions, and combining them with the table of data to produce rule models.
- *
- *
+ * This is a decision table model for a guided editor. It is not template or XLS
+ * based. (template could be done relatively easily by taking a template, as a
+ * String, and then String[][] data and driving the SheetListener interface in
+ * the decision tables module).
+ *
+ * This works by taking the column definitions, and combining them with the
+ * table of data to produce rule models.
+ *
+ *
* @author Michael Neale
*/
public class GuidedDecisionTable implements PortableObject {
/**
+ * Number of internal elements before ( used for offsets in serialization )
+ */
+ public static final int INTERNAL_ELEMENTS = 2;
+
+ /**
* The name - obviously.
*/
public String tableName;
-
+
public String parentName;
+ // metadata defined for table ( will be represented as a column per table
+ // row of DATA
private List<MetadataCol> metadataCols;
-
+
public List<AttributeCol> attributeCols = new ArrayList<AttributeCol>();
public List<ConditionCol> conditionCols = new ArrayList<ConditionCol>();
@@ -36,10 +47,9 @@
public List<ActionCol> actionCols = new ArrayList<ActionCol>();
/**
- * First column is always row number.
- * Second column is description.
- * Subsequent ones follow the above column definitions:
- * attributeCols, then conditionCols, then actionCols, in that order, left to right.
+ * First column is always row number. Second column is description.
+ * Subsequent ones follow the above column definitions: attributeCols, then
+ * conditionCols, then actionCols, in that order, left to right.
*/
public String[][] data = new String[0][0];
@@ -50,46 +60,51 @@
public String groupField;
- //TODO: add in precondition(s)
+ // TODO: add in precondition(s)
+ public GuidedDecisionTable() {
+ }
- public GuidedDecisionTable() {}
+ // /**
+ // * Will return an attribute col, or condition or action, depending on what
+ // column is requested.
+ // * This works through attributes, conditions and then actions, in left to
+ // right manner.
+ // */
+ // public DTColumnConfig getColumnConfiguration(int index) {
+ // if (index < attributeCols.size()) {
+ // return (DTColumnConfig) attributeCols.get(index);
+ // } else if (index < attributeCols.size() + conditionCols.size()) {
+ // return (DTColumnConfig) conditionCols.get(index - attributeCols.size());
+ // } else {
+ // return (DTColumnConfig) actionCols.get(index - attributeCols.size() -
+ // conditionCols.size());
+ // }
+ // }
-// /**
-// * Will return an attribute col, or condition or action, depending on what column is requested.
-// * This works through attributes, conditions and then actions, in left to right manner.
-// */
-// public DTColumnConfig getColumnConfiguration(int index) {
-// if (index < attributeCols.size()) {
-// return (DTColumnConfig) attributeCols.get(index);
-// } else if (index < attributeCols.size() + conditionCols.size()) {
-// return (DTColumnConfig) conditionCols.get(index - attributeCols.size());
-// } else {
-// return (DTColumnConfig) actionCols.get(index - attributeCols.size() - conditionCols.size());
-// }
-// }
-
/**
- * This will return a list of valid values. if there is no such "enumeration" of values,
- * then it will return an empty array.
+ * This will return a list of valid values. if there is no such
+ * "enumeration" of values, then it will return an empty array.
*/
public String[] getValueList(DTColumnConfig col, SuggestionCompletionEngine sce) {
if (col instanceof AttributeCol) {
AttributeCol at = (AttributeCol) col;
if (at.attr.equals("no-loop") || at.attr.equals("enabled")) {
- return new String[] {"true", "false"};
+ return new String[] { "true", "false" };
}
} else if (col instanceof ConditionCol) {
- //conditions: if its a formula etc, just return String[0], otherwise check with the sce
+ // conditions: if its a formula etc, just return String[0],
+ // otherwise check with the sce
ConditionCol c = (ConditionCol) col;
- if (c.constraintValueType == ISingleFieldConstraint.TYPE_RET_VALUE || c.constraintValueType == ISingleFieldConstraint.TYPE_PREDICATE) {
+ if (c.constraintValueType == ISingleFieldConstraint.TYPE_RET_VALUE
+ || c.constraintValueType == ISingleFieldConstraint.TYPE_PREDICATE) {
return new String[0];
} else {
if (c.valueList != null && !"".equals(c.valueList)) {
return c.valueList.split(",");
} else {
String[] r = sce.getEnumValues(c.factType, c.factField);
- return (r != null)? r : new String[0];
+ return (r != null) ? r : new String[0];
}
}
} else if (col instanceof ActionSetFieldCol) {
@@ -98,7 +113,7 @@
return c.valueList.split(",");
} else {
String[] r = sce.getEnumValues(getBoundFactType(c.boundName), c.factField);
- return (r != null)? r : new String[0];
+ return (r != null) ? r : new String[0];
}
} else if (col instanceof ActionInsertFactCol) {
ActionInsertFactCol c = (ActionInsertFactCol) col;
@@ -106,7 +121,7 @@
return c.valueList.split(",");
} else {
String[] r = sce.getEnumValues(c.factType, c.factField);
- return (r != null)? r : new String[0];
+ return (r != null) ? r : new String[0];
}
}
@@ -116,9 +131,9 @@
private String getBoundFactType(String boundName) {
for (Iterator iterator = conditionCols.iterator(); iterator.hasNext();) {
ConditionCol c = (ConditionCol) iterator.next();
- if (c.boundName.equals(boundName)) {
- return c.factType;
- }
+ if (c.boundName.equals(boundName)) {
+ return c.factType;
+ }
}
return null;
}
@@ -155,7 +170,8 @@
return true;
}
}
- //we can reuse text filter from guided editor to enforce this for data entry.
+ // we can reuse text filter from guided editor to enforce this for data
+ // entry.
return false;
}
@@ -163,12 +179,51 @@
this.metadataCols = metadataCols;
}
- public List<MetadataCol> getMetadataCols()
- {
- if(null == metadataCols){
+ public List<MetadataCol> getMetadataCols() {
+ if (null == metadataCols) {
metadataCols = new ArrayList<MetadataCol>();
}
return metadataCols;
}
+ /**
+ * Locate index of attribute name if it exists
+ *
+ * @param attributeName
+ * Name of metadata we are looking for
+ * @return index of attribute name or -1 if not found
+ */
+ public int getMetadataColIndex(String attributeName) {
+
+ for (int i = 0; metadataCols != null && i < metadataCols.size(); i++) {
+ if (attributeName.equals(metadataCols.get(i).attr)) {
+ return i;
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * Update all rows of metadata with value it attribute is present
+ *
+ * @param attributeName
+ * Name of metadata we are looking for
+ * @return true if values update, false if not
+ */
+ public boolean updateMetadataCol(String attributeName, String newValue) {
+
+ // see if metaData exists for
+ int metaIndex = getMetadataColIndex(attributeName);
+ if (metaIndex < 0)
+ return false;
+
+ for (int i = 0; i < data.length; i++) {
+
+ String[] row = data[i];
+
+ row[GuidedDecisionTable.INTERNAL_ELEMENTS + metaIndex] = newValue;
+ }
+ return true;
+ }
+
}
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/server/util/GuidedDTDRLPersistence.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/server/util/GuidedDTDRLPersistence.java 2008-11-24 17:38:46 UTC (rev 24071)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/guvnor/server/util/GuidedDTDRLPersistence.java 2008-11-24 19:42:56 UTC (rev 24072)
@@ -50,7 +50,7 @@
doMetadata(dt.getMetadataCols(), row, rm);
doAttribs(dt.getMetadataCols().size(), dt.attributeCols, row, rm);
doConditions(dt.getMetadataCols().size() + dt.attributeCols.size(), dt.conditionCols, row, rm);
- doActions(dt.getMetadataCols().size() +dt.attributeCols.size() + dt.conditionCols.size(), dt.actionCols, row, rm);
+ doActions(dt.getMetadataCols().size() + dt.attributeCols.size() + dt.conditionCols.size(), dt.actionCols, row, rm);
if(dt.parentName != null){
rm.parentName = dt.parentName;
@@ -71,7 +71,7 @@
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];
+ String cell = row[condAndAttrs + i + GuidedDecisionTable.INTERNAL_ELEMENTS];
if (validCell(cell)) {
if (c instanceof ActionInsertFactCol) {
ActionInsertFactCol ac = (ActionInsertFactCol)c;
@@ -132,7 +132,7 @@
for (int i = 0; i < conditionCols.size(); i++) {
ConditionCol c = (ConditionCol) conditionCols.get(i);
- String cell = row[i + 2 + numOfAttributesAndMeta];
+ String cell = row[i + GuidedDecisionTable.INTERNAL_ELEMENTS + numOfAttributesAndMeta];
if (validCell(cell)) {
//get or create the pattern it belongs too
@@ -199,7 +199,7 @@
List<RuleAttribute> attribs = new ArrayList<RuleAttribute>();
for (int j = 0; j < attributeCols.size(); j++) {
AttributeCol at = attributeCols.get(j);
- String cell = row[j + 2 + numOfMeta];
+ String cell = row[j + GuidedDecisionTable.INTERNAL_ELEMENTS + numOfMeta];
if (validCell(cell)) {
attribs.add(new RuleAttribute(at.attr, cell));
}
@@ -210,11 +210,13 @@
}
void doMetadata(List<MetadataCol> metadataCols, String[] row, RuleModel rm) {
+
+ // setup temp list
List<RuleMetadata> metadataList = new ArrayList<RuleMetadata>();
-
- for (int j = 0;j < metadataCols.size(); j++) {
+
+ for (int j = 0; j < metadataCols.size(); j++) {
MetadataCol meta = metadataCols.get(j);
- String cell = row[j + 2];
+ String cell = row[j + GuidedDecisionTable.INTERNAL_ELEMENTS];
if (validCell(cell)) {
metadataList.add(new RuleMetadata(meta.attr, cell));
}
@@ -229,7 +231,7 @@
}
boolean validCell(String c) {
- return c !=null && !c.trim().equals("");
+ return (c != null) && (!c.trim().equals(""));
}
private class LabelledAction {
More information about the jboss-svn-commits
mailing list