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 javax.xml.bind.JAXBElement;
13:
14: import org.apache.log4j.Logger;
15:
16: import nz.org.take.BinaryArithmeticFunction;
17: import nz.org.take.ComplexTerm;
18: import nz.org.take.Term;
19: import nz.org.take.r2ml.util.Functions;
20: import de.tu_cottbus.r2ml.DataTerm;
21: import de.tu_cottbus.r2ml.DatatypeFunctionTerm;
22:
23: /**
24: * @author Bastian Schenke (bastian.schenke(at)googlemail.com)
25: *
26: */
27: public class DatatypeFunctionTermHandler implements XmlTypeHandler {
28:
29: /**
30: *
31: * @param obj a DatatypeFunctionTerm
32: * @return a
33: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object)
34: */
35: public Object importObject(Object obj) throws R2MLException {
36: Logger logger = R2MLDriver.get().logger;
37: DatatypeFunctionTerm r2mlTerm = (DatatypeFunctionTerm) obj;
38: R2MLDriver driver = R2MLDriver.get();
39: ComplexTerm term = new ComplexTerm();
40: Term[] args = new Term[r2mlTerm.getDataArguments()
41: .getDataTerm().size()];
42:
43: int i = 0;
44: for (JAXBElement<? extends DataTerm> argElement : r2mlTerm
45: .getDataArguments().getDataTerm()) {
46: DataTerm dTerm = argElement.getValue();
47: XmlTypeHandler handler = driver.getHandlerByXmlType(dTerm
48: .getClass());
49: args[i++] = (Term) handler.importObject(dTerm);
50: }
51: term.setTerms(args);
52:
53: // set function here
54: if (r2mlTerm.getDataArguments().getDataTerm().size() == 2) {
55:
56: String name = Functions.getArithmeticFunctionName(r2mlTerm
57: .getDatatypeFunctionID());
58:
59: if (logger.isDebugEnabled())
60: logger.debug("function symbol for "
61: + r2mlTerm.getDatatypeFunctionID()
62: .getLocalPart() + " is " + name);
63:
64: BinaryArithmeticFunction f = BinaryArithmeticFunction
65: .getInstance(name, args[0].getType(), args[1]
66: .getType());
67: if (f == null || f.getName() == null
68: || f.getName().isEmpty()) {
69: throw new R2MLException("Unknown function-type: "
70: + r2mlTerm.getDatatypeFunctionID() + "!");
71: }
72: term.setFunction(f);
73:
74: } else {
75: throw new R2MLException(
76: "DatatypeFuntionTerms are supported only with 2 or more arguments ().",
77: R2MLException.FEATURE_NOT_SUPPORTED);
78: }
79:
80: driver.logger.debug("BinaryArithmeticFunction created: "
81: + term.getFunction().toString());
82:
83: return term;
84: }
85:
86: private BinaryArithmeticFunction buildBinaryArithmeticFunction(
87: Term arg1, Term arg2) {
88: return null;
89: }
90:
91: }
|