[MOBY-guts] biomoby commit
Gary Schlitz
gss at pub.open-bio.org
Fri Jun 18 21:42:34 UTC 2004
gss
Fri Jun 18 17:42:34 EDT 2004
Update of /home/repository/moby/moby-live/S-MOBY/ref-impl/server/src/org/smoby/ref/tools
In directory pub.open-bio.org:/tmp/cvs-serv30042/src/org/smoby/ref/tools
Modified Files:
DiscoveryQuery.java
Log Message:
Changes to utilize single model
moby-live/S-MOBY/ref-impl/server/src/org/smoby/ref/tools DiscoveryQuery.java,1.5,1.6
===================================================================
RCS file: /home/repository/moby/moby-live/S-MOBY/ref-impl/server/src/org/smoby/ref/tools/DiscoveryQuery.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- /home/repository/moby/moby-live/S-MOBY/ref-impl/server/src/org/smoby/ref/tools/DiscoveryQuery.java 2004/06/10 23:24:16 1.5
+++ /home/repository/moby/moby-live/S-MOBY/ref-impl/server/src/org/smoby/ref/tools/DiscoveryQuery.java 2004/06/18 21:42:34 1.6
@@ -6,6 +6,7 @@
import org.smoby.parser.*;
import org.smoby.ref.tools.db.*;
+import org.smoby.tools.Util;
import org.smoby.graph.*;
import org.smoby.graph.impl.*;
import org.smoby.vocabulary.*;
@@ -16,6 +17,7 @@
import com.hp.hpl.jena.rdql.*;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.util.iterator.*;
+import com.hp.hpl.jena.vocabulary.RDF;
/**
* This class finds graphs that match a query graph.
@@ -23,22 +25,24 @@
public class DiscoveryQuery
{
/**
- * Constant indicating that only the URI should be returned
- * in each matching graph.
+ * Constant indicating that only the minimal statements should
+ * be returned in each matching graph.
*/
- public final static int URI_ONLY = 0;
+ private final static int MINIMAL_STMTS = 0;
/**
* Constant indicating that only statements in the query should
* be returned in each matching graph.
*/
- public final static int QUERY_STMTS_ONLY = 1;
+ private final static int QUERY_STMTS = 1;
/**
- * Constants indicating that all statements (i.e. the ones found when
- * the graph was registered) should be returned in each matching graph.
+ * Constant indicating that all reachable statements (i.e. the
+ * ones that can be traversed, starting with the provider node,
+ * without leaving the scope of the provider's URI) should be
+ * returned in each matching graph.
*/
- public final static int ALL_STMTS = 2;
+ private final static int REACHABLE_STMTS = 2;
/**
* The graph that serves as a template for providers that match.
@@ -62,6 +66,21 @@
*/
private int lastBnodeNumber;
+ public static DiscoveryQuery newMinimalStmtsQuery(MOBYProvider provider)
+ {
+ return new DiscoveryQuery(provider, MINIMAL_STMTS);
+ }
+
+ public static DiscoveryQuery newQueryStmtsQuery(MOBYProvider provider)
+ {
+ return new DiscoveryQuery(provider, QUERY_STMTS);
+ }
+
+ public static DiscoveryQuery newReachableStmtsQuery(MOBYProvider provider)
+ {
+ return new DiscoveryQuery(provider, REACHABLE_STMTS);
+ }
+
/**
* Create an instance for the given query graph and return statements
* style.
@@ -69,7 +88,7 @@
* @param statementsToReturn which statements should be returned: URI_ONLY,
* QUERY_STMTS_ONLY, or ALL_STMTS (use the static constants on this class).
*/
- public DiscoveryQuery(MOBYProvider queryProvider, int statementsToReturn)
+ private DiscoveryQuery(MOBYProvider queryProvider, int statementsToReturn)
{
this.queryProvider = queryProvider;
this.statementsToReturn = statementsToReturn;
@@ -85,21 +104,22 @@
lastBnodeNumber = 0;
}
- public MOBYCollection findMatchingGraphs()
+ public MOBYProviderSet findMatchingGraphs()
{
init();
+ Model dbModel = null;
try
{
Model queryModel = ((MOBYProviderImpl) queryProvider).getUnderlying();
String queryString = buildQueryString(queryModel);
StorageManager manager = new StorageManager();
- Model model = manager.openDBModel();
- ((ModelRDB) model).setDoFastpath(false);
+ dbModel = manager.openDBModel();
+ ((ModelRDB) dbModel).setDoFastpath(false);
// Execute the query
//
- QueryResults results = Query.exec(queryString, model);
+ QueryResults results = Query.exec(queryString, dbModel);
ArrayList matching = new ArrayList();
while (results.hasNext())
@@ -109,23 +129,27 @@
}
results.close();
- List returnList = new ArrayList();
+ MOBYProviderSet returnSet = MOBYObjectFactory.newProviderSet();
for (Iterator it = matching.iterator(); it.hasNext();)
{
- MOBYProvider mp = buildGraphModel(queryModel, (ResultBinding) it.next());
- String uri = mp.getURI();
- // YUCK!
- Model providerModel = manager.openModel(uri);
- Parser parser = new Parser(providerModel);
- returnList.add(parser.parseProvider());
+ Object next = it.next();
+ MOBYProvider mp = buildGraphModel(dbModel,
+ queryModel, (ResultBinding) next);
+ returnSet.addProvider(mp);
}
- return MOBYObjectFactory.newUnorderedCollection(null, returnList, null);
+ return returnSet;
} catch (Throwable t) {
return null;
}
+ finally
+ {
+ if (dbModel != null) {
+ dbModel.close();
+ }
+ }
}
/**
@@ -221,11 +245,12 @@
* @param binding bindings for variables
* @return
*/
- private MOBYProvider buildGraphModel(Model queryModel, ResultBinding binding)
+ private MOBYProvider buildGraphModel(
+ Model dbModel, Model queryModel, ResultBinding binding)
{
// Create a default model to return
//
- Model model = ModelFactory.createDefaultModel();
+ Model model = Util.newJenaModel();
model.setNsPrefixes(queryModel.getNsPrefixMap());
// For each statement in the query, create a new statement
@@ -244,10 +269,117 @@
Statement copy = model.createStatement(subject, property, object);
model.add(copy);
}
- Parser parser = new Parser(model);
+
+ // Add/remove statements based on the desired set, i.e.
+ // based on the value of statementsToReturn, and remove
+ // unused namespace prefixes from the model.
+ //
+ Model adjusted = adjustedModel(model, dbModel);
+ Util.removeUnusedNsPrefixes(adjusted);
+
+ // Parse and return a MOBY provider using the adjusted model
+ //
+ Parser parser = new Parser(adjusted);
return parser.parseProvider();
}
+ /**
+ * Given a model built from the query template model, return a
+ * model adjusted to include the statements to match the
+ * statementsToReturn instance variable. The models that result
+ * from performing a query consists of exactly the statements
+ * that were in the query, with some blank nodes filled in. In
+ * the case of QUERY_STMTS, that is the set that should be
+ * returned. In the case of MINIMAL_STMTS, only the provider
+ * type, name, one line description, and about URI statements
+ * should be returned. In the case of REACHABLE_STMTS, all the
+ * statements that are reachable from the provider URI, without
+ * going outside the provider URI's scope, should be returned.
+ */
+ private Model adjustedModel(Model original, Model dbModel)
+ {
+ switch (statementsToReturn)
+ {
+ case QUERY_STMTS : return original;
+ case MINIMAL_STMTS : return minimalModel(original, dbModel);
+ default : return reachableModel(original, dbModel);
+ }
+ }
+
+ /**
+ * Return a model consisting of just the (rdf:type moby:Provider),
+ * (moby:name <name>), (moby:oneLineDescription <description>), and
+ * (moby:aboutURI <uri>) property statements for the provider.
+ */
+ private Model minimalModel(Model original, Model dbModel)
+ {
+ Model minimal = Util.newJenaModel();
+ minimal.setNsPrefixes(dbModel.getNsPrefixMap());
+
+ // Add the rdf:type MOBY:Provider statement
+ //
+ try {
+ Statement providerTypeStmt = original.listStatements(
+ null, RDF.type, MOBY.Provider).nextStatement();
+ minimal.add(providerTypeStmt);
+
+ Resource subject = providerTypeStmt.getSubject();
+
+ // Add a statement, if it exists in the repository, that
+ // says (<subject> moby:name <name>)
+ //
+ try {
+ minimal.add(dbModel.listStatements(subject,
+ MOBY.name, (RDFNode) null).nextStatement());
+ } catch (Throwable t) {} // There was no moby:name property
+
+ // Add a statement, if it exists in the repository, that
+ // says (<subject> moby:aboutURI <uri>)
+ //
+ try {
+ minimal.add(dbModel.listStatements(subject,
+ MOBY.aboutURI, (RDFNode) null).nextStatement());
+ } catch (Throwable t) {} // There was no moby:aboutURI property
+
+ // Add a statement, if it exists in the repository, that
+ // says <subject> moby:oneLineDescription <value>
+ //
+ try {
+ minimal.add(dbModel.listStatements(subject,
+ MOBY.oneLineDescription, (RDFNode) null).nextStatement());
+ } catch (Throwable t) {} // There was no moby:oneLineDescription
+ }
+ catch (Throwable t) {} // There was no (rdf:type moby:Provider)
+
+ return minimal;
+ }
+
+ /**
+ * Return a model consisting of the statements in the original (query)
+ * model, plus any statements not already in the model that are
+ * reachable from the subject of the rdf:type moby:Provider statement.
+ */
+ private Model reachableModel(Model original, Model dbModel)
+ {
+ // Start with a model consisting of the original statements
+ //
+ Model reachable = ModelFactory.createDefaultModel();
+ reachable.setNsPrefixes(dbModel.getNsPrefixMap());
+ reachable.add(original);
+
+ // Find the subject of the (rdf:type MOBY:Provider) statement
+ //
+ try {
+ Statement providerTypeStmt = original.listStatements(
+ null, RDF.type, MOBY.Provider).nextStatement();
+ Resource subject = providerTypeStmt.getSubject();
+ Util.addReachableStmts(dbModel, reachable, subject, subject.getURI());
+ }
+ catch (Throwable t) {} // There was no (rdf:type moby:Provider)
+
+ return reachable;
+ }
+
private RDFNode getBinding(RDFNode node, ResultBinding binding)
{
// If the resource is not a blank node, then just return the
@@ -256,23 +388,16 @@
if (! node.asNode().isBlank()) {
return node;
}
- //ResultBinding
ResultBindingIterator results = binding.iterator();
while (results.hasNext())
{
- Object object = results.next();
- Value value = (Value) object;
+ results.next();
String varName = results.varName();
-// Value value = results.value();
+ RDFNode value = (RDFNode) binding.get(varName);
Object cachedName = varNames.get(node.toString());
- if (varName.equals(cachedName))
- {
-// if (value.isRDFLiteral()) {
-// return value.getRDFLiteral();
-// } else if (value.isRDFResource()) {
-// return value.getRDFResource();
-// }
+ if (varName.equals(cachedName)) {
+ return value;
}
}
return node;
More information about the MOBY-guts
mailing list