changeset 5:fea6c3dde1c1

Added code for displaying the final result. Removed superfluous debug output.
author stevenh
date Tue, 01 Jan 2013 21:13:34 +0000
parents c616268aa9a8
children 7c2d7e0946b3
files src/org/qmul/eecs/c4dm/sia/SiaMain.java
diffstat 1 files changed, 146 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/qmul/eecs/c4dm/sia/SiaMain.java	Tue Jan 01 20:48:29 2013 +0000
+++ b/src/org/qmul/eecs/c4dm/sia/SiaMain.java	Tue Jan 01 21:13:34 2013 +0000
@@ -16,7 +16,9 @@
 import java.util.List;
 
 import org.mindswap.pellet.jena.PelletReasonerFactory;
+import org.qmul.eecs.c4dm.sia.exceptions.DimensionException;
 import org.qmul.eecs.c4dm.sia.model.Datapoint;
+import org.qmul.eecs.c4dm.sia.model.NDimensionalObject;
 import org.qmul.eecs.c4dm.sia.model.VectorTableElement;
 
 import com.clarkparsia.pellet.sparqldl.jena.SparqlDLExecutionFactory;
@@ -35,19 +37,13 @@
 
 /**
  * <p>
- * Title: SPARQLDLExample
+ * Title: SiaMain
  * </p>
  * <p>
- * Description: This program shows how to use the Pellet SPARQL-DL engine
- * </p>
- * <p>
- * Copyright: Copyright (c) 2008
- * </p>
- * <p>
- * Company: Clark & Parsia, LLC. <http://www.clarkparsia.com>
+ * Description: An RDF/OWL/Java implementation of the sia pattern discovery algorithm.
  * </p>
  * 
- * @author Markus Stocker
+ * @author Steve Hargreaves, C4DM, Queen Mary University of London
  */
 public class SiaMain {
 
@@ -60,8 +56,7 @@
 	// SPARQL queries
 	private static final String[] selectQueries = new String[] {
 		// A SPARQL-DL query
-//		"file:src/sparql/select_all.sparql" };
-		"file:src/sparql/select_sia_results.sparql" };
+	};
 
 	// CONSTRUCT queries
 	private static final String[] constructQueries = new String[] {
@@ -72,7 +67,6 @@
 	// ASK queries
 	private static final String[] askQueries = new String[] {
 		// A SPARQL-DL ASK query
-		"file:src/sparql/ask.sparql"
 		};
 	
 	public static String SIA_NS_URI = null;
@@ -87,24 +81,19 @@
 		// Then read the data from the file into the ontology model
 		ontModel.read(ontology, "N3");
 		
+		// Set the value of SIA_NS_URI as the namespace for 'sia' found in
+		// the ontology
 		SIA_NS_URI = ontModel.getNsPrefixURI("sia");
 
-		// Print out what we've got so far
-		StmtIterator stmtIterator = ontModel.listStatements();
-		printStmts(stmtIterator);
-		
 		// Create custom sia Datapoint objects
 		List<Datapoint> datapoints = SiaDatapointFactory.create(ontModel);
+		
+		// Order the datapoints
 		Collections.sort(datapoints);
-		
+
 		// Add datapoint order info to model
 		SiaDatapointFactory.assertOrder(ontModel, datapoints);
 		
-		for (Datapoint datapoint : datapoints)
-		{
-			System.out.println(datapoint.getResource().getLocalName());
-		}
-
 		// Run all the CONSTRUCT queries
 		for (int i = 0; i < constructQueries.length; i++) {
 			String constructQuery = constructQueries[i];
@@ -113,32 +102,15 @@
 
 			// Add new triples to the current model
 			ontModel.add(newModel);
-
-			// Print out what we've got now
-			System.out.println("------------------");
-			stmtIterator = ontModel.listStatements();
-			printStmts(stmtIterator);
 		}
 
-//		// Create custom sia VectorTableElement objects
+		// Create custom sia VectorTableElement objects
 		List<VectorTableElement> vteList = SiaVectorTableElementFactory.create(ontModel, datapoints);
 		Iterator<VectorTableElement> vteIter = vteList.iterator();
-		System.out.println("Unsorted");
-		System.out.println("----------");
-		while (vteIter.hasNext())
-		{
-			VectorTableElement vte = vteIter.next();
-			System.out.println("from: " + vte.getFromDatapoint().getResource().getLocalName() + " to: " + vte.getToDatapoint().getResource().getLocalName());			
-		}
+
+		// Order the VectorTableElement objects
 		Collections.sort(vteList);
 		vteIter = vteList.iterator();
-		System.out.println("Now Sorted");
-		System.out.println("----------");
-		while (vteIter.hasNext())
-		{
-			VectorTableElement vte = vteIter.next();
-			System.out.println("from: " + vte.getFromDatapoint().getResource().getLocalName() + " to: " + vte.getToDatapoint().getResource().getLocalName());			
-		}
 
 		// Add vector table element order info to model
 		SiaVectorTableElementFactory.assertOrder(ontModel, vteList);
@@ -164,21 +136,126 @@
 		File outFileN3 = new File(finalModelFileName + ".n3");
 		FileOutputStream outFileOutputStreamRdf;
 		FileOutputStream outFileOutputStreamN3;
+
+		// RDF/XML version
+		try {
+			outFileOutputStreamRdf = new FileOutputStream(outFileRdf);
+			ontModel.writeAll(outFileOutputStreamRdf, "RDF/XML", null);
+		} catch (FileNotFoundException e) {
+			System.out.println("Unable to write to file: "
+					+ outFileRdf.getAbsolutePath());
+			e.printStackTrace();
+			System.exit(1);
+		}
+
+		// N3 version
+		try {
+			outFileOutputStreamN3 = new FileOutputStream(outFileN3);
+			ontModel.writeAll(outFileOutputStreamN3, "N3", null);
+		} catch (FileNotFoundException e) {
+			System.out.println("Unable to write to file: "
+					+ outFileN3.getAbsolutePath());
+			e.printStackTrace();
+			System.exit(1);
+		}
+
 		System.out.println("Model written to files: "
 				+ outFileRdf.getAbsolutePath() + " and " + outFileN3.getAbsolutePath());
-		try {
-			outFileOutputStreamRdf = new FileOutputStream(outFileRdf);
-			outFileOutputStreamN3 = new FileOutputStream(outFileN3);
-			ontModel.writeAll(outFileOutputStreamRdf, "RDF/XML", null);
-			ontModel.writeAll(outFileOutputStreamN3, "N3", null);
-		} catch (FileNotFoundException e) {
-			// TODO Auto-generated catch block
-			System.out.println("Unable to write to one or both of files: "
-					+ outFileRdf.getAbsolutePath() + " and " + outFileN3.getAbsolutePath());
-			e.printStackTrace();
+		
+		// Display the results
+		displaySiaResults(vteList);
+	}
+
+	/**
+	 * @param vteList
+	 */
+	public void displaySiaResults(List<VectorTableElement> vteList) {
+		int m = vteList.size();
+		int i = 0;
+		
+		System.out.println();
+		System.out.print("{");
+		
+		while (i < m)
+		{
+			System.out.print("<");
+			printVector(vteList.get(i));
+			System.out.print(",{");
+			printVector(vteList.get(i).getFromDatapoint());
+			int j = i + 1;
+						
+			while (j < m && (vteList.get(i).compareToIgnoreDatapoints(vteList.get(j)) == 0))
+			{
+				System.out.print(",");
+				printVector(vteList.get(j).getFromDatapoint());
+				j++;
+			}
+			System.out.print("}>");
+			if (j < m)
+			{
+				System.out.print(",");
+				System.out.println();
+			}
+			i = j;
+		}
+		System.out.println("}");
+	}
+
+	/**
+	 * @param vteIter
+	 * @param message
+	 */
+	public void displayNDimensionalObjects(Iterator<VectorTableElement> vteIter, String message) {
+		System.out.println(message);
+		System.out.println("----------");
+		while (vteIter.hasNext())
+		{
+			VectorTableElement vte = vteIter.next();
+			System.out.println("from: " + vte.getFromDatapoint().getResource().getLocalName() + " to: " + vte.getToDatapoint().getResource().getLocalName());			
 		}
 	}
 
+	/**
+	 * @param datapointIter
+	 * @param message
+	 */
+	public void displayDatapoints(Iterator<Datapoint> datapointIter, String message) {
+		System.out.println(message);
+		System.out.println("----------");
+		while (datapointIter.hasNext())
+		{
+			Datapoint datapoint = datapointIter.next();
+			System.out.println( datapoint.getResource().getLocalName());			
+		}
+	}
+
+	/**
+	 * @param vector
+	 */
+	private void printVector(NDimensionalObject vector) {
+		System.out.print("<");
+		
+		int maxDimension = vector.getDimensionValues().size();
+		
+		for (int dim = 1; dim <= maxDimension; dim++)
+		{
+			try {
+				double value = vector.getDimensionValue(dim);
+				System.out.print(value + (dim == maxDimension ? "" : ","));
+			} catch (DimensionException e) {
+				e.printStackTrace();
+				System.exit(1);
+			}
+		}
+		System.out.print(">");
+		
+	}
+
+	/**
+	 * @param query
+	 * @param m
+	 * @return
+	 */
 	private Model executeConstructQuery(String query, Model m) {
 
 		// Now read the query file into a query object
@@ -203,6 +280,9 @@
 		return newModel;
 	}
 
+	/**
+	 * @param iter
+	 */
 	private void printStmts(StmtIterator iter) {
 		Statement statement;
 
@@ -217,6 +297,10 @@
 		System.out.println();
 	}
 
+	/**
+	 * @param query
+	 * @param m
+	 */
 	private void queryTheModel(String query, Model m) {
 
 		// Now read the query file into a query object
@@ -244,6 +328,10 @@
 		System.out.println();
 	}
 
+	/**
+	 * @param query
+	 * @param m
+	 */
 	private void askTheModel(String query, Model m) {
 
 		// Now read the query file into a query object
@@ -266,6 +354,12 @@
 		System.out.println();
 	}
 
+	/**
+	 * @param args
+	 */
+	/**
+	 * @param args
+	 */
 	public static void main(String[] args) {
 		SiaMain app = new SiaMain();
 		app.run();