Skip to content

Method: getValueAsSesameUri(Value)

1: /**
2: * Copyright (C) 2019 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.ontodriver.sesame;
14:
15: import cz.cvut.kbss.ontodriver.model.Assertion;
16: import cz.cvut.kbss.ontodriver.sesame.config.Constants;
17: import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
18: import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
19: import org.eclipse.rdf4j.model.IRI;
20: import org.eclipse.rdf4j.model.Value;
21: import org.eclipse.rdf4j.model.ValueFactory;
22:
23: class SesameValueConverter {
24:
25: private ValueFactory vf;
26:
27: SesameValueConverter(ValueFactory vf) {
28: this.vf = vf;
29: }
30:
31: Value toSesameValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) throws SesameDriverException {
32: switch (assertion.getType()) {
33: case DATA_PROPERTY:
34: return SesameUtils.createDataPropertyLiteral(val.getValue(), language(assertion), vf);
35: case CLASS:
36: case OBJECT_PROPERTY:
37: return getValueAsSesameUri(val);
38: case ANNOTATION_PROPERTY: // Intentional fall-through
39: case PROPERTY:
40: return resolvePropertyValue(assertion, val);
41: default:
42: // Failsafe
43: throw new IllegalArgumentException("Unsupported assertion type " + assertion.getType());
44: }
45: }
46:
47: private static String language(Assertion assertion) {
48: return assertion.hasLanguage() ? assertion.getLanguage() : Constants.DEFAULT_LANG;
49: }
50:
51: private IRI getValueAsSesameUri(cz.cvut.kbss.ontodriver.model.Value<?> val) throws SesameDriverException {
52: try {
53: return vf.createIRI(val.getValue().toString());
54: } catch (IllegalArgumentException e) {
55: throw new SesameDriverException(e);
56: }
57: }
58:
59: private Value resolvePropertyValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) {
60: if (SesameUtils.isResourceIdentifier(val.getValue())) {
61: return vf.createIRI(val.getValue().toString());
62: } else {
63: return SesameUtils.createDataPropertyLiteral(val.getValue(), language(assertion), vf);
64: }
65: }
66: }