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