Skip to content

Commit

Permalink
Language tag specification support - use language tag in assertion wh…
Browse files Browse the repository at this point in the history
…en persisting string literals into the repository.
  • Loading branch information
ledsoft committed Jun 13, 2017
1 parent beba45e commit e39c9da
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Triple(URI subject, URI property, Object value, String language) {
this.subject = Objects.requireNonNull(subject);
this.property = Objects.requireNonNull(property);
this.value = Objects.requireNonNull(value);
this.language = Objects.requireNonNull(language);
this.language = language;
}

public URI getSubject() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,4 +513,37 @@ public void persistSetsStringLiteralLanguageTagAccordingToDescriptor() throws Ex
"cs")), em);
assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
}

@Test
public void persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt()
throws Exception {
this.em = getEntityManager(
"persistSetsStringLiteralLanguageTagToGloballyConfiguredValueWhenDescriptorDoesNotSpecifyIt", false);
em.getTransaction().begin();
em.persist(entityA);
em.getTransaction().commit();

verifyStatementsPresent(Collections.singleton(
new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
"en")), em);
assertNotNull(em.find(OWLClassA.class, entityA.getUri()));
}

@Test
public void persistAllowsOverridingGlobalLanguageWithLocalEmptyTag() throws Exception {
this.em = getEntityManager("persistAllowsOverridingGlobalLanguageWithLocalEmptyTag", false);
em.getTransaction().begin();
final Descriptor descriptor = new EntityDescriptor();
descriptor.setAttributeLanguage(OWLClassA.class.getDeclaredField("stringAttribute"), null);
em.persist(entityA, descriptor);
em.getTransaction().commit();

verifyStatementsPresent(Collections.singleton(
new Triple(entityA.getUri(), URI.create(Vocabulary.P_A_STRING_ATTRIBUTE), entityA.getStringAttribute(),
null)), em);
final OWLClassA result = em.find(OWLClassA.class, entityA.getUri());
assertNotNull(result);
// The string attribute should be loaded even though PU language is set to en, because the persisted value has no lang tag
assertEquals(entityA.getStringAttribute(), result.getStringAttribute());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/**
* Copyright (C) 2016 Czech Technical University in Prague
*
* <p>
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
Expand All @@ -31,7 +31,7 @@ public AxiomAdapter(OWLDataFactory dataFactory, String language) {
this.language = language;
}

public OWLAxiom toOwlClassAssertionAxiom(Axiom<?> axiom) {
OWLAxiom toOwlClassAssertionAxiom(Axiom<?> axiom) {
final OWLClass owlClass = dataFactory.getOWLClass(IRI.create(axiom.getValue().stringValue()));
return dataFactory.getOWLClassAssertionAxiom(owlClass, toOWLIndividual(axiom.getSubject()));
}
Expand All @@ -45,16 +45,20 @@ public OWLAxiom toOwlObjectPropertyAssertionAxiom(Axiom<?> axiom) {
objectValue);
}

public OWLAxiom toOwlDataPropertyAssertionAxiom(Axiom<?> axiom) {
final OWLDataProperty dataProperty = dataFactory.getOWLDataProperty(
IRI.create(axiom.getAssertion().getIdentifier()));
OWLAxiom toOwlDataPropertyAssertionAxiom(Axiom<?> axiom) {
final OWLDataProperty dataProperty = dataFactory
.getOWLDataProperty(IRI.create(axiom.getAssertion().getIdentifier()));
final OWLLiteral dataValue = OwlapiUtils.createOWLLiteralFromValue(axiom.getValue().getValue(),
dataFactory, language);
dataFactory, language(axiom.getAssertion()));
return dataFactory
.getOWLDataPropertyAssertionAxiom(dataProperty, toOWLIndividual(axiom.getSubject()), dataValue);
}

public OWLAxiom toOwlAnnotationPropertyAssertionAxiom(Axiom<?> axiom) {
private String language(Assertion assertion) {
return assertion.hasLanguage() ? assertion.getLanguage() : language;
}

OWLAxiom toOwlAnnotationPropertyAssertionAxiom(Axiom<?> axiom) {
final OWLAnnotationProperty annotationProperty = dataFactory.getOWLAnnotationProperty(IRI.create(
axiom.getAssertion().getIdentifier()));
final Object value = axiom.getValue().getValue();
Expand All @@ -63,18 +67,18 @@ public OWLAxiom toOwlAnnotationPropertyAssertionAxiom(Axiom<?> axiom) {
annotationValue = IRI.create(value.toString());
} else {
annotationValue = OwlapiUtils.createOWLLiteralFromValue(
axiom.getValue().getValue(), dataFactory, language);
axiom.getValue().getValue(), dataFactory, language(axiom.getAssertion()));
}
return dataFactory
.getOWLAnnotationAssertionAxiom(annotationProperty, toOWLIndividual(axiom.getSubject()).getIRI(),
annotationValue);
}

public OWLNamedIndividual toOWLIndividual(NamedResource subject) {
private OWLNamedIndividual toOWLIndividual(NamedResource subject) {
return dataFactory.getOWLNamedIndividual(IRI.create(subject.getIdentifier()));
}

public Axiom<?> toAxiom(NamedResource subject, OWLDataPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
Axiom<?> toAxiom(NamedResource subject, OWLDataPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
final Assertion assertion = Assertion
.createDataPropertyAssertion(assertionAxiom.getProperty().asOWLDataProperty().getIRI().toURI(),
isInferred);
Expand All @@ -85,15 +89,15 @@ public <V> Axiom<V> createAxiom(NamedResource subject, Assertion assertion, V va
return new AxiomImpl<>(subject, assertion, new Value<>(value));
}

public Axiom<?> toAxiom(NamedResource subject, OWLObjectPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
Axiom<?> toAxiom(NamedResource subject, OWLObjectPropertyAssertionAxiom assertionAxiom, boolean isInferred) {
final Assertion assertion = Assertion
.createObjectPropertyAssertion(assertionAxiom.getProperty().asOWLObjectProperty().getIRI().toURI(),
isInferred);
final IRI target = assertionAxiom.getObject().asOWLNamedIndividual().getIRI();
return createAxiom(subject, assertion, NamedResource.create(target.toURI()));
}

public Axiom<?> toAxiom(NamedResource subject, OWLAnnotationAssertionAxiom assertionAxiom, boolean isInferred) {
Axiom<?> toAxiom(NamedResource subject, OWLAnnotationAssertionAxiom assertionAxiom, boolean isInferred) {
final Assertion assertion = Assertion
.createAnnotationPropertyAssertion(
assertionAxiom.getProperty().asOWLAnnotationProperty().getIRI().toURI(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package cz.cvut.kbss.ontodriver.owlapi;

import com.google.common.base.Optional;
import cz.cvut.kbss.ontodriver.model.*;
import cz.cvut.kbss.ontodriver.owlapi.environment.Generator;
import org.junit.Before;
import org.junit.Test;
import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom;
import org.semanticweb.owlapi.model.OWLLiteral;
import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl;

import java.net.URI;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class AxiomAdapterTest {

private static final String LANG = "en";

private static final URI INDIVIDUAL = Generator.generateUri();
private static final URI PROPERTY = Generator.generateUri();

private AxiomAdapter adapter;

@Before
public void setUp() {
this.adapter = new AxiomAdapter(new OWLDataFactoryImpl(), LANG);
}

@Test
public void toOwlDataPropertyAxiomUsesLanguageTagSpecifiedInAssertion() {
final Axiom<String> ax = new AxiomImpl<>(NamedResource.create(INDIVIDUAL),
Assertion.createDataPropertyAssertion(PROPERTY, "cs", false), new Value<>("cestina"));
final OWLAxiom axiom = adapter.toOwlDataPropertyAssertionAxiom(ax);
final OWLDataPropertyAssertionAxiom dpAxiom = (OWLDataPropertyAssertionAxiom) axiom;
final OWLLiteral literal = dpAxiom.getObject();
assertEquals(ax.getValue().getValue(), literal.getLiteral());
assertEquals("cs", literal.getLang());
}

@Test
public void toOwlDataPropertyAxiomUsesGloballyConfiguredLanguageWhenItIsNotSpecifiedInAssertion() {
final Axiom<String> ax = new AxiomImpl<>(NamedResource.create(INDIVIDUAL),
Assertion.createDataPropertyAssertion(PROPERTY, false), new Value<>("english"));
final OWLAxiom axiom = adapter.toOwlDataPropertyAssertionAxiom(ax);
final OWLDataPropertyAssertionAxiom dpAxiom = (OWLDataPropertyAssertionAxiom) axiom;
final OWLLiteral literal = dpAxiom.getObject();
assertEquals(ax.getValue().getValue(), literal.getLiteral());
assertEquals(LANG, literal.getLang());
}

@Test
public void toOwlAnnotationPropertyAxiomUsesLanguageTagSpecifiedInAssertion() {
final Axiom<String> ax = new AxiomImpl<>(NamedResource.create(INDIVIDUAL),
Assertion.createAnnotationPropertyAssertion(PROPERTY, "cs", false), new Value<>("cestina"));
final OWLAxiom axiom = adapter.toOwlAnnotationPropertyAssertionAxiom(ax);
final OWLAnnotationAssertionAxiom apAxiom = (OWLAnnotationAssertionAxiom) axiom;
final Optional<OWLLiteral> literal = apAxiom.getValue().asLiteral();
assertTrue(literal.isPresent());
assertEquals(ax.getValue().getValue(), literal.get().getLiteral());
assertEquals("cs", literal.get().getLang());
}

@Test
public void toOwlAnnotationPropertyAxiomUsesGloballyConfiguredLanguageWhenItIsNotSpecifiedInAssertion() {
final Axiom<String> ax = new AxiomImpl<>(NamedResource.create(INDIVIDUAL),
Assertion.createAnnotationPropertyAssertion(PROPERTY, false), new Value<>("english"));
final OWLAxiom axiom = adapter.toOwlAnnotationPropertyAssertionAxiom(ax);
final OWLAnnotationAssertionAxiom apAxiom = (OWLAnnotationAssertionAxiom) axiom;
final Optional<OWLLiteral> literal = apAxiom.getValue().asLiteral();
assertTrue(literal.isPresent());
assertEquals(ax.getValue().getValue(), literal.get().getLiteral());
assertEquals(LANG, literal.get().getLang());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.sesame.exceptions.SesameDriverException;
import cz.cvut.kbss.ontodriver.sesame.util.SesameUtils;
import org.eclipse.rdf4j.model.IRI;
import org.eclipse.rdf4j.model.Value;
import org.eclipse.rdf4j.model.ValueFactory;

Expand All @@ -30,37 +31,39 @@ class SesameValueConverter {
this.language = language;
}

Value toSesameValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val)
throws SesameDriverException {
Value toSesameValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) throws SesameDriverException {
switch (assertion.getType()) {
case DATA_PROPERTY:
return SesameUtils.createDataPropertyLiteral(val.getValue(), language, vf);
return SesameUtils.createDataPropertyLiteral(val.getValue(), language(assertion), vf);
case CLASS:
case OBJECT_PROPERTY:
return getValueAsSesameUri(val);
case ANNOTATION_PROPERTY: // Intentional fall-through
case PROPERTY:
return resolvePropertyValue(val);
return resolvePropertyValue(assertion, val);
default:
// Failsafe
throw new IllegalArgumentException("Unsupported assertion type " + assertion.getType());
}
}

private org.eclipse.rdf4j.model.IRI getValueAsSesameUri(cz.cvut.kbss.ontodriver.model.Value<?> val)
throws SesameDriverException {
private String language(Assertion assertion) {
return assertion.hasLanguage() ? assertion.getLanguage() : language;
}

private IRI getValueAsSesameUri(cz.cvut.kbss.ontodriver.model.Value<?> val) throws SesameDriverException {
try {
return vf.createIRI(val.getValue().toString());
} catch (IllegalArgumentException e) {
throw new SesameDriverException(e);
}
}

private org.eclipse.rdf4j.model.Value resolvePropertyValue(cz.cvut.kbss.ontodriver.model.Value<?> val) {
private Value resolvePropertyValue(Assertion assertion, cz.cvut.kbss.ontodriver.model.Value<?> val) {
if (SesameUtils.isResourceIdentifier(val.getValue())) {
return vf.createIRI(val.getValue().toString());
} else {
return SesameUtils.createDataPropertyLiteral(val.getValue(), language, vf);
return SesameUtils.createDataPropertyLiteral(val.getValue(), language(assertion), vf);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static boolean doesLanguageMatch(Literal literal, String language) {
* Creates Sesame literal from the specified value, which can be used as data property object.
*
* @param value The value to transform
* @param language Language to add to string literals
* @param language Language to add to string literals, optional
* @param vf Sesame value factory
* @return Sesame Literal
* @throws IllegalArgumentException If the type of the value is not supported
Expand All @@ -113,7 +113,7 @@ public static Literal createDataPropertyLiteral(Object value, String language, V
if (value instanceof Integer) {
return vf.createLiteral((Integer) value);
} else if (value instanceof String) {
return vf.createLiteral((String) value, language);
return language != null ? vf.createLiteral((String) value, language) : vf.createLiteral((String) value);
} else if (value instanceof Byte) {
return vf.createLiteral((Byte) value);
} else if (value instanceof Short) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,21 @@ public void enumValueIsReturnedAsStringLiteral() throws Exception {
assertEquals(Severity.MEDIUM.toString(), literal.stringValue());
assertTrue(literal.getDatatype() == null || literal.getDatatype().equals(XMLSchema.STRING));
}

@Test
public void createDataPropertyLiteralAttachesLanguageTagToStringLiteral() {
final String value = "literal";
final Literal result = SesameUtils.createDataPropertyLiteral(value, LANG, vf);
assertTrue(result.getLanguage().isPresent());
assertEquals(LANG, result.getLanguage().get());
assertEquals(value, result.stringValue());
}

@Test
public void createDataPropertyLiteralCreatesStringWithoutLanguageTagWhenNullIsPassedIn() {
final String value = "literal";
final Literal result = SesameUtils.createDataPropertyLiteral(value, null, vf);
assertFalse(result.getLanguage().isPresent());
assertEquals(value, result.stringValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,54 @@ public void convertsAnnotationPropertyNamedResourceToSesameUri() throws Exceptio
public void conversionThrowsExceptionWhenObjectPropertyValueIsNotUri() throws Exception {
converter.toSesameValue(assertion(Assertion.AssertionType.OBJECT_PROPERTY), value(117));
}

@Test
public void convertsStringLiteralIntoValueWithLanguageTagSpecifiedInAssertion() throws Exception {
final String value = "hodnota v cestine";
final Assertion dpAssertion = Assertion.createDataPropertyAssertion(PROPERTY, "cs", false);
final Value res = converter.toSesameValue(dpAssertion, value(value));
assertTrue(res instanceof Literal);
final Literal literal = (Literal) res;
assertTrue(literal.getLanguage().isPresent());
assertEquals("cs", literal.getLanguage().get());
assertEquals(value, literal.stringValue());
}

@Test
public void convertsStringLiteralIntoValueWithLanguageSpecifiedInConfigurationWhenAssertionHasNoLanguage()
throws Exception {
final String value = "hodnota v cestine";
final Assertion dpAssertion = Assertion.createDataPropertyAssertion(PROPERTY, false);
final Value res = converter.toSesameValue(dpAssertion, value(value));
assertTrue(res instanceof Literal);
final Literal literal = (Literal) res;
assertTrue(literal.getLanguage().isPresent());
assertEquals(LANG, literal.getLanguage().get());
assertEquals(value, literal.stringValue());
}

@Test
public void convertsAnnotationLiteralIntoValueWithLanguageTagSpecifiedInAssertion() throws Exception {
final String value = "hodnota v cestine";
final Assertion apAssertion = Assertion.createAnnotationPropertyAssertion(PROPERTY, "cs", false);
final Value res = converter.toSesameValue(apAssertion, value(value));
assertTrue(res instanceof Literal);
final Literal literal = (Literal) res;
assertTrue(literal.getLanguage().isPresent());
assertEquals("cs", literal.getLanguage().get());
assertEquals(value, literal.stringValue());
}

@Test
public void convertsAnnotationLiteralIntoValueWithLanguageTagSpecifiedGloballyWhenAssertionHasNoLanguage()
throws Exception {
final String value = "hodnota v cestine";
final Assertion apAssertion = Assertion.createAnnotationPropertyAssertion(PROPERTY, false);
final Value res = converter.toSesameValue(apAssertion, value(value));
assertTrue(res instanceof Literal);
final Literal literal = (Literal) res;
assertTrue(literal.getLanguage().isPresent());
assertEquals(LANG, literal.getLanguage().get());
assertEquals(value, literal.stringValue());
}
}

0 comments on commit e39c9da

Please sign in to comment.