Mercurial > hg > semantic-sia
view src/org/qmul/eecs/c4dm/sia/SiaVectorTableElementFactory.java @ 16:6863887e1b70
organized imports
added new "createV" and "createW" methods so that we can specifically create Vector Tables V or W as described in the paper
removed unnecessary code
added try/catch blocks where necessary
author | stevenh |
---|---|
date | Tue, 08 Jan 2013 18:30:14 +0000 |
parents | 08675ab08e7f |
children | d19a0e764a97 |
line wrap: on
line source
package org.qmul.eecs.c4dm.sia; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Vector; import org.qmul.eecs.c4dm.sia.exceptions.DimensionException; import org.qmul.eecs.c4dm.sia.model.Datapoint; import org.qmul.eecs.c4dm.sia.model.DimensionValue; import org.qmul.eecs.c4dm.sia.model.DirectlyFollows; import org.qmul.eecs.c4dm.sia.model.FromDatapoint; import org.qmul.eecs.c4dm.sia.model.MemberOfOrderedSet; import org.qmul.eecs.c4dm.sia.model.OrderedIndex; import org.qmul.eecs.c4dm.sia.model.OrderedSet; import org.qmul.eecs.c4dm.sia.model.ToDatapoint; import org.qmul.eecs.c4dm.sia.model.VectorTable; import org.qmul.eecs.c4dm.sia.model.VectorTableElement; import com.hp.hpl.jena.ontology.Individual; import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.rdf.model.AnonId; import com.hp.hpl.jena.rdf.model.Property; import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.util.iterator.ExtendedIterator; import com.hp.hpl.jena.vocabulary.RDF; public class SiaVectorTableElementFactory { /** * @param ontModel * @param datapoints * @return */ public static VectorTable createV(OntModel ontModel, List<Datapoint> datapoints) { return create(ontModel, datapoints, false); } /** * @param ontModel * @param datapoints * @return */ public static VectorTable createW(OntModel ontModel, List<Datapoint> datapoints) { return create(ontModel, datapoints, true); } /** * @param ontModel * @param datapoints * @param returnWResults * @return */ public static VectorTable create(OntModel ontModel, List<Datapoint> datapoints, boolean returnWResults) { OntClass vteClass = ontModel.getOntClass(VectorTableElement.RESOURCE_URI); Resource vteResource = ontModel.getOntResource(vteClass); ExtendedIterator<Individual> vteIter = ontModel.listIndividuals(vteResource); Property fromDatapointProperty = ontModel.getOntProperty(FromDatapoint.PROPERTY_URI); Property toDatapointProperty = ontModel.getOntProperty(ToDatapoint.PROPERTY_URI); List<VectorTableElement> vteList = new ArrayList<VectorTableElement>(); VectorTableElement vte; Individual vteIndividual; StmtIterator fromDPStmtIter; StmtIterator toDPStmtIter; Statement fromDatapointStmt; Statement toDatapointStmt; VectorTable vectorTable = new VectorTable(); // Find all rdf VectorTableElements while (vteIter.hasNext()) { vteIndividual = vteIter.next(); vte = new VectorTableElement(); vte.setResource(vteIndividual); // Find the 'fromDatapoint' for this VectorTableElement fromDPStmtIter = ontModel.listStatements(vteIndividual, fromDatapointProperty, (RDFNode)null); fromDatapointStmt = fromDPStmtIter.next(); Datapoint fromDatapoint = findDatapoint(ontModel, datapoints, fromDatapointStmt.getResource()); try { vte.setFromDatapoint(fromDatapoint); } catch (DimensionException e) { e.printStackTrace(); System.exit(1); } // Find the 'toDatapoint' for this VectorTableElement toDPStmtIter = ontModel.listStatements(vteIndividual, toDatapointProperty, (RDFNode)null); toDatapointStmt = toDPStmtIter.next(); Datapoint toDatapoint = findDatapoint(ontModel, datapoints, toDatapointStmt.getResource()); try { vte.setToDatapoint(toDatapoint); } catch (DimensionException e) { e.printStackTrace(); System.exit(1); } // Only add VectorTableElements whose 'from' ordered index is less than the 'to' ordered index if (returnWResults || vte.getFromDatapoint().getOrderedIndex() < vte.getToDatapoint().getOrderedIndex()) { // Find all dimensionValues for this VectorTableElement Vector<DimensionValue> dimensionValues = SiaDimensionValueFactory.getDimensionValuesForResource(ontModel, vteIndividual); vte.setDimensionValues(dimensionValues); vteList.add(vte); } } vectorTable.setVteList(vteList); vectorTable.setNumDatapoints(datapoints.size()); return vectorTable; } public static Datapoint findDatapoint(OntModel ontModel, List<Datapoint> datapoints, Resource resource) { Iterator<Datapoint> datapointsIter = datapoints.iterator(); Datapoint datapoint; while (datapointsIter.hasNext()) { datapoint = datapointsIter.next(); if (datapoint.getResource().getLocalName().equals(resource.getLocalName())) return datapoint; } datapoint = new Datapoint(); datapoint.setResource(resource); Vector<DimensionValue> dimensionValues = SiaDimensionValueFactory.getDimensionValuesForResource(ontModel, resource); datapoint.setDimensionValues(dimensionValues); return datapoint; } public static void assertOrder(OntModel ontModel, List<VectorTableElement> vteList) { Resource bnode = ontModel.createResource(AnonId.create()); Resource siaOrderedSet = ontModel.createResource(OrderedSet.RESOURCE_URI); ontModel.add(bnode, RDF.type, siaOrderedSet); int numVectorTableElements = vteList.size(); VectorTableElement vte; for (int orderedIndex = 0; orderedIndex < numVectorTableElements; orderedIndex++) { // Assert <bnode, rdf:type, sia:OrderedSet> vte = vteList.get(orderedIndex); Property memberOfOrderedSetProperty = ontModel.getProperty(MemberOfOrderedSet.PROPERTY_URI); ontModel.add(vte.getResource(), memberOfOrderedSetProperty, bnode); if (orderedIndex < numVectorTableElements - 1) { // Assert <nextVectorTable, sia:directlyFollows, currentVectorTableElement> VectorTableElement nextVte = vteList.get(orderedIndex + 1); Property directlyFollowsProperty = ontModel.getProperty(DirectlyFollows.PROPERTY_URI); ontModel.add(nextVte.getResource(), directlyFollowsProperty, vte.getResource()); } // Assert <currentVectorTableElement, sia:orderedIndex, orderedIndex> Property orderedIndexProperty = ontModel.getProperty(OrderedIndex.PROPERTY_URI); ontModel.addLiteral(vte.getResource(), orderedIndexProperty, orderedIndex); } } }