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 javax.xml.bind.JAXBElement;
16:
17: import nz.org.take.Fact;
18: import nz.org.take.SimplePredicate;
19: import nz.org.take.r2ml.util.R2MLUtil;
20:
21: import de.tu_cottbus.r2ml.GenericAtom;
22:
23: /**
24: * @author Bastian Schenke (bastian.schenke@googlemail.com)
25: *
26: */
27: class GenericAtomHandler implements XmlTypeHandler {
28:
29: /**
30: * Maps a GenericAtom to a Fact.
31: *
32: * TODO implement protected String predicateType;
33: *
34: * @param obj
35: * a GenericAtom containing a Atom
36: * @return Fact representing the GenericAtom
37: *
38: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
39: * nz.org.take.r2ml.R2MLDriver)
40: */
41: public Object importObject(Object obj) throws R2MLException {
42: GenericAtom genAtom = (GenericAtom) obj;
43: R2MLDriver driver = R2MLDriver.get();
44: MappingContext context = MappingContext.get();
45: List<JAXBElement<? extends de.tu_cottbus.r2ml.Term>> r2mlArgs = genAtom
46: .getArguments().getTerm();
47: Fact takeFact = R2MLUtil.newFact();
48: if (context.getPredicate(genAtom.getPredicateID().toString()) == null) {
49: SimplePredicate takePredicate = new SimplePredicate();
50: takePredicate.setName(genAtom.getPredicateID().toString());
51: List<nz.org.take.Term> takeTerms = new ArrayList<nz.org.take.Term>();
52: takePredicate.setSlotNames(driver.getNameMapper()
53: .getSlotNames(genAtom.getPredicateID()));
54: List<Class> takeSlotTypes = new ArrayList<Class>();
55: for (JAXBElement<? extends de.tu_cottbus.r2ml.Term> argElem : r2mlArgs) {
56: de.tu_cottbus.r2ml.Term r2mlTerm = argElem.getValue();
57: XmlTypeHandler handler = driver
58: .getHandlerByXmlType(r2mlTerm.getClass());
59: nz.org.take.Term takeArg = (nz.org.take.Term) handler
60: .importObject(r2mlTerm);
61: takeTerms.add(takeArg);
62: takeSlotTypes.add(takeArg.getType());
63: }
64: takePredicate.setSlotTypes(takeSlotTypes
65: .toArray(new Class[0]));
66: if (genAtom.isIsNegated() != null)
67: takePredicate.setNegated(genAtom.isIsNegated());
68: takeFact.setPredicate(takePredicate);
69: takeFact.setTerms(takeTerms
70: .toArray(new nz.org.take.Term[0]));
71: } else {
72: takeFact.setPredicate(context.getPredicate(genAtom
73: .getPredicateID().toString()));
74: }
75: return takeFact;
76: }
77: }
|