Source Code Cross Referenced for RuleSession.java in  » Rule-Engine » hammurapi-rules » biz » hammurapi » rules » jsr94 » 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 » hammurapi rules » biz.hammurapi.rules.jsr94 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package biz.hammurapi.rules.jsr94;
002:
003:        import java.util.ArrayList;
004:        import java.util.Iterator;
005:        import java.util.List;
006:
007:        import javax.rules.Handle;
008:        import javax.rules.InvalidRuleSessionException;
009:        import javax.rules.ObjectFilter;
010:        import javax.rules.RuleExecutionSetMetadata;
011:        import javax.rules.RuleRuntime;
012:        import javax.rules.RuleSessionCreateException;
013:        import javax.rules.RuleSessionTypeUnsupportedException;
014:        import javax.rules.StatefulRuleSession;
015:        import javax.rules.StatelessRuleSession;
016:
017:        import biz.hammurapi.config.Component;
018:        import biz.hammurapi.config.ConfigurationException;
019:        import biz.hammurapi.config.Context;
020:        import biz.hammurapi.rules.CollectionManager;
021:        import biz.hammurapi.rules.HandleManager;
022:        import biz.hammurapi.rules.KnowledgeBase;
023:        import biz.hammurapi.rules.KnowledgeCompactor;
024:
025:        /**
026:         * Rule session, a facade to internal components.
027:         * @author Pavel Vlasov
028:         * @revision $Revision$
029:         */
030:        class RuleSession implements  StatefulRuleSession, StatelessRuleSession {
031:
032:            /**
033:             * 
034:             */
035:            private static final long serialVersionUID = 5619441714641338077L;
036:
037:            private HandleManager handleManager;
038:            private KnowledgeBase knowledgeBase;
039:            private CollectionManager collectionManager;
040:            private ObjectFilter objectFilter;
041:            private int sessionType;
042:            private RuleExecutionSetMetadata metadata;
043:            private Component container;
044:
045:            private KnowledgeCompactor knowledgeCompactor;
046:
047:            /**
048:             * Wraps container into a session.
049:             * Container shall contain at least the following entries:
050:             * <code>/handle-manager</code> of type <code>HandleManager</code>,
051:             * <code>/collection-manager</code> of type <code>CollectionManager</code>,
052:             * <code>/rules</code> with <code>rule</code> subelements,
053:             * <code>/name</code> (String),
054:             * <code>/description</code> (String).
055:             * @throws ConfigurationException 
056:             * @throws RuleSessionTypeUnsupportedException 
057:             */
058:            public RuleSession(Context container, int sessionType, String uri)
059:                    throws RuleSessionCreateException,
060:                    RuleSessionTypeUnsupportedException {
061:
062:                if (sessionType == RuleRuntime.STATEFUL_SESSION_TYPE
063:                        || sessionType == RuleRuntime.STATELESS_SESSION_TYPE) {
064:                    this .sessionType = sessionType;
065:                } else {
066:                    throw new RuleSessionTypeUnsupportedException(
067:                            "Invalid session type: " + sessionType);
068:                }
069:
070:                handleManager = (HandleManager) container
071:                        .get("/handle-manager");
072:                if (handleManager == null) {
073:                    throw new RuleSessionCreateException(
074:                            "Handle manager not found");
075:                }
076:
077:                objectFilter = (ObjectFilter) container.get("/object-filter");
078:
079:                collectionManager = (CollectionManager) container
080:                        .get("/collection-manager");
081:                if (collectionManager == null) {
082:                    throw new RuleSessionCreateException(
083:                            "Collection manager not found");
084:                }
085:
086:                knowledgeBase = (KnowledgeBase) container.get("/rules");
087:                if (knowledgeBase == null) {
088:                    throw new RuleSessionCreateException(
089:                            "/rules element not found");
090:                }
091:
092:                knowledgeCompactor = (KnowledgeCompactor) container
093:                        .get("/knowledge-compactor");
094:
095:                metadata = new biz.hammurapi.rules.jsr94.RuleExecutionSetMetadata(
096:                        uri, (String) container.get("/name"),
097:                        (String) container.get("/description"));
098:
099:                if (container instanceof  Component) {
100:                    this .container = (Component) container;
101:                    try {
102:                        this .container.start();
103:                    } catch (ConfigurationException e) {
104:                        throw new RuleSessionCreateException(
105:                                "Could not start container: " + e, e);
106:                    }
107:                }
108:            }
109:
110:            /**
111:             * This call is delegated to <code>handleManager</code>
112:             */
113:            public boolean containsObject(Handle handle) {
114:                return handleManager.contains(handle);
115:            }
116:
117:            /**
118:             * Adds object to handle manager and to knowledge base.
119:             */
120:            public Handle addObject(Object obj) {
121:                Handle ret = handleManager.addObject(obj);
122:                knowledgeBase.add(obj);
123:                return ret;
124:            }
125:
126:            /**
127:             * Invokes addObject() for each member of the list and returns collection of handles.
128:             */
129:            public List addObjects(List objs) {
130:                List ret = new ArrayList();
131:                Iterator it = objs.iterator();
132:                while (it.hasNext()) {
133:                    ret.add(addObject(it.next()));
134:                }
135:                return ret;
136:            }
137:
138:            /**
139:             * Invokes knowledge base's remove() for the old object, add() for the new object
140:             * and rebinds the handle to the new object.
141:             */
142:            public void updateObject(Handle handle, Object obj) {
143:                knowledgeBase.remove(handleManager.getObject(handle));
144:                knowledgeBase.add(obj);
145:                handleManager.rebind(handle, obj);
146:            }
147:
148:            /**
149:             * Removes object from knowledge base. Knowledge base is 
150:             * responsible for removal of the object from the handle manager.
151:             */
152:            public void removeObject(Handle handle) {
153:                knowledgeBase.remove(handleManager.getObject(handle));
154:            }
155:
156:            /**
157:             * @return List of objects filtered by default object filter.
158:             */
159:            public List getObjects() {
160:                return getObjects(objectFilter);
161:            }
162:
163:            /**
164:             * @return List of handles.
165:             */
166:            public List getHandles() {
167:                return new ArrayList(handleManager.getHandles());
168:            }
169:
170:            /**
171:             * @return List of objects filtered by the <code>filter</code>
172:             * @param Object filter, can be null.
173:             */
174:            public List getObjects(ObjectFilter filter) {
175:                List ret = new ArrayList();
176:
177:                Iterator hit = getHandles().iterator();
178:                while (hit.hasNext()) {
179:                    Object obj = getObject((Handle) hit.next());
180:                    if (filter == null) {
181:                        ret.add(obj);
182:                    } else {
183:                        obj = filter.filter(obj);
184:                        if (obj != null) {
185:                            ret.add(obj);
186:                        }
187:                    }
188:                }
189:
190:                return knowledgeCompactor == null ? ret : knowledgeCompactor
191:                        .compact(ret);
192:            }
193:
194:            /**
195:             * Delegates to knowledgeBase.executeRules(). Normally rules are executed automatically when objects 
196:             * are added or updated. Invocation of this method may be required in multithreaded inference scenario to make sure that 
197:             * all threads finished inference process.
198:             */
199:            public void executeRules() {
200:                knowledgeBase.executeRules();
201:            }
202:
203:            /**
204:             * Clears collection manager, handle manager and resets object filter.
205:             */
206:            public void reset() {
207:                executeRules(); // Make sure that object queue is empty.
208:                collectionManager.clear();
209:                handleManager.clear();
210:                if (objectFilter != null) {
211:                    objectFilter.reset();
212:                }
213:                knowledgeBase.reset();
214:            }
215:
216:            /**
217:             * Returns object by handle.
218:             */
219:            public Object getObject(Handle handle) {
220:                return handleManager.getObject(handle);
221:            }
222:
223:            /**
224:             * Returns execution set metadata.
225:             */
226:            public RuleExecutionSetMetadata getRuleExecutionSetMetadata() {
227:                return metadata;
228:            }
229:
230:            /**
231:             * Stops container.
232:             */
233:            public void release() throws InvalidRuleSessionException {
234:                if (container != null) {
235:                    try {
236:                        container.stop();
237:                    } catch (ConfigurationException e) {
238:                        throw new InvalidRuleSessionException(
239:                                "Could not stop container", e);
240:                    }
241:                }
242:            }
243:
244:            /**
245:             * Returns session type.
246:             */
247:            public int getType() {
248:                return sessionType;
249:            }
250:
251:            // --- Stateless rule session methods ---
252:
253:            /**
254:             * Calls executeRules(object, null)
255:             */
256:            public List executeRules(List objects) {
257:                return executeRules(objects, null);
258:            }
259:
260:            /**
261:             * This method is a wrapper around stateful method.
262:             * It adds all objects to the session, executes rules,
263:             * returns objects and finally resets the session.
264:             */
265:            public List executeRules(List objects, ObjectFilter objectFilter) {
266:                try {
267:                    Iterator it = objects.iterator();
268:                    while (it.hasNext()) {
269:                        addObject(it.next());
270:                    }
271:                    executeRules();
272:                    return getObjects(objectFilter);
273:                } finally {
274:                    reset();
275:                }
276:            }
277:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.