/*
* Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.mandarax.rdf;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import org.apache.commons.collections.iterators.ArrayIterator;
import org.mandarax.kernel.*;
import org.mandarax.util.ClauseIterator;
/**
* Clause set implementation to access RDF documents.
* <p>
* A RDF document usually contains more than one predicate. However, the predicates must be known
* when adding the clause set to a knowledge base as the kb uses them for internal indexing (i.e.
* to support retrieval of knowledge for a certain predicate). There are two strategies how this can be achieved:
* <ol>
* <li>Using the property <code>predicates</code>
* <li>Gathering the predicates from the document. This is the default strategy, it happens when <code>getKey()</code>
* is called (e.g., by the kb) and the predicates have not been initialized (lazy initialization). This can be customized
* using the <code>maxFetchSize</code> property which constraints the max number of statements analyzed.
* <p>
* TODO implement a strategy in the kb for clause sets to be used for arbitrary predicates.
* <p>
* The RDF is read from a URL. If the URL is a network URL (= not a local file), firewalls and proxies
* must be configured correctly. Theer are various ways to accomplish this.
* E.g., one may include the following parameter on the java command line:
* <p><code>-DsocksProxyHost=[your-proxy-domain-name-or-ip-address] </code> // for a socks proxy
* <br><code>-DproxySet=true -DproxyHost=[your-proxy] -DproxyPort=[your-proxy-port-number] </code> // for an http proxy
* <p>
* These properties can also be set programatically as follows:
* <p><code>
* <br>System.getProperties().put("proxySet","true");
* <br>System.getProperties().put("proxyHost","proxy.hostname");
* <br>System.getProperties().put("proxyPort",port_number);
* </code>
* @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A> <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
* @version 1.1 <01 August 2004>
* @since 0.1
*/
public class RDFClauseSet extends AbstractPropertiesSupport implements ClauseSet, RDFLogger, RDFConstants {
private URL url = null;
private String language = RDFXML;
private Predicate[] predicates = null;
int maxFetchSize = -1; // this means NO LIMIT
/**
* Empty constructor.
* The url should be passed. This constructor is included to make this a POJO (plain old java object)
* and to support tools based on reflection.
*/
public RDFClauseSet() {
super();
}
/**
* Constructor.
* @param url the RDF model is loaded from this URL
*/
public RDFClauseSet(URL url) {
super();
this.url = url;
}
/**
* Constructor.
* @param f a local file
* @exception a MalformedURLException is thrown if the file cannot be converted into a URL
*/
public RDFClauseSet(java.io.File f) throws MalformedURLException {
super();
this.url = f.toURL();
}
/**
* Add a clause set listener.
* @param l the listener
*/
public void addClauseSetChangeListener(ClauseSetChangeListener l) {
// TODO
}
/**
* Get a key for indexing. The knowledge base is supposed to
* contain only clauses having the same key object.
* @return array of RDFPredicate
*/
public synchronized Object getKey() {
return predicates;
}
/**
* Remove a clause set listener.
* @param l the listener
*/
public void removeClauseSetChangeListener(ClauseSetChangeListener l) {
// TODO
}
/**
* Get an iterator iterating over the predicates contained in this clause set.
* @return an iterator
*/
public Iterator predicates() {
return new ArrayIterator(predicates);
}
/**
* Return a clause iterator.
* @return a clause iterator
*/
public ClauseIterator clauses() throws ClauseSetException {
return new RDFClauseIterator(url,language,null);
}
/**
* Return a clause iterator.
* @return a clause iterator
* @param query the query clause
* @param additionalParameter an optional additional parameter
* @see org.mandarax.kernel.KnowledgeOwner#clauses(org.mandarax.kernel.Clause, java.lang.Object)
*/
public ClauseIterator clauses(Clause query, Object additionalParameter) throws ClauseSetException {
Object obj = query.getKey();
if (obj==null || !(obj instanceof RDFPredicate)) throw new ClauseSetException("Cannot get clause iterator, rdf predicate expected instead of " + obj);
return new RDFClauseIterator(url,language,(RDFPredicate)obj);
}
/**
* Get the url.
* @return the url
*/
public URL getURL() {
return url;
}
/**
* Set the url.
* @param url the url
*/
public void setURL(URL url) {
this.url = url;
}
/**
* Convert this object to a string.
* @return a string
*/
public String toString() {
return new StringBuffer()
.append("aRDFClauseSet(")
.append(this.url)
.append(')')
.toString();
}
/**
* Get the max fetch size.
* @return Returns the maxFetchSize.
*/
public int getMaxFetchSize() {
return maxFetchSize;
}
/**
* Set the max fetch size.
* @param maxFetchSize The maxFetchSize to set.
*/
public void setMaxFetchSize(int maxFetchSize) {
this.maxFetchSize = maxFetchSize;
}
/**
* Get the predicates.
* @return Returns the predicates.
*/
public Predicate[] getPredicates() {
return predicates;
}
/**
* Set the predicates.
* @param predicates The predicates to set.
*/
public void setPredicates(Predicate[] predicates) {
this.predicates = predicates;
}
}
|