Source Code Cross Referenced for DerivationRuleHandler.java in  » Rule-Engine » take » nz » org » take » r2ml » 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 » take » nz.org.take.r2ml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2007 Bastian Schenke Licensed under the Apache License, Version 2.0 (the "License"); 
003:         * you may not use this file except in compliance with the License. 
004:         * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
005:         * Unless required by applicable law or agreed to in writing, software distributed under the 
006:         * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
007:         * either express or implied. See the License for the specific language governing permissions 
008:         * and limitations under the License.
009:         */
010:        package nz.org.take.r2ml;
011:
012:        import java.util.ArrayList;
013:        import java.util.HashMap;
014:        import java.util.List;
015:        import java.util.Map;
016:
017:        import de.tu_cottbus.r2ml.Atom;
018:        import de.tu_cottbus.r2ml.Conclusion;
019:        import nz.org.take.DerivationRule;
020:        import nz.org.take.Fact;
021:        import nz.org.take.Prerequisite;
022:
023:        /**
024:         * @author Bastian Schenke (bastian.schenke@googlemail.com)
025:         * 
026:         */
027:        class DerivationRuleHandler implements  XmlTypeHandler {
028:
029:            /**
030:             * Map a de.tu_cottbus.r2ml.DerivationRule to a List of
031:             * nz.ac.take.DerivationRule. If the body of the Rule contains a Disjunction
032:             * it is splitted into two or more separated rules (take does not supprt
033:             * disjunctions in the body).
034:             * 
035:             * @param obj
036:             *            a r2ml.DerivationRule object
037:             * @return List<nz.ac.take.DerivationRule> one rule for each disjunct in the condition
038:             * 
039:             * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
040:             *      nz.org.take.r2ml.R2MLDriver)
041:             */
042:            public List<DerivationRule> importObject(Object obj)
043:                    throws R2MLException {
044:                R2MLDriver driver = R2MLDriver.get();
045:                MappingContext context = MappingContext.get();
046:
047:                // R2ML DerivationRule
048:                de.tu_cottbus.r2ml.DerivationRule xDRule = (de.tu_cottbus.r2ml.DerivationRule) obj;
049:
050:                context.enter(this , xDRule.getRuleID());
051:
052:                try {
053:
054:                    if (driver.logger.isDebugEnabled())
055:                        driver.logger.debug("mapping rule "
056:                                + xDRule.getRuleID() != null ? xDRule
057:                                .getRuleID() : xDRule.toString());
058:                    // take DerivationRule
059:                    List<DerivationRule> dRules = new ArrayList<DerivationRule>();
060:
061:                    // Documentation
062:                    Map<String, String> documentation = extractDocumentation(
063:                            xDRule, context, driver);
064:                    Fact conclusion = extractHead(xDRule, context, driver);
065:
066:                    try {
067:                        int i = 0;
068:                        List<List<Prerequisite>> body = extractBody(xDRule,
069:                                context, driver);
070:                        for (List<Prerequisite> condition : body) {
071:                            DerivationRule dRule = new DerivationRule();
072:                            // Condition
073:                            dRule.setBody(condition);
074:                            // Conclusion
075:                            dRule.setHead(conclusion);
076:                            // set RuleID
077:
078:                            if (xDRule.getRuleID() != null) {
079:                                if (body.size() > 1)
080:                                    dRule.setId(xDRule.getRuleID() + '_' + i++);
081:                                else
082:                                    dRule.setId(xDRule.getRuleID());
083:                            } else {
084:                                if (body.size() > 1)
085:                                    dRule.setId(dRule.getHead().getPredicate()
086:                                            .getName()
087:                                            + "_" + i++);
088:                                else
089:                                    dRule.setId(dRule.getHead().getPredicate()
090:                                            .getName());
091:                            }
092:                            //System.out.println(xDRule.getRuleID());
093:                            //System.out.println(dRule.getId());
094:                            // add documentation
095:                            dRule.addAnnotations(documentation);
096:                            dRules.add(dRule);
097:                        } // for
098:                    } catch (R2MLException e) {
099:                        context.cleanUpToHandler(this );
100:                        throw new R2MLException("Error in rule \""
101:                                + xDRule.getRuleID() + "\": " + e.getMessage(),
102:                                e);
103:                    } // try catch
104:
105:                    context.leave(this , xDRule.getRuleID());
106:                    //			System.out.println("leaving rule " + xDRule.getRuleID());
107:                    return dRules;
108:                } catch (RuntimeException e) {
109:                    R2MLDriver.get().logger
110:                            .warn("Exception occured while handling rule "
111:                                    + xDRule.getRuleID());
112:                    e.printStackTrace();
113:                    MappingContext.get().leave(this , xDRule.getRuleID());
114:                    return new ArrayList<DerivationRule>();
115:                }
116:            }
117:
118:            @SuppressWarnings("unchecked")
119:            private Map<String, String> extractDocumentation(
120:                    de.tu_cottbus.r2ml.DerivationRule rule,
121:                    MappingContext context, R2MLDriver driver)
122:                    throws R2MLException {
123:
124:                Map<String, String> docAnnotations = null;
125:                XmlTypeHandler handler = null;
126:                try {
127:                    handler = driver.getHandlerByXmlType(rule
128:                            .getDocumentation().getClass());
129:                    docAnnotations = (Map<String, String>) handler
130:                            .importObject(rule.getDocumentation());
131:                } catch (NullPointerException e) {
132:                    driver.logger.info("No documentation element for rule "
133:                            + (rule.getRuleID() != null ? rule.getRuleID()
134:                                    : rule.toString()));
135:                    docAnnotations = new HashMap<String, String>();
136:                }
137:                return docAnnotations;
138:            }
139:
140:            private Fact extractHead(de.tu_cottbus.r2ml.DerivationRule rule,
141:                    MappingContext context, R2MLDriver driver)
142:                    throws R2MLException {
143:                Fact head = null;
144:
145:                Conclusion conclusion = rule.getConclusion();
146:                XmlTypeHandler handler = driver.getHandlerByXmlType(conclusion
147:                        .getClass());
148:                head = (Fact) handler.importObject(conclusion);
149:                return head;
150:            }
151:
152:            @SuppressWarnings("unchecked")
153:            private List<List<Prerequisite>> extractBody(
154:                    de.tu_cottbus.r2ml.DerivationRule rule,
155:                    MappingContext context, R2MLDriver driver)
156:                    throws R2MLException {
157:                List<List<Prerequisite>> ruleBodies = null;
158:                XmlTypeHandler handler = driver.getHandlerByXmlType(rule
159:                        .getConditions().getClass());
160:                ruleBodies = (List<List<Prerequisite>>) handler
161:                        .importObject(rule.getConditions());
162:                return ruleBodies;
163:            }
164:
165:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.