001: package org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import java.util.*;
021: import org.mandarax.kernel.*;
022:
023: /**
024: * Utility to clone facts and to rename variables.
025: * The renaming can be retrieved using <code>getRenamings()</code>.
026: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
027: * @version 3.4 <7 March 05>
028: * @since 1.0
029: * Prova re-integration modifications
030: * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
031: * @version 3.4 <7 March 05>
032: */
033: public final class VariableRenaming {
034:
035: public static String GENERATED_VAR_PREFIX = "@@";
036: private int counter = 0;
037: private Hashtable recentRenamings = new java.util.Hashtable();
038: // added in order to keep track of all replacements made during a derivation
039: private Hashtable allRenamings = new java.util.Hashtable();
040: private LogicFactory lFactory = LogicFactory.getDefaultFactory();
041:
042: /**
043: * Clone a constant term.
044: * @return the cloned term
045: * @param original the term to be cloned
046: */
047: private Term clone(ConstantTerm original) {
048: return lFactory.createConstantTerm(original.getObject());
049: }
050:
051: /**
052: * Clone a clause and rename all variables.
053: * @return the cloned clause
054: * @param original the original clause
055: */
056: public Clause cloneAndRenameVars(Clause original) {
057:
058: // first reset the var name server to clean its existing associations
059: reset();
060:
061: List posLit = original.getPositiveLiterals();
062: List negLit = original.getNegativeLiterals();
063: List clonedPosLiterals = new ArrayList(posLit.size());
064:
065: for (Iterator it = posLit.iterator(); it.hasNext();) {
066: clonedPosLiterals.add(cloneAndRenameVars((Fact) it.next()));
067: }
068:
069: List clonedNegLiterals = new ArrayList(negLit.size());
070:
071: for (Iterator it = negLit.iterator(); it.hasNext();) {
072: clonedNegLiterals.add(cloneAndRenameVars((Fact) it.next()));
073: }
074:
075: return new TmpClause(clonedPosLiterals, clonedNegLiterals);
076: }
077:
078: /**
079: * Clone a complex term and rename all variables.
080: * @return the cloned term
081: * @param original the original term
082: */
083: private Term cloneAndRenameVars(ComplexTerm original) {
084: Term[] subterms = original.getTerms();
085: Term[] copiedSubterms = new Term[subterms.length];
086:
087: for (int i = 0; i < subterms.length; i++) {
088: copiedSubterms[i] = cloneAndRenameVars(subterms[i]);
089: }
090:
091: return lFactory.createComplexTerm(original.getFunction(),
092: copiedSubterms);
093: }
094:
095: /**
096: * Clone a fact and rename all variables.
097: * @return the cloned fact
098: * @param original the original fact
099: * Prova: "extra" should be cloned as well.
100: */
101: public Fact cloneAndRenameVars(Fact original) {
102: Term[] t = original.getTerms();
103: Fact f = null;
104: if (original instanceof Prerequisite) {
105: f = lFactory.createPrerequisite(original.getPredicate(),
106: new Term[t.length], original.isNegatedAF());
107: ((Prerequisite) f).setExtra(((Prerequisite) original)
108: .getExtra());
109: } else
110: f = lFactory.createFact(original.getPredicate(),
111: new Term[t.length]);
112: for (int i = 0; i < t.length; i++) {
113: f.setTerm(cloneAndRenameVars(t[i]), i);
114: }
115: return f;
116: }
117:
118: /**
119: * Clone a term and rename all variables.
120: * @return the cloned term
121: * @param original the original term
122: */
123: private Term cloneAndRenameVars(Term original) {
124: if (original instanceof ConstantTerm) {
125: return clone((ConstantTerm) original);
126: }
127:
128: if (original instanceof VariableTerm) {
129: return cloneAndRenameVars((VariableTerm) original);
130: }
131:
132: if (original instanceof ComplexTerm) {
133: return cloneAndRenameVars((ComplexTerm) original);
134: }
135:
136: return original;
137: }
138:
139: /**
140: * Clone and rename a variable term. The new name is a generated temporary session name.
141: * @param original the original term
142: * @return the cloned term with the renamed variables
143: */
144: private org.mandarax.kernel.VariableTerm cloneAndRenameVars(
145: org.mandarax.kernel.VariableTerm original) {
146: org.mandarax.kernel.VariableTerm replacement = (org.mandarax.kernel.VariableTerm) recentRenamings
147: .get(original);
148:
149: if (replacement == null) {
150: // if there is no association in the cache generate and assign a new name
151: counter = counter + 1;
152: String aName = GENERATED_VAR_PREFIX
153: + String.valueOf(counter);
154: replacement = lFactory.createVariableTerm(aName, original
155: .getType());
156: recentRenamings.put(original, replacement);
157: allRenamings.put(original, replacement);
158: }
159:
160: return replacement;
161: }
162:
163: /**
164: * Get the recent renamings.
165: * @return a hashtable containing renamings (term -> term associations)
166: */
167: public Hashtable getRecentRenamings() {
168: return recentRenamings;
169: }
170:
171: /**
172: * Get all renamings made.
173: * @return a hashtable containing renamings (term -> term associations)
174: */
175: public Hashtable getAllRenamings() {
176: return allRenamings;
177: }
178:
179: /**
180: * Reset the cache.
181: */
182: private void reset() {
183: recentRenamings = new Hashtable();
184: }
185: }
|