| 1 |
package org.wikidata.wdtk.rdf.values; |
| 2 |
|
| 3 |
/* |
| 4 |
* #%L |
| 5 |
* Wikidata Toolkit RDF |
| 6 |
* %% |
| 7 |
* Copyright (C) 2014 Wikidata Toolkit Developers |
| 8 |
* %% |
| 9 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 |
* you may not use this file except in compliance with the License. |
| 11 |
* You may obtain a copy of the License at |
| 12 |
* |
| 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, software |
| 16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
* See the License for the specific language governing permissions and |
| 19 |
* limitations under the License. |
| 20 |
* #L% |
| 21 |
*/ |
| 22 |
|
| 23 |
import org.eclipse.rdf4j.model.Resource; |
| 24 |
import org.eclipse.rdf4j.model.IRI; |
| 25 |
import org.eclipse.rdf4j.model.Value; |
| 26 |
import org.eclipse.rdf4j.rio.RDFHandlerException; |
| 27 |
import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue; |
| 28 |
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; |
| 29 |
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue; |
| 30 |
import org.wikidata.wdtk.rdf.OwlDeclarationBuffer; |
| 31 |
import org.wikidata.wdtk.rdf.PropertyRegister; |
| 32 |
import org.wikidata.wdtk.rdf.RdfWriter; |
| 33 |
import org.wikidata.wdtk.rdf.Vocabulary; |
| 34 |
|
| 35 |
public class QuantityValueConverter extends |
| 36 |
BufferedValueConverter<QuantityValue> { |
| 37 |
|
| 38 |
public QuantityValueConverter(RdfWriter rdfWriter, |
| 39 |
PropertyRegister propertyRegister, |
| 40 |
OwlDeclarationBuffer rdfConversionBuffer) { |
| 41 |
super(rdfWriter, propertyRegister, rdfConversionBuffer); |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public Value getRdfValue(QuantityValue value, |
| 46 |
PropertyIdValue propertyIdValue, boolean simple) { |
| 47 |
String datatype = this.propertyRegister |
| 48 |
.setPropertyTypeFromQuantityValue(propertyIdValue, value); |
| 49 |
|
| 50 |
switch (datatype) { |
| 51 |
case DatatypeIdValue.DT_QUANTITY: |
| 52 |
if (simple) { |
| 53 |
this.rdfConversionBuffer.addDatatypeProperty(propertyIdValue); |
| 54 |
return this.rdfWriter.getLiteral(value.getNumericValue() |
| 55 |
.toPlainString(), RdfWriter.XSD_DECIMAL); |
| 56 |
} else { |
| 57 |
IRI valueUri = this.rdfWriter.getUri(Vocabulary.getQuantityValueUri(value)); |
| 58 |
|
| 59 |
this.rdfConversionBuffer.addObjectProperty(propertyIdValue); |
| 60 |
addValue(value, valueUri); |
| 61 |
|
| 62 |
return valueUri; |
| 63 |
} |
| 64 |
default: |
| 65 |
logIncompatibleValueError(propertyIdValue, datatype, "quantity"); |
| 66 |
return null; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void writeValue(QuantityValue value, Resource resource) |
| 72 |
throws RDFHandlerException { |
| 73 |
this.rdfWriter.writeTripleValueObject(resource, RdfWriter.RDF_TYPE, |
| 74 |
RdfWriter.WB_QUANTITY_VALUE); |
| 75 |
this.rdfWriter.writeTripleLiteralObject(resource, |
| 76 |
RdfWriter.WB_QUANTITY_AMOUNT, value.getNumericValue().toPlainString(), |
| 77 |
RdfWriter.XSD_DECIMAL); |
| 78 |
if(value.getLowerBound() != null) { |
| 79 |
this.rdfWriter.writeTripleLiteralObject(resource, |
| 80 |
RdfWriter.WB_QUANTITY_LOWER_BOUND, value.getLowerBound().toPlainString(), |
| 81 |
RdfWriter.XSD_DECIMAL); |
| 82 |
} |
| 83 |
if(value.getUpperBound() != null) { |
| 84 |
this.rdfWriter.writeTripleLiteralObject(resource, |
| 85 |
RdfWriter.WB_QUANTITY_UPPER_BOUND, value.getUpperBound().toPlainString(), |
| 86 |
RdfWriter.XSD_DECIMAL); |
| 87 |
} |
| 88 |
String unitIri = ("1".equals(value.getUnit())) |
| 89 |
? Vocabulary.WB_NO_UNIT |
| 90 |
: value.getUnit(); |
| 91 |
this.rdfWriter.writeTripleUriObject(resource, |
| 92 |
RdfWriter.WB_QUANTITY_UNIT, unitIri); |
| 93 |
} |
| 94 |
|
| 95 |
} |