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.util;
011:
012: import java.beans.BeanInfo;
013: import java.beans.Introspector;
014: import java.beans.PropertyDescriptor;
015: import java.lang.reflect.Method;
016: import java.lang.reflect.Modifier;
017:
018: import javax.xml.bind.JAXBElement;
019:
020: import de.tu_cottbus.r2ml.Atom;
021: import de.tu_cottbus.r2ml.AttributeFunctionTerm;
022: import de.tu_cottbus.r2ml.DataTerm;
023: import de.tu_cottbus.r2ml.DatatypePredicateAtom;
024: import de.tu_cottbus.r2ml.EqualityAtom;
025: import de.tu_cottbus.r2ml.GenericAtom;
026: import de.tu_cottbus.r2ml.InequalityAtom;
027: import de.tu_cottbus.r2ml.QfConjunction;
028: import de.tu_cottbus.r2ml.TypedLiteral;
029:
030: import nz.org.take.Fact;
031: import nz.org.take.Prerequisite;
032: import nz.org.take.Term;
033: import nz.org.take.r2ml.MappingContext;
034: import nz.org.take.r2ml.R2MLDriver;
035: import nz.org.take.r2ml.R2MLException;
036:
037: /**
038: * @author Bastian Schenke (bastian.schenke@googlemail.com)
039: *
040: */
041: public class R2MLUtil {
042:
043: public static Fact newFact() {
044: return MappingContext.get().isInsideCondition() ? new Prerequisite()
045: : new Fact();
046:
047: }
048:
049: public static Prerequisite factAsPrerequisite(Fact fact) {
050:
051: if (fact instanceof Prerequisite)
052: return (Prerequisite) fact;
053: Prerequisite prereq = new Prerequisite();
054: prereq.addAnnotations(fact.getAnnotations());
055: prereq.setId(fact.getId());
056: prereq.setPredicate(fact.getPredicate());
057: prereq.setTerms(fact.getTerms());
058: return prereq;
059:
060: }
061:
062: public static Fact PrerequisiteAsFact(Prerequisite prereq)
063: throws R2MLException {
064: Fact fact = new Fact();
065: fact.setId(prereq.getId());
066: fact.setPredicate(prereq.getPredicate());
067: fact.setTerms(prereq.getTerms());
068: return fact;
069: }
070:
071: /**
072: * @param name
073: * @param terms
074: * @return
075: * @throws R2MLException
076: */
077: public static Method getMethod(String name, Term[] terms)
078: throws R2MLException {
079: Class<?> clazz = terms[0].getType();
080: Class[] paramTypes = new Class[terms.length - 1];
081: Method m = null;
082:
083: // get parameter
084: for (int i = 1; i < terms.length; i++) {
085: paramTypes[i - 1] = terms[i].getType();
086: }
087: // find method
088: try {
089: m = clazz.getMethod(name, paramTypes);
090: } catch (Exception x) {
091: }
092: if (m == null) {
093: // start investigating supertypes
094: Method[] methods = clazz.getMethods();
095: for (Method m1 : methods) {
096: if (m1.getReturnType() == Boolean.TYPE
097: && Modifier.isPublic(m1.getModifiers())) {
098: if (m1.getName().equals(name)
099: && m1.getParameterTypes().length == paramTypes.length) {
100: // check types
101: boolean ok = true;
102: for (int i = 0; i < paramTypes.length; i++) {
103: ok = ok
104: && m1.getParameterTypes()[i]
105: .isAssignableFrom(paramTypes[i]);
106: }
107: if (ok) {
108: m = m1;
109: break;
110: }
111: }
112: }
113: }
114: }
115: return m;
116:
117: }
118:
119: public static boolean returnsListOfPrerequisites(Object formula) {
120: if (formula instanceof QfConjunction)
121: return true;
122: else
123: return false;
124: }
125:
126: public static boolean returnsListOfFacts(Object formula) {
127: if (formula instanceof EqualityAtom)
128: return true;
129: else if (formula instanceof EqualityAtom)
130: return true;
131: else if (formula instanceof InequalityAtom)
132: return true;
133: else
134: return false;
135: }
136:
137: public static boolean returnsFact(Object formula) {
138: if (formula instanceof GenericAtom)
139: return true;
140: return false;
141: }
142:
143: public static PropertyDescriptor buildProperty(String name,
144: Class clazz) throws R2MLException {
145: try {
146: BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
147: PropertyDescriptor[] properties = beanInfo
148: .getPropertyDescriptors();
149: for (PropertyDescriptor property : properties) {
150: if (name.equals(property.getName())
151: && property.getReadMethod() != null) {
152: return property;
153: }
154: }
155: } catch (Exception e) {
156: }
157: throw new R2MLException("No java property found for property "
158: + name + " in class " + clazz.getCanonicalName());
159: }
160:
161: public static boolean isNegated(Atom atom) {
162: return atom.isIsNegated() == null ? false : atom.isIsNegated();
163: }
164:
165: /**
166: * Checks if the given DatatypePredicateAtom contains an
167: * AttributioFunctionTerm and a TypedLiteral. STRELKA
168: *
169: * @param atom
170: * @return true - if the atom contains a boolean TypedLiteral and an
171: * AttributionFunctionTerm as DataArguments and is an equal or
172: * notequal comparison, false - otherwise
173: */
174: public static boolean isBooleanPredicate(DatatypePredicateAtom atom) {
175: try {
176: String symbol = DataPredicates.getComparisonSymbol(atom
177: .getDatatypePredicateID());
178: // equal or unequal?
179: if ("==".equals(symbol) || "!=".equals(symbol)) {
180: JAXBElement<? extends DataTerm> arg0 = atom
181: .getDataArguments().getDataTerm().get(0);
182: JAXBElement<? extends DataTerm> arg1 = atom
183: .getDataArguments().getDataTerm().get(1);
184: TypedLiteral tl = null;
185:
186: // a TypedLiteral and an AttributeFunctionTerm as Arguments?
187: if (arg0.getDeclaredType() == TypedLiteral.class
188: && arg1.getDeclaredType() == AttributeFunctionTerm.class) {
189: tl = (TypedLiteral) arg0.getValue();
190:
191: } else if (arg1.getDeclaredType() == TypedLiteral.class
192: && arg0.getDeclaredType() == AttributeFunctionTerm.class) {
193: tl = (TypedLiteral) arg1.getValue();
194: }
195:
196: Class type = R2MLDriver.get().getDatatypeMapper()
197: .getType(tl.getDatatypeID());
198: if (Boolean.class == type || Boolean.TYPE == type) {
199: return true;
200: }
201: }
202:
203: } catch (Exception e) {
204: //e.printStackTrace();
205: return false;
206: }
207: return false;
208: }
209:
210: }
|