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 nz.org.take.Comparison;
013: import nz.org.take.Fact;
014: import nz.org.take.JPredicate;
015: import nz.org.take.TakeException;
016: import nz.org.take.Term;
017: import nz.org.take.r2ml.util.DataPredicates;
018: import nz.org.take.r2ml.util.R2MLUtil;
019: import de.tu_cottbus.r2ml.AttributeFunctionTerm;
020: import de.tu_cottbus.r2ml.AttributionAtom;
021: import de.tu_cottbus.r2ml.DatatypePredicateAtom;
022: import de.tu_cottbus.r2ml.ObjectTerm;
023: import de.tu_cottbus.r2ml.TypedLiteral;
024:
025: /**
026: * @author Bastian Schenke (bastian.schenke@googlemail.com) TODO implement this
027: * Handler
028: */
029: class DatatypePredicateAtomHandler implements XmlTypeHandler {
030:
031: /**
032: * Map a DatatypePredicateAtom to a Comparison.
033: *
034: * @param obj
035: * a
036: * @return a
037: *
038: * @see nz.org.take.r2ml.XmlTypeHandler#importObject(java.lang.Object,
039: * nz.org.take.r2ml.R2MLDriver)
040: */
041: public Object importObject(Object obj) throws R2MLException {
042: DatatypePredicateAtom atom = (DatatypePredicateAtom) obj;
043:
044: if (atom.getDataArguments().getDataTerm().size() != 2) {
045: throw new R2MLException(
046: "DatatypePredicates are supported only for exactly two arguments.");
047: }
048:
049: Fact fact = R2MLUtil.newFact();
050:
051: String symbol = DataPredicates.getComparisonSymbol(atom
052: .getDatatypePredicateID());
053:
054: if (R2MLUtil.isBooleanPredicate(atom)) {
055: buildAsJPredicate(atom, fact, symbol);
056: return fact;
057: }
058:
059: Term arg0 = null;
060: Term arg1 = null;
061:
062: XmlTypeHandler h0 = R2MLDriver.get().getHandlerByXmlType(
063: atom.getDataArguments().getDataTerm().get(0)
064: .getDeclaredType());
065: XmlTypeHandler h1 = R2MLDriver.get().getHandlerByXmlType(
066: atom.getDataArguments().getDataTerm().get(1)
067: .getDeclaredType());
068: arg1 = (Term) h0.importObject(atom.getDataArguments()
069: .getDataTerm().get(0).getValue());
070: arg0 = (Term) h1.importObject(atom.getDataArguments()
071: .getDataTerm().get(1).getValue());
072:
073: Comparison c;
074: try {
075: c = new Comparison(symbol);
076: } catch (TakeException e) {
077: throw new R2MLException("Unable to create Comparison.", e);
078: }
079: if ((Number.class.isAssignableFrom(arg0.getType()) && Number.class
080: .isAssignableFrom(arg1.getType()))
081: || (arg0.getType().isPrimitive()
082: && arg1.getType().isPrimitive()
083: && arg0.getType() != Boolean.class && arg1
084: .getType() != Boolean.class)) {
085: c.setNegated(R2MLUtil.isNegated(atom));
086: c.setTypes(new Class[] { arg1.getType(), arg0.getType() });
087: c.setSlotNames(R2MLDriver.get().getNameMapper()
088: .getSlotNames(atom.getDatatypePredicateID()));
089: fact.setId(c.getName());
090: fact.setPredicate(c);
091: } else if ((symbol.equals("==") || symbol.equals("!="))
092: && ((arg0.getType() == Boolean.class && arg1.getType() == Boolean.class) || (arg0
093: .getType() == String.class && arg1.getType() == String.class))) {
094: c.setNegated(R2MLUtil.isNegated(atom));
095: c.setTypes(new Class[] { arg1.getType(), arg0.getType() });
096: c.setSlotNames(R2MLDriver.get().getNameMapper()
097: .getSlotNames(atom.getDatatypePredicateID()));
098: fact.setId(c.getName());
099: fact.setPredicate(c);
100: // JPredicate jp = new JPredicate();
101: // jp.setNegated(symbol.equals("==") ? false : true);
102: // jp.setMethod(R2MLUtil.getMethod(EQUALITY_CHECK_METHOD, new Term[]
103: // {
104: // arg0, arg1 }));
105: // fact.setId(jp.getName());
106: // fact.setPredicate(jp);
107: } else {
108: throw new R2MLException("Unable to create Comparison.");
109: }
110:
111: fact.setTerms(new Term[] { arg0, arg1 });
112:
113: return fact;
114: }
115:
116: /**
117: * @param atom
118: * @param fact
119: * @param symbol
120: * @throws R2MLException
121: */
122: private void buildAsJPredicate(DatatypePredicateAtom atom,
123: Fact fact, String symbol) throws R2MLException {
124: TypedLiteral tl = null;
125: AttributeFunctionTerm aft = null;
126: ObjectTerm objT = null;
127: if (atom.getDataArguments().getDataTerm().get(0)
128: .getDeclaredType() == TypedLiteral.class) {
129: aft = (AttributeFunctionTerm) (atom.getDataArguments()
130: .getDataTerm().get(1).getValue());
131: tl = (TypedLiteral) (atom.getDataArguments().getDataTerm()
132: .get(0).getValue());
133: objT = aft.getContextArgument().getObjectTerm().getValue();
134: } else {
135: aft = (AttributeFunctionTerm) (atom.getDataArguments()
136: .getDataTerm().get(0).getValue());
137: tl = (TypedLiteral) (atom.getDataArguments().getDataTerm()
138: .get(1).getValue());
139: objT = aft.getContextArgument().getObjectTerm().getValue();
140: }
141: // value of the typed literal
142: boolean tlv = new Boolean(tl.getLexicalValue());
143: XmlTypeHandler handler = R2MLDriver.get().getHandlerByXmlType(
144: aft.getContextArgument().getObjectTerm()
145: .getDeclaredType());
146: Term domain = (Term) handler.importObject(objT);
147:
148: String[] slotNames = null;//R2MLDriver.get().getNameMapper().getSlotNames(atom.getDatatypePredicateID());
149: fact.setPredicate(AttributionAtomHandler.buildJPredciate(
150: domain, aft.getAttributeID().getLocalPart(), isNegated(
151: atom, symbol, tlv), slotNames));
152: fact.setTerms(new Term[] { domain });
153: }
154:
155: private boolean isNegated(DatatypePredicateAtom atom,
156: String symbol, boolean tlv) {
157: boolean negated = R2MLUtil.isNegated(atom);
158: if (!tlv) {
159: // flip negated if comparing to false
160: negated = negated ? false : true;
161: }
162: if (symbol.equals("!=")) {
163: // flip negated if the symbol is not-equal
164: negated = negated ? false : true;
165: }
166: return negated;
167: }
168:
169: }
|