view src/org/qmul/eecs/c4dm/sia/SiaMain.java @ 13:5a3a1c7fa3a8

organized imports changed finalModelFileName to "src/rdf/finalSiaModel" (to distinguish from finalSiaTecModel) removed select query changed one of the sparql construct query filenames from construct_vector_table_bnodes to construct_sia_vector_table_bnodes moved public static String SIA_NS_URI to the (new) "Namespaces" class moved sparql-related methods to (new) class "SparqlWrapperMethods" now use the newly-created "SiaVectorTableElementFactory.createV" method instead of the old "SiaVectorTableElementFactory.create" method for creating specifically the Vector Table V (as opposed to W) described in the paper removed unnecessary code moved display code to the "Display" class
author stevenh
date Tue, 08 Jan 2013 18:15:46 +0000
parents fea6c3dde1c1
children f3f6ef9bd5bd
line wrap: on
line source
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.model.Datapoint;
import org.qmul.eecs.c4dm.sia.model.VectorTable;
import org.qmul.eecs.c4dm.sia.model.VectorTableElement;
import org.qmul.eecs.c4dm.sia.rdf.Namespaces;
import org.qmul.eecs.c4dm.sia.utilities.Display;
import org.qmul.eecs.c4dm.sparql.utilities.SparqlWrapperMethods;

import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;

/**
 * <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/finalSiaModel";

	// SPARQL queries
	private static final String[] selectQueries = new String[] {
		// A SPARQL-DL query
		"file:src/sparql/select_all.sparql"
	};

	// CONSTRUCT queries
	private static final String[] constructQueries = new String[] {
		// A SPARQL-DL CONSTRUCT query
		"file:src/sparql/construct_sia_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 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
		Namespaces.SIA_NS_URI = ontModel.getNsPrefixURI("sia");

		// 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 CONSTRUCT queries
		for (int i = 0; i < constructQueries.length; i++) {
			String constructQuery = constructQueries[i];

			Model newModel = SparqlWrapperMethods.executeConstructQuery(constructQuery, ontModel);

			// Add new triples to the current model
			ontModel.add(newModel);
		}

		// 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);

		// Run all the SELECT queries
		for (int i = 0; i < selectQueries.length; i++) {

			String selectQuery = selectQueries[i];
			SparqlWrapperMethods.queryTheModel(selectQuery, ontModel);

		}

		// Run all the ASK queries
		for (int i = 0; i < askQueries.length; i++) {

			String askQuery = askQueries[i];
			SparqlWrapperMethods.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());
		
		// STEP 4 - 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("<");
			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) {
		SiaMain app = new SiaMain();
		app.run();
	}

}