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.Collection;
13: import java.util.Date;
14: import java.util.List;
15:
16: import javax.xml.bind.JAXBElement;
17:
18: import nz.org.take.DefaultKnowledgeBase;
19: import nz.org.take.DerivationRule;
20: import nz.org.take.KnowledgeBase;
21: import nz.org.take.KnowledgeElement;
22: import nz.org.take.r2ml.util.R2MLConstants;
23: import de.tu_cottbus.r2ml.RuleBase;
24: import de.tu_cottbus.r2ml.RuleSet;
25:
26: /**
27: * @author Bastian Schenke (bastian.schenke@googlemail.com)
28: *
29: */
30: class RuleBaseHandler implements XmlTypeHandler {
31:
32: KnowledgeBase kb = null;
33:
34: /*
35: * (non-Javadoc)
36: *
37: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
38: * nz.org.take.r2ml.R2MLDriver)
39: */
40: @SuppressWarnings("unchecked")
41: public Object importObject(Object obj) throws R2MLException {
42: kb = new DefaultKnowledgeBase();
43: R2MLDriver driver = R2MLDriver.get();
44: MappingContext context = MappingContext.get();
45: RuleBase ruleBase = (RuleBase) obj;
46: context.enter(this );
47: kb = new DefaultKnowledgeBase();
48: kb.addAnnotation(R2MLConstants.ANNOTATION_KEY_AUTHOR, System
49: .getProperty("user.name"));
50: kb.addAnnotation(R2MLConstants.ANNOTATION_KEY_CREATOR,
51: driver.ID);
52: kb.addAnnotation(R2MLConstants.ANNOTATION_KEY_DATE, new Date()
53: .toString());
54:
55: for (JAXBElement<? extends RuleSet> ruleSet : ruleBase
56: .getRuleSet()) {
57: XmlTypeHandler ruleSetHandler = null;
58: ruleSetHandler = driver.getHandlerByXmlType(ruleSet
59: .getValue().getClass());
60: Collection<KnowledgeElement> rules = null;
61: rules = (List<KnowledgeElement>) ruleSetHandler
62: .importObject(ruleSet.getValue());
63: for (KnowledgeElement rule : rules) {
64: kb.add(rule);
65: }
66: }
67: // KnowledgeBase retKB = kb;
68: // kb=null;
69: context.leave(this );
70:
71: return kb;
72: }
73:
74: void addRuleToKB(DerivationRule rule) {
75: kb.add(rule);
76: }
77:
78: }
|