Source Code Cross Referenced for SessionImpl.java in  » Rule-Engine » Mandarax » org » mandarax » reference » 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 » Mandarax » org.mandarax.reference 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 1998-2002 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003:         *
004:         * This library is free software; you can redistribute it and/or
005:         * modify it under the terms of the GNU Lesser General Public
006:         * License as published by the Free Software Foundation; either
007:         * version 2 of the License, or (at your option) any later version.
008:         *
009:         * This library is distributed in the hope that it will be useful,
010:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
011:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012:         * Lesser General Public License for more details.
013:         *
014:         * You should have received a copy of the GNU Lesser General Public
015:         * License along with this library; if not, write to the Free Software
016:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017:         */
018:        package org.mandarax.reference;
019:
020:        import java.util.*;
021:        import org.mandarax.kernel.*;
022:
023:        /**
024:         * Default session implementation.
025:         * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
026:         * @version 3.4 <7 March 05>
027:         * @since 3.2
028:         */
029:
030:        public class SessionImpl implements  Session,
031:                org.mandarax.util.logging.LogCategories {
032:            private Object id = null;
033:            private Map attributes = null;
034:            private org.mandarax.kernel.KnowledgeBase kb = null;
035:            private InferenceEngine ie = null;
036:            private Query query = null;
037:            private Map variableReplacements = null;
038:            private List queryVariables = null;
039:            private List lastConstraintPool = null;
040:            private Map lastVariableRenamings = null;
041:            private int exceptionHandlingStrategy = InferenceEngine.BUBBLE_EXCEPTIONS;
042:            private int cardinalityConstraint = InferenceEngine.ALL;
043:
044:            /**
045:             * Constructor.
046:             * @param kb a knowledge base
047:             * @param ie an inference engine
048:             * @param query a query
049:             * @param exceptionHandlingStrategy the exception handling strategy
050:             * @param cardinality constraint the cardinality constraint
051:             */
052:            public SessionImpl(org.mandarax.kernel.KnowledgeBase kb,
053:                    InferenceEngine ie, Query query,
054:                    int exceptionHandlingStrategy, int cardinalityConstraint) {
055:                super ();
056:                this .kb = kb;
057:                this .ie = ie;
058:                this .query = query;
059:                this .exceptionHandlingStrategy = exceptionHandlingStrategy;
060:                this .cardinalityConstraint = cardinalityConstraint;
061:                queryVariables = IEUtils.getVars(query);
062:            }
063:
064:            /**
065:             * Get a unique session id.
066:             * @return a session id
067:             */
068:            public Object getId() {
069:                if (id == null)
070:                    id = new java.rmi.server.UID();
071:                return id;
072:            }
073:
074:            /**
075:             * Set (bind) a session attribute.
076:             * @param attrName the attribute name
077:             * @param attrValue an attribute value
078:             */
079:            public void setAttribute(Object attrName, Object attrValue) {
080:                getAttributes().put(attrName, attrValue);
081:            }
082:
083:            /**
084:             * Get (lookup) a session attribute.
085:             * @param attrName the attribute name
086:             * @return the attribute value (null indicates that there was no registered value.
087:             */
088:            public Object getAttribute(Object attrName) {
089:                return getAttributes().get(attrName);
090:            }
091:
092:            /**
093:             * Get the attributes.
094:             * @return the attribute map.
095:             */
096:            private Map getAttributes() {
097:                if (attributes == null)
098:                    attributes = new Hashtable();
099:                return attributes;
100:            }
101:
102:            /**
103:             * Compares objects.
104:             * @param obj another object
105:             * @return a boolean
106:             */
107:            public boolean equals(Object obj) {
108:                if (obj instanceof  SessionImpl)
109:                    return getId().equals(((SessionImpl) obj).getId());
110:                return false;
111:            }
112:
113:            /**
114:             * Get the hashcode for this object.
115:             * @return an integer hash value
116:             */
117:            public int hashCode() {
118:                return getId().hashCode();
119:            }
120:
121:            /**
122:             * Get the knowledge base used in this session.
123:             * @return a knowledge base
124:             */
125:            public org.mandarax.kernel.KnowledgeBase getKnowledgeBase() {
126:                return kb;
127:            }
128:
129:            /**
130:             * Get the inference engine used in this session.
131:             * @return an inference engine
132:             */
133:            public InferenceEngine getInferenceEngine() {
134:                return ie;
135:            }
136:
137:            /**
138:             * Get the query for this session.
139:             * @return a query
140:             */
141:            public Query getQuery() {
142:                return query;
143:            }
144:
145:            /**
146:             * Get the replacement value for a variable.
147:             * @param varRenamings a map of variable renamings
148:             * @param constraintPoll a constraint pool
149:             */
150:            private Term resolve(VariableTerm var) {
151:                // TODO optimize
152:                Term replacement = resolveWithConstraints(var);
153:                if (replacement instanceof  ConstantTerm)
154:                    return replacement;
155:                if (replacement == null)
156:                    replacement = (VariableTerm) lastVariableRenamings.get(var);
157:                if (replacement != null) {
158:                    return resolve((VariableTerm) replacement);
159:                }
160:                return null;
161:
162:            }
163:
164:            /**
165:             * Get the replacement value for a variable.
166:             * @param var a variable
167:             */
168:            private Term resolveWithConstraints(VariableTerm var) {
169:                if (lastConstraintPool == null)
170:                    return null;
171:                for (int i = 0; i < lastConstraintPool.size(); i++) {
172:                    Replacement r = (Replacement) lastConstraintPool.get(i);
173:                    if (r.original.equals(var))
174:                        return r.replacement;
175:                }
176:                return null;
177:            }
178:
179:            /**
180:             * Get a map containing the current query variable replacements. 
181:             * I.e., the map contains association VariableTerm -> ConstantTerm
182:             * @return a map
183:             */
184:            public Map getVariableReplacements() {
185:                if (variableReplacements == null) {
186:                    variableReplacements = new Hashtable();
187:                    // protect this block against runtime exceptions 
188:                    try {
189:                        for (int i = 0; i < queryVariables.size(); i++) {
190:                            VariableTerm var = (VariableTerm) queryVariables
191:                                    .get(i);
192:                            Object con = resolve(var);
193:                            variableReplacements.put(var, con);
194:                        }
195:                    } catch (Throwable x) {
196:                        LOG_IE
197:                                .error(
198:                                        "Error building variable replacement map for derivation event listener",
199:                                        x);
200:                    }
201:                }
202:                return variableReplacements;
203:            }
204:
205:            /**
206:             * Update the variable renamings and the constraints.
207:             * @param a map containing variable renamings (var term -> var term associations)
208:             * @param a list of constraints 
209:             */
210:            void update(Map variableRenamings, List constraints) {
211:                this .lastConstraintPool = constraints;
212:                this .lastVariableRenamings = variableRenamings;
213:                // computing the actual replacements might be expensive and is therefore
214:                // done using lazy initialization
215:                this .variableReplacements = null;
216:            }
217:
218:            /**
219:             * Get the exception handling strategy used in this session.
220:             * @return an integer
221:             * @see InferenceEngine.BUBBLE_EXEPTIONS
222:             * @see InferenceEngine.TRY_NEXT
223:             */
224:            public int getExceptionHandlingStrategy() {
225:                return exceptionHandlingStrategy;
226:            }
227:
228:            /**
229:             * Get the cardinality constraints for this session.
230:             * @return an integer
231:             * @see InferenceEngine.ALL
232:             */
233:            public int getCardinalityContraint() {
234:                return cardinalityConstraint;
235:            }
236:
237:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.