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.beans.PropertyDescriptor;
13:
14: import nz.org.take.Fact;
15: import nz.org.take.JPredicate;
16: import nz.org.take.Predicate;
17: import nz.org.take.Term;
18: import nz.org.take.r2ml.util.R2MLUtil;
19: import de.tu_cottbus.r2ml.ObjectClassificationAtom;
20: import de.tu_cottbus.r2ml.ObjectTerm;
21: import de.tu_cottbus.r2ml.ObjectVariable;
22:
23: /**
24: * @author Bastian Schenke (bastian.schenke@googlemail.com)
25: */
26: class ObjectClassificationAtomHandler extends AbstractPropertyHandler {
27:
28: /**
29: * Maps an <code>ObjectClassificationAtom</code> to a take <code>JPredicate</code>.
30: *
31: * <p>
32: * In R2ML <code>ObjectClassificationAtoms</code> are sometimes used for
33: * variabledeclaration. In that case an <code>ObjectClassificationAtom</code>
34: * contains an <code>ObjectVariable</code> wihtout a <code>classID</code>
35: * attribute as <code>ObjectTerm</code>. This <code>classID</code>
36: * attribute is then set to the value of the <code>classID</code> attribute
37: * of the <code>ObjectClassificationAtom</code>.
38: * </p>
39: *
40: * @param obj hopefully an ObjectClassificationAtom
41: *
42: * @return a take Fact with a JPredicate
43: *
44: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
45: * nz.org.take.r2ml.R2MLDriver)
46: */
47: public Object importObject(Object obj) throws R2MLException {
48: ObjectClassificationAtom atom = (ObjectClassificationAtom) obj;
49: MappingContext context = MappingContext.get();
50:
51: ObjectTerm term = atom.getObjectTerm().getValue();
52: if (term instanceof ObjectVariable
53: && ((ObjectVariable) term).getClassID() == null) {
54: ((ObjectVariable) term).setClassID(atom.getClassID());
55: }
56:
57: XmlTypeHandler handler = R2MLDriver.get().getHandlerByXmlType(
58: term.getClass());
59: // takeTerm
60: Term tTerm = (Term) handler.importObject(term);
61:
62: String localName = atom.getClassID().getLocalPart();
63: // build Predicate for classificationcheck
64: Predicate p = context.getPredicate(localName);
65: if (p == null) {
66: JPredicate jp = new JPredicate();
67: StringBuffer name = new StringBuffer(localName.substring(0,
68: 1).toLowerCase());
69: name.append(localName.substring(1));
70: Class type = R2MLDriver.get().getDatatypeMapper().getType(
71: atom.getClassID());
72: PropertyDescriptor prop = AbstractPropertyHandler
73: .buildProperty(name.toString(), type);
74: if (prop == null) {
75: R2MLDriver.get().logger
76: .warn("Error in domain model, missing classification predicate method is"
77: + type.getSimpleName());
78: throw new R2MLException(
79: "Error in domain model, missing classification predicate method is"
80: + type.getSimpleName());
81: }
82: jp.setMethod(prop.getReadMethod());
83: if (R2MLDriver.get().logger.isDebugEnabled())
84: R2MLDriver.get().logger.debug(name.toString() + " : "
85: + tTerm.toString());
86: jp.setNegated(R2MLUtil.isNegated(atom));
87: jp.setSlotNames(R2MLDriver.get().getNameMapper()
88: .getSlotNames(atom.getClassID()));
89: context.addPredicate(jp);
90: p = jp;
91: }
92: Fact fact = R2MLUtil.newFact();
93: fact.setId(p.getName());
94: fact.setPredicate(p);
95: fact.setTerms(new Term[] { tTerm });
96: return fact;
97: }
98:
99: }
|