[Mulgara-svn] r1831 - in trunk/src/jar: query/java/org/mulgara/query/rdf resolver-prefix/java/org/mulgara/resolver/prefix
pag at mulgara.org
pag at mulgara.org
Wed Oct 28 07:56:40 UTC 2009
Author: pag
Date: 2009-10-28 00:56:39 -0700 (Wed, 28 Oct 2009)
New Revision: 1831
Modified:
trunk/src/jar/query/java/org/mulgara/query/rdf/Mulgara.java
trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolver.java
trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolverFactory.java
Log:
Added string searching support to the prefix resolver
Modified: trunk/src/jar/query/java/org/mulgara/query/rdf/Mulgara.java
===================================================================
--- trunk/src/jar/query/java/org/mulgara/query/rdf/Mulgara.java 2009-10-27 11:32:47 UTC (rev 1830)
+++ trunk/src/jar/query/java/org/mulgara/query/rdf/Mulgara.java 2009-10-28 07:56:39 UTC (rev 1831)
@@ -73,10 +73,13 @@
/** The URI for prefix graph types. */
public final static URI PREFIX_GRAPH_TYPE_URI = URI.create(NAMESPACE + "PrefixGraph");
- /** The URI for prefixes. */
+ /** The URI for URI prefixes. */
public final static URI PREFIX_URI = URI.create(NAMESPACE + "prefix");
- /** The URI for prefixes. */
+ /** The URI for string prefixes. */
+ public final static URI STR_PREFIX_URI = URI.create(NAMESPACE + "stringPrefix");
+
+ /** The URI for binding variables. */
public final static URI IS_URI = URI.create(NAMESPACE + "is");
}
Modified: trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolver.java
===================================================================
--- trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolver.java 2009-10-27 11:32:47 UTC (rev 1830)
+++ trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolver.java 2009-10-28 07:56:39 UTC (rev 1831)
@@ -18,6 +18,7 @@
// Java 2 standard packages
import java.net.*;
+
import javax.transaction.xa.XAResource;
// Third party packages
@@ -29,7 +30,6 @@
import org.mulgara.resolver.spi.*;
import org.mulgara.store.stringpool.SPObject;
import org.mulgara.store.stringpool.SPObjectFactory;
-import org.mulgara.store.stringpool.SPURI;
import org.mulgara.store.stringpool.StringPoolException;
import org.mulgara.store.stringpool.xa.SPObjectFactoryImpl;
import org.mulgara.store.tuples.Tuples;
@@ -56,8 +56,11 @@
private URI modelTypeURI;
/** The preallocated local node representing the mulgara:prefix property. */
- private long mulgaraPrefix;
+ private final long mulgaraPrefix;
+ /** The preallocated local node representing the mulgara:stringPrefix property. */
+ private final long mulgaraStringPrefix;
+
//
// Constructors
//
@@ -74,6 +77,7 @@
ResolverSession resolverSession,
Resolver systemResolver,
long mulgaraPrefix,
+ long mulgaraStringPrefix,
URI modelTypeURI
) throws ResolverFactoryException {
@@ -90,6 +94,7 @@
this.resolverSession = resolverSession;
this.modelTypeURI = modelTypeURI;
this.mulgaraPrefix = mulgaraPrefix;
+ this.mulgaraStringPrefix = mulgaraStringPrefix;
}
//
@@ -144,9 +149,7 @@
* @throws ResolverException if the <var>statements</var> can't be
* added to the <var>model</var>
*/
- public void modifyModel(long model, Statements statements, boolean occurs)
- throws ResolverException
- {
+ public void modifyModel(long model, Statements statements, boolean occurs) throws ResolverException {
if (logger.isDebugEnabled()) {
logger.debug("Modify prefix model " + model);
}
@@ -158,8 +161,7 @@
/**
* Remove the cached model containing the contents of a URL.
*/
- public void removeModel(long model) throws ResolverException
- {
+ public void removeModel(long model) throws ResolverException {
if (logger.isDebugEnabled()) {
logger.debug("Remove prefix model " + model);
}
@@ -170,8 +172,7 @@
*
* Resolution is by filtration of a URL stream, and thus very slow.
*/
- public Resolution resolve(Constraint constraint) throws QueryException
- {
+ public Resolution resolve(Constraint constraint) throws QueryException {
if (logger.isDebugEnabled()) {
logger.debug("Resolve " + constraint);
}
@@ -200,33 +201,15 @@
Node prefixNode = resolverSession.globalize(object.getValue());
// check the constraint for consistency
- if (property != mulgaraPrefix || !(prefixNode instanceof Literal || prefixNode instanceof URIReference)) {
- logger.error("property = " + property +", mulgaraPrefix = " + mulgaraPrefix);
- logger.error("element(2): " + prefixNode + " [" + prefixNode.getClass().getName() + "]");
+ if ((property != mulgaraPrefix && property != mulgaraStringPrefix) || !(prefixNode instanceof Literal || prefixNode instanceof URIReference)) {
+ logger.debug("property = " + property +", mulgaraPrefix = " + mulgaraPrefix);
+ logger.debug("element(2): " + prefixNode + " [" + prefixNode.getClass().getName() + "]");
throw new QueryException("Prefix resolver can only be used for prefix constraints: " + constraint);
}
- String prefix;
- // extract the string from the literal
- if (prefixNode instanceof Literal) {
- prefix = ((Literal)prefixNode).getLexicalForm();
- } else {
- prefix = ((URIReference)prefixNode).getURI().toString();
- }
-
if (logger.isDebugEnabled()) {
- logger.debug("Evaluating " + constraint.getElement(0) +
- " has prefix " +
- constraint.getElement(2));
+ logger.debug("Evaluating " + constraint.getElement(0) + " has prefix " + constraint.getElement(2));
}
- URI startPrefixUri;
- URI endPrefixUri;
- try {
- startPrefixUri = new URI(prefix);
- endPrefixUri = new URI(prefix + Character.MAX_VALUE);
- } catch (URISyntaxException e) {
- throw new QueryException("Prefix resolver can only be used for URI prefixes: " + e.getMessage());
- }
ConstraintElement node = constraint.getElement(0);
assert node != null;
@@ -237,8 +220,8 @@
// convert the prefix into a string pool object
SPObjectFactory spoFact = SPObjectFactoryImpl.getInstance();
- SPURI startPrefixObj = spoFact.newSPURI(startPrefixUri);
- SPURI endPrefixObj = spoFact.newSPURI(endPrefixUri);
+ SPObject startPrefixObj = getStartObject(spoFact, prefixNode, property);
+ SPObject endPrefixObj = getEndObject(spoFact, prefixNode, property);
// get the extents of the prefix from the string pool
tuples = resolverSession.findStringPoolRange(startPrefixObj, true, endPrefixObj, false);
@@ -281,6 +264,14 @@
} else {
// see if the node starts with the required prefix
+
+ String prefix;
+ // extract the string from the literal
+ if (prefixNode instanceof Literal) {
+ prefix = ((Literal) prefixNode).getLexicalForm();
+ } else {
+ prefix = ((URIReference) prefixNode).getURI().toString();
+ }
if (spo.getLexicalForm().startsWith(prefix)) {
tuples = TuplesOperations.unconstrained();
} else {
@@ -302,8 +293,57 @@
}
+ /**
+ * Create a string pool object to represent the beginning of a search range.
+ * @param factory The string pool factory that will create the object
+ * @param prefixNode The data for the object.
+ * @param predType Predicate to indicate the type of data to get from the string pool.
+ * @return An object representing the start of a range of data to get from the string pool.
+ * @throws QueryException If the predicate indicates a URI but the prefix data is not a valid URI.
+ */
+ private SPObject getStartObject(SPObjectFactory factory, Node prefixNode, long predType) throws QueryException {
+ if (prefixNode instanceof Literal) {
+ String prefix = ((Literal) prefixNode).getLexicalForm();
+ if (predType == mulgaraPrefix) {
+ try {
+ return factory.newSPURI(new URI(prefix));
+ } catch (URISyntaxException e) {
+ throw new QueryException("Bad URI prefix provided: " + prefix + " (should this be using mulgara:stringPrefix?)");
+ }
+ } else return factory.newSPString(prefix);
+ } else {
+ URI startPrefixUri = ((URIReference) prefixNode).getURI();
+ if (predType == mulgaraPrefix) return factory.newSPURI(startPrefixUri);
+ else return factory.newSPString(startPrefixUri.toString());
+ }
+ }
/**
+ * Create a string pool object to represent the end of a search range.
+ * @param factory The string pool factory that will create the object
+ * @param prefixNode The data for the object.
+ * @param predType Predicate to indicate the type of data to get from the string pool.
+ * @return An object representing the end of a range of data to get from the string pool.
+ * @throws QueryException If the predicate indicates a URI but the prefix data is not a valid URI.
+ */
+ private SPObject getEndObject(SPObjectFactory factory, Node prefixNode, long predType) throws QueryException {
+ String prefix;
+ if (prefixNode instanceof Literal) {
+ prefix = ((Literal) prefixNode).getLexicalForm();
+ } else {
+ prefix = ((URIReference) prefixNode).getURI().toString();
+ }
+
+ if (predType == mulgaraPrefix) {
+ try {
+ return factory.newSPURI(new URI(prefix + Character.MAX_VALUE));
+ } catch (URISyntaxException e) {
+ throw new QueryException("Bad URI prefix provided: " + prefix + " (should this be using mulgara:stringPrefix?)");
+ }
+ } else return factory.newSPString(prefix + Character.MAX_VALUE);
+ }
+
+ /**
* Close all sessions and factories used by this resolver.
*/
public void close() {
Modified: trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolverFactory.java
===================================================================
--- trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolverFactory.java 2009-10-27 11:32:47 UTC (rev 1830)
+++ trunk/src/jar/resolver-prefix/java/org/mulgara/resolver/prefix/PrefixResolverFactory.java 2009-10-28 07:56:39 UTC (rev 1831)
@@ -42,8 +42,11 @@
private static final URI DEFAULT_GRAPH = URI.create(Mulgara.PREFIX_GRAPH);
/** The preallocated local node representing the prefix predicate. */
- private long mulgaraPrefix;
+ private final long mulgaraPrefix;
+ /** The preallocated local node representing the string prefix predicate. */
+ private final long mulgaraStringPrefix;
+
//
// Constructors
//
@@ -61,6 +64,7 @@
// intialize the fields
mulgaraPrefix = initializer.preallocate(new URIReferenceImpl(Mulgara.PREFIX_URI));
+ mulgaraStringPrefix = initializer.preallocate(new URIReferenceImpl(Mulgara.STR_PREFIX_URI));
// no need to claim the types supported by this resolver, as this is detected in the default graphs
}
@@ -110,7 +114,7 @@
boolean canWrite, ResolverSession resolverSession, Resolver systemResolver
) throws ResolverFactoryException {
if (logger.isDebugEnabled()) logger.debug("Creating new Prefix resolver");
- return new PrefixResolver(resolverSession, systemResolver, mulgaraPrefix, Mulgara.PREFIX_GRAPH_TYPE_URI);
+ return new PrefixResolver(resolverSession, systemResolver, mulgaraPrefix, mulgaraStringPrefix, Mulgara.PREFIX_GRAPH_TYPE_URI);
}
/**
More information about the Mulgara-svn
mailing list