001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.xkb.ruleml;
019:
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import org.jdom.Element;
024: import org.mandarax.kernel.ClauseSet;
025: import org.mandarax.kernel.Fact;
026: import org.mandarax.kernel.KnowledgeBase;
027: import org.mandarax.kernel.Rule;
028: import org.mandarax.reference.AdvancedKnowledgeBase;
029: import org.mandarax.xkb.XKBException;
030:
031: /**
032: * Driver implementation for the RULE ML version 0.8.
033: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
034: * @version 3.4 <7 March 05>
035: * @since 1.9
036: */
037: public class RuleML0_8Driver extends AbstractRuleMLDriver {
038:
039: /**
040: * Constructor.
041: */
042: public RuleML0_8Driver() {
043: super ();
044: }
045:
046: /**
047: * Export a knowledge base, e.g. convert it into an xml element.
048: * @return an element (of the jdom tree)
049: * @param kb a knowledge base
050: */
051: protected Element export(KnowledgeBase kb) {
052: Element e = new Element(RULEBASE);
053: List clauseSets = kb.getClauseSets();
054: ClauseSet nextClauseSet = null;
055: // export clause sets
056: for (Iterator it = clauseSets.iterator(); it.hasNext();) {
057: nextClauseSet = (ClauseSet) it.next();
058: if (nextClauseSet instanceof Fact)
059: e.addContent(export((Fact) nextClauseSet));
060: else if (nextClauseSet instanceof Rule)
061: e.addContent(export((Rule) nextClauseSet));
062: else
063: LOG_XKB
064: .warn("Only facts and rules are suported by driver "
065: + getName()
066: + ", cannot export "
067: + nextClauseSet);
068: }
069: return e;
070: }
071:
072: /**
073: * Get a short text describing the driver.
074: * @return a text
075: */
076: public String getDescription() {
077: return "Driver for rulebases as specified by the RULE ML version 0.8 specification";
078: }
079:
080: /**
081: * Get the location (URL) of the associated DTD.
082: * @return a text
083: */
084: public String getDTD() {
085: return "http://www.dfki.uni-kl.de/ruleml/dtd/0.8/ruleml-datalog-monolith.dtd";
086: }
087:
088: /**
089: * Get the name of the driver.
090: * @return a text
091: */
092: public String getName() {
093: return "RuleML 0.8";
094: }
095:
096: /**
097: * Import a knowledge base.
098: * @return a knowledge base
099: * @param e an xml element
100: * @throws an XKBException is thrown if import fails
101: */
102: protected KnowledgeBase importKnowledgeBase(Element e)
103: throws XKBException {
104:
105: // since we have no chance to store the class name in this xml format,
106: // we choose the default class
107: KnowledgeBase kb = new AdvancedKnowledgeBase();
108:
109: // build the clause sets
110: List all = e.getChildren();
111: Element next = null;
112: ClauseSet nextCS = null;
113:
114: for (Iterator it = all.iterator(); it.hasNext();) {
115: next = (Element) it.next();
116: if (compare(next.getName(), IMP))
117: kb.add(importRule(next));
118: else if (compare(next.getName(), FACT))
119: kb.add(importFact(next));
120: else
121: LOG_XKB.warn("Cannot import element <" + next.getName()
122: + "> - driver " + getName()
123: + " does not support this tag");
124: }
125: return kb;
126: }
127:
128: /**
129: * Indicates whether the driver (and the underlying xml format (=dtd))
130: * supports auto facts.
131: * @return a boolean
132: */
133: public boolean supportsAutoFacts() {
134: return false;
135: }
136:
137: /**
138: * Indicates whether the driver (and the underlying xml format (=dtd))
139: * supports clause sets.
140: * @return a boolean
141: */
142: public boolean supportsClauseSets() {
143: return false;
144: }
145:
146: /**
147: * Indicates whether the driver (and the underlying xml format (=dtd))
148: * supports facts. (some formats might see facts as rules without body)
149: * @return a boolean
150: */
151: public boolean supportsFacts() {
152: return true;
153: }
154:
155: /**
156: * Indicates whether the driver (and the underlying xml format (=dtd))
157: * supports functions.
158: * @return a boolean
159: */
160: public boolean supportsFunctions() {
161: return false;
162: }
163:
164: /**
165: * Indicates whether the driver (and the underlying xml format (=dtd))
166: * supports the java semantics (e.g. JFunctions, JPredicate).
167: * @return a boolean
168: */
169: public boolean supportsJavaSemantics() {
170: return false;
171: }
172:
173: /**
174: * Indicates whether the driver (and the underlying xml format (=dtd))
175: * supports multiple premises.
176: * @return a boolean
177: */
178: public boolean supportsMultiplePremises() {
179: return true;
180: }
181:
182: /**
183: * Indicates whether the driver (and the underlying xml format (=dtd))
184: * supports multiple premises connected by OR.
185: * @return a boolean
186: */
187: public boolean supportsOrPremises() {
188: return false;
189: }
190:
191: /**
192: * Indicates whether the driver (and the underlying xml format (=dtd))
193: * supports types.
194: * @return a boolean
195: */
196: public boolean supportsTypes() {
197: return false;
198: }
199:
200: /**
201: * Indicates whether the driver (and the underlying xml format (=dtd))
202: * supports queries.
203: * @return a boolean
204: */
205: public boolean supportsQueries() {
206: return false;
207: }
208:
209: /**
210: * Indicates whether the driver (and the underlying xml format (=dtd))
211: * supports negation as failure.
212: * @return a boolean
213: */
214: public boolean supportsNegationAsFailure() {
215: return false;
216: }
217: }
|