001: package org.drools.eclipse.editors.rete;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.io.IOException;
005: import java.io.InputStream;
006: import java.io.OutputStream;
007: import java.util.Arrays;
008: import java.util.Comparator;
009: import java.util.List;
010:
011: import junit.framework.TestCase;
012:
013: import org.drools.PackageIntegrationException;
014: import org.drools.RuleBase;
015: import org.drools.RuleBaseFactory;
016: import org.drools.compiler.DrlParser;
017: import org.drools.compiler.DroolsParserException;
018: import org.drools.compiler.PackageBuilder;
019: import org.drools.eclipse.editors.rete.model.ReteGraph;
020: import org.drools.lang.descr.PackageDescr;
021: import org.drools.reteoo.AlphaNodeVertex;
022: import org.drools.reteoo.BaseVertex;
023: import org.drools.reteoo.LeftInputAdapterNodeVertex;
024: import org.drools.reteoo.ObjectTypeNodeVertex;
025: import org.drools.reteoo.ReteVertex;
026: import org.drools.reteoo.ReteooRuleBase;
027: import org.drools.reteoo.ReteooVisitor;
028: import org.drools.reteoo.RuleTerminalNodeVertex;
029: import org.drools.rule.Package;
030:
031: /**
032: *
033: * Integration-like tests
034: *
035: * Testing {@link ReteooLayoutFactory}
036: * It is using following components:
037: * {@link Row},
038: * {@link RowList},
039: * org.drools.reteoo.*Vertex,
040: * org.drools.ide.editors.rete.model.*
041: *
042: * @author Ahti Kitsik
043: *
044: */
045: public class ReteooLayoutFactoryTest extends TestCase {
046:
047: /**
048: * Constructor.
049: *
050: * @param name case name
051: */
052: public ReteooLayoutFactoryTest(String name) {
053: super (name);
054: }
055:
056: /**
057: * Test method for {@link org.drools.eclipse.editors.rete.ReteooLayoutFactory#calculateReteRows(org.drools.reteoo.BaseVertex)}.
058: * @throws IOException
059: * @throws DroolsParserException
060: * @throws PackageIntegrationException
061: * @throws DroolsParserException
062: * @throws PackageIntegrationException
063: */
064: public final void testCalculateReteRows() throws IOException,
065: PackageIntegrationException, DroolsParserException {
066: ReteGraph graph = new ReteGraph();
067: BaseVertex root = loadRete(graph);
068: final RowList rows = ReteooLayoutFactory
069: .calculateReteRows(root);
070:
071: int rownum = rows.getDepth();
072:
073: assertEquals(5, rownum);
074:
075: int[] expectedDepths = new int[] { -1, 0, 1, 2, 3 };
076: int[] expectedSizes = new int[] { 1, 1, 2, 2, 2 };
077:
078: for (int j = 0; j < rownum; j++) {
079: final Row row = rows.get(j);
080: final int rowDepth = row.getDepth();
081: assertEquals(expectedDepths[j], rowDepth);
082: assertEquals(expectedSizes[j], row.getVertices().size());
083: }
084:
085: }
086:
087: /**
088: * Test method for {@link org.drools.eclipse.editors.rete.ReteooLayoutFactory#layoutRowList(org.drools.eclipse.editors.rete.model.ReteGraph, org.drools.eclipse.editors.rete.RowList)}.
089: *
090: * @throws IOException
091: * @throws DroolsParserException
092: * @throws PackageIntegrationException
093: * @throws DroolsParserException
094: * @throws PackageIntegrationException
095: */
096: public final void testLayoutRowList() throws IOException,
097: PackageIntegrationException, DroolsParserException {
098: ReteGraph graph = new ReteGraph();
099: BaseVertex root = loadRete(graph);
100: final RowList rows = ReteooLayoutFactory
101: .calculateReteRows(root);
102:
103: ReteooLayoutFactory.layoutRowList(graph, rows);
104:
105: final List nodes = graph.getChildren();
106:
107: BaseVertex[] yOrder = (BaseVertex[]) nodes
108: .toArray(new BaseVertex[0]);
109: Arrays.sort(yOrder, new Comparator() {
110: public int compare(Object o1, Object o2) {
111: BaseVertex v1 = (BaseVertex) o1;
112: BaseVertex v2 = (BaseVertex) o2;
113: int y1 = v1.getLocation().y;
114: int y2 = v2.getLocation().y;
115: return new Integer(y1).compareTo(new Integer(y2));
116: }
117:
118: });
119:
120: Class[] expectedTypes = new Class[] { ReteVertex.class,
121: ObjectTypeNodeVertex.class, AlphaNodeVertex.class,
122: AlphaNodeVertex.class,
123: LeftInputAdapterNodeVertex.class,
124: LeftInputAdapterNodeVertex.class,
125: RuleTerminalNodeVertex.class,
126: RuleTerminalNodeVertex.class };
127:
128: for (int i = 0; i < yOrder.length; i++) {
129: assertEquals(expectedTypes[i], yOrder[i].getClass());
130: if (i > 0) {
131: // If current vertex has same type as previous then
132: // y-pos should match and x-pos should not match.
133: // If type is different then y-pos should *not* match.
134:
135: BaseVertex current = yOrder[i];
136: BaseVertex previous = yOrder[i - 1];
137: if (current.getClass().equals(previous.getClass())) {
138: assertEquals(current.getLocation().y, previous
139: .getLocation().y);
140: assertNotSame(new Integer(current.getLocation().x),
141: new Integer(previous.getLocation().x));
142: } else {
143: assertNotSame(new Integer(current.getLocation().y),
144: new Integer(previous.getLocation().y));
145: }
146: }
147: }
148:
149: }
150:
151: private BaseVertex loadRete(ReteGraph graph) throws IOException,
152: PackageIntegrationException, DroolsParserException {
153: final InputStream is = getClass().getClassLoader()
154: .getResourceAsStream("simplerule.drl");
155: String drl = streamToString(is);
156:
157: DrlParser parser = new DrlParser();
158: PackageDescr packageDescr = parser.parse(drl);
159: PackageBuilder builder = new PackageBuilder();
160: builder.addPackage(packageDescr);
161: Package pkg = builder.getPackage();
162: ReteooRuleBase ruleBase = (ReteooRuleBase) RuleBaseFactory
163: .newRuleBase(RuleBase.RETEOO);
164: ruleBase.addPackage(pkg);
165:
166: final ReteooVisitor visitor = new ReteooVisitor(graph);
167: visitor.visit(ruleBase);
168:
169: BaseVertex root = visitor.getRootVertex();
170: return root;
171: }
172:
173: private String streamToString(InputStream is) throws IOException {
174: byte[] buffer = new byte[4096];
175: OutputStream outputStream = new ByteArrayOutputStream();
176:
177: while (true) {
178: int read = is.read(buffer);
179:
180: if (read == -1) {
181: break;
182: }
183:
184: outputStream.write(buffer, 0, read);
185: }
186:
187: outputStream.close();
188: is.close();
189:
190: return outputStream.toString();
191: }
192:
193: /**
194: * Used by simplerule.drl
195: *
196: */
197: public static class Message {
198: public static final int HELLO = 0;
199: public static final int GOODBYE = 1;
200:
201: private String message;
202:
203: private int status;
204:
205: public String getMessage() {
206: return this .message;
207: }
208:
209: public void setMessage(final String message) {
210: this .message = message;
211: }
212:
213: public int getStatus() {
214: return this .status;
215: }
216:
217: public void setStatus(final int status) {
218: this.status = status;
219: }
220: }
221:
222: }
|