Mercurial > hg > semantic-sia
view src/org/qmul/eecs/c4dm/sia/SiaMain.java @ 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 | 08675ab08e7f |
children | 5a3a1c7fa3a8 |
line wrap: on
line source
// Copyright (c) 2006 - 2008, Clark & Parsia, LLC. <http://www.clarkparsia.com> // This source code is available under the terms of the Affero General Public // License v3. // // Please see LICENSE.txt for full license terms, including the availability of // proprietary exceptions. // Questions, comments, or requests for clarification: licensing@clarkparsia.com package org.qmul.eecs.c4dm.sia; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Collections; import java.util.Iterator; 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; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.query.ResultSetFormatter; import com.hp.hpl.jena.query.Syntax; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.StmtIterator; /** * <p> * Title: SiaMain * </p> * <p> * Description: An RDF/OWL/Java implementation of the sia pattern discovery algorithm. * </p> * * @author Steve Hargreaves, C4DM, Queen Mary University of London */ public class SiaMain { // The ontology loaded as dataset private static final String ontology = "file:src/rdf/siaDatapointOntology.n3"; // The final output file private static final String finalModelFileName = "src/rdf/finalModel"; // SPARQL queries private static final String[] selectQueries = new String[] { // A SPARQL-DL query }; // CONSTRUCT queries private static final String[] constructQueries = new String[] { // A SPARQL-DL CONSTRUCT query "file:src/sparql/construct_vector_table_bnodes.sparql", "file:src/sparql/construct_vector_table_details.sparql" }; // ASK queries private static final String[] askQueries = new String[] { // A SPARQL-DL ASK query }; public static String SIA_NS_URI = null; public void run() { // First create a Jena ontology model backed by the Pellet reasoner // (note, the Pellet reasoner is required) OntModel ontModel = ModelFactory .createOntologyModel(PelletReasonerFactory.THE_SPEC); // 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"); // 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); // Run all the CONSTRUCT queries for (int i = 0; i < constructQueries.length; i++) { String constructQuery = constructQueries[i]; Model newModel = executeConstructQuery(constructQuery, ontModel); // Add new triples to the current model ontModel.add(newModel); } // Create custom sia VectorTableElement objects List<VectorTableElement> vteList = SiaVectorTableElementFactory.create(ontModel, datapoints); Iterator<VectorTableElement> vteIter = vteList.iterator(); // Order the VectorTableElement objects Collections.sort(vteList); vteIter = vteList.iterator(); // Add vector table element order info to model SiaVectorTableElementFactory.assertOrder(ontModel, vteList); // Run all the SELECT queries for (int i = 0; i < selectQueries.length; i++) { String selectQuery = selectQueries[i]; queryTheModel(selectQuery, ontModel); } // Run all the ASK queries for (int i = 0; i < askQueries.length; i++) { String askQuery = askQueries[i]; askTheModel(askQuery, ontModel); } // Write the model to a file File outFileRdf = new File(finalModelFileName + ".rdf"); 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()); // 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 Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required // if using // (e.g.) LET in // SPARQL QueryExecution qe = QueryExecutionFactory.create(q, m); // We want to execute a CONSTRUCT query, do it, and return the new // triples Model newModel = qe.execConstruct(); // Print the query for better understanding System.out.println(q.toString()); // Print the new triples StmtIterator iter = newModel.listStatements(); printStmts(iter); return newModel; } /** * @param iter */ private void printStmts(StmtIterator iter) { Statement statement; while (iter.hasNext()) { statement = iter.nextStatement(); System.out.println(" | <" + statement.getSubject() + "> | <" + statement.getPredicate() + "> | <" + statement.getObject() + "> | "); } // And an empty line to make it pretty System.out.println(); } /** * @param query * @param m */ private void queryTheModel(String query, Model m) { // Now read the query file into a query object Query q = QueryFactory.read(query, Syntax.syntaxARQ); // only required // if using // (e.g.) LET in // SPARQL // Create a SPARQL-DL query execution for the given query and // ontology model QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // We want to execute a SELECT query, do it, and return the result set ResultSet rs = qe.execSelect(); // Print the query for better understanding System.out.println(q.toString()); // There are different things we can do with the result set, for // instance iterate over it and process the query solutions or, what we // do here, just print out the results ResultSetFormatter.out(rs); // And an empty line to make it pretty System.out.println(); } /** * @param query * @param m */ private void askTheModel(String query, Model m) { // Now read the query file into a query object Query q = QueryFactory.read(query); // Create a SPARQL-DL query execution for the given query and // ontology model QueryExecution qe = SparqlDLExecutionFactory.create(q, m); // We want to execute a SELECT query, do it, and return the result set boolean result = qe.execAsk(); // Print the query for better understanding System.out.println(q.toString()); // Print the result System.out.println("Result: " + result); // And an empty line to make it pretty System.out.println(); } /** * @param args */ /** * @param args */ public static void main(String[] args) { SiaMain app = new SiaMain(); app.run(); } }