annotate src/org/qmul/eecs/c4dm/sia/SiaVectorTableElementFactory.java @ 94:fedf516f6a78 tip

test file
author stevenh
date Mon, 29 Dec 2014 15:16:22 +0000
parents 19076fd08f74
children
rev   line source
steve@0 1 package org.qmul.eecs.c4dm.sia;
steve@0 2
steve@0 3 import java.util.ArrayList;
steve@0 4 import java.util.Iterator;
steve@0 5 import java.util.List;
steve@0 6 import java.util.Vector;
steve@0 7
stevenh@16 8 import org.qmul.eecs.c4dm.sia.exceptions.DimensionException;
steve@0 9 import org.qmul.eecs.c4dm.sia.model.Datapoint;
steve@0 10 import org.qmul.eecs.c4dm.sia.model.DimensionValue;
steve@0 11 import org.qmul.eecs.c4dm.sia.model.DirectlyFollows;
steve@0 12 import org.qmul.eecs.c4dm.sia.model.FromDatapoint;
steve@0 13 import org.qmul.eecs.c4dm.sia.model.MemberOfOrderedSet;
steve@0 14 import org.qmul.eecs.c4dm.sia.model.OrderedIndex;
steve@0 15 import org.qmul.eecs.c4dm.sia.model.OrderedSet;
steve@0 16 import org.qmul.eecs.c4dm.sia.model.ToDatapoint;
stevenh@16 17 import org.qmul.eecs.c4dm.sia.model.VectorTable;
steve@0 18 import org.qmul.eecs.c4dm.sia.model.VectorTableElement;
steve@0 19
steve@0 20 import com.hp.hpl.jena.ontology.OntClass;
steve@0 21 import com.hp.hpl.jena.ontology.OntModel;
stevenh@45 22 import com.hp.hpl.jena.ontology.OntResource;
steve@0 23 import com.hp.hpl.jena.rdf.model.AnonId;
stevenh@45 24 import com.hp.hpl.jena.rdf.model.ModelFactory;
stevenh@45 25 import com.hp.hpl.jena.rdf.model.NodeIterator;
steve@0 26 import com.hp.hpl.jena.rdf.model.Property;
steve@0 27 import com.hp.hpl.jena.rdf.model.RDFNode;
steve@0 28 import com.hp.hpl.jena.rdf.model.Resource;
steve@0 29 import com.hp.hpl.jena.rdf.model.Statement;
steve@0 30 import com.hp.hpl.jena.rdf.model.StmtIterator;
steve@0 31 import com.hp.hpl.jena.util.iterator.ExtendedIterator;
steve@0 32 import com.hp.hpl.jena.vocabulary.RDF;
steve@0 33
steve@0 34 public class SiaVectorTableElementFactory {
steve@0 35
stevenh@16 36 /**
stevenh@16 37 * @param datapoints
stevenh@16 38 * @return
stevenh@90 39 * @throws DimensionException
stevenh@16 40 */
stevenh@90 41 public static VectorTable createV(List<Datapoint> datapoints) throws DimensionException {
stevenh@90 42 return create(datapoints, false);
stevenh@16 43 }
stevenh@16 44
stevenh@16 45 /**
stevenh@16 46 * @param datapoints
stevenh@16 47 * @return
stevenh@90 48 * @throws DimensionException
stevenh@16 49 */
stevenh@90 50 public static VectorTable createW(List<Datapoint> datapoints) throws DimensionException {
stevenh@90 51 return create(datapoints, true);
stevenh@16 52 }
stevenh@16 53
stevenh@16 54 /**
stevenh@16 55 * @param datapoints
stevenh@16 56 * @param returnWResults
stevenh@16 57 * @return
stevenh@90 58 * @throws DimensionException
stevenh@16 59 */
stevenh@90 60 public static VectorTable create(List<Datapoint> datapoints, boolean returnWResults) throws DimensionException {
steve@0 61
stevenh@45 62 VectorTable vectorTable = new VectorTable();
stevenh@90 63
stevenh@90 64 List<Datapoint> fromDatapoints = datapoints;
stevenh@90 65 Iterator<Datapoint> fromDatapointsIter = fromDatapoints.iterator();
stevenh@45 66
steve@0 67 List<VectorTableElement> vteList = new ArrayList<VectorTableElement>();
steve@0 68 VectorTableElement vte;
stevenh@90 69 while (fromDatapointsIter.hasNext())
stevenh@90 70 {
stevenh@90 71 Datapoint fromDatapoint = fromDatapointsIter.next();
stevenh@90 72
stevenh@90 73 List<Datapoint> toDatapoints = datapoints;
stevenh@90 74 Iterator<Datapoint> toDatapointsIter = toDatapoints.iterator();
stevenh@90 75
stevenh@90 76 while (toDatapointsIter.hasNext())
stevenh@90 77 {
stevenh@90 78 Datapoint toDatapoint = toDatapointsIter.next();
stevenh@45 79
stevenh@90 80 vte = new VectorTableElement();
stevenh@16 81 vte.setFromDatapoint(fromDatapoint);
stevenh@16 82 vte.setToDatapoint(toDatapoint);
stevenh@90 83
stevenh@90 84 // Only add VectorTableElements whose 'from' ordered index is less than the 'to' ordered index
stevenh@90 85 if (returnWResults || vte.getFromDatapoint().getOrderedIndex() < vte.getToDatapoint().getOrderedIndex())
stevenh@90 86 {
stevenh@90 87 // Find all dimensionValues for this VectorTableElement
stevenh@90 88 Vector<DimensionValue> dimensionValues = SiaDimensionValueFactory.getDimensionValuesForVectorTableElement(vte);
stevenh@90 89 vte.setDimensionValues(dimensionValues);
stevenh@90 90 vteList.add(vte);
stevenh@90 91 }
stevenh@16 92 }
steve@0 93 }
stevenh@90 94
stevenh@16 95 vectorTable.setVteList(vteList);
stevenh@16 96 vectorTable.setNumDatapoints(datapoints.size());
stevenh@16 97 return vectorTable;
steve@0 98 }
steve@0 99
stevenh@45 100 /**
stevenh@45 101 * Java version of the two sparql construct queries
stevenh@45 102 * <i>construct_sia(tec)_vector_table_bnodes.sparql</i> and
stevenh@45 103 * <i>construct_vector_table_details.sparql</i>
stevenh@45 104 * for execution time comparison purposes.
stevenh@45 105 *
stevenh@45 106 * @param ontModel
stevenh@45 107 * @param addWResults
stevenh@45 108 */
stevenh@45 109 public static void addToModel(OntModel ontModel, boolean addWResults) {
stevenh@45 110 // Replacement code for "construct_sia(tec)_vector_table_bnodes.sparql" start
stevenh@45 111
stevenh@45 112 long startTime = System.currentTimeMillis();
stevenh@45 113
stevenh@45 114 // (note - we could do this by working exclusively with our OntModel, or we could
stevenh@45 115 // create separate java objects (as we already have done with Datapoint, VectorTable etc.)
stevenh@45 116 // as a first attempt, we'll use OntModel in order to avoid too much data model duplication,
stevenh@45 117 // but if performance is poor then we should try java objects
stevenh@45 118
stevenh@45 119 // Find all instances of sia:OrderedSet
stevenh@45 120 OntModel vteTempOntModel = ModelFactory
stevenh@45 121 .createOntologyModel(); // OntModelSpec.OWL_MEM
stevenh@45 122 OntClass orderedSetClass = ontModel.getOntClass(OrderedSet.RESOURCE_URI);
stevenh@45 123 ExtendedIterator<? extends OntResource> orderedSetIter = orderedSetClass.listInstances();
stevenh@45 124 Property memberOfOrderedSetProperty = ontModel.getOntProperty(MemberOfOrderedSet.PROPERTY_URI);
stevenh@45 125 Property fromDatapointProperty = ontModel.getOntProperty(FromDatapoint.PROPERTY_URI);
stevenh@45 126 Property toDatapointProperty = ontModel.getOntProperty(ToDatapoint.PROPERTY_URI);
stevenh@45 127 Property orderedIndexProperty = ontModel.getOntProperty(OrderedIndex.PROPERTY_URI);
stevenh@45 128 Property dimensionValueProperty = ontModel.getOntProperty(DimensionValue.PROPERTY_URI);
stevenh@45 129 Property dimensionProperty = ontModel.getOntProperty(DimensionValue.DIMENSION_URI);
stevenh@45 130 Property valueProperty = ontModel.getOntProperty(DimensionValue.VALUE_URI);
stevenh@45 131 Resource vectorTableElementResource = ontModel.createResource(VectorTableElement.RESOURCE_URI);
stevenh@45 132
stevenh@45 133 // For each sia:OrderedSet, find every sia:Datapoint (which has property sia:memberOfOrderedSet object sia:OrderedSet)
stevenh@45 134 // Make two copies of the list of sia:datapoints
stevenh@45 135
stevenh@45 136 OntResource orderedSet;
stevenh@45 137 StmtIterator datapoint1StmtIter;
stevenh@45 138 StmtIterator datapoint2StmtIter;
stevenh@45 139 while (orderedSetIter.hasNext())
stevenh@45 140 {
stevenh@45 141 orderedSet = orderedSetIter.next();
stevenh@45 142 datapoint1StmtIter = ontModel.listStatements((Resource)null, memberOfOrderedSetProperty, orderedSet);
stevenh@45 143
stevenh@45 144 // For every pair of sia:Datapoints datapoint1 and datapoint2 (one taken from each list), create a sia:VectorTableElement
stevenh@45 145 while (datapoint1StmtIter.hasNext())
stevenh@45 146 {
stevenh@45 147 Statement datapoint1Stmt = datapoint1StmtIter.next();
stevenh@45 148 datapoint2StmtIter = ontModel.listStatements((Resource)null, memberOfOrderedSetProperty, orderedSet);
stevenh@45 149
stevenh@45 150 while (datapoint2StmtIter.hasNext())
stevenh@45 151 {
stevenh@45 152 // Assert that the sia:VectorTableElement has sia:fromDatapoint ?datapoint1 and sia:toDatapoint ?datapoint2
stevenh@45 153 // (in the case where we're creating the 'V' vector table (i.e. returnWResults == false), only make this assertion
stevenh@45 154 // if ((?datapoint1 != ?datapoint2) && (?i1 < ?i2)) where ?datapoint1 sia:orderedIndex ?i1 and ?datapoint2 sia:orderedIndex ?i2
stevenh@45 155 Statement datapoint2Stmt = datapoint2StmtIter.next();
stevenh@45 156 Resource fromDatapoint = datapoint1Stmt.getSubject();
stevenh@45 157 Resource toDatapoint = datapoint2Stmt.getSubject();
stevenh@45 158
stevenh@45 159 Statement i1Stmt = ontModel.getProperty(fromDatapoint, orderedIndexProperty);
stevenh@45 160 Statement i2Stmt = ontModel.getProperty(toDatapoint, orderedIndexProperty);
stevenh@45 161 int i1 = i1Stmt.getInt();
stevenh@45 162 int i2 = i2Stmt.getInt();
stevenh@45 163
stevenh@45 164 if (addWResults || i1 < i2)
stevenh@45 165 {
stevenh@45 166 Resource vteBnode = ontModel.createResource(AnonId.create());
stevenh@45 167 vteTempOntModel.add(vteBnode, RDF.type, vectorTableElementResource);
stevenh@45 168 vteTempOntModel.add(vteBnode, fromDatapointProperty, fromDatapoint);
stevenh@45 169 vteTempOntModel.add(vteBnode, toDatapointProperty, toDatapoint);
stevenh@45 170
stevenh@45 171 // Replacement code for "construct_sia(tec)_vector_table_bnodes.sparql" end
stevenh@45 172
stevenh@45 173 // Replacement code for "construct_vector_table_details.sparql" start
stevenh@45 174 NodeIterator fromDimValIter = ontModel.listObjectsOfProperty(fromDatapoint, dimensionValueProperty);
stevenh@45 175 while (fromDimValIter.hasNext())
stevenh@45 176 {
stevenh@45 177 RDFNode fromDimValNode = fromDimValIter.next();
stevenh@45 178 Statement fromDimensionStmt = ontModel.getProperty(fromDimValNode.asResource(), dimensionProperty);
stevenh@45 179 Statement fromValueStmt = ontModel.getProperty(fromDimValNode.asResource(), valueProperty);
stevenh@45 180
stevenh@45 181 int fromDimension = fromDimensionStmt.getInt();
stevenh@45 182 double fromValue = fromValueStmt.getDouble();
stevenh@45 183
stevenh@45 184 NodeIterator toDimValIter = ontModel.listObjectsOfProperty(toDatapoint, dimensionValueProperty);
stevenh@45 185 while (toDimValIter.hasNext())
stevenh@45 186 {
stevenh@45 187 RDFNode toDimValNode = toDimValIter.next();
stevenh@45 188 Statement toDimensionStmt = ontModel.getProperty(toDimValNode.asResource(), dimensionProperty);
stevenh@45 189
stevenh@45 190 int toDimension = toDimensionStmt.getInt();
stevenh@45 191
stevenh@45 192 if (fromDimension == toDimension)
stevenh@45 193 {
stevenh@45 194 Statement toValueStmt = ontModel.getProperty(toDimValNode.asResource(), valueProperty);
stevenh@45 195 double toValue = toValueStmt.getDouble();
stevenh@45 196 double vteValue = toValue - fromValue;
stevenh@45 197
stevenh@45 198 Resource dimValBnode = ontModel.createResource(AnonId.create());
stevenh@45 199 vteTempOntModel.add(vteBnode, dimensionValueProperty, dimValBnode);
stevenh@45 200 vteTempOntModel.addLiteral(dimValBnode, dimensionProperty, toDimension);
stevenh@45 201 vteTempOntModel.addLiteral(dimValBnode, valueProperty, vteValue);
stevenh@45 202
stevenh@45 203 break;
stevenh@45 204 }
stevenh@45 205 }
stevenh@45 206 }
stevenh@45 207 }
stevenh@45 208 }
stevenh@45 209 }
stevenh@45 210 }
stevenh@45 211
stevenh@45 212 ontModel.add(vteTempOntModel);
stevenh@45 213 long endTime = System.currentTimeMillis();
stevenh@45 214 long elapsedTime = endTime - startTime;
stevenh@45 215 System.out.println("Completed java replacement for all SPARQL CONSTRUCTs in " + elapsedTime + " ms");
stevenh@45 216 // Replacement code for "construct_vector_table_details.sparql" end
stevenh@45 217 }
stevenh@45 218
steve@0 219 public static Datapoint findDatapoint(OntModel ontModel, List<Datapoint> datapoints, Resource resource) {
steve@0 220 Iterator<Datapoint> datapointsIter = datapoints.iterator();
steve@0 221 Datapoint datapoint;
stevenh@16 222
steve@0 223 while (datapointsIter.hasNext())
steve@0 224 {
steve@0 225 datapoint = datapointsIter.next();
stevenh@45 226
stevenh@45 227 if (datapoint.getResource().toString().equals(resource.toString()))
steve@0 228 return datapoint;
steve@0 229 }
stevenh@16 230
steve@0 231 datapoint = new Datapoint();
steve@0 232 datapoint.setResource(resource);
steve@0 233 Vector<DimensionValue> dimensionValues = SiaDimensionValueFactory.getDimensionValuesForResource(ontModel, resource);
steve@0 234 datapoint.setDimensionValues(dimensionValues);
steve@0 235 return datapoint;
steve@0 236 }
steve@0 237
steve@0 238 public static void assertOrder(OntModel ontModel, List<VectorTableElement> vteList) {
steve@0 239
steve@0 240 Resource bnode = ontModel.createResource(AnonId.create());
steve@0 241 Resource siaOrderedSet = ontModel.createResource(OrderedSet.RESOURCE_URI);
steve@0 242
steve@0 243 ontModel.add(bnode, RDF.type, siaOrderedSet);
steve@0 244
steve@0 245 int numVectorTableElements = vteList.size();
steve@0 246
steve@0 247 VectorTableElement vte;
steve@0 248 for (int orderedIndex = 0; orderedIndex < numVectorTableElements; orderedIndex++)
steve@0 249 {
steve@0 250 // Assert <bnode, rdf:type, sia:OrderedSet>
steve@0 251 vte = vteList.get(orderedIndex);
steve@0 252 Property memberOfOrderedSetProperty = ontModel.getProperty(MemberOfOrderedSet.PROPERTY_URI);
steve@0 253 ontModel.add(vte.getResource(), memberOfOrderedSetProperty, bnode);
steve@0 254
steve@0 255 if (orderedIndex < numVectorTableElements - 1)
steve@0 256 {
steve@0 257 // Assert <nextVectorTable, sia:directlyFollows, currentVectorTableElement>
steve@0 258 VectorTableElement nextVte = vteList.get(orderedIndex + 1);
steve@0 259 Property directlyFollowsProperty = ontModel.getProperty(DirectlyFollows.PROPERTY_URI);
steve@0 260 ontModel.add(nextVte.getResource(), directlyFollowsProperty, vte.getResource());
steve@0 261 }
steve@0 262
steve@0 263 // Assert <currentVectorTableElement, sia:orderedIndex, orderedIndex>
steve@0 264 Property orderedIndexProperty = ontModel.getProperty(OrderedIndex.PROPERTY_URI);
steve@0 265 ontModel.addLiteral(vte.getResource(), orderedIndexProperty, orderedIndex);
steve@0 266
steve@0 267 }
steve@0 268
steve@0 269 }
steve@0 270
steve@0 271 }