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.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.Map;
017:
018: import nz.org.take.KnowledgeBase;
019: import nz.org.take.r2ml.reference.AssociationAsReferenceResolvPolicy;
020: import nz.org.take.r2ml.reference.CheckOnlyNormalizer;
021: import nz.org.take.r2ml.reference.DefaultDatatypeMapper;
022: import nz.org.take.r2ml.reference.DefaultNameMapper;
023: import nz.org.take.r2ml.util.ReplacePropertyFunctionTermFilter;
024: import nz.org.take.r2ml.util.RuleBaseFilter;
025: import nz.org.take.r2ml.util.TypeVariablesFilter;
026:
027: import org.apache.commons.jxpath.JXPathContext;
028: import org.apache.log4j.Level;
029: import org.apache.log4j.Logger;
030:
031: import de.tu_cottbus.r2ml.AssociationAtom;
032: import de.tu_cottbus.r2ml.AttributeFunctionTerm;
033: import de.tu_cottbus.r2ml.AttributionAtom;
034: import de.tu_cottbus.r2ml.Conclusion;
035: import de.tu_cottbus.r2ml.Condition;
036: import de.tu_cottbus.r2ml.Conjunction;
037: import de.tu_cottbus.r2ml.DataClassificationAtom;
038: import de.tu_cottbus.r2ml.DataValue;
039: import de.tu_cottbus.r2ml.DataVariable;
040: import de.tu_cottbus.r2ml.DatatypeFunctionTerm;
041: import de.tu_cottbus.r2ml.DatatypePredicateAtom;
042: import de.tu_cottbus.r2ml.DerivationRule;
043: import de.tu_cottbus.r2ml.DerivationRuleSet;
044: import de.tu_cottbus.r2ml.Disjunction;
045: import de.tu_cottbus.r2ml.Documentation;
046: import de.tu_cottbus.r2ml.EqualityAtom;
047: import de.tu_cottbus.r2ml.GenericAtom;
048: import de.tu_cottbus.r2ml.InequalityAtom;
049: import de.tu_cottbus.r2ml.IntegrityRuleSet;
050: import de.tu_cottbus.r2ml.Negation;
051: import de.tu_cottbus.r2ml.NegationAsFailure;
052: import de.tu_cottbus.r2ml.ObjectClassificationAtom;
053: import de.tu_cottbus.r2ml.ObjectDescriptionAtom;
054: import de.tu_cottbus.r2ml.ObjectVariable;
055: import de.tu_cottbus.r2ml.PlainLiteral;
056: import de.tu_cottbus.r2ml.ProductionRuleSet;
057: import de.tu_cottbus.r2ml.PropertyAtom;
058: import de.tu_cottbus.r2ml.QfConjunction;
059: import de.tu_cottbus.r2ml.QfDisjunction;
060: import de.tu_cottbus.r2ml.ReactionRuleSet;
061: import de.tu_cottbus.r2ml.ReferencePropertyAtom;
062: import de.tu_cottbus.r2ml.ReferencePropertyFunctionTerm;
063: import de.tu_cottbus.r2ml.RuleBase;
064: import de.tu_cottbus.r2ml.RuleText;
065: import de.tu_cottbus.r2ml.StrongNegation;
066: import de.tu_cottbus.r2ml.Subject;
067: import de.tu_cottbus.r2ml.TypedLiteral;
068:
069: /**
070: * @author Bastian Schenke (bastian.schenke@googlemail.com)
071: *
072: */
073: public class R2MLDriver {
074:
075: /**
076: * default mode
077: */
078: public static final int DEFAULT_PROPERTY_MODE = 0;
079:
080: /**
081: * Properties can be infered even if there are properties in the domainclasses
082: */
083: public static final int INFER_PROPERTIES_MODE = 1;
084:
085: private int propertyMode = DEFAULT_PROPERTY_MODE;
086:
087: public static final String ID = "R2MLAdapter v0.1";
088:
089: public static final String R2ML_NS = "http://www.rewerse.net/I1/2006/R2ML";
090:
091: public static final String R2ML_VOCABULARY_NS = "http://www.rewerse.net/I1/2006/R2ML/R2MLV";
092:
093: private static R2MLDriver singletonDriver = null;
094:
095: private Map<Class, XmlTypeHandler> typeHandler = new HashMap<Class, XmlTypeHandler>();
096:
097: private boolean initialized = false;
098:
099: private Normalizer normy = null;
100:
101: private NameMapper nameMapper = null;
102:
103: private DatatypeMapper datty = null;
104:
105: public Logger logger = Logger.getLogger("r2mlAdapter");
106:
107: private AssociationResolvPolicy associationResolvPolicy = new AssociationAsReferenceResolvPolicy();
108:
109: private RuleBase ruleBase;
110:
111: private RuleBaseHandler rbHandler;
112:
113: private List<RuleBaseFilter> ruleBaseFilter = new ArrayList<RuleBaseFilter>();
114:
115: private R2MLDriver() {
116: super ();
117: }
118:
119: public static R2MLDriver get() {
120: if (singletonDriver == null) {
121: singletonDriver = new R2MLDriver();
122: singletonDriver.addHandlers();
123: singletonDriver.addFilter();
124: singletonDriver.logger.setLevel(Level.ALL);
125: }
126:
127: return singletonDriver;
128: }
129:
130: /**
131: * @param ruleBase
132: * a RuleBase unmarshalled out of a R2ML/XML file
133: * @return a KnowledgeBase representing the input RuleBase
134: * @throws R2MLException
135: */
136: public KnowledgeBase importKB(RuleBase rb) throws R2MLException {
137: MappingContext.reset();
138: if (logger.isDebugEnabled())
139: logger.debug("entering RuleBaseHandler");
140: this .ruleBase = rb;
141: for (RuleBaseFilter filter : ruleBaseFilter) {
142: if (logger.isDebugEnabled()) {
143: logger.debug("applying filter " + filter.getName());
144: }
145: filter.repair(ruleBase);
146: }
147:
148: rbHandler = (RuleBaseHandler) getHandlerByXmlType(ruleBase
149: .getClass());
150:
151: KnowledgeBase kb = (KnowledgeBase) rbHandler
152: .importObject(ruleBase);
153:
154: assert MappingContext.get().isClean();
155: return kb;
156:
157: }
158:
159: void addRuleToRuleBase(nz.org.take.DerivationRule rule)
160: throws R2MLException {
161: if (rbHandler == null) {
162: throw new R2MLException(
163: "Unable to add rule to non-existing rulebase.");
164: }
165: rbHandler.addRuleToKB(rule);
166: }
167:
168: /**
169: * @param key
170: * @return
171: * @throws R2MLException
172: */
173: public XmlTypeHandler getHandlerByXmlType(
174: Class<? extends java.lang.Object> key) {
175:
176: XmlTypeHandler handler = typeHandler.get(key);
177: if (logger.isDebugEnabled() && (handler == null)) {
178: logger.warn("XmlTypeHandler not found for "
179: + key.toString());
180: }
181: if (handler == null)
182: throw new NullPointerException(
183: "There must be a handler for class "
184: + key.getCanonicalName());
185: return handler;
186:
187: }
188:
189: public NameMapper getNameMapper() {
190: if (nameMapper == null)
191: nameMapper = new DefaultNameMapper();
192: return nameMapper;
193: }
194:
195: public void setNameMapper(NameMapper newSlotty) {
196: this .nameMapper = newSlotty;
197: }
198:
199: public DatatypeMapper getDatatypeMapper() {
200: if (datty == null)
201: datty = new DefaultDatatypeMapper();
202: return datty;
203: }
204:
205: public void setDatatypeMapper(DatatypeMapper newDatty) {
206: this .datty = newDatty;
207: }
208:
209: private void add(Class clazz, XmlTypeHandler handler) {
210: typeHandler.put(clazz, handler);
211: }
212:
213: public Normalizer getNormalizer() {
214: if (normy == null)
215: normy = new CheckOnlyNormalizer();
216: return this .normy;
217: }
218:
219: public void setNormalizer(Normalizer normy) {
220: this .normy = normy;
221: }
222:
223: private void addHandlers() {
224:
225: typeHandler = new HashMap<Class, XmlTypeHandler>();
226: add(AssociationAtom.class, new AssociationAtomHandler());
227: add(AttributionAtom.class, new AttributionAtomHandler());
228: add(AttributeFunctionTerm.class,
229: new AttributeFunctionTermHandler());
230: add(Conclusion.class, new ConclusionHandler());
231: add(Condition.class, new ConditionHandler());
232: add(Conjunction.class, new ConjunctionHandler());
233: add(DataClassificationAtom.class,
234: new DataClassificationAtomHandler());
235: add(DatatypeFunctionTerm.class,
236: new DatatypeFunctionTermHandler());
237: add(DatatypePredicateAtom.class,
238: new DatatypePredicateAtomHandler());
239: add(DataVariable.class, new DataVariableHandler());
240: add(DataValue.class, new DataValueHandler());
241: add(DerivationRule.class, new DerivationRuleHandler());
242: add(DerivationRuleSet.class, new DerivationRuleSetHandler());
243: add(Disjunction.class, new DisjunctionHandler());
244: add(Documentation.class, new DocumentationHandler());
245: add(EqualityAtom.class, new EqualityAtomHandler());
246: add(GenericAtom.class, new GenericAtomHandler());
247: add(InequalityAtom.class, new InequalityAtomHandler());
248: add(IntegrityRuleSet.class, new IntegrityRuleSetHandler());
249: add(NegationAsFailure.class, new NegationAsFailureHandler());
250: add(Negation.class, new NegationHandler());
251: add(ObjectClassificationAtom.class,
252: new ObjectClassificationAtomHandler());
253: add(ObjectDescriptionAtom.class,
254: new ObjectDescriptionAtomHandler());
255: add(ObjectVariable.class, new ObjectVariableHandler());
256: add(PlainLiteral.class, new PlainLiteralHandler());
257: add(ProductionRuleSet.class, new ProductionRuleSetHandler());
258: add(PropertyAtom.class, new PropertyAtomHandler());
259: add(QfConjunction.class, new QfConjunctionHandler());
260: add(QfDisjunction.class, new QfDisjunctionHandler());
261: add(ReactionRuleSet.class, new ReactionRuleSetHandler());
262: add(ReferencePropertyAtom.class,
263: new ReferencePropertyAtomHandler());
264: add(ReferencePropertyFunctionTerm.class,
265: new ReferencePropertyFunctionTermHandler());
266: add(RuleBase.class, new RuleBaseHandler());
267: add(RuleText.class, new RuleTextHandler());
268: add(StrongNegation.class, new StrongNegationHandler());
269: add(Subject.class, new SubjectHandler());
270: add(TypedLiteral.class, new TypedLiteralHandler());
271:
272: }
273:
274: private void addFilter() {
275: ruleBaseFilter = new ArrayList<RuleBaseFilter>();
276:
277: if (propertyMode == INFER_PROPERTIES_MODE) {
278: ruleBaseFilter.add(new ReplacePropertyFunctionTermFilter());
279: }
280: ruleBaseFilter.add(new TypeVariablesFilter());
281: }
282:
283: public void setAssociationResolvPolicy(
284: AssociationResolvPolicy policy) {
285: this .associationResolvPolicy = policy;
286: }
287:
288: public AssociationResolvPolicy getAssociationResolvPolicy() {
289: return associationResolvPolicy;
290: }
291:
292: protected boolean isInitialized() {
293: return initialized;
294: }
295:
296: public RuleBase getRuleBase() {
297: return ruleBase;
298: }
299:
300: public void setRuleBase(RuleBase ruleBase) {
301: this .ruleBase = ruleBase;
302: }
303:
304: public int getPropertyMode() {
305: return propertyMode;
306: }
307:
308: public void setPropertyMode(int newPropertyMode) {
309: this.propertyMode = newPropertyMode;
310: addFilter();
311: }
312:
313: }
|