steve@0: package org.qmul.eecs.c4dm.sia; steve@0: steve@0: import java.util.ArrayList; steve@0: import java.util.Iterator; steve@0: import java.util.List; steve@0: import java.util.Vector; steve@0: stevenh@16: import org.qmul.eecs.c4dm.sia.exceptions.DimensionException; steve@0: import org.qmul.eecs.c4dm.sia.model.Datapoint; steve@0: import org.qmul.eecs.c4dm.sia.model.DimensionValue; steve@0: import org.qmul.eecs.c4dm.sia.model.DirectlyFollows; steve@0: import org.qmul.eecs.c4dm.sia.model.FromDatapoint; steve@0: import org.qmul.eecs.c4dm.sia.model.MemberOfOrderedSet; steve@0: import org.qmul.eecs.c4dm.sia.model.OrderedIndex; steve@0: import org.qmul.eecs.c4dm.sia.model.OrderedSet; steve@0: import org.qmul.eecs.c4dm.sia.model.ToDatapoint; stevenh@16: import org.qmul.eecs.c4dm.sia.model.VectorTable; steve@0: import org.qmul.eecs.c4dm.sia.model.VectorTableElement; steve@0: steve@0: import com.hp.hpl.jena.ontology.OntClass; steve@0: import com.hp.hpl.jena.ontology.OntModel; stevenh@45: import com.hp.hpl.jena.ontology.OntResource; steve@0: import com.hp.hpl.jena.rdf.model.AnonId; stevenh@45: import com.hp.hpl.jena.rdf.model.ModelFactory; stevenh@45: import com.hp.hpl.jena.rdf.model.NodeIterator; 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: import com.hp.hpl.jena.util.iterator.ExtendedIterator; steve@0: import com.hp.hpl.jena.vocabulary.RDF; steve@0: steve@0: public class SiaVectorTableElementFactory { steve@0: stevenh@16: /** stevenh@16: * @param datapoints stevenh@16: * @return stevenh@90: * @throws DimensionException stevenh@16: */ stevenh@90: public static VectorTable createV(List datapoints) throws DimensionException { stevenh@90: return create(datapoints, false); stevenh@16: } stevenh@16: stevenh@16: /** stevenh@16: * @param datapoints stevenh@16: * @return stevenh@90: * @throws DimensionException stevenh@16: */ stevenh@90: public static VectorTable createW(List datapoints) throws DimensionException { stevenh@90: return create(datapoints, true); stevenh@16: } stevenh@16: stevenh@16: /** stevenh@16: * @param datapoints stevenh@16: * @param returnWResults stevenh@16: * @return stevenh@90: * @throws DimensionException stevenh@16: */ stevenh@90: public static VectorTable create(List datapoints, boolean returnWResults) throws DimensionException { steve@0: stevenh@45: VectorTable vectorTable = new VectorTable(); stevenh@90: stevenh@90: List fromDatapoints = datapoints; stevenh@90: Iterator fromDatapointsIter = fromDatapoints.iterator(); stevenh@45: steve@0: List vteList = new ArrayList(); steve@0: VectorTableElement vte; stevenh@90: while (fromDatapointsIter.hasNext()) stevenh@90: { stevenh@90: Datapoint fromDatapoint = fromDatapointsIter.next(); stevenh@90: stevenh@90: List toDatapoints = datapoints; stevenh@90: Iterator toDatapointsIter = toDatapoints.iterator(); stevenh@90: stevenh@90: while (toDatapointsIter.hasNext()) stevenh@90: { stevenh@90: Datapoint toDatapoint = toDatapointsIter.next(); stevenh@45: stevenh@90: vte = new VectorTableElement(); stevenh@16: vte.setFromDatapoint(fromDatapoint); stevenh@16: vte.setToDatapoint(toDatapoint); stevenh@90: stevenh@90: // Only add VectorTableElements whose 'from' ordered index is less than the 'to' ordered index stevenh@90: if (returnWResults || vte.getFromDatapoint().getOrderedIndex() < vte.getToDatapoint().getOrderedIndex()) stevenh@90: { stevenh@90: // Find all dimensionValues for this VectorTableElement stevenh@90: Vector dimensionValues = SiaDimensionValueFactory.getDimensionValuesForVectorTableElement(vte); stevenh@90: vte.setDimensionValues(dimensionValues); stevenh@90: vteList.add(vte); stevenh@90: } stevenh@16: } steve@0: } stevenh@90: stevenh@16: vectorTable.setVteList(vteList); stevenh@16: vectorTable.setNumDatapoints(datapoints.size()); stevenh@16: return vectorTable; steve@0: } steve@0: stevenh@45: /** stevenh@45: * Java version of the two sparql construct queries stevenh@45: * construct_sia(tec)_vector_table_bnodes.sparql and stevenh@45: * construct_vector_table_details.sparql stevenh@45: * for execution time comparison purposes. stevenh@45: * stevenh@45: * @param ontModel stevenh@45: * @param addWResults stevenh@45: */ stevenh@45: public static void addToModel(OntModel ontModel, boolean addWResults) { stevenh@45: // Replacement code for "construct_sia(tec)_vector_table_bnodes.sparql" start stevenh@45: stevenh@45: long startTime = System.currentTimeMillis(); stevenh@45: stevenh@45: // (note - we could do this by working exclusively with our OntModel, or we could stevenh@45: // create separate java objects (as we already have done with Datapoint, VectorTable etc.) stevenh@45: // as a first attempt, we'll use OntModel in order to avoid too much data model duplication, stevenh@45: // but if performance is poor then we should try java objects stevenh@45: stevenh@45: // Find all instances of sia:OrderedSet stevenh@45: OntModel vteTempOntModel = ModelFactory stevenh@45: .createOntologyModel(); // OntModelSpec.OWL_MEM stevenh@45: OntClass orderedSetClass = ontModel.getOntClass(OrderedSet.RESOURCE_URI); stevenh@45: ExtendedIterator orderedSetIter = orderedSetClass.listInstances(); stevenh@45: Property memberOfOrderedSetProperty = ontModel.getOntProperty(MemberOfOrderedSet.PROPERTY_URI); stevenh@45: Property fromDatapointProperty = ontModel.getOntProperty(FromDatapoint.PROPERTY_URI); stevenh@45: Property toDatapointProperty = ontModel.getOntProperty(ToDatapoint.PROPERTY_URI); stevenh@45: Property orderedIndexProperty = ontModel.getOntProperty(OrderedIndex.PROPERTY_URI); stevenh@45: Property dimensionValueProperty = ontModel.getOntProperty(DimensionValue.PROPERTY_URI); stevenh@45: Property dimensionProperty = ontModel.getOntProperty(DimensionValue.DIMENSION_URI); stevenh@45: Property valueProperty = ontModel.getOntProperty(DimensionValue.VALUE_URI); stevenh@45: Resource vectorTableElementResource = ontModel.createResource(VectorTableElement.RESOURCE_URI); stevenh@45: stevenh@45: // For each sia:OrderedSet, find every sia:Datapoint (which has property sia:memberOfOrderedSet object sia:OrderedSet) stevenh@45: // Make two copies of the list of sia:datapoints stevenh@45: stevenh@45: OntResource orderedSet; stevenh@45: StmtIterator datapoint1StmtIter; stevenh@45: StmtIterator datapoint2StmtIter; stevenh@45: while (orderedSetIter.hasNext()) stevenh@45: { stevenh@45: orderedSet = orderedSetIter.next(); stevenh@45: datapoint1StmtIter = ontModel.listStatements((Resource)null, memberOfOrderedSetProperty, orderedSet); stevenh@45: stevenh@45: // For every pair of sia:Datapoints datapoint1 and datapoint2 (one taken from each list), create a sia:VectorTableElement stevenh@45: while (datapoint1StmtIter.hasNext()) stevenh@45: { stevenh@45: Statement datapoint1Stmt = datapoint1StmtIter.next(); stevenh@45: datapoint2StmtIter = ontModel.listStatements((Resource)null, memberOfOrderedSetProperty, orderedSet); stevenh@45: stevenh@45: while (datapoint2StmtIter.hasNext()) stevenh@45: { stevenh@45: // Assert that the sia:VectorTableElement has sia:fromDatapoint ?datapoint1 and sia:toDatapoint ?datapoint2 stevenh@45: // (in the case where we're creating the 'V' vector table (i.e. returnWResults == false), only make this assertion stevenh@45: // if ((?datapoint1 != ?datapoint2) && (?i1 < ?i2)) where ?datapoint1 sia:orderedIndex ?i1 and ?datapoint2 sia:orderedIndex ?i2 stevenh@45: Statement datapoint2Stmt = datapoint2StmtIter.next(); stevenh@45: Resource fromDatapoint = datapoint1Stmt.getSubject(); stevenh@45: Resource toDatapoint = datapoint2Stmt.getSubject(); stevenh@45: stevenh@45: Statement i1Stmt = ontModel.getProperty(fromDatapoint, orderedIndexProperty); stevenh@45: Statement i2Stmt = ontModel.getProperty(toDatapoint, orderedIndexProperty); stevenh@45: int i1 = i1Stmt.getInt(); stevenh@45: int i2 = i2Stmt.getInt(); stevenh@45: stevenh@45: if (addWResults || i1 < i2) stevenh@45: { stevenh@45: Resource vteBnode = ontModel.createResource(AnonId.create()); stevenh@45: vteTempOntModel.add(vteBnode, RDF.type, vectorTableElementResource); stevenh@45: vteTempOntModel.add(vteBnode, fromDatapointProperty, fromDatapoint); stevenh@45: vteTempOntModel.add(vteBnode, toDatapointProperty, toDatapoint); stevenh@45: stevenh@45: // Replacement code for "construct_sia(tec)_vector_table_bnodes.sparql" end stevenh@45: stevenh@45: // Replacement code for "construct_vector_table_details.sparql" start stevenh@45: NodeIterator fromDimValIter = ontModel.listObjectsOfProperty(fromDatapoint, dimensionValueProperty); stevenh@45: while (fromDimValIter.hasNext()) stevenh@45: { stevenh@45: RDFNode fromDimValNode = fromDimValIter.next(); stevenh@45: Statement fromDimensionStmt = ontModel.getProperty(fromDimValNode.asResource(), dimensionProperty); stevenh@45: Statement fromValueStmt = ontModel.getProperty(fromDimValNode.asResource(), valueProperty); stevenh@45: stevenh@45: int fromDimension = fromDimensionStmt.getInt(); stevenh@45: double fromValue = fromValueStmt.getDouble(); stevenh@45: stevenh@45: NodeIterator toDimValIter = ontModel.listObjectsOfProperty(toDatapoint, dimensionValueProperty); stevenh@45: while (toDimValIter.hasNext()) stevenh@45: { stevenh@45: RDFNode toDimValNode = toDimValIter.next(); stevenh@45: Statement toDimensionStmt = ontModel.getProperty(toDimValNode.asResource(), dimensionProperty); stevenh@45: stevenh@45: int toDimension = toDimensionStmt.getInt(); stevenh@45: stevenh@45: if (fromDimension == toDimension) stevenh@45: { stevenh@45: Statement toValueStmt = ontModel.getProperty(toDimValNode.asResource(), valueProperty); stevenh@45: double toValue = toValueStmt.getDouble(); stevenh@45: double vteValue = toValue - fromValue; stevenh@45: stevenh@45: Resource dimValBnode = ontModel.createResource(AnonId.create()); stevenh@45: vteTempOntModel.add(vteBnode, dimensionValueProperty, dimValBnode); stevenh@45: vteTempOntModel.addLiteral(dimValBnode, dimensionProperty, toDimension); stevenh@45: vteTempOntModel.addLiteral(dimValBnode, valueProperty, vteValue); stevenh@45: stevenh@45: break; stevenh@45: } stevenh@45: } stevenh@45: } stevenh@45: } stevenh@45: } stevenh@45: } stevenh@45: } stevenh@45: stevenh@45: ontModel.add(vteTempOntModel); stevenh@45: long endTime = System.currentTimeMillis(); stevenh@45: long elapsedTime = endTime - startTime; stevenh@45: System.out.println("Completed java replacement for all SPARQL CONSTRUCTs in " + elapsedTime + " ms"); stevenh@45: // Replacement code for "construct_vector_table_details.sparql" end stevenh@45: } stevenh@45: steve@0: public static Datapoint findDatapoint(OntModel ontModel, List datapoints, Resource resource) { steve@0: Iterator datapointsIter = datapoints.iterator(); steve@0: Datapoint datapoint; stevenh@16: steve@0: while (datapointsIter.hasNext()) steve@0: { steve@0: datapoint = datapointsIter.next(); stevenh@45: stevenh@45: if (datapoint.getResource().toString().equals(resource.toString())) steve@0: return datapoint; steve@0: } stevenh@16: steve@0: datapoint = new Datapoint(); steve@0: datapoint.setResource(resource); steve@0: Vector dimensionValues = SiaDimensionValueFactory.getDimensionValuesForResource(ontModel, resource); steve@0: datapoint.setDimensionValues(dimensionValues); steve@0: return datapoint; steve@0: } steve@0: steve@0: public static void assertOrder(OntModel ontModel, List vteList) { steve@0: steve@0: Resource bnode = ontModel.createResource(AnonId.create()); steve@0: Resource siaOrderedSet = ontModel.createResource(OrderedSet.RESOURCE_URI); steve@0: steve@0: ontModel.add(bnode, RDF.type, siaOrderedSet); steve@0: steve@0: int numVectorTableElements = vteList.size(); steve@0: steve@0: VectorTableElement vte; steve@0: for (int orderedIndex = 0; orderedIndex < numVectorTableElements; orderedIndex++) steve@0: { steve@0: // Assert steve@0: vte = vteList.get(orderedIndex); steve@0: Property memberOfOrderedSetProperty = ontModel.getProperty(MemberOfOrderedSet.PROPERTY_URI); steve@0: ontModel.add(vte.getResource(), memberOfOrderedSetProperty, bnode); steve@0: steve@0: if (orderedIndex < numVectorTableElements - 1) steve@0: { steve@0: // Assert steve@0: VectorTableElement nextVte = vteList.get(orderedIndex + 1); steve@0: Property directlyFollowsProperty = ontModel.getProperty(DirectlyFollows.PROPERTY_URI); steve@0: ontModel.add(nextVte.getResource(), directlyFollowsProperty, vte.getResource()); steve@0: } steve@0: steve@0: // Assert steve@0: Property orderedIndexProperty = ontModel.getProperty(OrderedIndex.PROPERTY_URI); steve@0: ontModel.addLiteral(vte.getResource(), orderedIndexProperty, orderedIndex); steve@0: steve@0: } steve@0: steve@0: } steve@0: steve@0: }