steve@0: package org.qmul.eecs.c4dm.sia; steve@0: steve@0: import java.util.Vector; steve@0: stevenh@89: import org.qmul.eecs.c4dm.sia.exceptions.DimensionException; stevenh@89: import org.qmul.eecs.c4dm.sia.model.Datapoint; steve@0: import org.qmul.eecs.c4dm.sia.model.DimensionValue; stevenh@89: import org.qmul.eecs.c4dm.sia.model.VectorTableElement; steve@0: steve@0: import com.hp.hpl.jena.ontology.OntModel; steve@0: import com.hp.hpl.jena.rdf.model.Literal; steve@0: import com.hp.hpl.jena.rdf.model.Property; steve@0: import com.hp.hpl.jena.rdf.model.RDFNode; steve@0: import com.hp.hpl.jena.rdf.model.Resource; steve@0: import com.hp.hpl.jena.rdf.model.Statement; steve@0: import com.hp.hpl.jena.rdf.model.StmtIterator; steve@0: steve@0: public class SiaDimensionValueFactory { steve@0: stevenh@89: public static Vector getDimensionValuesForVectorTableElement(VectorTableElement vte) throws DimensionException stevenh@89: { stevenh@89: Datapoint fromDatapoint = vte.getFromDatapoint(); stevenh@89: Datapoint toDatapoint = vte.getToDatapoint(); stevenh@89: stevenh@89: int dimensionality = fromDatapoint.getDimensionValues().size(); stevenh@89: Vector dimVals = new Vector(); stevenh@89: stevenh@89: for (int dim = 1; dim <= dimensionality; dim++) stevenh@89: { stevenh@89: double val = toDatapoint.getDimensionValue(dim) - fromDatapoint.getDimensionValue(dim); stevenh@89: DimensionValue dimVal = new DimensionValue(); stevenh@89: dimVal.setDimension(dim); stevenh@89: dimVal.setValue(val); stevenh@89: dimVals.add(dimVal); stevenh@89: } stevenh@89: stevenh@89: return dimVals; stevenh@89: } stevenh@89: steve@0: public static Vector getDimensionValuesForResource( steve@0: OntModel ontModel, Resource subject) { steve@0: DimensionValue dimVal; steve@0: StmtIterator dimValStmtIter; steve@0: Property dimValProperty = ontModel.getOntProperty(DimensionValue.PROPERTY_URI); steve@0: Property dimensionProperty = ontModel.getOntProperty(DimensionValue.DIMENSION_URI); steve@0: Property valueProperty = ontModel.getOntProperty(DimensionValue.VALUE_URI); steve@0: dimValStmtIter = ontModel.listStatements(subject, dimValProperty, (RDFNode)null); steve@0: steve@0: Vector dimValsList = new Vector(); steve@0: StmtIterator dimValDimensionStmtIter; steve@0: StmtIterator dimValValueStmtIter; steve@0: steve@0: while (dimValStmtIter.hasNext()) steve@0: { stevenh@12: Statement dimValStmt = dimValStmtIter.next(); steve@0: Resource dimValResource = dimValStmt.getResource(); steve@0: steve@0: dimValDimensionStmtIter = ontModel.listStatements(dimValResource, dimensionProperty, (RDFNode)null); steve@0: dimValValueStmtIter = ontModel.listStatements(dimValResource, valueProperty, (RDFNode)null); steve@0: dimVal = new DimensionValue(); stevenh@66: dimVal.setResource(dimValResource); steve@0: stevenh@39: // There should only be one statment here - if not, application behaviour is undefined stevenh@39: while (dimValDimensionStmtIter.hasNext()) stevenh@39: { stevenh@39: Statement dimValDimensionStmt = dimValDimensionStmtIter.next(); stevenh@39: Literal dimension = dimValDimensionStmt.getLiteral(); stevenh@39: dimVal.setDimension(dimension.getInt()); stevenh@39: } steve@0: stevenh@39: // There should only be one statment here - if not, application behaviour is undefined stevenh@39: while (dimValValueStmtIter.hasNext()) stevenh@39: { stevenh@39: Statement dimValValueStmt = dimValValueStmtIter.next(); stevenh@39: Literal value = dimValValueStmt.getLiteral(); stevenh@39: dimVal.setValue(value.getDouble()); stevenh@39: } steve@0: stevenh@12: dimValsList.add(dimVal); steve@0: } steve@0: return dimValsList; steve@0: } steve@0: steve@0: }