Skip to content

Commit

Permalink
Language tag specification support - pass language tag to axiom value…
Browse files Browse the repository at this point in the history
… descriptor on JOPA level.
  • Loading branch information
ledsoft committed Jun 13, 2017
1 parent 7f149c7 commit 04fd649
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 25 deletions.
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 @@ -16,12 +16,12 @@

import cz.cvut.kbss.jopa.exceptions.OWLEntityExistsException;
import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.Connection;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
import cz.cvut.kbss.ontodriver.exception.OWLIndividualExistsException;
import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
Expand All @@ -31,8 +31,6 @@

/**
* Gathers axiom values in descriptor for persist or update.
*
* @author ledvima1
*/
class AxiomValueGatherer {

Expand All @@ -52,7 +50,7 @@ class AxiomValueGatherer {
}

void addValue(Assertion assertion, Value<?> value, URI context) {
addValues(assertion, Collections.<Value<?>>singleton(value), context);
addValues(assertion, Collections.singleton(value), context);
}

void addValues(Assertion assertion, Collection<Value<?>> values, URI context) {
Expand Down
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 Down Expand Up @@ -42,6 +42,6 @@ Object resolveEnumValue(Object value) {

@Override
Assertion createAssertion() {
return Assertion.createDataPropertyAssertion(attribute.getIRI().toURI(), attribute.isInferred());
return Assertion.createDataPropertyAssertion(attribute.getIRI().toURI(), getLanguage(), attribute.isInferred());
}
}
15 changes: 13 additions & 2 deletions jopa-impl/src/main/java/cz/cvut/kbss/jopa/oom/FieldStrategy.java
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 @@ -14,6 +14,7 @@
*/
package cz.cvut.kbss.jopa.oom;

import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.metamodel.*;
import cz.cvut.kbss.jopa.utils.EntityPropertiesUtils;
Expand Down Expand Up @@ -142,6 +143,16 @@ URI getAttributeContext() {
return attributeDescriptor.getContext();
}

/**
* Gets the language tag that should be used when mapping this field to an assertion.
*
* @return Language tag, possibly {@code null}
*/
String getLanguage() {
return attributeDescriptor.hasLanguage() ? attributeDescriptor.getLanguage() :
mapper.getConfiguration().get(JOPAPersistenceProperties.LANG);
}

/**
* Adds value from the specified axioms to this strategy. </p>
* <p>
Expand Down
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 Down Expand Up @@ -71,6 +71,7 @@ void buildAxiomValuesFromInstance(X instance, AxiomValueGatherer valueBuilder) t

@Override
Assertion createAssertion() {
return Assertion.createAnnotationPropertyAssertion(attribute.getIRI().toURI(), attribute.isInferred());
return Assertion
.createAnnotationPropertyAssertion(attribute.getIRI().toURI(), getLanguage(), attribute.isInferred());
}
}
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 @@ -23,7 +23,7 @@
import cz.cvut.kbss.jopa.model.annotations.OWLObjectProperty;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.metamodel.EntityTypeImpl;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Assertion.AssertionType;
Expand Down Expand Up @@ -96,8 +96,8 @@ public static void setUpBeforeClass() throws Exception {
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
this.mocks = new MetamodelMocks();
when(oomMock.getEntityType(OWLClassA.class))
.thenReturn(mocks.forOwlClassA().entityType());
when(oomMock.getEntityType(OWLClassA.class)).thenReturn(mocks.forOwlClassA().entityType());
when(oomMock.getConfiguration()).thenReturn(new Configuration(Collections.emptyMap()));
entityA.setTypes(null);
entityA.setStringAttribute("someStringAttribute");
entityB.setProperties(null);
Expand Down Expand Up @@ -314,8 +314,7 @@ private Map<String, Set<String>> createProperties() {
public void testMapEntityWithObjectPropertyWithGeneratedIdentifier() throws Exception {
final URI eUri = URI.create("http://eUri");
when(oomMock.generateIdentifier(mocks.forOwlClassE().entityType())).thenReturn(eUri);
when(oomMock.getEntityType(OWLClassE.class))
.thenReturn((EntityTypeImpl<OWLClassE>) mocks.forOwlClassE().entityType());
when(oomMock.getEntityType(OWLClassE.class)).thenReturn(mocks.forOwlClassE().entityType());
final Descriptor eDescriptor = new EntityDescriptor(CONTEXT);
final Descriptor kDescriptor = new EntityDescriptor();
kDescriptor.addAttributeDescriptor(OWLClassK.getOwlClassEField(), eDescriptor);
Expand Down
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 @@ -18,8 +18,10 @@
import cz.cvut.kbss.jopa.environment.Vocabulary;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.Before;
Expand Down Expand Up @@ -50,6 +52,9 @@ public class PluralDataPropertyStrategyTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final Configuration configuration = new Configuration(
Collections.singletonMap(JOPAPersistenceProperties.LANG, "en"));
when(mapperMock.getConfiguration()).thenReturn(configuration);

this.gatherer = new AxiomValueGatherer(INDIVIDUAL, null);
this.mocks = new MetamodelMocks();
Expand Down Expand Up @@ -168,4 +173,31 @@ public void buildAxiomValuesFromInstanceAddsNullValueWhenAttributeValueIsEmptyCo
assertEquals(1, values.size());
assertTrue(values.contains(Value.nullValue()));
}

@Test
public void buildAxiomsSetsLanguageTagAccordingToDescriptorLanguage() throws Exception {
descriptor.setLanguage("en");
buildAxiomsAndVerifyLanguageTag();
}

private void buildAxiomsAndVerifyLanguageTag() throws Exception {
// Yes, the plural attribute contains integers, but it is not important on this level
final PluralDataPropertyStrategy<OWLClassM> strategy = createStrategyForM();
final OWLClassM m = new OWLClassM();
m.setIntegerSet(Collections.singleton(117));

final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null);
strategy.buildAxiomValuesFromInstance(m, builder);
final AxiomValueDescriptor valueDescriptor = OOMTestUtils.getAxiomValueDescriptor(builder);
assertEquals(1, valueDescriptor.getAssertions().size());
final Assertion assertion = valueDescriptor.getAssertions().iterator().next();
assertTrue(assertion.hasLanguage());
assertEquals("en", assertion.getLanguage());
}

@Test
public void buildAxiomsSetsLanguageTagAccordingToPUConfigurationWhenItIsNotSpecifiedInDescriptor()
throws Exception {
buildAxiomsAndVerifyLanguageTag();
}
}
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 @@ -18,9 +18,11 @@
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.exceptions.IntegrityConstraintViolatedException;
import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.model.metamodel.SingularAttribute;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.model.*;
import org.junit.Before;
Expand All @@ -30,9 +32,11 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;

public class SingularAnnotationPropertyStrategyTest {

Expand All @@ -48,6 +52,9 @@ public class SingularAnnotationPropertyStrategyTest {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final Configuration configuration = new Configuration(
Collections.singletonMap(JOPAPersistenceProperties.LANG, "en"));
when(mapperMock.getConfiguration()).thenReturn(configuration);
this.mocks = new MetamodelMocks();
}

Expand Down Expand Up @@ -163,4 +170,31 @@ public void buildAxiomsUsesNullValueWhenExtractedFieldValueIsNull() throws Excep
final Value<?> val = getValueForAssertion(builder, annotationWithUriForN());
assertSame(Value.nullValue(), val);
}

@Test
public void buildAxiomsSetsLanguageTagAccordingToDescriptorLanguage() throws Exception {
descriptor.setLanguage("en");
buildAxiomsAndVerifyLanguageTag();
}

private void buildAxiomsAndVerifyLanguageTag() throws Exception {
final SingularAnnotationPropertyStrategy<OWLClassN> strategy = forN(mocks.forOwlClassN().annotationAttribute());
final OWLClassN n = new OWLClassN();
n.setId(PK.toString());
n.setAnnotationProperty("english");

final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null);
strategy.buildAxiomValuesFromInstance(n, builder);
final AxiomValueDescriptor valueDescriptor = OOMTestUtils.getAxiomValueDescriptor(builder);
assertEquals(1, valueDescriptor.getAssertions().size());
final Assertion assertion = valueDescriptor.getAssertions().iterator().next();
assertTrue(assertion.hasLanguage());
assertEquals("en", assertion.getLanguage());
}

@Test
public void buildAxiomsSetsLanguageTagAccordingToPUConfigurationWhenItIsNotSpecifiedInDescriptor()
throws Exception {
buildAxiomsAndVerifyLanguageTag();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cz.cvut.kbss.jopa.oom;

import cz.cvut.kbss.jopa.environment.OWLClassA;
import cz.cvut.kbss.jopa.environment.utils.Generators;
import cz.cvut.kbss.jopa.environment.utils.MetamodelMocks;
import cz.cvut.kbss.jopa.model.JOPAPersistenceProperties;
import cz.cvut.kbss.jopa.model.descriptors.Descriptor;
import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor;
import cz.cvut.kbss.jopa.utils.Configuration;
import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.net.URI;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

public class SingularDataPropertyStrategyTest {

private static final URI PK = Generators.createIndividualIdentifier();

@Mock
private EntityMappingHelper mapperMock;

private MetamodelMocks mocks;

private Descriptor descriptor = new EntityDescriptor();

@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
final Configuration configuration = new Configuration(
Collections.singletonMap(JOPAPersistenceProperties.LANG, "en"));
when(mapperMock.getConfiguration()).thenReturn(configuration);
this.mocks = new MetamodelMocks();
}

@Test
public void buildAxiomsSetsLanguageTagAccordingToDescriptorLanguage() throws Exception {
descriptor.setLanguage("en");
buildAxiomsAndVerifyLanguageTag();
}

private void buildAxiomsAndVerifyLanguageTag() throws Exception {
final SingularDataPropertyStrategy<OWLClassA> strategy = new SingularDataPropertyStrategy<>(
mocks.forOwlClassA().entityType(), mocks.forOwlClassA().stringAttribute(), descriptor, mapperMock);
final OWLClassA a = new OWLClassA();
a.setUri(PK);
a.setStringAttribute("english");

final AxiomValueGatherer builder = new AxiomValueGatherer(NamedResource.create(PK), null);
strategy.buildAxiomValuesFromInstance(a, builder);
final AxiomValueDescriptor valueDescriptor = OOMTestUtils.getAxiomValueDescriptor(builder);
assertEquals(1, valueDescriptor.getAssertions().size());
final Assertion assertion = valueDescriptor.getAssertions().iterator().next();
assertTrue(assertion.hasLanguage());
assertEquals("en", assertion.getLanguage());
}

@Test
public void buildAxiomsSetsLanguageTagAccordingToPUConfigurationWhenItIsNotSpecifiedInDescriptor()
throws Exception {
buildAxiomsAndVerifyLanguageTag();
}
}

0 comments on commit 04fd649

Please sign in to comment.