[MOBY-guts] biomoby commit
Gary Schlitz
gss at pub.open-bio.org
Wed Jun 16 23:29:20 UTC 2004
gss
Wed Jun 16 19:29:20 EDT 2004
Update of /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/parser
In directory pub.open-bio.org:/tmp/cvs-serv22562/S-MOBY/ref-impl/core/src/org/smoby/parser
Modified Files:
Parser.java
Log Message:
Documentation
moby-live/S-MOBY/ref-impl/core/src/org/smoby/parser Parser.java,1.1,1.2
===================================================================
RCS file: /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/parser/Parser.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/parser/Parser.java 2004/05/24 22:05:35 1.1
+++ /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/parser/Parser.java 2004/06/16 23:29:20 1.2
@@ -3,15 +3,17 @@
import java.io.*;
import java.net.*;
import java.util.*;
-
import org.smoby.graph.*;
import org.smoby.graph.impl.*;
import org.smoby.parser.*;
import org.smoby.vocabulary.*;
-
import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.vocabulary.*;
+/**
+ * This class is used to parse OWL models, stored as Jena2 models, into
+ * objects that implement interfaces from the org.smoby.graph package.
+ */
public class Parser
{
/**
@@ -25,15 +27,17 @@
public final static String LANGUAGE_N3 = "N3";
/**
- * The underlying Jena model on which the parsed graph is based
+ * Inference models containing inferred statements derived using
+ * rdfs:subClassOf relationships.
*/
- private Model model;
+ private Map inferenceModels = new HashMap();
/**
- * An inference model containing inferred statements derived using
- * rdfs:subClassOf relationships.
+ * Return the inference model associated with a given plain model
*/
- private InfModel inferenceModel;
+ private InfModel getInferenceModel(Model model) {
+ return (InfModel) inferenceModels.get(model);
+ }
/**
* Create and return a parser for parsing an N3 file
@@ -86,8 +90,8 @@
}
/**
- * Create and return a parser for parsing an input stream having
- * the given format
+ * Create and return a parser for parsing an input stream containing
+ * the given language
* @return a new instance of Parser
* @throws IOException if reading the stream
* @throws UnparsableGraphException if the file doesn't contain a
@@ -105,6 +109,11 @@
return new Parser(model);
}
+ /**
+ * Create and return a parser for parsing a string in a given language
+ * @param graphString the string containing the graph
+ * @param language the language the model is expressed in
+ */
public static Parser forString(String graphString, String language)
throws UnparsableGraphException
{
@@ -114,28 +123,71 @@
}
/**
- * Create an instance for parsing from the given model
+ * Create an instance for parsing the given model.
*/
public Parser(Model model)
{
- this.model = model;
- this.inferenceModel = ModelFactory.createRDFSModel(model);
- }
-
- /**
- * Return the underlying model
- */
- public Model getModel()
- {
- return model;
+
+ StmtIterator it = model.listStatements(null, RDF.type, MOBY.Provider);
+ while (it.hasNext())
+ {
+ Statement stmt = it.nextStatement();
+ Model subModel = ModelFactory.createDefaultModel();
+ subModel.setNsPrefixes(model.getNsPrefixMap());
+ Resource subject = stmt.getSubject();
+ addReachableStmts(subModel, subject, subject.getURI());
+ inferenceModels.put(subModel,
+ ModelFactory.createRDFSModel(subModel));
+ }
}
+ private void addReachableStmts(Model model, Resource subject, String uri)
+ {
+ // For each statement whose subject is the given resource
+ //
+ StmtIterator it = subject.getModel().listStatements(
+ subject, null, (RDFNode) null);
+
+ while (it.hasNext())
+ {
+ Statement stmt = it.nextStatement();
+
+ // The statement itself should be added
+ //
+ model.add(model.createStatement(
+ stmt.getSubject(), stmt.getPredicate(), stmt.getObject()));
+
+ // If the object of the statement is a blank node or a
+ // resource whose URI starts with the provider URI, then
+ // recursively add the statements that are reachable
+ // from the object
+ //
+ if (stmt.getObject().canAs(Resource.class))
+ {
+ Resource object = (Resource) stmt.getObject().as(Resource.class);
+ if (object.isAnon() || object.getURI().startsWith(uri)) {
+ addReachableStmts(model, object, uri);
+ }
+ }
+ }
+ }
+
/**
- * Return the inference model
+ * Parse a provider from the model. If the model contains
+ * multiple providers, only one will be returned (at random). If
+ * the model is expected to contain multiple providers, then call
+ * parseProviders() instead to get all the providers in a
+ * MOBYCollection.
*/
- public InfModel getInferenceModel()
+ public MOBYProvider parseProvider()
{
- return inferenceModel;
+ MOBYUnorderedCollection providers = parseProviders();
+
+ if (providers.size() == 0) {
+ return null;
+ } else {
+ return (MOBYProvider) providers.iterator().next();
+ }
}
/**
@@ -147,38 +199,28 @@
// Find the statements that say something is of type Provider
//
- StmtIterator it = model.listStatements(null, RDF.type, MOBY.Provider);
+// StmtIterator it = underlying.listStatements(null, RDF.type, MOBY.Provider);
+
+ Iterator it = inferenceModels.keySet().iterator();
// Parse each provider
//
while (it.hasNext())
{
- Statement stmt = it.nextStatement();
- Resource provider = stmt.getSubject();
- providers.add(parseProvider(provider));
+ Model model = (Model) it.next();
+ StmtIterator it2 = model.listStatements(null, RDF.type, MOBY.Provider);
+
+ if (it2 != null)
+ {
+ Statement stmt = it2.nextStatement();
+ Resource provider = stmt.getSubject();
+ providers.add(parseProvider(provider));
+ }
}
// Create and return an unordered collection of the parsed providers
//
- return MOBYObjectFactory.newUnorderedCollection(null, providers, model);
- }
-
- /**
- * Parse a provider from the model. If the model contains
- * multiple providers, only one will be parsed (at random). If
- * the model is expected to contain multiple providers, then call
- * parseProviders() instead to get all the providers in a
- * MOBYCollection.
- */
- public MOBYProvider parseProvider()
- {
- StmtIterator it = model.listStatements(null, RDF.type, MOBY.Provider);
-
- if (it.hasNext()) {
- return parseProvider(it.nextStatement().getSubject());
- } else {
- return null;
- }
+ return MOBYObjectFactory.newUnorderedCollection(null, providers, null);
}
/**
@@ -187,6 +229,8 @@
*/
public MOBYProvider parseProvider(Resource provider)
{
+ Model model = provider.getModel();
+
// First parse the scalar properties name, oneLineDescription, and aboutURI
//
String name = getDataPropertyValue(provider, MOBY.name);
@@ -223,6 +267,8 @@
*/
private MOBYGraphNode parseOperatesOn(Resource res)
{
+ Model model = res.getModel();
+
if (isGraph(res))
{
// The resource is a MOBY Graph, so should have exactly one
@@ -263,6 +309,8 @@
*/
private MOBYSubject parseHasMapping(Resource res)
{
+ Model model = res.getModel();
+
// Collect the properties of this subject resource that are
// subproperties of moby:Property, and create MOBY statements
// for each.
@@ -354,6 +402,7 @@
// Create and return a new MOBYObject
//
+ Model model = res.getModel();
return MOBYObjectFactory.newObject(res.getURI(),
MOBYObjectFactory.newFixedCollection(null, statements, model),
collectionFor(res, nested), model);
@@ -375,6 +424,9 @@
Property p = stmt.getPredicate();
RDFNode pval = stmt.getObject();
+ Model model = res.getModel();
+ Model inferenceModel = getInferenceModel(model);
+
if (model.contains(res, p, pval) &&
inferenceModel.contains(p, RDFS.subPropertyOf, MOBY.Property))
{
@@ -409,13 +461,13 @@
private String getDataPropertyValue(Resource subject, Property property)
{
- Statement s = model.getProperty(subject, property);
+ Statement s = subject.getModel().getProperty(subject, property);
return (s == null) ? null : s.getObject().toString();
}
private Resource getResourcePropertyValue(Resource subject, Property property)
{
- Statement s = model.getProperty(subject, property);
+ Statement s = subject.getModel().getProperty(subject, property);
return (s == null) ? null : (Resource) s.getObject();
}
@@ -445,185 +497,100 @@
private MOBYCollection collectionFor(Resource res, List elements)
{
if (isList(res)) {
- return MOBYObjectFactory.newFixedCollection(null, elements, model);
+ return MOBYObjectFactory.newFixedCollection(null, elements, res.getModel());
} else if (isBag(res)) {
- return MOBYObjectFactory.newUnorderedCollection(null, elements, model);
+ return MOBYObjectFactory.newUnorderedCollection(null, elements, res.getModel());
} else if (isSeq(res)) {
- return MOBYObjectFactory.newOrderedCollection(null, elements, model);
+ return MOBYObjectFactory.newOrderedCollection(null, elements, res.getModel());
} else if (isAlt(res)) {
- return MOBYObjectFactory.newEnumeration(null, elements, model);
+ return MOBYObjectFactory.newEnumeration(null, elements, res.getModel());
} else {
return null;
}
}
- private boolean isList(Resource res) { return res.canAs(RDFList.class); }
- private boolean isBag (Resource res) { return isType(res, RDF.Bag); }
- private boolean isSeq (Resource res) { return isType(res, RDF.Seq); }
- private boolean isAlt (Resource res) { return isType(res, RDF.Alt); }
-
- private boolean isDataStructure(Resource r) {
- return isList(r) || isBag(r) || isSeq(r) || isAlt(r);
+ /**
+ * Return whether or not the given resource is an RDF List
+ */
+ private boolean isList(Resource res)
+ {
+ return res.canAs(RDFList.class);
}
-
- private boolean isGraph(Resource r) {
- return isType(r, MOBY.Graph);
+
+ /**
+ * Return whether or not the given resource is an RDF Bag
+ */
+ private boolean isBag (Resource res)
+ {
+ return isType(res, RDF.Bag);
}
-
- private boolean isSubject(Resource res) {
- return isType(res, MOBY.Subject);
+
+ /**
+ * Return whether or not the given resource is an RDF Seq
+ */
+ private boolean isSeq (Resource res)
+ {
+ return isType(res, RDF.Seq);
}
-
- private boolean isObject(Resource res) {
- return isType(res, MOBY.Object);
+
+ /**
+ * Return whether or not the given resource is an RDF Alt
+ */
+ private boolean isAlt (Resource res)
+ {
+ return isType(res, RDF.Alt);
}
- private boolean isType(Resource res, Resource type) {
- return model.contains(res, RDF.type, type);
+ /**
+ * Return whether or not the given resource is an RDF data structure,
+ * i.e. a List, Bag, Seq, or Alt
+ */
+ private boolean isDataStructure(Resource r)
+ {
+ return isList(r) || isBag(r) || isSeq(r) || isAlt(r);
}
- public static String unparse(MOBYProvider provider) {
- return Unparser.unparse(provider, 0);
+ /**
+ * Return whether or not the given resource is a MOBY Graph,
+ * i.e. the model contains a statement with the given resource
+ * as its subject, a predicate of rdf:type, and an object of
+ * moby:Graph
+ */
+ private boolean isGraph(Resource r)
+ {
+ return isType(r, MOBY.Graph);
}
-
- public static String unparse(MOBYGraph graph) {
- return Unparser.unparse(graph, 0);
+
+ /**
+ * Return whether or not the given resource is a MOBY Subject,
+ * i.e. the model contains a statement with the given resource
+ * as its subject, a predicate of rdf:type, and an object of
+ * moby:Subject
+ */
+ private boolean isSubject(Resource res)
+ {
+ return isType(res, MOBY.Subject);
}
-
- public static String unparse(MOBYCollection collection) {
- return Unparser.unparse(collection, 0);
+
+ /**
+ * Return whether or not the given resource is a MOBY Object,
+ * i.e. the model contains a statement with the given resource
+ * as its subject, a predicate of rdf:type, and an object of
+ * moby:Object
+ */
+ private boolean isObject(Resource res)
+ {
+ return isType(res, MOBY.Object);
}
-
- private static class Unparser
+
+ /**
+ * Return whether or not the given resource is of the given type,
+ * i.e. the model contains a statement with the given resource
+ * as its subject, a predicate of rdf:type, and an object of the
+ * given type
+ */
+ private boolean isType(Resource res, Resource type)
{
- private static String unparse(MOBYProvider provider, int indent)
- {
- StringBuffer sb = new StringBuffer();
-
- indent(sb, indent);
- sb.append("Provider {");
- newline(sb);
- indent(sb, indent+4);
- sb.append("name=\""); sb.append(provider.getName()); sb.append("\"");
- newline(sb);
- indent(sb, indent+4);
- sb.append("oneLineDescription=\"");
- sb.append(provider.getOneLineDescription()); sb.append("\"");
- newline(sb);
- indent(sb, indent+4);
- sb.append("moreInfoURI=\"");
- sb.append(provider.getMoreInfoURI().toString()); sb.append("\"");
-
- for (Iterator it = provider.getOperatesOn().iterator(); it.hasNext();)
- {
- MOBYGraphNode node = (MOBYGraphNode) it.next();
- newline(sb);
- indent(sb, indent+4);
- sb.append("Operates on:");
- newline(sb);
- if (node.isSingular()) {
- sb.append(unparse((MOBYGraph) node, indent+8));
- } else {
- sb.append(unparse((MOBYCollection) node, indent+8));
- }
- }
- newline(sb);
- indent(sb, indent);
- sb.append("}");
-
- return sb.toString();
- }
-
- private static String unparse(MOBYGraph graph, int indent)
- {
- StringBuffer sb = new StringBuffer();
-
- indent(sb, indent);
- sb.append("Graph {");
- newline(sb);
- MOBYGraphNode node = graph.gethasMapping();
- if (node.isSingular()) {
- sb.append(unparse((MOBYSubject) node, indent+4));
- } else {
- sb.append(unparse((MOBYCollection) node, indent+4));
- }
- indent(sb, indent);
- sb.append("}");
-
- return sb.toString();
- }
-
- private static String unparse(MOBYSubject subject, int indent)
- {
- StringBuffer sb = new StringBuffer();
- indent(sb, indent);
- if (subject.isBlank()) {
- sb.append("<bnode>");
- } else {
- sb.append("<"); sb.append(subject.getURI()); sb.append(">");
- }
- newline(sb);
-
- return sb.toString();
- }
-
- private static String unparse(MOBYObject object, int indent)
- {
- StringBuffer sb = new StringBuffer();
-
- return sb.toString();
- }
-
- private static String unparse(MOBYCollection c, int indent)
- {
- StringBuffer sb = new StringBuffer();
- indent(sb, indent);
- sb.append(descriptionOf(c));
- sb.append(" {");
- for (Iterator it = c.iterator(); it.hasNext();)
- {
- newline(sb);
- Object element = it.next();
- if (element instanceof MOBYProvider) {
- sb.append(unparse((MOBYProvider) element, indent+4));
- } else if (element instanceof MOBYCollection) {
- sb.append(unparse((MOBYCollection) element, indent+4));
- } else if (element instanceof MOBYGraph) {
- sb.append(unparse((MOBYGraph) element, indent+4));
- } else if (element instanceof MOBYSubject) {
- sb.append(unparse((MOBYSubject) element, indent+4));
- } else if (element instanceof MOBYObject) {
- sb.append(unparse((MOBYObject) element, indent+4));
- } else {
- indent(sb, indent+4);
- sb.append(element.toString());
- }
- }
- newline(sb);
- indent(sb, indent);
- sb.append("}");
- return sb.toString();
- }
-
- private static void newline(StringBuffer sb) {
- sb.append("\n");
- }
-
- private static void indent(StringBuffer sb, int count) {
- for (int i = 0; i < count; i++) {
- sb.append(' ');
- }
- }
-
- private static String descriptionOf(MOBYCollection c) {
-
- String name = "Collection";
- if (c instanceof MOBYFixedCollection) name = "Fixed collection";
- else if (c instanceof MOBYEnumeration) name = "Choice collection";
- else if (c instanceof MOBYOrderedCollection) name = "Ordered collection";
- else if (c instanceof MOBYUnorderedCollection) name = "Unordered collection";
-
- return name + " of " + c.size() + " elements";
- }
+ return res.getModel().contains(res, RDF.type, type);
}
}
\ No newline at end of file
More information about the MOBY-guts
mailing list