Skip to content

Commit

Permalink
Basic implementation for compound value serializations
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroetzsch committed May 3, 2014
1 parent db5c05c commit 20232cd
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

import org.openrdf.model.Resource;
import org.openrdf.rio.RDFHandlerException;
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
import org.wikidata.wdtk.datamodel.interfaces.TimeValue;

/**
* This class stores information about data that should be serialized in RDF
Expand All @@ -42,10 +44,18 @@ public class RdfConversionBuffer {

List<QuantityValue> quantityValueQueue;
List<Resource> quantityValueSubjectQueue;
List<TimeValue> timeValueQueue;
List<Resource> timeValueSubjectQueue;
List<GlobeCoordinatesValue> coordinatesValueQueue;
List<Resource> coordinatesValueSubjectQueue;

public RdfConversionBuffer() {
this.quantityValueQueue = new ArrayList<QuantityValue>();
this.quantityValueSubjectQueue = new ArrayList<Resource>();
this.timeValueQueue = new ArrayList<TimeValue>();
this.timeValueSubjectQueue = new ArrayList<Resource>();
this.coordinatesValueQueue = new ArrayList<GlobeCoordinatesValue>();
this.coordinatesValueSubjectQueue = new ArrayList<Resource>();
}

/**
Expand All @@ -62,16 +72,63 @@ public void addQuantityValue(QuantityValue quantitiyValue, Resource resource) {
this.quantityValueSubjectQueue.add(resource);
}

/**
* Adds the given time value to the list of values that should still be
* serialized. The given RDF resource will be used as a subject.
*
* @param timeValue
* the value to be serialized
* @param resource
* the RDF resource that is used as a subject for serialization
*/
public void addTimeValue(TimeValue timeValue, Resource resource) {
this.timeValueQueue.add(timeValue);
this.timeValueSubjectQueue.add(resource);
}

/**
* Adds the given globe coordinates value to the list of values that should
* still be serialized. The given RDF resource will be used as a subject.
*
* @param globeCoordinatesValue
* the value to be serialized
* @param resource
* the RDF resource that is used as a subject for serialization
*/
public void addGlobeCoordinatesValue(
GlobeCoordinatesValue globeCoordinatesValue, Resource resource) {
this.coordinatesValueQueue.add(globeCoordinatesValue);
this.coordinatesValueSubjectQueue.add(resource);
}

public void writeValues(ValueRdfConverter valueRdfConverter)
throws RDFHandlerException {
Iterator<QuantityValue> valueIterator = this.quantityValueQueue
Iterator<QuantityValue> quantitiyValueIterator = this.quantityValueQueue
.iterator();
for (Resource resource : this.quantityValueSubjectQueue) {
QuantityValue quantityValue = valueIterator.next();
QuantityValue quantityValue = quantitiyValueIterator.next();
valueRdfConverter.writeQuantityValue(quantityValue, resource);
}
this.quantityValueSubjectQueue.clear();
this.quantityValueQueue.clear();

Iterator<TimeValue> timeValueIterator = this.timeValueQueue.iterator();
for (Resource resource : this.timeValueSubjectQueue) {
TimeValue timeValue = timeValueIterator.next();
valueRdfConverter.writeTimeValue(timeValue, resource);
}
this.timeValueSubjectQueue.clear();
this.timeValueQueue.clear();

Iterator<GlobeCoordinatesValue> globeCoordinatesValueIterator = this.coordinatesValueQueue
.iterator();
for (Resource resource : this.coordinatesValueSubjectQueue) {
GlobeCoordinatesValue globeCoordinatesValue = globeCoordinatesValueIterator
.next();
valueRdfConverter.writeGlobeCoordinatesValue(globeCoordinatesValue,
resource);
}
this.coordinatesValueSubjectQueue.clear();
this.coordinatesValueQueue.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,50 @@ public ValueRdfConverter(RdfWriter rdfWriter,
*/
public void writeQuantityValue(QuantityValue quantityValue,
Resource resource) throws RDFHandlerException {
// TODO move all strings to Vocabulary
this.rdfWriter.writeTripleUriObject(resource, Vocabulary.RDF_TYPE,
Vocabulary.WB_QUANTITY_VALUE);
this.rdfWriter.writeTripleLiteralObject(resource,
Vocabulary.WB_NUMERIC_VALUE, quantityValue.getNumericValue()
.toString(), Vocabulary.PREFIX_XSD + "decimal");
.toString(), Vocabulary.XSD_DECIMAL);
this.rdfWriter.writeTripleLiteralObject(resource,
Vocabulary.WB_LOWER_BOUND, quantityValue.getLowerBound()
.toString(), Vocabulary.PREFIX_XSD + "decimal");
.toString(), Vocabulary.XSD_DECIMAL);
this.rdfWriter.writeTripleLiteralObject(resource,
Vocabulary.WB_UPPER_BOUND, quantityValue.getUpperBound()
.toString(), Vocabulary.PREFIX_XSD + "decimal");
.toString(), Vocabulary.XSD_DECIMAL);
}

/**
* Write the auxiliary RDF data for encoding the given value.
*
* @param timeValue
* the value to write
* @param resource
* the (subject) URI to use to represent this value in RDF
* @throws RDFHandlerException
*/
public void writeTimeValue(TimeValue timeValue, Resource resource)
throws RDFHandlerException {
this.rdfWriter.writeTripleUriObject(resource, Vocabulary.RDF_TYPE,
Vocabulary.WB_TIME_VALUE);
// TODO finish
}

/**
* Write the auxiliary RDF data for encoding the given value.
*
* @param globeCoordinatesValue
* the value to write
* @param resource
* the (subject) URI to use to represent this value in RDF
* @throws RDFHandlerException
*/
public void writeGlobeCoordinatesValue(
GlobeCoordinatesValue globeCoordinatesValue, Resource resource)
throws RDFHandlerException {
this.rdfWriter.writeTripleUriObject(resource, Vocabulary.RDF_TYPE,
Vocabulary.WB_GLOBE_COORDINATES_VALUE);
// TODO finish
}

public Value getRdfValueForWikidataValue(
Expand Down Expand Up @@ -136,7 +168,8 @@ public Value visit(GlobeCoordinatesValue value) {
URI valueUri = this.factory.createURI(Vocabulary.PREFIX_WIKIDATA
+ VALUE_PREFIX_GLOBECOORDS + bytesToHex(md.digest()));

// TODO add attributes
this.rdfConversionBuffer.addGlobeCoordinatesValue(value, valueUri);

return valueUri;
}

Expand Down Expand Up @@ -205,7 +238,8 @@ public Value visit(TimeValue value) {
URI valueUri = this.factory.createURI(Vocabulary.PREFIX_WIKIDATA
+ VALUE_PREFIX_TIME + bytesToHex(md.digest()));

// TODO add attributes
this.rdfConversionBuffer.addTimeValue(value, valueUri);

return valueUri;
}

Expand Down
23 changes: 23 additions & 0 deletions wdtk-rdf/src/main/java/org/wikidata/wdtk/rdf/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public class Vocabulary {
+ "ObjectProperty";
public static final String OWL_DATATYPE_PROPERTY = PREFIX_OWL
+ "DatatypeProperty";
public static final String XSD_DECIMAL = PREFIX_XSD + "decimal";
public static final String XSD_INT = PREFIX_XSD + "int";
public static final String XSD_DATE = PREFIX_XSD + "date";
public static final String XSD_G_YEAR = PREFIX_XSD + "gYear";
public static final String XSD_G_YEAR_MONTH = PREFIX_XSD + "gYearMonth";
public static final String XSD_DATETIME = PREFIX_XSD + "dateTime";

/**
* Hash map defining the OWL declaration types of the standard vocabulary.
Expand Down Expand Up @@ -162,6 +168,23 @@ public class Vocabulary {
VOCABULARY_TYPES.put(WB_QUANTITY_VALUE, OWL_CLASS);
}

/**
* Class for Wikibase time values.
*/
public static final String WB_TIME_VALUE = PREFIX_WBONTO + "TimeValue";
static {
VOCABULARY_TYPES.put(WB_TIME_VALUE, OWL_CLASS);
}

/**
* Class for Wikibase globe coordinates values.
*/
public static final String WB_GLOBE_COORDINATES_VALUE = PREFIX_WBONTO
+ "GlobeCoordinatesValue";
static {
VOCABULARY_TYPES.put(WB_GLOBE_COORDINATES_VALUE, OWL_CLASS);
}

/**
* Property for defining the datatype of a Wikibase property.
*/
Expand Down

0 comments on commit 20232cd

Please sign in to comment.