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: }
|