view src/org/qmul/eecs/c4dm/sia/SiaDatapointFactory.java @ 94:fedf516f6a78 tip

test file
author stevenh
date Mon, 29 Dec 2014 15:16:22 +0000
parents affdfe375b41
children
line wrap: on
line source
package org.qmul.eecs.c4dm.sia;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.qmul.eecs.c4dm.sia.model.Datapoint;
import org.qmul.eecs.c4dm.sia.model.DimensionValue;
import org.qmul.eecs.c4dm.sia.model.SiaVector;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;

public class SiaDatapointFactory {

	/**
	 * @param ontModel
	 * @return List<Datapoint>
	 */
	public static List<Datapoint> create(OntModel ontModel) {
		
		List<Datapoint> datapointsList = new ArrayList<Datapoint>();
		Datapoint datapoint;
		OntClass datapointClass = ontModel.getOntClass(Datapoint.RESOURCE_URI);
		
		ExtendedIterator<? extends OntResource> datapointIter;
		try
		{
			datapointIter = datapointClass.listInstances();
		}
		catch (NullPointerException npe)
		{
			return datapointsList;
		}
		
		// Find all rdf datapoints
		OntResource datapointIndividual;
		while (datapointIter.hasNext())
		{
			datapointIndividual = datapointIter.next();
			datapoint = new Datapoint();
			datapoint.setResource(datapointIndividual);
			Property vectorProperty = ontModel.getOntProperty(SiaVector.PROPERTY_URI);
			Statement vector = datapointIndividual.getProperty(vectorProperty);
			
			Vector<DimensionValue> dimValsList = SiaDimensionValueFactory.getDimensionValuesForResource(
					ontModel, vector.getResource());
			
			datapoint.setDimensionValues(dimValsList);
			datapointsList.add(datapoint);
			
		}
		return datapointsList;
	}

	/**
	 * @param datapoints
	 */
	public static void assertOrder(List<Datapoint> datapoints) {
		
		int numDatapoints = datapoints.size();

		Datapoint datapoint;
		
		for (int orderedIndex = 0; orderedIndex < numDatapoints; orderedIndex++)
		{
			datapoint = datapoints.get(orderedIndex);
			datapoint.setOrderedIndex(orderedIndex + 1);
		}
	}

}