001: package org.drools.decisiontable.parser;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009:
010: import junit.framework.TestCase;
011:
012: import org.drools.Agenda;
013: import org.drools.FactException;
014: import org.drools.FactHandle;
015: import org.drools.ObjectFilter;
016: import org.drools.QueryResults;
017: import org.drools.RuleBase;
018: import org.drools.StatefulSession;
019: import org.drools.concurrent.Future;
020: import org.drools.event.AgendaEventListener;
021: import org.drools.event.RuleFlowEventListener;
022: import org.drools.event.WorkingMemoryEventListener;
023: import org.drools.rule.Rule;
024: import org.drools.ruleflow.common.instance.ProcessInstance;
025: import org.drools.spi.Activation;
026: import org.drools.spi.AgendaFilter;
027: import org.drools.spi.AgendaGroup;
028: import org.drools.spi.AsyncExceptionHandler;
029: import org.drools.spi.GlobalResolver;
030:
031: public class ExternalSheetListenerTest extends TestCase {
032:
033: private ExternalSheetListener esl;
034:
035: private Map assertedRows = new HashMap();
036:
037: private List currentRow = new ArrayList();
038:
039: protected void setUp() throws Exception {
040: esl = new ExternalSheetListener(2, 2,
041: new TestTemplateContainer(),
042: new TestTemplateRuleBase(), new TestGenerator());
043:
044: }
045:
046: public void testRenderDrl() {
047: String drl = esl.renderDRL();
048: assertEquals("Test Template Header\nTest Generated DRL\n", drl);
049: }
050:
051: public void testRowHandling() {
052: esl.newRow(0, 3);
053: esl.newCell(0, 0, "row0col0", 0);
054: esl.newCell(0, 1, "row0col1", 0);
055: esl.newCell(0, 2, "row0col2", 0);
056: esl.newRow(1, 3);
057: esl.newCell(1, 0, "row1col0", 0);
058: esl.newCell(1, 1, "row1col1", 0);
059: esl.newCell(1, 2, "row1col2", 0);
060: esl.newRow(2, 3);
061: esl.newCell(2, 0, "row2col0", 0);
062: esl.newCell(2, 1, "row2col1", 0);
063: esl.newCell(2, 2, "row2col2", 0);
064: esl.finishSheet();
065: assertEquals(2, assertedRows.size());
066: for (Iterator it = assertedRows.entrySet().iterator(); it
067: .hasNext();) {
068: Map.Entry entry = (Map.Entry) it.next();
069: Row row = (Row) entry.getKey();
070: List cells = (List) entry.getValue();
071: // first column is not part of the decision table
072: int i = 1;
073: for (Iterator it2 = cells.iterator(); it2.hasNext(); i++) {
074: Cell cell = (Cell) it2.next();
075: assertEquals("row" + row.getRowNumber() + "col" + i,
076: cell.getValue());
077: assertEquals("Pattern " + i, cell.getColumn());
078: }
079: }
080: }
081:
082: public void testRowHandlingBlankRows() {
083: esl.newRow(0, 3);
084: esl.newCell(0, 0, "row0col0", 0);
085: esl.newCell(0, 1, "row0col1", 0);
086: esl.newCell(0, 2, "row0col2", 0);
087: esl.newRow(1, 3);
088: esl.newCell(1, 0, "row1col0", 0);
089: esl.newCell(1, 1, "row1col1", 0);
090: esl.newCell(1, 2, "row1col2", 0);
091: esl.newRow(2, 3);
092: esl.newCell(2, 0, "row2col0", 0);
093: esl.newCell(2, 1, "row2col1", 0);
094: esl.newCell(2, 2, "row2col2", 0);
095: esl.newRow(3, 3);
096: esl.newCell(3, 0, "", 0);
097: esl.newCell(3, 1, "", 0);
098: esl.newCell(3, 2, "", 0);
099: esl.newRow(4, 3);
100: esl.newCell(4, 0, "", 0);
101: esl.newCell(4, 1, "", 0);
102: esl.newCell(4, 2, "", 0);
103:
104: esl.finishSheet();
105: assertEquals(2, assertedRows.size());
106: }
107:
108: private class TestTemplateRuleBase implements TemplateRuleBase {
109:
110: public StatefulSession newStatefulSession() {
111: return new StatefulSession() {
112:
113: public void addEventListener(
114: WorkingMemoryEventListener arg0) {
115:
116: }
117:
118: public void addEventListener(AgendaEventListener arg0) {
119:
120: }
121:
122: public void addEventListener(RuleFlowEventListener arg0) {
123:
124: }
125:
126: public FactHandle insert(Object fact)
127: throws FactException {
128: if (fact instanceof Row) {
129: assertedRows.put(fact, currentRow);
130: currentRow = new ArrayList();
131: } else {
132: currentRow.add(fact);
133: }
134: return null;
135: }
136:
137: public FactHandle insert(Object arg0, boolean arg1)
138: throws FactException {
139: return null;
140: }
141:
142: public void clearAgenda() {
143:
144: }
145:
146: public void clearAgendaGroup(String arg0) {
147:
148: }
149:
150: public void dispose() {
151:
152: }
153:
154: public void fireAllRules() throws FactException {
155:
156: }
157:
158: public void fireAllRules(AgendaFilter arg0)
159: throws FactException {
160:
161: }
162:
163: public Agenda getAgenda() {
164: return null;
165: }
166:
167: public List getAgendaEventListeners() {
168: return null;
169: }
170:
171: public FactHandle getFactHandle(Object arg0) {
172: return null;
173: }
174:
175: public List getFactHandles() {
176: return null;
177: }
178:
179: public AgendaGroup getFocus() {
180: return null;
181: }
182:
183: public Object getGlobal(String arg0) {
184: return null;
185: }
186:
187: public Object getObject(FactHandle arg0) {
188: return null;
189: }
190:
191: public QueryResults getQueryResults(String arg0) {
192: return null;
193: }
194:
195: public RuleBase getRuleBase() {
196: return null;
197: }
198:
199: public List getWorkingMemoryEventListeners() {
200: return null;
201: }
202:
203: public void update(FactHandle arg0, Object arg1)
204: throws FactException {
205:
206: }
207:
208: public void removeEventListener(
209: WorkingMemoryEventListener arg0) {
210:
211: }
212:
213: public void removeEventListener(AgendaEventListener arg0) {
214:
215: }
216:
217: public void removeEventListener(
218: RuleFlowEventListener arg0) {
219:
220: }
221:
222: public void retract(FactHandle arg0)
223: throws FactException {
224:
225: }
226:
227: public void setAsyncExceptionHandler(
228: AsyncExceptionHandler arg0) {
229:
230: }
231:
232: public void setFocus(String arg0) {
233:
234: }
235:
236: public void setFocus(AgendaGroup arg0) {
237:
238: }
239:
240: public void setGlobal(String arg0, Object arg1) {
241:
242: }
243:
244: public void setGlobalResolver(
245: GlobalResolver globalResolver) {
246:
247: }
248:
249: public ProcessInstance startProcess(String processId) {
250: return null;
251: }
252:
253: public Iterator iterateFactHandles() {
254: return null;
255: }
256:
257: public Iterator iterateFactHandles(ObjectFilter filter) {
258: return null;
259: }
260:
261: public Iterator iterateObjects() {
262: return null;
263: }
264:
265: public Iterator iterateObjects(ObjectFilter filter) {
266: return null;
267: }
268:
269: public Future asyncInsert(Object object) {
270: // TODO Auto-generated method stub
271: return null;
272: }
273:
274: public Future asyncInsert(Object[] list) {
275: // TODO Auto-generated method stub
276: return null;
277: }
278:
279: public Future asyncInsert(Collection collection) {
280: // TODO Auto-generated method stub
281: return null;
282: }
283:
284: public Future asyncFireAllRules() {
285: // TODO Auto-generated method stub
286: return null;
287: }
288:
289: public Future asyncFireAllRules(
290: AgendaFilter agendaFilter) {
291: // TODO Auto-generated method stub
292: return null;
293: }
294:
295: public Future asyncUpdate(FactHandle factHandle,
296: Object object) {
297: // TODO Auto-generated method stub
298: return null;
299: }
300:
301: public Future asyncRetract(FactHandle factHandle) {
302: // TODO Auto-generated method stub
303: return null;
304: }
305:
306: public QueryResults getQueryResults(String query,
307: Object[] arguments) {
308: // TODO Auto-generated method stub
309: return null;
310: }
311:
312: public void modifyInsert(FactHandle factHandle,
313: Object object, Rule rule, Activation activation) {
314: // TODO Auto-generated method stub
315:
316: }
317:
318: public void modifyRetract(FactHandle factHandle,
319: Rule rule, Activation activation) {
320: // TODO Auto-generated method stub
321:
322: }
323:
324: public void modifyInsert(FactHandle factHandle,
325: Object object) {
326: // TODO Auto-generated method stub
327:
328: }
329:
330: public void modifyRetract(FactHandle factHandle) {
331: // TODO Auto-generated method stub
332:
333: }
334:
335: public void halt() {
336: // TODO Auto-generated method stub
337:
338: }
339:
340: public void fireAllRules(int fireLimit)
341: throws FactException {
342: // TODO Auto-generated method stub
343:
344: }
345:
346: public void fireAllRules(AgendaFilter agendaFilter,
347: int fireLimit) throws FactException {
348: // TODO Auto-generated method stub
349:
350: }
351:
352: public Future asyncInsert(List list) {
353: // TODO Auto-generated method stub
354: return null;
355: }
356:
357: public GlobalResolver getGlobalResolver() {
358: // TODO Auto-generated method stub
359: return null;
360: }
361:
362: public List getRuleFlowEventListeners() {
363: // TODO Auto-generated method stub
364: return null;
365: }
366:
367: public void clearActivationGroup(String group) {
368: // TODO Auto-generated method stub
369:
370: }
371:
372: public void clearRuleFlowGroup(String group) {
373: // TODO Auto-generated method stub
374:
375: }
376: };
377: }
378: }
379:
380: private class TestGenerator implements Generator {
381:
382: public void generate(String templateName, Row row) {
383: }
384:
385: public String getDrl() {
386: return "Test Generated DRL";
387: }
388:
389: }
390:
391: private class TestTemplateContainer implements TemplateContainer {
392:
393: public void addColumn(Column c) {
394: }
395:
396: public void addTemplate(RuleTemplate template) {
397: }
398:
399: public Column[] getColumns() {
400: return new Column[] { new Column("Pattern 1"),
401: new Column("Pattern 2"), new Column("Pattern 3") };
402: }
403:
404: public String getHeader() {
405: return "Test Template Header";
406: }
407:
408: public Map getTemplates() {
409: return null;
410: }
411:
412: public void setHeader(String head) {
413: }
414:
415: public void setTemplates(Map templates) {
416:
417: }
418: }
419:
420: }
|