Source Code Cross Referenced for ReteooLayoutFactoryTest.java in  » Rule-Engine » drolls-Rule-Engine » org » drools » eclipse » editors » rete » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Rule Engine » drolls Rule Engine » org.drools.eclipse.editors.rete 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.