001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">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:
019: package org.mandarax.rdf;
020:
021: import java.net.MalformedURLException;
022: import java.net.URL;
023: import java.util.*;
024: import org.apache.commons.collections.iterators.ArrayIterator;
025: import org.mandarax.kernel.*;
026: import org.mandarax.util.ClauseIterator;
027:
028: /**
029: * Clause set implementation to access RDF documents.
030: * <p>
031: * A RDF document usually contains more than one predicate. However, the predicates must be known
032: * when adding the clause set to a knowledge base as the kb uses them for internal indexing (i.e.
033: * to support retrieval of knowledge for a certain predicate). There are two strategies how this can be achieved:
034: * <ol>
035: * <li>Using the property <code>predicates</code>
036: * <li>Gathering the predicates from the document. This is the default strategy, it happens when <code>getKey()</code>
037: * is called (e.g., by the kb) and the predicates have not been initialized (lazy initialization). This can be customized
038: * using the <code>maxFetchSize</code> property which constraints the max number of statements analyzed.
039: * <p>
040: * TODO implement a strategy in the kb for clause sets to be used for arbitrary predicates.
041: * <p>
042: * The RDF is read from a URL. If the URL is a network URL (= not a local file), firewalls and proxies
043: * must be configured correctly. Theer are various ways to accomplish this.
044: * E.g., one may include the following parameter on the java command line:
045: * <p><code>-DsocksProxyHost=[your-proxy-domain-name-or-ip-address] </code> // for a socks proxy
046: * <br><code>-DproxySet=true -DproxyHost=[your-proxy] -DproxyPort=[your-proxy-port-number] </code> // for an http proxy
047: * <p>
048: * These properties can also be set programatically as follows:
049: * <p><code>
050: * <br>System.getProperties().put("proxySet","true");
051: * <br>System.getProperties().put("proxyHost","proxy.hostname");
052: * <br>System.getProperties().put("proxyPort",port_number);
053: * </code>
054: * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A> <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
055: * @version 1.1 <01 August 2004>
056: * @since 0.1
057: */
058:
059: public class RDFClauseSet extends AbstractPropertiesSupport implements
060: ClauseSet, RDFLogger, RDFConstants {
061:
062: private URL url = null;
063: private String language = RDFXML;
064: private Predicate[] predicates = null;
065: int maxFetchSize = -1; // this means NO LIMIT
066:
067: /**
068: * Empty constructor.
069: * The url should be passed. This constructor is included to make this a POJO (plain old java object)
070: * and to support tools based on reflection.
071: */
072: public RDFClauseSet() {
073: super ();
074: }
075:
076: /**
077: * Constructor.
078: * @param url the RDF model is loaded from this URL
079: */
080: public RDFClauseSet(URL url) {
081: super ();
082: this .url = url;
083: }
084:
085: /**
086: * Constructor.
087: * @param f a local file
088: * @exception a MalformedURLException is thrown if the file cannot be converted into a URL
089: */
090: public RDFClauseSet(java.io.File f) throws MalformedURLException {
091: super ();
092: this .url = f.toURL();
093: }
094:
095: /**
096: * Add a clause set listener.
097: * @param l the listener
098: */
099: public void addClauseSetChangeListener(ClauseSetChangeListener l) {
100: // TODO
101: }
102:
103: /**
104: * Get a key for indexing. The knowledge base is supposed to
105: * contain only clauses having the same key object.
106: * @return array of RDFPredicate
107: */
108: public synchronized Object getKey() {
109: return predicates;
110: }
111:
112: /**
113: * Remove a clause set listener.
114: * @param l the listener
115: */
116: public void removeClauseSetChangeListener(ClauseSetChangeListener l) {
117: // TODO
118: }
119:
120: /**
121: * Get an iterator iterating over the predicates contained in this clause set.
122: * @return an iterator
123: */
124: public Iterator predicates() {
125: return new ArrayIterator(predicates);
126: }
127:
128: /**
129: * Return a clause iterator.
130: * @return a clause iterator
131: */
132: public ClauseIterator clauses() throws ClauseSetException {
133: return new RDFClauseIterator(url, language, null);
134: }
135:
136: /**
137: * Return a clause iterator.
138: * @return a clause iterator
139: * @param query the query clause
140: * @param additionalParameter an optional additional parameter
141: * @see org.mandarax.kernel.KnowledgeOwner#clauses(org.mandarax.kernel.Clause, java.lang.Object)
142: */
143: public ClauseIterator clauses(Clause query,
144: Object additionalParameter) throws ClauseSetException {
145: Object obj = query.getKey();
146: if (obj == null || !(obj instanceof RDFPredicate))
147: throw new ClauseSetException(
148: "Cannot get clause iterator, rdf predicate expected instead of "
149: + obj);
150: return new RDFClauseIterator(url, language, (RDFPredicate) obj);
151: }
152:
153: /**
154: * Get the url.
155: * @return the url
156: */
157: public URL getURL() {
158: return url;
159: }
160:
161: /**
162: * Set the url.
163: * @param url the url
164: */
165: public void setURL(URL url) {
166: this .url = url;
167: }
168:
169: /**
170: * Convert this object to a string.
171: * @return a string
172: */
173: public String toString() {
174: return new StringBuffer().append("aRDFClauseSet(").append(
175: this .url).append(')').toString();
176: }
177:
178: /**
179: * Get the max fetch size.
180: * @return Returns the maxFetchSize.
181: */
182: public int getMaxFetchSize() {
183: return maxFetchSize;
184: }
185:
186: /**
187: * Set the max fetch size.
188: * @param maxFetchSize The maxFetchSize to set.
189: */
190: public void setMaxFetchSize(int maxFetchSize) {
191: this .maxFetchSize = maxFetchSize;
192: }
193:
194: /**
195: * Get the predicates.
196: * @return Returns the predicates.
197: */
198: public Predicate[] getPredicates() {
199: return predicates;
200: }
201:
202: /**
203: * Set the predicates.
204: * @param predicates The predicates to set.
205: */
206: public void setPredicates(Predicate[] predicates) {
207: this.predicates = predicates;
208: }
209: }
|