view src/org/qmul/eecs/c4dm/sia/SiaTecMain.java @ 44:d1fed3d54a82

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 in printSetOfTranslators, now uses compareToIgnoreDatapoints instead of compareTo
author stevenh
date Tue, 02 Apr 2013 21:57:19 +0100
parents b43260af3de6
children
line wrap: on
line source
package org.qmul.eecs.c4dm.sia;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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.SiatecX;
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: SiaTecMain
 * </p>
 * <p>
 * Description: An RDF/OWL/Java implementation of the SIATEC pattern discovery algorithm.
 * See "Algorithms for discovering repeated patterns in multidimensional representations of polyphonic music"
 * by Meredith, D. and Lemstr&ouml;m, K. and Wiggins, G.A.
 * </p>
 * 
 * @author Steve Hargreaves, C4DM, Queen Mary University of London
 */
public class SiaTecMain {

	// UPDATE queries
	private static final String insertSiatecVectorTableBNodesQuery = "file:src/sparql/insert_siatec_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(SiaMain.assemblerFile);

		// Get the sia graph, as a model, from the dataset
		Model siaModel = dataset.getNamedModel(SiaMain.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(insertSiatecVectorTableBNodesQuery, ontModel);
    	UpdateAction.readExecute(insertVectorTableDetailsQuery, ontModel);
				
		VectorTable vectorTableW = SiaVectorTableElementFactory.createW(ontModel, datapoints);

		// STEP 3 - Create custom SIATEC VectorTableElement objects (V)
		VectorTable vectorTableV = SiaVectorTableElementFactory.createV(ontModel, datapoints);
		List<VectorTableElement> vteListV = vectorTableV.getVteList();

		// STEP 4 - Sort V
		Collections.sort(vteListV);

		// Add vector table element order info to model
		SiaVectorTableElementFactory.assertOrder(ontModel, vteListV);
		
		// STEP 5 - "Vectorize" the MTPs
		int m = vteListV.size();
		int i = 0;
		ArrayList<SiatecX> x = new ArrayList<SiatecX>();
		
		while (i < m)
		{
			ArrayList<NDimensionalObject> q = new ArrayList<NDimensionalObject>();
			int j = i + 1;
			while (j < m && (vteListV.get(i).compareToIgnoreDatapoints(vteListV.get(j)) == 0))
			{
				NDimensionalObject vector = vteListV.get(j).getFromDatapoint().subtract(vteListV.get(j - 1).getFromDatapoint());
				q.add(vector);
				j++;
			}
			
			SiatecX iq = new SiatecX();
			iq.setI(i+1);
			iq.setQ(q);
			x.add(iq);
			i = j;
		}
		
		// Sort the elements of x
		Collections.sort(x);
		
		// Rename as y to match algorithm description in paper
		ArrayList<SiatecX> y = (ArrayList<SiatecX>) x.clone();
		
		// Display results using algorithm figures 23, 24 and 25 of the research paper
		int r = y.size();
		m = vteListV.size();
		i = 0;
		System.out.println();
		System.out.print("{");
		if (r > 0)
		{
			do
			{
				int j = y.get(i).getI() - 1;
				List<Integer> iList = new ArrayList<Integer>();
				while (j < m && (vteListV.get(j).compareToIgnoreDatapoints(vteListV.get(y.get(i).getI() - 1)) == 0))
				{
					iList.add(vteListV.get(j).getFromDatapoint().getOrderedIndex());					
					j++;
				}
				System.out.print("<");
				Display.printPattern(iList, datapoints);
				System.out.print(",");
				printSetOfTranslators(iList, datapoints, vectorTableW);
				System.out.print(">");
				do
				{
					i++;
				} while (i < r && (y.get(i).compareToIgnoreI(y.get(i - 1)) == 0));
				
				if (i < r)
				{
					System.out.print(",");
					System.out.println();
				}
			} while (i < r);
		}
		System.out.println("}");

		// Write the model to the TDB dataset
        dataset.begin(ReadWrite.WRITE) ;
        try {
        	dataset.replaceNamedModel(SiaMain.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");
		
    	System.out.println("Number of Datapoints (n) = " + 		datapoints.size() + ", k = 3");
	}

	private void printSetOfTranslators(List<Integer> iList, List<Datapoint> datapoints, VectorTable vectorTableW) {
		int p = iList.size();
		int n = datapoints.size();

		if (p == 1)
		{
			System.out.print("{");
			Display.printVector(vectorTableW.getVector(iList.get(0), 1));
			for (int k = 2; k <= n; k++)
			{
				System.out.print(",");
				Display.printVector(vectorTableW.getVector(iList.get(0), k));
			}
			System.out.print("}");
		}
		else
		{
			System.out.print("{");
			List<Integer> jList = new ArrayList<Integer>();
			for (int k = 0; k < p; k++)
			{
				jList.add(1);
			}
			boolean finished = false;
			boolean first_vector = true;
			int k = 1;

			while (!finished)
			{
				if (jList.get(k) <= jList.get(k - 1))
				{
					jList.set(k, jList.get(k - 1) + 1);
				}
				
				while (jList.get(k) <= (n - p + k + 1)
						&& (((VectorTableElement)vectorTableW.getVector(iList.get(k), jList.get(k))).compareToIgnoreDatapoints(((VectorTableElement)vectorTableW.getVector(iList.get(k - 1), jList.get(k - 1)))) < 0))
				{
					jList.set(k, jList.get(k) + 1);
				}
				
				if (jList.get(k) > n - p + k + 1)
				{
					finished = true;
				}
				else if (((VectorTableElement)vectorTableW.getVector(iList.get(k), jList.get(k))).compareToIgnoreDatapoints(((VectorTableElement)vectorTableW.getVector(iList.get(k - 1), jList.get(k - 1)))) > 0)
				{
					k = 1;
					jList.set(0, jList.get(0) + 1);
					if (jList.get(0) > n - p + 1)
					{
						finished = true;
					}
				}
				else if (k + 1 == p)
				{
					if (!first_vector)
					{
						System.out.print(",");
					}
					else
					{
						first_vector = false;
					}
					
					Display.printVector(vectorTableW.getVector(iList.get(k), jList.get(k)));
					k = 0;
					
					while (k < p)
					{
						jList.set(k, jList.get(k) + 1);
						if (jList.get(k) > n - p + k +1)
						{
							finished = true;
							k = p -1;
						}
						k++;
					}
					k = 1;
				}
				else
				{
					k++;
				}
			}
			System.out.print("}");
		}
	}

	private void printX(ArrayList<SiatecX> x) {
		for (SiatecX iq : x)
		{
			System.out.print(iq.getI() + ",");
			for (NDimensionalObject q : iq.getQ())
			{
				System.out.print("<");
				int dimensions = q.getDimensionValues().size();
				for (int dim = 1; dim <= dimensions; dim++)
				{
					try {
						System.out.print(q.getDimensionValue(dim) + (dim == dimensions ? "" : ","));
					} catch (DimensionException e) {
						e.printStackTrace();
						System.exit(1);
					}
				}
				System.out.println(">");
			}
			System.out.println();
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		SiaTecMain app = new SiaTecMain();
		app.run();
		long endTime = System.currentTimeMillis();
		long elapsedTime = endTime - startTime;
		System.out.println("Completed in " + elapsedTime + " ms");
	}

}