[MOBY-guts] biomoby commit

Eddie Kawas kawas at pub.open-bio.org
Wed Mar 15 16:22:38 UTC 2006


kawas
Wed Mar 15 11:22:38 EST 2006
Update of /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder
In directory pub.open-bio.org:/tmp/cvs-serv32102/org/biomoby/client/rdf/builder

Modified Files:
	ServiceInstanceRDF.java RDFConfigure.java 
Log Message:
updated RDF generators and clients so that LSIDs with versioining information is used. This is only preliminary. More updates to come

moby-live/Java/src/main/org/biomoby/client/rdf/builder ServiceInstanceRDF.java,1.12,1.13 RDFConfigure.java,1.8,1.9
===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder/ServiceInstanceRDF.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder/ServiceInstanceRDF.java	2006/02/27 22:42:30	1.12
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder/ServiceInstanceRDF.java	2006/03/15 16:22:38	1.13
@@ -335,10 +335,7 @@
 			MobyData[] secondaryInputs = services[i].getSecondaryInputs(); // consumes
 			MobyData[] outputs = services[i].getPrimaryOutputs(); // produces
 			// start creating the rdf
-			Resource subject = model.createResource(
-					"urn:lsid:biomoby.org:serviceinstance:"
-							+ services[i].getAuthority() + ","
-							+ services[i].getName(),
+			Resource subject = model.createResource(services[i].getLSID(),
 					FetaVocabulary.serviceDescription);
 			subject.addProperty(FetaVocabulary.hasServiceDescriptionLocation,
 					services[i].getSignatureURL());
@@ -396,10 +393,9 @@
 								.addProperty(
 										ServiceDescriptionPredicates.object_type,
 										model
-												.createResource("http://biomoby.org/RESOURCES/MOBY-S/Objects#"
-														+ simpleData
+												.createResource(simpleData
 																.getDataType()
-																.getName()));
+																.getLSID()));
 
 						MobyNamespace[] namespaces = simpleData.getNamespaces();
 						if (namespaces.length > 0) {
@@ -412,9 +408,8 @@
 								parameterNamespace
 										.addProperty(
 												ServiceDescriptionPredicates.namespace_type,
-												model.createResource(NS
-														+ namespaces[k]
-																.getName()));
+												model.createResource(namespaces[k]
+																.getLSID()));
 							}
 						}
 						hasOperation.addProperty(FetaVocabulary.inputParameter,
@@ -442,10 +437,9 @@
 								parameter
 										.addProperty(
 												ServiceDescriptionPredicates.object_type,
-												model.createResource(OBJ
-														+ setOfSimpleData[k]
+												model.createResource(setOfSimpleData[k]
 																.getDataType()
-																.getName()));
+																.getLSID()));
 								MobyNamespace[] namespaces = setOfSimpleData[k]
 										.getNamespaces();
 								if (namespaces.length > 0) {
@@ -456,9 +450,8 @@
 												.addProperty(
 														ServiceDescriptionPredicates.namespace_type,
 														model
-																.createResource(NS
-																		+ namespaces[index]
-																				.getName()));
+																.createResource(namespaces[index]
+																				.getLSID()));
 									}
 									parameter
 											.addProperty(
@@ -565,10 +558,9 @@
 								.addProperty(
 										ServiceDescriptionPredicates.object_type,
 										model
-												.createResource("http://biomoby.org/RESOURCES/MOBY-S/Objects#"
-														+ simpleData
+												.createResource(simpleData
 																.getDataType()
-																.getName()));
+																.getLSID()));
 						MobyNamespace[] namespaces = simpleData.getNamespaces();
 						if (namespaces.length > 0) {
 							Resource parameterNamespace = model
@@ -584,9 +576,8 @@
 								parameterNamespace
 										.addProperty(
 												ServiceDescriptionPredicates.namespace_type,
-												model.createResource(NS
-														+ namespaces[k]
-																.getName()));
+												model.createResource(namespaces[k]
+																.getLSID()));
 							}
 						}
 						hasOperation.addProperty(
@@ -614,10 +605,9 @@
 								parameter
 										.addProperty(
 												ServiceDescriptionPredicates.object_type,
-												model.createResource(OBJ
-														+ setOfSimpleData[k]
+												model.createResource(setOfSimpleData[k]
 																.getDataType()
-																.getName()));
+																.getLSID()));
 								MobyNamespace[] namespaces = setOfSimpleData[k]
 										.getNamespaces();
 								if (namespaces.length > 0) {
@@ -628,9 +618,8 @@
 												.addProperty(
 														ServiceDescriptionPredicates.namespace_type,
 														model
-																.createResource(NS
-																		+ namespaces[index]
-																				.getName()));
+																.createResource(namespaces[index]
+																				.getLSID()));
 									}
 									parameter
 											.addProperty(
@@ -715,6 +704,98 @@
 		}
 		return services.length == 1;
 	}
+	
+	public final static boolean serviceInstanceExists(String uri, String name, String lsid) {
+		Central central = null;
+		MobyService service = null;
+		MobyService[] services = null;
+		// initialize central
+		try {
+			String registryURL;
+			String registryNamespace;
+			Map map = RegistryOS.getEnv();
+			if (!map.isEmpty()) {
+				registryURL = (String) map.get("MOBY_SERVER");
+				registryNamespace = (String) map.get("MOBY_URI");
+				if (registryURL == null) {
+					// use default registry, since we dont have a local one
+					central = new CentralImpl();
+				} else if (registryNamespace == null) {
+					// use default namespace since one isnt defined
+					central = new CentralImpl(registryURL);
+				} else {
+					// use defined url and namespacce
+					central = new CentralImpl(registryURL, registryNamespace);
+				}
+			} else {
+				// use default registry since we cant determine if local one
+				// exists
+				central = new CentralImpl();
+			}
+		} catch (MobyException e) {
+			return false;
+		}
+		// set up the query service
+		service = new MobyService(name);
+		service.setAuthority(uri);
+		// query for the service
+		try {
+			services = central.findService(service);
+		} catch (MobyException e1) {
+			return false;
+		}
+		if (services.length == 1) {
+			service = services[0];
+			return (service.getLSID().equals(lsid));
+		}
+		return false;
+	}
+	
+	public final static String getLatestLSID(String uri, String name, String lsid) {
+		Central central = null;
+		MobyService service = null;
+		MobyService[] services = null;
+		// initialize central
+		try {
+			String registryURL;
+			String registryNamespace;
+			Map map = RegistryOS.getEnv();
+			if (!map.isEmpty()) {
+				registryURL = (String) map.get("MOBY_SERVER");
+				registryNamespace = (String) map.get("MOBY_URI");
+				if (registryURL == null) {
+					// use default registry, since we dont have a local one
+					central = new CentralImpl();
+				} else if (registryNamespace == null) {
+					// use default namespace since one isnt defined
+					central = new CentralImpl(registryURL);
+				} else {
+					// use defined url and namespacce
+					central = new CentralImpl(registryURL, registryNamespace);
+				}
+			} else {
+				// use default registry since we cant determine if local one
+				// exists
+				central = new CentralImpl();
+			}
+		} catch (MobyException e) {
+			return "";
+		}
+		// set up the query service
+		service = new MobyService(name);
+		service.setAuthority(uri);
+		// query for the service
+		try {
+			services = central.findService(service);
+		} catch (MobyException e1) {
+			return "";
+		}
+		if (services.length == 1) {
+			service = services[0];
+			return (service.getLSID());
+		}
+		return "";
+	}
 
 	/**
 	 * 
@@ -729,11 +810,8 @@
 	 * @throws LSIDException
 	 */
 	public final static void main(String[] args) throws Exception {
-		Central central = new CentralImpl();
-		System.out.println(central.findService(new MobyService(
-				"aSimpleInputOutputService"))[0]);
 		String str = (findService("bioinfo.icapture.ubc.ca",
-				"aSimpleInputOutputService", false));
+				"parseBlastText", false));
 		System.out.println(str);
 		// System.out.println(findService("bioinfo.genopole-toulouse.prd.fr",null,
 		// false));

===================================================================
RCS file: /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder/RDFConfigure.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder/RDFConfigure.java	2006/02/20 05:51:09	1.8
+++ /home/repository/moby/moby-live/Java/src/main/org/biomoby/client/rdf/builder/RDFConfigure.java	2006/03/15 16:22:38	1.9
@@ -18,20 +18,21 @@
 import org.biomoby.shared.Central;
 import org.biomoby.shared.MobyResourceRef;
 
-import com.hp.hpl.jena.graph.Node;
-import com.hp.hpl.jena.rdf.model.Bag;
 import com.hp.hpl.jena.rdf.model.Model;
 import com.hp.hpl.jena.rdf.model.ModelFactory;
 import com.hp.hpl.jena.rdf.model.NodeIterator;
-import com.hp.hpl.jena.rdf.model.Property;
 import com.hp.hpl.jena.rdf.model.RDFNode;
 import com.hp.hpl.jena.rdf.model.RDFReader;
 import com.hp.hpl.jena.rdf.model.RDFWriter;
+import com.hp.hpl.jena.rdf.model.ResIterator;
 import com.hp.hpl.jena.rdf.model.Resource;
 import com.hp.hpl.jena.rdf.model.Statement;
 import com.hp.hpl.jena.rdf.model.StmtIterator;
+import com.hp.hpl.jena.vocabulary.DC;
 import com.hp.hpl.jena.vocabulary.RDF;
 import com.hp.hpl.jena.vocabulary.RDFS;
+import com.ibm.lsid.LSID;
+import com.ibm.lsid.MalformedLSIDException;
 
 /*
  * Created on Oct 21, 2004 <p>
@@ -43,8 +44,8 @@
  * @author Eddie Kawas
  *         <p>
  *         This class was created to create RDF documents describing BioMoby
- *         ontologies. This class requires the use of Environment variables in 
- *         order to work correctly.  
+ *         ontologies. This class requires the use of Environment variables in
+ *         order to work correctly.
  *         <p>
  *         For questions, comments, or bugs
  *         <p>
@@ -57,77 +58,78 @@
 	 * Some constants for use in the rdfs
 	 */
 	private static Map URI_MAP = new HashMap();
-	static { 
-    	boolean __success = false;
+	static {
+		boolean __success = false;
 		// attempt 1 - servlet context
-    	try {
-    		Context c = new InitialContext();
-    		String MOBY_SERVER = (String) c.lookup("java:comp/env/MOBY_SERVER");
-    		if (MOBY_SERVER != null && !MOBY_SERVER.equals("")) {
-    			Central central = new CentralImpl(MOBY_SERVER);
-    			MobyResourceRef[] refs = central.getResourceRefs();
-    			for (int i = 0; i < refs.length; i++) {
-    				String url = refs[i].getResourceLocation().toExternalForm();
-    				if (url.lastIndexOf("#") < 0)
-    					url = url + "#";
-					URI_MAP.put(refs[i].getResourceName(),url);
+		try {
+			Context c = new InitialContext();
+			String MOBY_SERVER = (String) c.lookup("java:comp/env/MOBY_SERVER");
+			if (MOBY_SERVER != null && !MOBY_SERVER.equals("")) {
+				Central central = new CentralImpl(MOBY_SERVER);
+				MobyResourceRef[] refs = central.getResourceRefs();
+				for (int i = 0; i < refs.length; i++) {
+					String url = refs[i].getResourceLocation().toExternalForm();
+					if (url.lastIndexOf("#") < 0)
+						url = url + "#";
+					URI_MAP.put(refs[i].getResourceName(), url);
 				}
-    			__success = true;
-    		}
-    		
-    	} catch (Exception e) {
-    		e.printStackTrace();
-    	}
-    	// attempt 2 - environment variables
-    	if (!__success) {
-    		// maybe try for environment variables
-    		Properties p = MobyProperties.PROPERTIES();
-    		String MOBY_SERVER = (String) p.get("MOBY_SERVER");
-    		try {
-    		if (MOBY_SERVER != null && !MOBY_SERVER.equals("")) {
-    			Central central = new CentralImpl(MOBY_SERVER);
-    			MobyResourceRef[] refs = central.getResourceRefs();
-    			for (int i = 0; i < refs.length; i++) {
-    				String url = refs[i].getResourceLocation().toExternalForm();
-    				if (url.lastIndexOf("#") < 0)
-    					url = url + "#";
-					URI_MAP.put(refs[i].getResourceName(),url);
+				__success = true;
+			}
+
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
+		// attempt 2 - environment variables
+		if (!__success) {
+			// maybe try for environment variables
+			Properties p = MobyProperties.PROPERTIES();
+			String MOBY_SERVER = (String) p.get("MOBY_SERVER");
+			try {
+				if (MOBY_SERVER != null && !MOBY_SERVER.equals("")) {
+					Central central = new CentralImpl(MOBY_SERVER);
+					MobyResourceRef[] refs = central.getResourceRefs();
+					for (int i = 0; i < refs.length; i++) {
+						String url = refs[i].getResourceLocation().toExternalForm();
+						if (url.lastIndexOf("#") < 0)
+							url = url + "#";
+						URI_MAP.put(refs[i].getResourceName(), url);
+					}
+					__success = true;
 				}
-    			__success = true;
-    		}
-    		}catch (Exception ex) {
-    			// die here?
-    			ex.printStackTrace();
-    		}
-    	}
-    	//attempt 3 - default to mobycentral
-    	if (!__success) {
-    		// default to mobycentral
-    		try {
-    			Central central = new CentralImpl();
-    			MobyResourceRef[] refs = central.getResourceRefs();
-    			for (int i = 0; i < refs.length; i++) {
-    				String url = refs[i].getResourceLocation().toExternalForm();
-    				if (url.lastIndexOf("#") < 0)
-    					url = url + "#";
-					URI_MAP.put(refs[i].getResourceName(),url);
+			} catch (Exception ex) {
+				// die here?
+				ex.printStackTrace();
+			}
+		}
+		// attempt 3 - default to mobycentral
+		if (!__success) {
+			// default to mobycentral
+			try {
+				Central central = new CentralImpl();
+				MobyResourceRef[] refs = central.getResourceRefs();
+				for (int i = 0; i < refs.length; i++) {
+					String url = refs[i].getResourceLocation().toExternalForm();
+					if (url.lastIndexOf("#") < 0)
+						url = url + "#";
+					URI_MAP.put(refs[i].getResourceName(), url);
 				}
-    			__success = true;
-    		}catch (Exception ex) {
-    			// die here?
-    			ex.printStackTrace();
-    		}
-    	}
+				__success = true;
+			} catch (Exception ex) {
+				// die here?
+				ex.printStackTrace();
+			}
+		}
 	}
-		final public static String URI_OBJECT = (String)URI_MAP.get("Object"); //"http://biomoby.org/RESOURCES/MOBY-S/Objects#";
 
-		final public static String URI_SERVICE = (String)URI_MAP.get("Service"); //"http://biomoby.org/RESOURCES/MOBY-S/Services#";
+	final public static String URI_OBJECT = (String) URI_MAP.get("Object"); // "http://biomoby.org/RESOURCES/MOBY-S/Objects#";
 
-		final public static String URI_NAMESPACE = (String)URI_MAP.get("Namespace"); //"http://biomoby.org/RESOURCES/MOBY-S/Namespaces#";
+	final public static String URI_SERVICE = (String) URI_MAP.get("Service"); // "http://biomoby.org/RESOURCES/MOBY-S/Services#";
 
-		final public static String URI_PREDICATE = "http://biomoby.org/RESOURCES/MOBY-S/Predicates#";
+	final public static String URI_NAMESPACE = (String) URI_MAP.get("Namespace"); // "http://biomoby.org/RESOURCES/MOBY-S/Namespaces#";
 
-		final public static String URI_ServiceINSTANCE = (String)URI_MAP.get("ServiceInstance"); //"http://biomoby.org/RESOURCES/MOBY-S/ServiceInstances#";
+	final public static String URI_PREDICATE = "http://biomoby.org/RESOURCES/MOBY-S/Predicates#";
+
+	final public static String URI_ServiceINSTANCE = (String) URI_MAP.get("ServiceInstance"); // "http://biomoby.org/RESOURCES/MOBY-S/ServiceInstances#";
 
 	private Model model = null;
 
@@ -160,26 +162,52 @@
 	 * <p>
 	 * 
 	 * @param myObject -
-	 *            the URI of the object in question
+	 *            the URI (lsid) of the object in question
 	 * @return the rdf representation of the object in question.
 	 * @throws IOException
 	 */
 	public final byte[] retrieveObjectRDF(String myObject) throws IOException {
-		rdf = ModelFactory.createDefaultModel();
-		rdf.setNsPrefixes(model.getNsPrefixMap());
-		Resource r = model.getResource(myObject);
-		Resource resource = rdf.createResource(r.getURI()).addProperty(
-				RDF.type, RDFS.Class);
-
-		// copy resource r's properties
-		copyResourceAndProperties(r, resource);
-		// now get the output ready
-
-		FilteredStream aPrintStream = new FilteredStream(
-				new ByteArrayOutputStream());
-		createOutputRDF(aPrintStream);
-		// System.out.println(aPrintStream.getOutput());
-		return aPrintStream.getOutput().getBytes();
+
+		String latest = getLatestLSID(myObject);
+		boolean exists = objectExists(myObject) || objectExisted(myObject);
+		if (exists)
+			if (latest.equals(myObject)) {
+				rdf = ModelFactory.createDefaultModel();
+				rdf.setNsPrefixes(model.getNsPrefixMap());
+				Resource r = model.getResource(myObject);
+				Resource resource = rdf.createResource(r.getURI())
+						.addProperty(RDF.type, RDFS.Class);
+
+				// copy resource r's properties
+				copyResourceAndProperties(r, resource);
+				// now get the output ready
+
+				FilteredStream aPrintStream = new FilteredStream(new ByteArrayOutputStream());
+				createOutputRDF(aPrintStream);
+				// System.out.println(aPrintStream.getOutput());
+				return aPrintStream.getOutput().getBytes();
+			} else {
+				Model model = ModelFactory.createDefaultModel();
+				Resource resource = model.createResource(myObject);
+				model.add(model.createStatement(resource, RDFS.isDefinedBy, model
+						.createLiteral(latest)));
+				model.add(model.createStatement(resource, RDFS.comment, model
+						.createLiteral("The Namespace/Datatype/ServiceType described by the LSID: " + myObject
+								+ " has since been modified. Please update your lsid.")));
+				FilteredStream aPrintStream = new FilteredStream(new ByteArrayOutputStream());
+				RDFWriter writer = model.getWriter("RDF/XML-ABBREV");
+				writer.setProperty("showXmlDeclaration", "false");
+				writer.setProperty("tab", "5");
+				writer.write(model, aPrintStream, null);
+				return aPrintStream.getOutput().getBytes();
+			}
+		else {
+			return ("<?xml version=\"1.0\"?>"
+					+ "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
+					+ "  xmlns:dc=\"http://purl.org/dc/elements/1.1/\">" + "</rdf:RDF>")
+					.getBytes();
+		}
+			
 	}
 
 	/*
@@ -195,39 +223,45 @@
 	/*
 	 * given the source, the new resource is a copy
 	 */
-	private final void copyResourceAndProperties(Resource sourceResource,
-			Resource newResource) {
+	private final void copyResourceAndProperties(Resource sourceResource, Resource newResource) {
 		// add the properties to the resource in question
 		if (sourceResource.hasProperty(RDFS.comment)) {
-			String str = sourceResource.getProperty(RDFS.comment).getObject()
-					.toString();
+			String str = sourceResource.getProperty(RDFS.comment).getObject().toString();
 			if (str.indexOf("~") != -1)
 				str = str.substring(0, str.indexOf("~"));
-			newResource.addProperty(RDFS.comment, rdf.createLiteral(str, "en"));
+			newResource.addProperty(RDFS.comment, rdf.createLiteral(str));
 		}
 		if (sourceResource.hasProperty(RDFS.label)) {
-			String str = sourceResource.getProperty(RDFS.label).getObject()
-					.toString();
+			String str = sourceResource.getProperty(RDFS.label).getObject().toString();
+			if (str.indexOf("~") != -1)
+				str = str.substring(0, str.indexOf("~"));
+			newResource.addProperty(RDFS.label, rdf.createLiteral(str));
+		}
+		if (sourceResource.hasProperty(DC.publisher)) {
+			String str = sourceResource.getProperty(DC.publisher).getObject().toString();
 			if (str.indexOf("~") != -1)
 				str = str.substring(0, str.indexOf("~"));
-			newResource.addProperty(RDFS.label, rdf.createLiteral(str, "en"));
+			newResource.addProperty(DC.publisher, rdf.createLiteral(str));
 		}
-		// TODO - add the ofType property if it exists
+		if (sourceResource.hasProperty(DC.creator)) {
+			String str = sourceResource.getProperty(RDFS.label).getObject().toString();
+			if (str.indexOf("~") != -1)
+				str = str.substring(0, str.indexOf("~"));
+			newResource.addProperty(DC.creator, rdf.createLiteral(str));
+		}
+		// add the ofType property if it exists
 		if (sourceResource.hasProperty(Predicates.ofType)) {
-			String str = sourceResource.getProperty(Predicates.ofType)
-					.getObject().toString();
+			String str = sourceResource.getProperty(Predicates.ofType).getObject().toString();
 			if (str.indexOf("~") != -1)
 				str = str.substring(0, str.indexOf("~"));
 			newResource.addProperty(Predicates.ofType, str);
 		}
 		if (sourceResource.hasProperty(RDFS.subClassOf)) {
-			String parent = sourceResource.getProperty(RDFS.subClassOf)
-					.getObject().asNode().getURI();
-			Resource parentResource = rdf.createResource(parent).addProperty(
-					RDF.type, RDFS.Class);
+			String parent = sourceResource.getProperty(RDFS.subClassOf).getObject().asNode()
+					.getURI();
+			Resource parentResource = rdf.createResource(parent).addProperty(RDF.type, RDFS.Class);
 
-			copyResourceAndProperties(model
-					.getResource(parentResource.getURI()), parentResource);
+			copyResourceAndProperties(model.getResource(parentResource.getURI()), parentResource);
 			newResource.addProperty(RDFS.subClassOf, parentResource);
 		}
 
@@ -237,26 +271,22 @@
 		if (it != null) {
 			while (it.hasNext()) {
 				Statement next = it.nextStatement();
-//				Resource subject = next.getSubject();
-//				Property predicate = next.getPredicate();
+				// Resource subject = next.getSubject();
+				// Property predicate = next.getPredicate();
 				RDFNode object = next.getObject();
-//				Node node = object.asNode();
+				// Node node = object.asNode();
 				if (object instanceof Resource) {
 					Resource res = (Resource) object;
-					String type = res.getProperty(RDF.type).getObject()
-							.toString();
-					String articleName = res
-							.getProperty(Predicates.articleName).getObject()
+					String type = res.getProperty(RDF.type).getObject().toString();
+					String articleName = res.getProperty(Predicates.articleName).getObject()
 							.toString();
 					if (articleName.indexOf("~") != -1)
-						articleName = articleName.substring(0, articleName
-								.indexOf("~"));
+						articleName = articleName.substring(0, articleName.indexOf("~"));
 					type = type.substring(type.indexOf("#") + 1);
 					Resource hasa = rdf.createResource(
 					// ModelFactory.createDefaultModel().createResource(
 							rdf.createResource(URI_OBJECT + type)).addProperty(
-							Predicates.articleName,
-							rdf.createTypedLiteral(articleName));
+							Predicates.articleName, rdf.createTypedLiteral(articleName));
 					newResource.addProperty(Predicates.hasa, hasa);
 
 				} else {
@@ -271,26 +301,22 @@
 		if (it != null) {
 			while (it.hasNext()) {
 				Statement next = it.nextStatement();
-//				Resource subject = next.getSubject();
-//				Property predicate = next.getPredicate();
+				// Resource subject = next.getSubject();
+				// Property predicate = next.getPredicate();
 				RDFNode object = next.getObject();
-//				Node node = object.asNode();
+				// Node node = object.asNode();
 				if (object instanceof Resource) {
 					Resource res = (Resource) object;
-					String type = res.getProperty(RDF.type).getObject()
-							.toString();
-					String articleName = res
-							.getProperty(Predicates.articleName).getObject()
+					String type = res.getProperty(RDF.type).getObject().toString();
+					String articleName = res.getProperty(Predicates.articleName).getObject()
 							.toString();
 					if (articleName.indexOf("~") != -1)
-						articleName = articleName.substring(0, articleName
-								.indexOf("~"));
+						articleName = articleName.substring(0, articleName.indexOf("~"));
 					type = type.substring(type.indexOf("#") + 1);
 					Resource has = rdf.createResource(
 					// ModelFactory.createDefaultModel().createResource(
 							rdf.createResource(URI_OBJECT + type)).addProperty(
-							Predicates.articleName,
-							rdf.createTypedLiteral(articleName));
+							Predicates.articleName, rdf.createTypedLiteral(articleName));
 					newResource.addProperty(Predicates.has, has);
 
 				} else {
@@ -312,204 +338,43 @@
 	 * @return the rdf representation of the service instance.
 	 * @throws IOException
 	 */
-	public final byte[] retrieveServiceInstanceRDF(String myObject)
-			throws IOException {
-		String localname = myObject.substring(myObject.indexOf("#") + 1);
+	public final byte[] retrieveServiceInstanceRDF(String lsid) throws IOException {
+		LSID theLsid;
+		try {
+			theLsid = new LSID(lsid);
+		} catch (MalformedLSIDException e) {
+			throw new IOException(e.getLocalizedMessage());
+		}
+		String localname = theLsid.getObject();
 		String[] details = localname.split(",");
-		return ServiceInstanceRDF.findService(details[0], details[1], false)
-				.getBytes();
-
-	}
-
-	/*
-	 * @param resource - the resource to add the property to @param _bag - the
-	 * bag to add all the properties to @param property - the predicate for the
-	 * adding the bag to the resource
-	 */
-	private final void addPropertyToResourceInBag(Resource resource, Bag _bag,
-			Property property) {
-		// get all of the objects of uri the
-		// same as resource with the property we are looking for.
-		NodeIterator _resource;
-		_resource = model.listObjectsOfProperty(resource, property);
-
-		// iterator should only have one item - the main bag
-		while (_resource.hasNext()) {
-			// create a bag - should be a bag.
-			Resource _mainBag = (Resource) model.getRDFNode(_resource
-					.nextNode().asNode());
-
-			StmtIterator mainBagIterator = _mainBag.listProperties();
-			while (mainBagIterator.hasNext()) {
-				// iterate through the statements for the bag
-				Statement statement = mainBagIterator.nextStatement();
-				Node object = statement.getObject().asNode();
-//				Property pred = statement.getPredicate();
-				if (object.isBlank()) {
-					// object is an item in the mainBag
-					// the item that is a resource
-					Resource item = statement.getResource();
-					// get the statements for the item in the bag
-					// could be a bag or just a simple
-//					StmtIterator sit = item.listProperties();
-					if (item.hasProperty(RDF.type, Predicates.Simple)) {
-						// create a simple article
-						Resource _li = rdf.createResource();
-						_li.addProperty(RDF.type, Predicates.Simple);
-						_li.addProperty(Predicates.object_type, model
-								.getResource(item.getProperty(
-										Predicates.object_type).getObject()
-										.toString()));
-						if (item.hasProperty(Predicates.articleName)) {
-							String str = item.getProperty(
-									Predicates.articleName).getObject()
-									.toString();
-							if (str.indexOf("~") > 0)
-								str = str.substring(0, str.indexOf("~"));
-							_li.addProperty(Predicates.articleName, rdf
-									.createTypedLiteral(str));
-						}
-						StmtIterator namespaceIt = item
-								.listProperties(Predicates.namespace_type);
-						while (namespaceIt.hasNext()) {
-							_li.addProperty(Predicates.namespace_type, model
-									.getResource(namespaceIt.nextStatement()
-											.getObject().toString()));
-						}
-						_bag.add(_li);
-					}
-					if (item.hasProperty(RDF.type, Predicates.Secondary)) {
-						// create a secondary article
-						Resource _li = rdf.createResource();
-						_li.addProperty(RDF.type, Predicates.Secondary);
-						boolean isInt = false;
-						if (item.hasProperty(Predicates.datatype)) {
-							String str = item.getProperty(Predicates.datatype)
-									.getObject().toString();
-							if (str.indexOf("~") > 0) {
-								str = str.substring(0, str.indexOf("~"));
-							}
-							if (str.equalsIgnoreCase("Integer"))
-								isInt = true;
-							_li.addProperty(Predicates.datatype, rdf
-									.createTypedLiteral(str));
-						}
-						if (item.hasProperty(Predicates.articleName)) {
-							String str = item.getProperty(
-									Predicates.articleName).getObject()
-									.toString();
-							if (str.indexOf("~") > 0) {
-								str = str.substring(0, str.indexOf("~"));
-							}
-							_li.addProperty(Predicates.articleName, rdf
-									.createTypedLiteral(str));
-						}
-						if (item.hasProperty(Predicates.default_value)) {
-							String str = item.getProperty(
-									Predicates.default_value).getObject()
-									.toString();
-							if (str.indexOf("~") > 0) {
-								str = str.substring(0, str.indexOf("~"));
-							}
-							_li.addProperty(Predicates.default_value, rdf
-									.createTypedLiteral(str));
-						}
-						if (item.hasProperty(Predicates.min)) {
-							String str = item.getProperty(Predicates.min)
-									.getObject().toString();
-							if (str.indexOf("~") > 0) {
-								str = str.substring(0, str.indexOf("~"));
-							}
-							_li.addProperty(Predicates.min, rdf
-									.createTypedLiteral(Integer.parseInt(str)));
-						}
-						if (item.hasProperty(Predicates.max)) {
-							String str = item.getProperty(Predicates.max)
-									.getObject().toString();
-							if (str.indexOf("~") > 0) {
-								str = str.substring(0, str.indexOf(":"));
-							}
-							_li.addProperty(Predicates.max, rdf
-									.createTypedLiteral(Integer.parseInt(str)));
-						}
-						StmtIterator enumIter = item
-								.listProperties(Predicates.enumeration);
-						while (enumIter.hasNext()) {
-							String str = enumIter.nextStatement().getObject()
-									.toString();
-							if (str.indexOf("~") > 0) {
-								str = str.substring(0, str.indexOf("~"));
-							}
-							if (isInt && !str.equals(""))
-								_li.addProperty(Predicates.enumeration, rdf
-										.createTypedLiteral(Integer
-												.parseInt(str)));
-							else
-								_li.addProperty(Predicates.enumeration, rdf
-										.createTypedLiteral(str));
-						}
-						// add the item to the bag
-						_bag.add(_li);
-					}
-					if (item.hasProperty(RDF.type, Predicates.Collection)) {
-						// create a collection
-						Bag collectionBag = rdf.createBag();
-						collectionBag.addProperty(RDF.type,
-								Predicates.Collection);
-						collectionBag.addProperty(RDFS.subClassOf, RDF.Bag);
-						if (item.hasProperty(Predicates.articleName)) {
-							String str = item.getProperty(
-									Predicates.articleName).getObject()
-									.toString();
-							if (str.indexOf("~") > 0)
-								str = str.substring(0, str.indexOf("~"));
-							collectionBag.addProperty(Predicates.articleName,
-									str);
-						}
-						// Resource collectionResource =
-						// item.getProperty(MobyResources.Collection).getResource();
-						StmtIterator simples = item.listProperties();
-						while (simples.hasNext()) {
-							Statement cStatement = simples.nextStatement();
-							if (cStatement.getObject().asNode().isBlank()) {
-								Resource simple = cStatement.getResource();
-								Resource _li = rdf.createResource();
-								_li.addProperty(RDF.type, Predicates.Simple);
-								_li.addProperty(Predicates.object_type, model
-										.getResource(simple.getProperty(
-												Predicates.object_type)
-												.getObject().toString()));
-								if (simple.hasProperty(Predicates.articleName)) {
-									String str = simple.getProperty(
-											Predicates.articleName).getObject()
-											.toString();
-									if (str.indexOf(":") > 0)
-										str = str
-												.substring(0, str.indexOf("~"));
-									_li.addProperty(Predicates.articleName, rdf
-											.createTypedLiteral(str));
-								}
-								StmtIterator namespaceIt = simple
-										.listProperties(Predicates.namespace_type);
-								while (namespaceIt.hasNext()) {
-									_li.addProperty(Predicates.namespace_type,
-											model.getResource(namespaceIt
-													.nextStatement()
-													.getObject().toString()));
-								}
-								collectionBag.add(_li);
-							}
-						}
-						// add the collection to the bag
-						_bag.add(collectionBag);
-					}
-				}
+		if (ServiceInstanceRDF.serviceInstanceExists(details[0], details[1], lsid))
+			return ServiceInstanceRDF.findService(details[0], details[1], false).getBytes();
+		else {
+			if (ServiceInstanceRDF.serviceInstanceExists(details[0], details[1])) {
+				// return rdf that points to latest
+				Model model = ModelFactory.createDefaultModel();
+				Resource resource = model.createResource(lsid);
+				model.add(model.createStatement(resource, RDFS.isDefinedBy, model
+						.createLiteral(ServiceInstanceRDF.getLatestLSID(details[0], details[1],
+								lsid))));
+				model.add(model.createStatement(resource, RDFS.comment, model
+						.createLiteral("The service described by the LSID: " + lsid
+								+ " has since been modified. Please update your lsid.")));
+
+				FilteredStream aPrintStream = new FilteredStream(new ByteArrayOutputStream());
+				RDFWriter writer = model.getWriter("RDF/XML-ABBREV");
+				writer.setProperty("showXmlDeclaration", "false");
+				writer.setProperty("tab", "5");
+				writer.write(model, aPrintStream, null);
+				return aPrintStream.getOutput().getBytes();
+			} else {
+				return ("<?xml version=\"1.0\"?>"
+						+ "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\""
+						+ "  xmlns:dc=\"http://purl.org/dc/elements/1.1/\">" + "</rdf:RDF>")
+						.getBytes();
 			}
-
 		}
 
-		// finally add the property to our resource
-		resource.addProperty(property, _bag);
 	}
 
 	/**
@@ -522,36 +387,70 @@
 	 * otherwise false is returned.
 	 * 
 	 * @param myObject -
-	 *            the string to query the RDF model with.
+	 *            the string to query the RDF model with (an lsid).
 	 * @return - true if myObject exists in the RDF model, false otherwise.
 	 */
 	public final boolean objectExists(String myObject) {
 		NodeIterator rits = model.listObjects();
-		System.out.println("Searching for Object " + myObject);
 		while (rits.hasNext()) {
 			RDFNode RDFnode = rits.nextNode();
 			if (RDFnode == null) {
-				System.out.println("Search for Object " + myObject + " failed (1).");
 				return false;
 			}
 			String node = RDFnode.toString();
 			if (node.indexOf("~") > 0)
 				node = node.substring(0, node.indexOf("~"));
-			if (node.indexOf(":") > 0)
-				node = node.substring(0, node.indexOf(":"));
 			if (node.equalsIgnoreCase(myObject))
 				return true;
 		}
-		System.out.println("Search for Object " + myObject + " failed (2).");
 		return false;
 	}
 
+	public final boolean objectExisted(String lsid) {
+		ResIterator rIter = model.listSubjectsWithProperty(RDFS.label);
+		LSID theLsid = null;
+		try {
+			theLsid = new LSID(lsid);
+		} catch (MalformedLSIDException e) {
+			System.err.println("Error checking if an object existed " + e.getLocalizedMessage());
+			return false;
+		}
+
+		while (rIter.hasNext()) {
+			Resource resource = rIter.nextResource();
+			if (resource.getLocalName() != null)
+				if (resource.getLocalName().equals(theLsid.getObject()))
+					return true;
+		}
+		return false;
+	}
+
+	public final String getLatestLSID(String lsid) {
+		ResIterator rIter = model.listSubjectsWithProperty(RDFS.label);
+		LSID theLsid = null;
+		try {
+			theLsid = new LSID(lsid);
+		} catch (MalformedLSIDException e) {
+			System.err.println("Error obtaining the latest LSID - " + e.getLocalizedMessage());
+			return "";
+		}
+
+		while (rIter.hasNext()) {
+			Resource resource = rIter.nextResource();
+			if (resource.getLocalName() != null)
+				if (resource.getLocalName().equals(theLsid.getObject()))
+					return resource.getURI();
+		}
+		return "";
+	}
+
 	public static void main(String[] args) throws IOException {
 		RDFConfigure rdfc = new RDFConfigure(
-				"http://biomoby.org/RESOURCES/MOBY-S/Objects#");
+				"http://biomoby.org/RESOURCES/MOBY-S/Services#");
 		System.out
-				.println(rdfc.objectExists(
-								("SW")));
+				.println(new String(
+						rdfc
+								.retrieveObjectRDF(("urn:lsid:biomoby.org:servicetype:Analysis"))));
 
 	}
 
@@ -560,8 +459,7 @@
 
 		public FilteredStream(OutputStream aStream) {
 			super(aStream);
-			sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
-					+ newline);
+			sb = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newline);
 		}
 
 		public void write(byte b[]) throws IOException {




More information about the MOBY-guts mailing list