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.Collection;
14: import java.util.List;
15:
16: import javax.xml.bind.JAXBElement;
17:
18: import nz.org.take.Fact;
19: import nz.org.take.Prerequisite;
20: import nz.org.take.r2ml.util.R2MLUtil;
21:
22: import de.tu_cottbus.r2ml.Condition;
23: import de.tu_cottbus.r2ml.QfAndOrNafNegFormula;
24: import de.tu_cottbus.r2ml.QfDisjunction;
25:
26: class ConditionHandler implements XmlTypeHandler {
27:
28: /**
29: * Maps a Condition to a List of Lists of Prerequisites.
30: *
31: * The "inner" lists represent disjuncts of the original condition and the
32: * elements are suppossed to be conjuncted Prerequisites. This is neccessary
33: * hence take doesnt support Disjunctions in rule bodies. Each disjunct is
34: * represented by a single take rule with the same head.
35: *
36: * @param obj
37: * a Condition
38: * @return a List of Lists that contain Prerequisites
39: *
40: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
41: * nz.org.take.r2ml.R2MLDriver)
42: */
43: @SuppressWarnings("unchecked")
44: public Object importObject(Object obj) throws R2MLException {
45: R2MLDriver driver = R2MLDriver.get();
46: Condition condition = (Condition) obj;
47: List<List<Prerequisite>> bodies = new ArrayList<List<Prerequisite>>();
48: // normalize condition into DNF
49: List<JAXBElement<? extends QfAndOrNafNegFormula>> formula = driver
50: .getNormalizer().normalize(condition)
51: .getQfAndOrNafNegFormula();
52: MappingContext.get().enter(this );
53: List<Prerequisite> body = null;
54: for (JAXBElement<? extends QfAndOrNafNegFormula> item : formula) {
55: XmlTypeHandler handler = driver.getHandlerByXmlType(item
56: .getValue().getClass());
57: // disjunctions occur only as single toplevel elements, see
58: // Normalizer.normalize()
59: if (item.getValue() instanceof QfDisjunction) {
60: bodies = (List<List<Prerequisite>>) handler
61: .importObject(item.getValue());
62: break;
63: } // if
64: if (body == null) {
65: body = new ArrayList<Prerequisite>();
66: bodies.add(body);
67: } // if
68: if (R2MLUtil.returnsListOfPrerequisites(item.getValue())) {
69: body.addAll((List<? extends Prerequisite>) handler
70: .importObject(item.getValue()));
71: } else if (R2MLUtil.returnsListOfFacts(item.getValue())) {
72: for (Fact fact : (List<Fact>) handler.importObject(item
73: .getValue())) {
74: body.add(R2MLUtil.factAsPrerequisite(fact));
75: } // for
76: } else
77: // if (R2MLUtil.returnsFact(item.getValue())) {
78: body.add((Prerequisite) handler.importObject(item
79: .getValue()));
80: // } else {
81: // driver.logger.warn("Condition dont know the return type of " +
82: // item.getName());
83: // throw new RuntimeException("Condition dont know the return type
84: // of " + item.getName());
85: // }
86: } // for
87: MappingContext.get().leave(this );
88: return bodies;
89: } // importObject()
90:
91: }
|