01: /*
02: * Copyright 2007 Bastian Schenke Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */
10: package nz.org.take.r2ml;
11:
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: import de.tu_cottbus.r2ml.DerivationRuleSet;
16: import de.tu_cottbus.r2ml.Term;
17: import nz.org.take.DerivationRule;
18: import nz.org.take.KnowledgeElement;
19:
20: /**
21: * @author Bastian Schenke (bastian.schenke@googlemail.com)
22: *
23: */
24: class DerivationRuleSetHandler implements XmlTypeHandler {
25:
26: /**
27: * Maps a DerivationRuleSet to a List containing KnowledgeElements
28: * where each rule is transformed into one KnowledgeElement.
29: *
30: * @param obj
31: * a DerivationRuleSet
32: * @return List<KnowledgeElement> containing all rules of the
33: * DerivationRuleSet obj
34: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
35: * nz.org.take.r2ml.R2MLDriver)
36: */
37: public Object importObject(Object obj) throws R2MLException {
38:
39: R2MLDriver driver = R2MLDriver.get();
40: MappingContext.get().enter(this );
41:
42: List<KnowledgeElement> ret = new ArrayList<KnowledgeElement>();
43:
44: DerivationRuleSet dRuleSet = (DerivationRuleSet) obj;
45:
46: for (Term term : dRuleSet
47: .getGenericVariableOrObjectVariableOrDataVariable()) {
48: XmlTypeHandler handler = driver.getHandlerByXmlType(term
49: .getClass());
50: // the handler is responsible for registering the variable to the context
51: handler.importObject(term);
52: }
53: XmlTypeHandler dRuleHandler = driver
54: .getHandlerByXmlType(de.tu_cottbus.r2ml.DerivationRule.class);
55: // mapping the ruleSetId to annotations of each rule
56: String id = dRuleSet.getRuleSetID();
57: for (de.tu_cottbus.r2ml.DerivationRule derivationRule : dRuleSet
58: .getDerivationRule()) {
59: try {
60: for (Object ruleObj : (List) dRuleHandler
61: .importObject(derivationRule)) {
62: DerivationRule rule = (DerivationRule) ruleObj;
63: if (id != null)
64: rule.addAnnotation(RULE_SET_ID_KEY, id);
65: ret.add(rule);
66: } // for
67: } catch (R2MLException e) {
68: driver.logger.info(e.getMessage());
69: driver.logger
70: .warn("Error while importing DerivationRuleSet "
71: + id + "." + e.getLocalizedMessage());
72: e.printStackTrace();
73: } catch (ClassCastException cce) {
74: MappingContext.get().cleanUpToHandler(this );
75: throw new R2MLException(
76: "Error while processing DerivationRuleSet "
77: + id + "", cce,
78: R2MLException.CLASS_CAST_ERROR);
79: } // try-catch
80: }
81: MappingContext.get().leave(this);
82: return ret;
83: }
84:
85: }
|