| 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 |
import org.eclipse.rdf4j.model.Value; |
| 23 |
import org.eclipse.rdf4j.rio.RDFHandlerException; |
| 24 |
import org.slf4j.Logger; |
| 25 |
import org.slf4j.LoggerFactory; |
| 26 |
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; |
| 27 |
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue; |
| 28 |
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue; |
| 29 |
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; |
| 30 |
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue; |
| 31 |
import org.wikidata.wdtk.datamodel.interfaces.StringValue; |
| 32 |
import org.wikidata.wdtk.datamodel.interfaces.TimeValue; |
| 33 |
import org.wikidata.wdtk.datamodel.interfaces.UnsupportedValue; |
| 34 |
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor; |
| 35 |
import org.wikidata.wdtk.rdf.OwlDeclarationBuffer; |
| 36 |
import org.wikidata.wdtk.rdf.PropertyRegister; |
| 37 |
import org.wikidata.wdtk.rdf.RdfWriter; |
| 38 |
|
| 39 |
/** |
| 40 |
* Class to convert Wikibase data values to RDF. The class is a visitor that |
| 41 |
* that computes an RDF value (URI or literal) to represent any kind of Wikibase |
| 42 |
* data value. Some values are complex and require further RDF triples to be |
| 43 |
* written. In such cases, the class stores the values to a buffer. Methods for |
| 44 |
* writing additional triples for these buffered values can be called later. |
| 45 |
* |
| 46 |
* @author Markus Kroetzsch |
| 47 |
* |
| 48 |
*/ |
| 49 |
public class AnyValueConverter implements |
| 50 |
ValueConverter<org.wikidata.wdtk.datamodel.interfaces.Value>, |
| 51 |
ValueVisitor<Value> { |
| 52 |
|
| 53 |
final private RdfWriter rdfWriter; |
| 54 |
final EntityIdValueConverter entityIdValueConverter; |
| 55 |
final StringValueConverter stringValueConverter; |
| 56 |
final TimeValueConverter timeValueConverter; |
| 57 |
final GlobeCoordinatesValueConverter globeCoordinatesValueConverter; |
| 58 |
final QuantityValueConverter quantityValueConverter; |
| 59 |
final MonolingualTextValueConverter monolingualTextValueConverter; |
| 60 |
|
| 61 |
PropertyIdValue currentPropertyIdValue; |
| 62 |
boolean simple; |
| 63 |
|
| 64 |
static final Logger logger = LoggerFactory |
| 65 |
.getLogger(AnyValueConverter.class); |
| 66 |
|
| 67 |
public AnyValueConverter(RdfWriter rdfWriter, |
| 68 |
OwlDeclarationBuffer rdfConversionBuffer, |
| 69 |
PropertyRegister propertyRegister) { |
| 70 |
|
| 71 |
this.rdfWriter = rdfWriter; |
| 72 |
this.entityIdValueConverter = new EntityIdValueConverter(rdfWriter, |
| 73 |
propertyRegister, rdfConversionBuffer); |
| 74 |
this.stringValueConverter = new StringValueConverter(rdfWriter, |
| 75 |
propertyRegister, rdfConversionBuffer); |
| 76 |
this.timeValueConverter = new TimeValueConverter(rdfWriter, |
| 77 |
propertyRegister, rdfConversionBuffer); |
| 78 |
this.globeCoordinatesValueConverter = new GlobeCoordinatesValueConverter( |
| 79 |
rdfWriter, propertyRegister, rdfConversionBuffer); |
| 80 |
this.quantityValueConverter = new QuantityValueConverter(rdfWriter, |
| 81 |
propertyRegister, rdfConversionBuffer); |
| 82 |
this.monolingualTextValueConverter = new MonolingualTextValueConverter( |
| 83 |
rdfWriter, propertyRegister, rdfConversionBuffer); |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public Value getRdfValue( |
| 88 |
org.wikidata.wdtk.datamodel.interfaces.Value value, |
| 89 |
PropertyIdValue propertyIdValue, boolean simple) { |
| 90 |
this.currentPropertyIdValue = propertyIdValue; |
| 91 |
this.simple = simple; |
| 92 |
return value.accept(this); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public Value visit(EntityIdValue value) { |
| 97 |
return this.entityIdValueConverter.getRdfValue(value, |
| 98 |
this.currentPropertyIdValue, this.simple); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Value visit(GlobeCoordinatesValue value) { |
| 103 |
return this.globeCoordinatesValueConverter.getRdfValue(value, |
| 104 |
this.currentPropertyIdValue, this.simple); |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public Value visit(MonolingualTextValue value) { |
| 109 |
return this.monolingualTextValueConverter.getRdfValue(value, |
| 110 |
this.currentPropertyIdValue, this.simple); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public Value visit(QuantityValue value) { |
| 115 |
return this.quantityValueConverter.getRdfValue(value, |
| 116 |
this.currentPropertyIdValue, this.simple); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public Value visit(StringValue value) { |
| 121 |
return this.stringValueConverter.getRdfValue(value, |
| 122 |
this.currentPropertyIdValue, this.simple); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public Value visit(TimeValue value) { |
| 127 |
return this.timeValueConverter.getRdfValue(value, |
| 128 |
this.currentPropertyIdValue, this.simple); |
| 129 |
} |
| 130 |
|
| 131 |
@Override |
| 132 |
public void writeAuxiliaryTriples() throws RDFHandlerException { |
| 133 |
this.entityIdValueConverter.writeAuxiliaryTriples(); |
| 134 |
this.stringValueConverter.writeAuxiliaryTriples(); |
| 135 |
this.globeCoordinatesValueConverter.writeAuxiliaryTriples(); |
| 136 |
this.timeValueConverter.writeAuxiliaryTriples(); |
| 137 |
this.quantityValueConverter.writeAuxiliaryTriples(); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public Value visit(UnsupportedValue value) { |
| 142 |
return this.rdfWriter.getFreshBNode(); |
| 143 |
} |
| 144 |
|
| 145 |
} |