Mercurial > hg > semantic-sia
view src/org/qmul/eecs/c4dm/sia/SiaMain.java @ 43:f3f6ef9bd5bd
organised imports
changed various public/private static final Strings
no longer uses Pellet - now uses a basic jena ontology model
reads/writes model to/from an external TDB dataset instead of flat files
performs sparql updates (inserts) when creating the vector table instead of sparql constructs followed by adding new triples to the model
removed unnecessary sparql select and ask queries
the whole procedure is now timed
author | stevenh |
---|---|
date | Tue, 02 Apr 2013 21:27:38 +0100 |
parents | 5a3a1c7fa3a8 |
children | 6027dbd0758d |
line wrap: on
line source
package org.qmul.eecs.c4dm.sia; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.qmul.eecs.c4dm.sia.model.Datapoint; import org.qmul.eecs.c4dm.sia.model.VectorTable; import org.qmul.eecs.c4dm.sia.model.VectorTableElement; import org.qmul.eecs.c4dm.sia.utilities.Display; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.query.Dataset; import com.hp.hpl.jena.query.ReadWrite; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.tdb.TDBFactory; import com.hp.hpl.jena.update.UpdateAction; /** * <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 location of the TDB database public static final String assemblerFile = "src/assemblers/tdb-assembler.ttl"; // The name of the graph used in the dataset public static final String graph = "http://localhost:3030/siaGraph"; // SPARQL queries private static final String selectAllQuery = "file:src/sparql/select_all.sparql"; // UPDATE queries private static final String insertSiaVectorTableBNodesQuery = "file:src/sparql/insert_sia_vector_table_bnodes.sparql"; private static final String insertVectorTableDetailsQuery = "file:src/sparql/insert_vector_table_details.sparql"; public void run() { // Obtain a dataset context Dataset dataset = TDBFactory.assembleDataset(assemblerFile); // Get the sia graph, as a model, from the dataset Model siaModel = dataset.getNamedModel(graph); OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, siaModel); // Create custom sia Datapoint objects List<Datapoint> datapoints = SiaDatapointFactory.create(ontModel); // STEP 1 - Order the datapoints Collections.sort(datapoints); // Add datapoint order info to model SiaDatapointFactory.assertOrder(ontModel, datapoints); // STEP 2 - Compute the vector table // Run all the UPDATE queries UpdateAction.readExecute(insertSiaVectorTableBNodesQuery, ontModel); UpdateAction.readExecute(insertVectorTableDetailsQuery, ontModel); // Create custom sia VectorTableElement objects VectorTable vectorTableV = SiaVectorTableElementFactory.createV(ontModel, datapoints); List<VectorTableElement> vteList = vectorTableV.getVteList(); // STEP 3 - Order the VectorTableElement objects Collections.sort(vteList); // Add vector table element order info to model SiaVectorTableElementFactory.assertOrder(ontModel, vteList); // STEP 4 - Display the results displaySiaResults(vteList); // Write the model to the TDB dataset dataset.begin(ReadWrite.WRITE) ; try { dataset.replaceNamedModel(graph, ontModel); dataset.commit(); System.out.println("dataset.commit() done"); } finally { dataset.end(); System.out.println("dataset.end() done"); } dataset.close(); System.out.println("dataset.close() done"); } /** * @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("<"); Display.printVector(vteList.get(i)); System.out.print(",{"); Display.printVector(vteList.get(i).getFromDatapoint()); int j = i + 1; while (j < m && (vteList.get(i).compareToIgnoreDatapoints(vteList.get(j)) == 0)) { System.out.print(","); Display.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 args */ public static void main(String[] args) { long startTime = System.currentTimeMillis(); SiaMain app = new SiaMain(); app.run(); long endTime = System.currentTimeMillis(); long elapsedTime = endTime - startTime; System.out.println("Completed in " + elapsedTime + " ms"); } }