Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gh#288 rename ontodriver multilingualstring #293

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import cz.cvut.kbss.jopa.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.Translations;

/**
* Supports mapping selected value types to multilingual strings.
Expand All @@ -30,17 +31,16 @@ public class ToMultilingualStringConverter implements ConverterWrapper<Multiling

@Override
public Object convertToAxiomValue(MultilingualString value) {
return new cz.cvut.kbss.ontodriver.model.MultilingualString(value.getValue());
return new Translations(value.getValue());
}

@Override
public MultilingualString convertToAttribute(Object value) {
final Class<?> type = value.getClass();
assert supportsAxiomValueType(type);
if (value instanceof cz.cvut.kbss.ontodriver.model.MultilingualString) {
return new MultilingualString(((cz.cvut.kbss.ontodriver.model.MultilingualString) value).getValue());
} else if (value instanceof LangString) {
final LangString ls = (LangString) value;
if (value instanceof Translations translations) {
return new MultilingualString(translations.getValue());
} else if (value instanceof LangString ls) {
return MultilingualString.create(ls.getValue(), ls.getLanguage().orElse(null));
} else {
return MultilingualString.create(value.toString(), null);
Expand All @@ -49,7 +49,7 @@ public MultilingualString convertToAttribute(Object value) {

@Override
public boolean supportsAxiomValueType(Class<?> type) {
return cz.cvut.kbss.ontodriver.model.MultilingualString.class.isAssignableFrom(type)
return Translations.class.isAssignableFrom(type)
|| LangString.class.isAssignableFrom(type)
|| String.class.isAssignableFrom(type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package cz.cvut.kbss.jopa.oom.converter;

import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
Expand All @@ -45,7 +45,7 @@ void supportsReturnsTrueForSupportedAxiomValueTypes(Class<?> type) {

static Stream<Arguments> supportedTypes() {
return Stream.of(
Arguments.of(MultilingualString.class),
Arguments.of(Translations.class),
Arguments.of(LangString.class),
Arguments.of(String.class)
);
Expand All @@ -61,13 +61,13 @@ void convertToAxiomValueReturnsOntoDriverMultilingualStringWithProvidedValue() {
final cz.cvut.kbss.jopa.model.MultilingualString value = cz.cvut.kbss.jopa.model.MultilingualString.create("en", "Test");

final Object result = sut.convertToAxiomValue(value);
assertInstanceOf(MultilingualString.class, result);
assertEquals(value.getValue(), ((MultilingualString) result).getValue());
assertInstanceOf(Translations.class, result);
assertEquals(value.getValue(), ((Translations) result).getValue());
}

@Test
void convertToAttributeConvertsOntoDriverMultilingualStringToJopaMultilingualString() {
final MultilingualString value = new MultilingualString(Map.of("en", "Test"));
final Translations value = new Translations(Map.of("en", "Test"));

final cz.cvut.kbss.jopa.model.MultilingualString result = sut.convertToAttribute(value);
assertEquals(value.getValue(), result.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
* string need to be treated as a single value. Its main purpose is to allow support for multilingual strings in
* referenced lists (e.g., {@link cz.cvut.kbss.ontodriver.Lists#loadReferencedList(ReferencedListDescriptor)}).
*/
public final class MultilingualString implements Serializable {
public final class Translations implements Serializable {

private final Map<String, String> value;

public MultilingualString() {
public Translations() {
this.value = new HashMap<>();
}

public MultilingualString(Map<String, String> value) {
public Translations(Map<String, String> value) {
this.value = new HashMap<>(Objects.requireNonNull(value));
}

Expand Down Expand Up @@ -79,10 +79,9 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof MultilingualString)) {
if (!(o instanceof Translations that)) {
return false;
}
MultilingualString that = (MultilingualString) o;
return value.equals(that.value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import cz.cvut.kbss.ontodriver.jena.util.JenaUtils;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.Value;
import org.apache.jena.rdf.model.RDFNode;

Expand All @@ -33,7 +33,7 @@ private ReferencedListHelper() {
}

static Stream<RDFNode> toRdfNodes(Object value, Assertion nodeContentAssertion) {
if (value instanceof MultilingualString mls) {
if (value instanceof Translations mls) {
return mls.getValue().entrySet().stream()
.map(e -> JenaUtils.valueToRdfNode(nodeContentAssertion, new Value<>(new LangString(e.getValue(), e.getKey()))));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.AxiomImpl;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import org.apache.jena.rdf.model.Literal;
Expand Down Expand Up @@ -79,7 +79,7 @@ T nextValue() {
return (T) (value.isResource() ? NamedResource.create(value.asResource()
.getURI()) : JenaUtils.literalToValue(value.asLiteral()));
} else {
final MultilingualString mls = new MultilingualString();
final Translations mls = new Translations();
content.forEach(n -> {
assert n.isLiteral();
final Literal lit = n.asLiteral();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
import cz.cvut.kbss.ontodriver.jena.environment.Generator;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
Expand Down Expand Up @@ -118,10 +118,10 @@ void initReferencedListStatements(List<?> items) {
final List<Statement> contentStatements;
if (content instanceof URI) {
contentStatements = List.of(createStatement(node, hasContent, createResource(content.toString())));
} else if (content instanceof MultilingualString) {
contentStatements = ((MultilingualString) content).getValue().entrySet().stream()
.map(e -> createStatement(node, hasContent, createLangLiteral(e.getValue(), e.getKey())))
.collect(Collectors.toList());
} else if (content instanceof Translations) {
contentStatements = ((Translations) content).getValue().entrySet().stream()
.map(e -> createStatement(node, hasContent, createLangLiteral(e.getValue(), e.getKey())))
.collect(Collectors.toList());
} else {
contentStatements = List.of(createStatement(node, hasContent, createTypedLiteral(content)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
Expand Down Expand Up @@ -330,12 +330,12 @@ public void loadListRetrievesAllDataPropertyListElements() {

@Test
void persistListSavesMultilingualStringTranslationsAsContentOfSingleNode() {
final ReferencedListValueDescriptor<MultilingualString> desc = new ReferencedListValueDescriptor<>(OWNER,
HAS_LIST,
HAS_NEXT, Assertion.createDataPropertyAssertion(HAS_CONTENT.getIdentifier(), false));
final List<MultilingualString> refList = List.of(
new MultilingualString(Map.of("en", "one", "cs", "jedna")),
new MultilingualString(Map.of("en", "two", "cs", "dva"))
final ReferencedListValueDescriptor<Translations> desc = new ReferencedListValueDescriptor<>(OWNER,
HAS_LIST,
HAS_NEXT, Assertion.createDataPropertyAssertion(HAS_CONTENT.getIdentifier(), false));
final List<Translations> refList = List.of(
new Translations(Map.of("en", "one", "cs", "jedna")),
new Translations(Map.of("en", "two", "cs", "dva"))
);
refList.forEach(desc::addValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cz.cvut.kbss.ontodriver.jena.exception.ListProcessingException;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
Expand Down Expand Up @@ -221,11 +221,11 @@ void nextAxiomReturnsAxiomWithMultilingualStringValueWhenNodeContentIsMultilingu
createStatement(node, HAS_CONTENT, createLangLiteral("jedna", "cs"))
);
when(connectorMock.find(node, HAS_CONTENT, null, Collections.emptySet())).thenReturn(content);
final ReferencedListIterator<MultilingualString> sut = new ReferencedListIterator<>(descriptor(Assertion.AssertionType.DATA_PROPERTY, null), connectorMock);
final ReferencedListIterator<Translations> sut = new ReferencedListIterator<>(descriptor(Assertion.AssertionType.DATA_PROPERTY, null), connectorMock);

final Axiom<MultilingualString> result = sut.nextAxiom();
final Axiom<Translations> result = sut.nextAxiom();
assertNotNull(result);
assertEquals(new MultilingualString(Map.of("en", "one", "cs", "jedna")), result.getValue().getValue());
assertEquals(new Translations(Map.of("en", "one", "cs", "jedna")), result.getValue().getValue());
}

@Test
Expand All @@ -238,9 +238,9 @@ void replaceReplacesAllTranslationsOfMultilingualStringInCurrentNode() {
createStatement(node, HAS_CONTENT, createLangLiteral("jedna", "cs"))
);
when(connectorMock.find(node, HAS_CONTENT, null, Collections.emptySet())).thenReturn(content);
final ReferencedListIterator<MultilingualString> sut = new ReferencedListIterator<>(descriptor(Assertion.AssertionType.DATA_PROPERTY, null), connectorMock);
final ReferencedListIterator<Translations> sut = new ReferencedListIterator<>(descriptor(Assertion.AssertionType.DATA_PROPERTY, null), connectorMock);

final MultilingualString newContent = new MultilingualString(Map.of(
final Translations newContent = new Translations(Map.of(
"en", "New",
"cs", "Nový"
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import cz.cvut.kbss.ontodriver.exception.IntegrityConstraintViolatedException;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
import cz.cvut.kbss.ontodriver.owlapi.change.MutableAddAxiom;
Expand Down Expand Up @@ -158,25 +158,25 @@ protected T extractNodeContent() {
return (T) OwlapiUtils.owlLiteralToValue(literal);
}
} else {
final MultilingualString mls = new MultilingualString();
final Translations translations = new Translations();
nextItem.forEach(n -> {
assert n instanceof OWLLiteral;
final OWLLiteral lit = (OWLLiteral) n;
assert lit.getLang() != null;
mls.set(lit.getLang(), lit.getLiteral());
translations.set(lit.getLang(), lit.getLiteral());
});
return (T) mls;
return (T) translations;
}
}

private void verifyContentValueCount(Collection<? extends OWLObject> values) {
if (values.isEmpty()) {
throw icViolatedException(currentNode.toStringID(), 0);
}
final Set<String> langs = new HashSet<>();
if (values.size() == 1) {
return;
}
final Set<String> langs = new HashSet<>();
for (OWLObject s : values) {
if (s.isIndividual()) {
throw icViolatedException(currentNode.toStringID(), values.size());
Expand All @@ -202,15 +202,14 @@ public NamedResource getCurrentNode() {

@Override
List<TransactionalChange> removeWithoutReconnect() {
final List<TransactionalChange> changes = new ArrayList<>(2);
changes.add(new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(previousNextNodeProperty, previousNode, currentNode)));
final MutableRemoveAxiom removeFromPrevious = new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(previousNextNodeProperty, previousNode, currentNode));
final OWLIndividual nextNode = getNextNode();
if (nextNode != null) {
changes.add(new MutableRemoveAxiom(ontology,
return List.of(removeFromPrevious, new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(currentNextNodeProperty, currentNode, nextNode)));
}
return changes;
return List.of(removeFromPrevious);
}

private OWLIndividual getNextNode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.AxiomImpl;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.model.Value;
import cz.cvut.kbss.ontodriver.owlapi.AxiomAdapter;
Expand Down Expand Up @@ -80,8 +80,8 @@ <V> Collection<TransactionalChange> generateNodeContent(NamedResource node, V va
return List.of(new MutableAddAxiom(ontology, valueAxiom));
} else {
assert descriptor.getNodeContent().getType() == Assertion.AssertionType.DATA_PROPERTY;
if (value instanceof MultilingualString) {
final MultilingualString mls = (MultilingualString) value;
if (value instanceof Translations) {
final Translations mls = (Translations) value;
return mls.getValue().entrySet().stream().map(e -> {
final String lang = e.getKey();
final String val = e.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ NamedResource getCurrentNode() {

@Override
List<TransactionalChange> removeWithoutReconnect() {
final List<TransactionalChange> changes = new ArrayList<>(2);
changes.add(new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(previousProperty, previousNode, currentNode)));
final TransactionalChange removeFromPrevious = new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(previousProperty, previousNode, currentNode));
final OWLIndividual nextNode = getNextNode();
if (nextNode != null) {
changes.add(new MutableRemoveAxiom(ontology,
return List.of(removeFromPrevious, new MutableRemoveAxiom(ontology,
dataFactory.getOWLObjectPropertyAssertionAxiom(currentProperty, currentNode, nextNode)));
}
return changes;
return List.of(removeFromPrevious);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.Axiom;
import cz.cvut.kbss.ontodriver.model.LangString;
import cz.cvut.kbss.ontodriver.model.MultilingualString;
import cz.cvut.kbss.ontodriver.model.Translations;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import cz.cvut.kbss.ontodriver.owlapi.OwlapiAdapter;
import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
Expand Down Expand Up @@ -382,11 +382,11 @@ public void loadListLoadsListWithDataPropertyValues() {

@Test
void persistListSavesMultilingualStringTranslationsAsContentOfSingleNode() {
final List<MultilingualString> refList = List.of(
new MultilingualString(Map.of("en", "one", "cs", "jedna")),
new MultilingualString(Map.of("en", "two", "cs", "dva"))
final List<Translations> refList = List.of(
new Translations(Map.of("en", "one", "cs", "jedna")),
new Translations(Map.of("en", "two", "cs", "dva"))
);
final ReferencedListValueDescriptor<MultilingualString> valueDescriptor = new ReferencedListValueDescriptor<>(ListTestHelper.SUBJECT, ListTestHelper.HAS_LIST, ListTestHelper.HAS_NEXT, Assertion.createDataPropertyAssertion(URI.create(ListTestHelper.HAS_CONTENT_PROPERTY), false));
final ReferencedListValueDescriptor<Translations> valueDescriptor = new ReferencedListValueDescriptor<>(ListTestHelper.SUBJECT, ListTestHelper.HAS_LIST, ListTestHelper.HAS_NEXT, Assertion.createDataPropertyAssertion(URI.create(ListTestHelper.HAS_CONTENT_PROPERTY), false));
refList.forEach(valueDescriptor::addValue);

sut.persistList(valueDescriptor);
Expand Down
Loading
Loading