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.reference;
11:
12: import java.util.ArrayList;
13: import java.util.List;
14:
15: import javax.xml.bind.JAXBElement;
16:
17: import de.tu_cottbus.r2ml.AssociationAtom;
18: import de.tu_cottbus.r2ml.ObjectTerm;
19: import nz.org.take.Fact;
20: import nz.org.take.SimplePredicate;
21: import nz.org.take.Term;
22: import nz.org.take.r2ml.AssociationResolvPolicy;
23: import nz.org.take.r2ml.MappingContext;
24: import nz.org.take.r2ml.R2MLDriver;
25: import nz.org.take.r2ml.R2MLException;
26: import nz.org.take.r2ml.XmlTypeHandler;
27: import nz.org.take.r2ml.util.R2MLUtil;
28:
29: /**
30: *
31: * @author schenke
32: *
33: */
34: public class SimpleAssociationResolvPolicy implements
35: AssociationResolvPolicy {
36:
37: public Fact resolv(AssociationAtom atom) throws R2MLException {
38:
39: R2MLDriver driver = R2MLDriver.get();
40: MappingContext context = MappingContext.get();
41:
42: context.getPredicate(atom.getAssociationPredicateID()
43: .getLocalPart());
44: Fact result = R2MLUtil.newFact();
45: result.setId(atom.getAssociationPredicateID().getLocalPart());
46: // build fact for association
47: List<Term> terms = new ArrayList<Term>();
48: List<Class> termTypes = new ArrayList<Class>();
49: // build all terms and types of terms
50: for (JAXBElement<? extends ObjectTerm> objectTermElement : atom
51: .getObjectArguments().getObjectTerm()) {
52: XmlTypeHandler handler = driver
53: .getHandlerByXmlType(objectTermElement
54: .getDeclaredType());
55: Term argument = null;
56: try {
57: argument = (Term) handler
58: .importObject(objectTermElement.getValue());
59: terms.add(argument);
60: termTypes.add(argument.getType());
61: } catch (ClassCastException e) {
62: throw new R2MLException(
63: "Error while handling ObjectTerms for Association "
64: + atom.getAssociationPredicateID(), e);
65: }
66: }
67: // try to reuse predicate build earlier
68: if (context.getPredicate(atom.getAssociationPredicateID()
69: .getLocalPart()) != null) {
70: result.setPredicate(context.getPredicate(atom
71: .getAssociationPredicateID().getLocalPart()));
72: } else {
73: // build new predicate
74: SimplePredicate simpleAssociationPredicate = new SimplePredicate();
75: simpleAssociationPredicate.setName(atom
76: .getAssociationPredicateID().getLocalPart());
77: simpleAssociationPredicate
78: .setNegated(atom.isIsNegated() == null ? false
79: : atom.isIsNegated());
80: simpleAssociationPredicate.setSlotNames(driver
81: .getNameMapper().getSlotNames(
82: atom.getAssociationPredicateID()));
83: simpleAssociationPredicate.setSlotTypes(termTypes
84: .toArray(new Class[] {}));
85: result.setPredicate(simpleAssociationPredicate);
86: }
87: // apply terms
88: result.setTerms(terms.toArray(new Term[] {}));
89: return result;
90: }
91:
92: }
|