# HG changeset patch # User stevenh # Date 1357074814 0 # Node ID fea6c3dde1c1a8574058c1d9a4a0d62c4902ef4b # Parent c616268aa9a8f5b688c0e9ae07edba3b53dc5ee7 Added code for displaying the final result. Removed superfluous debug output. diff -r c616268aa9a8 -r fea6c3dde1c1 src/org/qmul/eecs/c4dm/sia/SiaMain.java --- 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 @@ /** *

- * Title: SPARQLDLExample + * Title: SiaMain *

*

- * Description: This program shows how to use the Pellet SPARQL-DL engine - *

- *

- * Copyright: Copyright (c) 2008 - *

- *

- * Company: Clark & Parsia, LLC. + * Description: An RDF/OWL/Java implementation of the sia pattern discovery algorithm. *

* - * @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 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 vteList = SiaVectorTableElementFactory.create(ontModel, datapoints); Iterator 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 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 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 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();