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


001:        package org.mandarax.zkb.framework;
002:
003:        /**
004:         *  Copyright (C) 1999-2004  Jens Dietrich (mailto:mandarax@jbdietrich.com)
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:
021:        import java.io.PrintWriter;
022:        import java.util.Comparator;
023:        import java.util.Iterator;
024:        import java.util.List;
025:        import org.jdom.Comment;
026:        import org.jdom.Element;
027:        import org.mandarax.kernel.ClauseSet;
028:        import org.mandarax.kernel.ExtendedKnowledgeBase;
029:        import org.mandarax.kernel.Fact;
030:        import org.mandarax.kernel.KnowledgeBase;
031:        import org.mandarax.kernel.LogicFactory;
032:        import org.mandarax.kernel.Query;
033:        import org.mandarax.kernel.Rule;
034:        import org.mandarax.sql.SQLClauseSet;
035:        import org.mandarax.zkb.ObjectPersistencyService;
036:        import org.mandarax.zkb.ZKBException;
037:        import org.mandarax.kernel.validation.*;
038:
039:        /**
040:         * An adapter class for knowledge bases.
041:         * New in this driver is the support for test cases.
042:         * @see org.mandarax.kernel.KnowledgeBase
043:         * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
044:         * @version 3.4 <7 March 05>
045:         * @since 3.4
046:         */
047:
048:        public class Adapter4KnowledgeBases_2_0 extends AbstractAdapter {
049:
050:            // constant for comparator attribute
051:
052:            /**
053:             * Export an object, i.e., convert it to an element in the DOM.
054:             * @param obj an object
055:             * @param driver the generic driver
056:             * @param ops the object persistency service
057:             * @exception a ZKBException is thrown if export fails
058:             */
059:            public Element exportObject(Object obj, GenericDriver driver,
060:                    ObjectPersistencyService ops) throws ZKBException {
061:                check(obj, KnowledgeBase.class);
062:                int ftl = driver.getFaultToleranceLevel();
063:                PrintWriter out = driver.getLogWriter();
064:                KnowledgeBase kb = (KnowledgeBase) obj;
065:                Element e = new Element(KNOWLEDGE_BASE);
066:
067:                e.setAttribute(CLASS, kb.getClass().getName());
068:
069:                // export comparator
070:                if (kb instanceof  ExtendedKnowledgeBase) {
071:                    Comparator comp = ((ExtendedKnowledgeBase) kb)
072:                            .getComparator();
073:                    if (comp != null) {
074:                        e
075:                                .addContent(exportObject(comp, COMPARATOR,
076:                                        driver, ops));
077:                    }
078:                }
079:
080:                // export knowledge
081:                List clauseSets = kb.getClauseSets();
082:                for (Iterator it = clauseSets.iterator(); it.hasNext();) {
083:                    ClauseSet cs = (ClauseSet) it.next();
084:                    String kindOfObject = getClauseSetType(cs);
085:                    if (kindOfObject == null) {
086:                        String msg = "Cannot figure out what kind of clause set the following object is: "
087:                                + cs;
088:                        if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
089:                            throw new ZKBException(msg);
090:                        // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
091:                        else {
092:                            out.println("Warning");
093:                            out.println(msg);
094:                            out.println("The clause is ignored");
095:                            LOG_XKB.warn(msg);
096:                            LOG_XKB.warn("The clause is ignored");
097:                        }
098:                    } else {
099:                        try {
100:                            Element el = exportObject(cs, kindOfObject, driver,
101:                                    ops);
102:                            e.addContent(el);
103:                        } catch (ZKBException x) {
104:                            String msg = "Problems exporting the knowledge base, export failed for "
105:                                    + cs;
106:                            if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
107:                                throw new ZKBException(msg);
108:                            // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
109:                            else {
110:                                if (out != null) {
111:                                    out.println(msg);
112:                                    out.println("See log for details");
113:                                }
114:                                e.addContent(new Comment(msg));
115:                                LOG_XKB.error(msg, x);
116:                            }
117:                        }
118:
119:                    }
120:                }
121:                // export queries
122:                for (Iterator it = kb.queryNames(); it.hasNext();) {
123:                    Query q = kb.getQuery((String) it.next());
124:                    try {
125:                        Element el = exportObject(q, GenericDriver.QUERY,
126:                                driver, ops);
127:                        e.addContent(el);
128:                    } catch (ZKBException x) {
129:                        String msg = "Problems exporting the knowledge base, export failed for query "
130:                                + q;
131:                        if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
132:                            throw new ZKBException(msg);
133:                        // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
134:                        else {
135:                            out.println(msg);
136:                            out.println("See log for details");
137:                            e.addContent(new Comment(msg));
138:                            LOG_XKB.error(msg, x);
139:                        }
140:                    }
141:                }
142:
143:                // export test cases
144:                for (Iterator it = kb.testcases(); it.hasNext();) {
145:                    TestCase tc = (TestCase) it.next();
146:                    try {
147:                        Element el = exportObject(tc, GenericDriver.TESTCASE,
148:                                driver, ops);
149:                        e.addContent(el);
150:                    } catch (ZKBException x) {
151:                        String msg = "Problems exporting the knowledge base, export failed for test case "
152:                                + tc;
153:                        if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
154:                            throw new ZKBException(msg);
155:                        // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
156:                        else {
157:                            out.println(msg);
158:                            out.println("See log for details");
159:                            e.addContent(new Comment(msg));
160:                            LOG_XKB.error(msg, x);
161:                        }
162:                    }
163:                }
164:
165:                return e;
166:            }
167:
168:            /**
169:             * Build an object from an XML element.
170:             * @param e an element
171:             * @param driver the generic driver
172:             * @param ops the object persistency service
173:             * @param lfactory the logic factory used to create objects
174:             * @exception a ZKBException is thrown if export fails
175:             */
176:            public Object importObject(Element e, GenericDriver driver,
177:                    ObjectPersistencyService ops, LogicFactory lfactory)
178:                    throws ZKBException {
179:                // try to instanciate saved class
180:                KnowledgeBase kb = null;
181:                String implClassName = e.getAttributeValue(CLASS);
182:                if (implClassName != null) {
183:                    try {
184:                        Class implClass = Class.forName(implClassName);
185:                        kb = (KnowledgeBase) implClass.newInstance();
186:                    } catch (ClassNotFoundException x) {
187:                        LOG_ZKB.error("Cannot find class " + implClassName
188:                                + ", will use default", x);
189:                    } catch (IllegalAccessException x) {
190:                        LOG_ZKB.error("Cannot instanciate class "
191:                                + implClassName + ", will use default", x);
192:                    } catch (InstantiationException x) {
193:                        LOG_ZKB.error("Cannot instanciate class "
194:                                + implClassName + ", will use default", x);
195:                    }
196:                }
197:                if (kb == null)
198:                    kb = new org.mandarax.reference.AdvancedKnowledgeBase();
199:
200:                int ftl = driver.getFaultToleranceLevel();
201:                PrintWriter out = driver.getLogWriter();
202:                List children = e.getChildren();
203:                for (Iterator it = children.iterator(); it.hasNext();) {
204:                    Element child = (Element) it.next();
205:                    Adapter adapter = driver.getAdapter(child.getName());
206:                    if (child.getName().equals(COMPARATOR)) {
207:                        Comparator comp = (Comparator) adapter.importObject(
208:                                child, driver, ops, lfactory);
209:                        if (kb instanceof  ExtendedKnowledgeBase)
210:                            ((ExtendedKnowledgeBase) kb).setComparator(comp);
211:                    } else {
212:                        try {
213:                            Object obj = adapter.importObject(child, driver,
214:                                    ops, lfactory);
215:                            if (obj instanceof  ClauseSet)
216:                                kb.add((ClauseSet) obj);
217:                            else if (obj instanceof  Query)
218:                                kb.addQuery((Query) obj);
219:                            else if (obj instanceof  TestCase)
220:                                kb.addTestCase((TestCase) obj);
221:                            else
222:                                throw new ZKBException(
223:                                        "Can only import clause sets and queries into repository, import for object failed: "
224:                                                + obj);
225:                        } catch (Exception x) {
226:                            String msg = "Problems importing the knowledge base, import failed for element "
227:                                    + e.getText();
228:                            if (ftl == GenericDriver.LOW_FAULT_TOLERANCE)
229:                                throw new ZKBException(msg);
230:                            // do nothing if (ftd==driver.HIGH_FAULT_TOLERANCE)
231:                            else {
232:                                out.println(msg == null ? "null" : msg);
233:                                out.println("See log for details");
234:                                LOG_XKB.error(msg, x);
235:                            }
236:                        }
237:                    }
238:                }
239:                return kb;
240:            }
241:
242:            /**
243:             * Get the name of the associated tag (element).
244:             * @return a string
245:             */
246:            public String getTagName() {
247:                return KNOWLEDGE_BASE;
248:            }
249:
250:            /**
251:             * Get the kind of object the adapter can export/import.
252:             * @return a string
253:             */
254:            public String getKindOfObject() {
255:                return KNOWLEDGE_BASE;
256:            }
257:
258:            /**
259:             * Print the DTD associated with this adapter on a string buffer.
260:             * @param out the buffer to print on. 
261:             */
262:            public void printDTD(StringBuffer out) {
263:                out
264:                        .append("<!ELEMENT knowledgebase (comparator?,(atom|rule|sql_clause_set|custom_clause_set|query)*)>\n");
265:                out.append("<!ATTLIST knowledgebase class CDATA #IMPLIED>\n");
266:            }
267:
268:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.