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