[MOBY-guts] biomoby commit

Gary Schlitz gss at pub.open-bio.org
Fri Jun 18 21:38:46 UTC 2004


gss
Fri Jun 18 17:38:46 EDT 2004
Update of /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/tools
In directory pub.open-bio.org:/tmp/cvs-serv29852/src/org/smoby/tools

Modified Files:
	Util.java 
Log Message:
New utility methods

moby-live/S-MOBY/ref-impl/core/src/org/smoby/tools Util.java,1.1,1.2
===================================================================
RCS file: /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/tools/Util.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/tools/Util.java	2004/05/24 22:05:36	1.1
+++ /home/repository/moby/moby-live/S-MOBY/ref-impl/core/src/org/smoby/tools/Util.java	2004/06/18 21:38:46	1.2
@@ -4,6 +4,15 @@
 import java.util.*;
 
 import org.apache.commons.httpclient.util.DateParser;
+import org.smoby.vocabulary.MOBY;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import com.hp.hpl.jena.rdf.model.ModelFactory;
+import com.hp.hpl.jena.rdf.model.NsIterator;
+import com.hp.hpl.jena.rdf.model.RDFNode;
+import com.hp.hpl.jena.rdf.model.Resource;
+import com.hp.hpl.jena.rdf.model.Statement;
+import com.hp.hpl.jena.rdf.model.StmtIterator;
 
 /**
  * This abstract class contains a miscellaneous set of useful static methods
@@ -26,4 +35,119 @@
     {
     	return HTTP_DATE_FORMAT.format(date);
     }
+    
+    public static Model newJenaModel()
+    {
+    	Model model = ModelFactory.createDefaultModel();
+    	MOBY.addNsPrefix(model);
+    	return model;
+    }
+    
+	public static void addReachableStmts(
+		Model source, Model dest, Resource subject, String uri)
+	{
+		// For each statement whose subject is the given resource
+		//
+		StmtIterator it = source.listStatements(
+			subject, null, (RDFNode) null);
+		
+		while (it.hasNext())
+		{
+			Statement stmt = it.nextStatement();
+			
+			// The statement itself should be added
+			//
+			dest.add(dest.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() || (uri != null && object.getURI().startsWith(uri))) {
+					addReachableStmts(source, dest, object, uri);
+				}
+			}
+		}
+	}
+	
+	/**
+	 * Add the namespace prefix mappings from a source model to a destination
+	 * model. A prefix mapping associates a qualified name (e.g. 'moby') with
+	 * a full URL (e.g. 'http://www.s-moby.org:8080/terms/'). In case a prefix
+	 * is already in the destination model, then use the next available prefix
+	 * formed by adding a number to the end of the prefix (e.g. if 'foo' is
+	 * taken, then add 'foo_1' if it is available, or 'foo_2' if not...).
+	 */
+	public static void addNsPrefixes(Model source, Model dest)
+	{
+		Map prefixes = source.getNsPrefixMap();
+		Iterator it = prefixes.keySet().iterator();
+		
+		while (it.hasNext())
+		{
+			// Prefix and associated URI to be added
+			//
+			String prefix = it.next().toString();
+			String uri = prefixes.get(prefix).toString();
+			
+			// Check to see if the prefix is already in the destination model
+			//
+			String prevURI = dest.getNsPrefixURI(prefix);
+			
+			if (prevURI == null)
+			{
+				// Not used, so just add it
+				//
+				dest.setNsPrefix(prefix, uri);
+			}
+			else if (! prevURI.equals(uri))
+			{
+				// Used and doesn't match previous, so add the next
+				// available prefix
+				//
+				boolean added = false;
+				for (int i = 1; (! added); i++)
+				{
+					String newPrefix = prefix + "_" + i;
+					if (dest.getNsPrefixURI(newPrefix) == null)
+					{
+						dest.setNsPrefix(newPrefix, uri);
+						added = true;
+					}
+				}
+			}
+		}
+	}
+	
+	/**
+	 * Remove unused namespace prefixes from the given model.
+	 */
+	public static void removeUnusedNsPrefixes(Model model)
+	{
+		Map newMap = new HashMap();
+		
+		for (NsIterator it = model.listNameSpaces(); it.hasNext();)
+		{
+			String uri = it.nextNs();
+			String prefix = model.getNsURIPrefix(uri);
+			newMap.put(prefix, uri);
+		}
+		
+		List toRemove = new ArrayList();
+		Iterator it = model.getNsPrefixMap().keySet().iterator();
+		while (it.hasNext()) {
+			toRemove.add(it.next());
+		}
+		it = toRemove.iterator();
+		while (it.hasNext()) {
+			model.removeNsPrefix(it.next().toString());
+		}
+		
+		model.setNsPrefixes(newMap);
+	}
 }




More information about the MOBY-guts mailing list