001: package org.drools.decisiontable.parser;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Map;
020:
021: /**
022: * Simple holder class identifying a condition or action column etc.
023: * This is stored in a map in the main listener class, to track what type of values
024: * you can expect to see in the rows directly below.
025: *
026: * There are five types of columns relevant to a rule table.
027: * @author Michael Neale
028: */
029: public class ActionType {
030:
031: public static final int CONDITION = 0;
032:
033: public static final int ACTION = 1;
034:
035: // 08 - 16 - 2005 RIK: Define 3 new ActionType types
036: // PRIORITY is used to set the salience parameter of a rule tag
037: public static final int PRIORITY = 2;
038:
039: // DURATION is used to set a duration tag inside a rule tag
040: public static final int DURATION = 3;
041:
042: // NAME is used to set the name parameter of a rule tag
043: public static final int NAME = 4;
044:
045: // 10 - 05 - 2005 RIK: Add 2 new AtcionType types
046: // DESCRIPTION is used to set the description parameter of a rule tag
047: public static final int DESCRIPTION = 5;
048:
049: // NOLOOP is used to set the no-loop parameter of a rule tag
050: public static final int NOLOOP = 6;
051:
052: // XOR-GROUP is used to set the activation-group parameter of a rule tag
053: public static final int ACTIVATIONGROUP = 7;
054:
055: // 14 September 2006 SJW: Add new Agenda Group ActionType
056: // AGENDA-GROUP is used to set the agenda-group parameter of a rule tag
057: public static final int AGENDAGROUP = 8;
058:
059: public static final int RULEFLOWGROUP = 9;
060:
061: int type;
062:
063: private SourceBuilder sourceBuilder = null;
064:
065: // private String value;
066:
067: ActionType(final int actionType) {
068: this .type = actionType;
069: }
070:
071: /**
072: * This is only set for LHS or RHS building.
073: */
074: public void setSourceBuilder(SourceBuilder src) {
075: this .sourceBuilder = src;
076: }
077:
078: public SourceBuilder getSourceBuilder() {
079: return this .sourceBuilder;
080: }
081:
082: // String getSnippet(final String cellValue) {
083: // final SnippetBuilder builder = new SnippetBuilder( this.value );
084: // return builder.build( cellValue );
085: // }
086:
087: /**
088: * Create a new action type that matches this cell, and add it to the map,
089: * keyed on that column.
090: */
091: public static void addNewActionType(final Map actionTypeMap,
092: final String value, final int column, final int row) {
093:
094: if (value.toUpperCase().startsWith("U")
095: || value.toUpperCase().equals("NO-LOOP")) // if the title cell
096: // value starts with
097: // "U" then put a
098: // ActionType.NOLOOP
099: // to the _actions
100: // list
101: {
102: actionTypeMap.put(new Integer(column), new ActionType(
103: ActionType.NOLOOP));
104: } else if (value.toUpperCase().equals("RULEFLOW-GROUP")
105: || value.toUpperCase().startsWith("R")) {
106: actionTypeMap.put(new Integer(column), new ActionType(
107: ActionType.RULEFLOWGROUP));
108:
109: } else if (value.toUpperCase().equals("AGENDA-GROUP")) // if the title cell
110: // value equals "AGENDA-GROUP"
111: // then put a
112: // ActionType.AGENDAGROUP
113: // to the _actions
114: // list
115: {
116: actionTypeMap.put(new Integer(column), new ActionType(
117: ActionType.AGENDAGROUP));
118: } else if (value.toUpperCase().startsWith("X")
119: || value.toUpperCase().equals("ACTIVATION-GROUP")) // if the title cell
120: // value starts with
121: // "X" then put a
122: // ActionType.XORGROUP
123: // to the _actions
124: // list
125: {
126: actionTypeMap.put(new Integer(column), new ActionType(
127: ActionType.ACTIVATIONGROUP));
128: } else if (value.toUpperCase().startsWith("C")) {
129: actionTypeMap.put(new Integer(column), new ActionType(
130: ActionType.CONDITION));
131: } else if (value.toUpperCase().startsWith("A")) {
132: actionTypeMap.put(new Integer(column), new ActionType(
133: ActionType.ACTION));
134: } else if (value.toUpperCase().startsWith("P")) // if the title cell
135: // value starts with
136: // "P" then put a
137: // ActionType.PRIORITY
138: // to the _actions
139: // list
140: {
141: actionTypeMap.put(new Integer(column), new ActionType(
142: ActionType.PRIORITY));
143: } else if (value.toUpperCase().startsWith("D")) // if the title cell
144: // value starts with
145: // "D" then put a
146: // ActionType.DURATION
147: // to the _actions
148: // list
149: {
150: actionTypeMap.put(new Integer(column), new ActionType(
151: ActionType.DURATION));
152: } else if (value.toUpperCase().startsWith("N")) // if the title cell
153: // value starts with
154: // "N" then put a
155: // ActionType.NAME
156: // to the _actions
157: // list
158: {
159: actionTypeMap.put(new Integer(column), new ActionType(
160: ActionType.NAME));
161: } else if (value.toUpperCase().startsWith("I")) // if the title cell
162: // value starts with
163: // "I" then put a
164: // ActionType.DESCRIPTION
165: // to the _actions
166: // list
167: {
168: actionTypeMap.put(new Integer(column), new ActionType(
169: ActionType.DESCRIPTION));
170: } else {
171: throw new DecisionTableParseException(
172: "Invalid column header (ACTION type), "
173: + "should be CONDITION or ACTION (etc..) row number:"
174: + (row + 1)
175: + " cell number:"
176: + (column + 1)
177: + " - does not contain a leading C or A identifer.");
178: }
179: }
180:
181: /**
182: * This is where a code snippet template is added.
183: */
184: public void addTemplate(int col, String content) {
185: this .sourceBuilder.addTemplate(col, content);
186: }
187:
188: /**
189: * Values are added to populate the template.
190: * The source builder contained needs to be "cleared" when the resultant snippet is extracted.
191: */
192: public void addCellValue(int col, String content) {
193: this.sourceBuilder.addCellValue(col, content);
194: }
195:
196: }
|