001: /*
002: * Copyright 2007 Bastian Schenke Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005: * Unless required by applicable law or agreed to in writing, software distributed under the
006: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
007: * either express or implied. See the License for the specific language governing permissions
008: * and limitations under the License.
009: */
010: package nz.org.take.r2ml;
011:
012: import java.lang.reflect.Method;
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import javax.xml.bind.JAXBElement;
017:
018: import nz.org.take.Fact;
019: import nz.org.take.JPredicate;
020: import nz.org.take.Predicate;
021: import nz.org.take.Term;
022: import nz.org.take.r2ml.util.R2MLUtil;
023: import de.tu_cottbus.r2ml.InequalityAtom;
024: import de.tu_cottbus.r2ml.ObjectTerm;
025:
026: /**
027: * @author Bastian Schenke (bastian.schenke@googlemail.com)
028: */
029: class InequalityAtomHandler implements XmlTypeHandler {
030:
031: class InEqualityPair {
032: InEqualityPair(ObjectTerm obj1, ObjectTerm obj2) {
033: this .obj1 = obj1;
034: this .obj2 = obj2;
035: }
036:
037: ObjectTerm obj1 = null;
038: ObjectTerm obj2 = null;
039: }
040:
041: /**
042: * Map an InEqualityAtom to a List of negated equals-Facts
043: *
044: * TODO implement protected Boolean isNegated;
045: *
046: * @param obj
047: * an InequalityAtom
048: * @return a List of negated equals-Facts
049: *
050: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
051: * nz.org.take.r2ml.R2MLDriver)
052: */
053: public Object importObject(Object obj) throws R2MLException {
054: InequalityAtom atom = (InequalityAtom) obj;
055: if (atom.isIsNegated() != null && atom.isIsNegated())
056: throw new R2MLException(
057: "Negation is not yet supported in InEqualityAtoms",
058: R2MLException.FEATURE_NOT_YET_IMPLEMENTED);
059: List<Fact> facts = new ArrayList<Fact>();
060: try {
061: for (InEqualityPair pair : getArgsAsPairs(atom
062: .getObjectTerm())) {
063: facts.add(buildFact(pair, MappingContext.get(),
064: R2MLDriver.get()));
065: }
066: } catch (Exception e) {
067: throw new R2MLException(
068: "Error while handling EqualityAtom!", e);
069: }
070: return facts;
071: }
072:
073: private Fact buildFact(InEqualityPair pair, MappingContext context,
074: R2MLDriver driver) throws R2MLException {
075: XmlTypeHandler handler1 = driver.getHandlerByXmlType(pair.obj1
076: .getClass());
077: XmlTypeHandler handler2 = driver.getHandlerByXmlType(pair.obj2
078: .getClass());
079: Term term1 = (Term) handler1.importObject(pair.obj1);
080: Term term2 = (Term) handler2.importObject(pair.obj2);
081: Fact fact = R2MLUtil.newFact();
082: fact.setTerms(new Term[] { term1, term2 });
083: fact.setPredicate(buildInEqualsPredicate(term1, term2, driver));
084: return fact;
085: }
086:
087: private Predicate buildInEqualsPredicate(Term term1, Term term2,
088: R2MLDriver driver) throws R2MLException {
089: JPredicate jp = new JPredicate();
090: Method method = R2MLUtil.getMethod(EQUALITY_CHECK_METHOD,
091: new Term[] { term1, term2 });
092: jp.setMethod(method);
093: jp.setNegated(true);
094: return jp;
095: }
096:
097: private List<InEqualityPair> getArgsAsPairs(
098: List<JAXBElement<? extends ObjectTerm>> args) {
099: List<InEqualityPair> pairs = new ArrayList<InEqualityPair>(args
100: .size()
101: * (args.size() - 1));
102: for (JAXBElement<? extends ObjectTerm> i : args) {
103: for (JAXBElement<? extends ObjectTerm> j : args) {
104: if (!(i == j))
105: pairs.add(new InEqualityPair(i.getValue(), j
106: .getValue()));
107: }
108: }
109: return pairs;
110: }
111: }
|