Skip to content

Commit

Permalink
Language specification support per assertion - OWLAPI driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jun 12, 2017
1 parent dd4b7f9 commit eef32fb
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static Assertion createClassAssertion(boolean isInferred) {
/**
* Creates a property assertion without specifying the assertion identifier.
* <p>
* Note that the returned instances are all equals as long as their inferred status is the same.
* Note that the returned instances have the same identifier throughout one JVM - a randomly generated URI.
*
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
Expand All @@ -154,7 +154,7 @@ public static Assertion createUnspecifiedPropertyAssertion(boolean isInferred) {
/**
* Creates a property assertion without specifying the assertion identifier.
* <p>
* Note that the returned instances are all equal as long as their inferred status is the same.
* Note that the returned instances have the same identifier throughout one JVM - a randomly generated URI.
*
* @param language Language tag, optional
* @param isInferred Whether the assertion uses inferred values
Expand All @@ -179,7 +179,8 @@ public static Assertion createPropertyAssertion(URI assertionIdentifier, boolean
* Creates new property assertion without specifying what kind of property it is.
*
* @param assertionIdentifier Assertion identifier
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is supported
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is
* supported
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
Expand Down Expand Up @@ -213,7 +214,8 @@ public static Assertion createDataPropertyAssertion(URI assertionIdentifier, boo
* Creates new data property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is supported
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is
* supported
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
Expand All @@ -236,7 +238,8 @@ public static Assertion createAnnotationPropertyAssertion(URI assertionIdentifie
* Creates new annotation property assertion.
*
* @param assertionIdentifier Assertion identifier
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is supported
* @param language Language tag. Passing {@code null} explicitly specifies that any language tag is
* supported
* @param isInferred Whether the assertion uses inferred values
* @return Assertion
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import org.semanticweb.owlapi.model.*;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;

class ExplicitAxiomLoader implements AxiomLoader {
Expand All @@ -38,8 +36,7 @@ class ExplicitAxiomLoader implements AxiomLoader {
private final AxiomAdapter axiomAdapter;
private final String language;

private Set<Assertion> assertions;
private Set<URI> assertionUris;
private Map<URI, Assertion> assertionMap;

ExplicitAxiomLoader(OwlapiAdapter adapter, OntologySnapshot snapshot) {
this.adapter = adapter;
Expand All @@ -51,8 +48,8 @@ class ExplicitAxiomLoader implements AxiomLoader {

@Override
public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> assertions) {
this.assertions = assertions;
this.assertionUris = assertions.stream().map(NamedResource::getIdentifier).collect(Collectors.toSet());
this.assertionMap = new HashMap<>(assertions.size());
assertions.forEach(a -> assertionMap.put(a.getIdentifier(), a));
final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
final Collection<Axiom<?>> axioms = new ArrayList<>();
if (assertions.contains(Assertion.createClassAssertion(false))) {
Expand All @@ -73,16 +70,28 @@ public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> ass

private Collection<Axiom<?>> dataPropertyValuesToAxioms(NamedResource subject,
Collection<OWLDataPropertyAssertionAxiom> axioms) {
return axioms.stream().filter(axiom -> {
final OWLLiteral value = axiom.getObject();
final boolean propertyExists = doesPropertyExist(axiom.getProperty().asOWLDataProperty().getIRI());
return propertyExists && OwlapiUtils.doesLanguageMatch(value, language);
}).map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
return axioms.stream().filter(this::shouldLoadDataPropertyValue)
.map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
.collect(Collectors.toList());
}

private boolean shouldLoadDataPropertyValue(OWLDataPropertyAssertionAxiom axiom) {
final OWLLiteral value = axiom.getObject();
final IRI dpIri = axiom.getProperty().asOWLDataProperty().getIRI();
final boolean propertyExists = doesPropertyExist(dpIri);
if (!propertyExists) {
return false;
}
final URI dpUri = dpIri.toURI();
// Note: I don't really like the fact that we are basing this on a randomly generated identifier of the unspecified
// property. Perhaps the strategy of using unspecified properties should be revisited.
final Assertion assertion = assertionMap.containsKey(dpUri) ? assertionMap.get(dpUri) :
assertionMap.get(UNSPECIFIED_ASSERTION.getIdentifier());
return OwlapiUtils.doesLanguageMatch(value, assertion.hasLanguage() ? assertion.getLanguage() : language);
}

private boolean doesPropertyExist(IRI o) {
return assertions.contains(UNSPECIFIED_ASSERTION) || assertionUris.contains(o.toURI());
return assertionMap.containsKey(o.toURI()) || assertionMap.containsKey(UNSPECIFIED_ASSERTION.getIdentifier());
}

private Collection<Axiom<?>> objectPropertyValuesToAxioms(NamedResource subject,
Expand All @@ -95,15 +104,25 @@ private Collection<Axiom<?>> objectPropertyValuesToAxioms(NamedResource subject,

private Collection<Axiom<?>> annotationPropertyValuesToAxioms(NamedResource subject,
Collection<OWLAnnotationAssertionAxiom> axioms) {
return axioms.stream().filter(axiom -> {
final OWLAnnotationValue value = axiom.getValue();
final boolean propertyExists = doesPropertyExist(axiom.getProperty().asOWLAnnotationProperty().getIRI());
return propertyExists && (!value.asLiteral().isPresent() ||
OwlapiUtils.doesLanguageMatch(value.asLiteral().get(), language));
}).map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
return axioms.stream().filter(this::shouldLoadAnnotationPropertyValue)
.map(axiom -> axiomAdapter.toAxiom(subject, axiom, false))
.collect(Collectors.toList());
}

private boolean shouldLoadAnnotationPropertyValue(OWLAnnotationAssertionAxiom axiom) {
final OWLAnnotationValue value = axiom.getValue();
final IRI apIri = axiom.getProperty().asOWLAnnotationProperty().getIRI();
final boolean propertyExists = doesPropertyExist(apIri);
if (!propertyExists) {
return false;
}
final URI apUri = apIri.toURI();
final Assertion assertion = assertionMap.containsKey(apUri) ? assertionMap.get(apUri) :
assertionMap.get(UNSPECIFIED_ASSERTION.getIdentifier());
return !value.asLiteral().isPresent() || OwlapiUtils.doesLanguageMatch(value.asLiteral().get(),
assertion.hasLanguage() ? assertion.getLanguage() : language);
}

@Override
public Collection<Axiom<?>> loadPropertyAxioms(NamedResource subject) {
final OWLNamedIndividual individual = OwlapiUtils.getIndividual(subject, dataFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public Collection<Axiom<?>> loadAxioms(NamedResource subject, Set<Assertion> ass

private Collection<Axiom<?>> inferDataPropertyValues(OWLNamedIndividual individual, Assertion dpAssertion) {
final Set<OWLLiteral> literals = reasoner.getDataPropertyValues(individual, dataProperty(dpAssertion));
return literals.stream().filter(lit -> OwlapiUtils.doesLanguageMatch(lit, adapter.getLanguage()))
final String lang = dpAssertion.hasLanguage() ? dpAssertion.getLanguage() : adapter.getLanguage();
return literals.stream().filter(lit -> OwlapiUtils.doesLanguageMatch(lit, lang))
.map(owlLiteral -> new AxiomImpl<>(subject, dpAssertion,
new Value<>(OwlapiUtils.owlLiteralToValue(owlLiteral)))).collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
Expand All @@ -43,6 +44,7 @@
public class MainAxiomLoaderTest {

private static final NamedResource SUBJECT = NamedResource.create("http://krizik.felk.cvut.cz/Individual");
private static final String RDFS_LABEL = "http://www.w3.org/2000/01/rdf-schema#label";
private static final String LANG = "en";

@Mock
Expand Down Expand Up @@ -284,19 +286,22 @@ public void skipsExplicitAssertionValueIfThereIsTheSameAssertionAlsoWithInferenc

@Test
public void loadsStringLiteralValueForExplicitAnnotationPropertyWithCorrectLanguageTag() throws Exception {
initExplicitAnnotationPropertyStringsWithLanguageTag();

final Collection<Axiom<?>> result = axiomLoader
.findAxioms(descriptor(Assertion.createAnnotationPropertyAssertion(URI.create(RDFS_LABEL), false)));
checkLoadedAxiomsForStringValue(result, "a");
}

private void initExplicitAnnotationPropertyStringsWithLanguageTag() {
final Set<OWLAnnotationAssertionAxiom> axioms = new HashSet<>();
final String propertyUri = "http://www.w3.org/2000/01/rdf-schema#label";
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(SUBJECT.getIdentifier()));
final OWLAnnotationProperty property = dataFactory.getOWLAnnotationProperty(IRI.create(propertyUri));
final OWLAnnotationProperty property = dataFactory.getOWLAnnotationProperty(IRI.create(RDFS_LABEL));
axioms.add(dataFactory.getOWLAnnotationAssertionAxiom(property, individual.getIRI(),
dataFactory.getOWLLiteral("a", LANG)));
axioms.add(dataFactory.getOWLAnnotationAssertionAxiom(property, individual.getIRI(),
dataFactory.getOWLLiteral("b", "cs")));
when(ontologyMock.getAnnotationAssertionAxioms(individual.getIRI())).thenReturn(axioms);

final Collection<Axiom<?>> result = axiomLoader
.findAxioms(descriptor(Assertion.createAnnotationPropertyAssertion(URI.create(propertyUri), false)));
checkLoadedAxiomsForStringValue(result, "a");
}

private void checkLoadedAxiomsForStringValue(Collection<Axiom<?>> result, String expected) {
Expand All @@ -307,32 +312,97 @@ private void checkLoadedAxiomsForStringValue(Collection<Axiom<?>> result, String

@Test
public void loadsStringLiteralValueForExplicitDataPropertyWithCorrectLanguageTag() throws Exception {
final Set<OWLDataPropertyAssertionAxiom> axioms = new HashSet<>();
final String propertyUri = "http://krizik.felk.cvut.cz/dataPropertyOne";
initExplicitDataPropertyStringsWithLanguageTag(propertyUri);

final Collection<Axiom<?>> result = axiomLoader
.findAxioms(descriptor(Assertion.createDataPropertyAssertion(URI.create(propertyUri), false)));
checkLoadedAxiomsForStringValue(result, "a");
}

private void initExplicitDataPropertyStringsWithLanguageTag(String propertyUri) {
final Set<OWLDataPropertyAssertionAxiom> axioms = new HashSet<>();
final OWLNamedIndividual individual = dataFactory.getOWLNamedIndividual(IRI.create(SUBJECT.getIdentifier()));
final OWLDataProperty property = dataFactory.getOWLDataProperty(IRI.create(propertyUri));
axioms.add(dataFactory
.getOWLDataPropertyAssertionAxiom(property, individual, dataFactory.getOWLLiteral("a", LANG)));
axioms.add(dataFactory
.getOWLDataPropertyAssertionAxiom(property, individual, dataFactory.getOWLLiteral("b", "cs")));
when(ontologyMock.getDataPropertyAssertionAxioms(individual)).thenReturn(axioms);
}

@Test
public void loadsStringLiteralValueForInferredDataPropertyWithCorrectLanguageTag() throws Exception {
final OWLDataProperty dp = dataFactory.getOWLDataProperty(IRI.create(RDFS_LABEL));
final Set<OWLLiteral> values = new HashSet<>();
values.add(dataFactory.getOWLLiteral("a", LANG));
values.add(dataFactory.getOWLLiteral("b", "cs"));
when(reasonerMock.getDataPropertyValues(individual, dp)).thenReturn(values);

final Collection<Axiom<?>> result = axiomLoader
.findAxioms(descriptor(Assertion.createDataPropertyAssertion(URI.create(propertyUri), false)));
.findAxioms(descriptor(Assertion.createDataPropertyAssertion(URI.create(RDFS_LABEL), true)));
checkLoadedAxiomsForStringValue(result, "a");
}

@Test
public void loadsStringLiteralValueForInferredDataPropertyWithCorrectLanguageTag() throws Exception {
final String propertyUri = "http://www.w3.org/2000/01/rdf-schema#label";
public void loadsStringLiteralWithCorrectLanguageTagWhenItIsSpecifiedInExplicitDataPropertyAssertion()
throws Exception {
final String propertyUri = "http://krizik.felk.cvut.cz/dataPropertyOne";
initExplicitDataPropertyStringsWithLanguageTag(propertyUri);
final Assertion dpa = Assertion.createDataPropertyAssertion(URI.create(propertyUri), "cs", false);
final Collection<Axiom<?>> result = axiomLoader.findAxioms(descriptor(dpa));
checkLoadedAxiomsForStringValue(result, "b");
}

@Test
public void loadsStringLiteralWithCorrectLanguageTagWhenItIsSpecifiedInExplicitAnnotationPropertyAssertion()
throws Exception {
initExplicitAnnotationPropertyStringsWithLanguageTag();
final Assertion apa = Assertion.createAnnotationPropertyAssertion(URI.create(RDFS_LABEL), "cs", false);
final Collection<Axiom<?>> result = axiomLoader.findAxioms(descriptor(apa));
checkLoadedAxiomsForStringValue(result, "b");
}

@Test
public void loadsStringLiteralWithAllLanguagesWhenLanguageTagIsExplicitlySetToNull() throws Exception {
initExplicitAnnotationPropertyStringsWithLanguageTag();
final Assertion apa = Assertion.createAnnotationPropertyAssertion(URI.create(RDFS_LABEL), null, false);
final Collection<Axiom<?>> result = axiomLoader.findAxioms(descriptor(apa));
assertEquals(2, result.size());
final Set<String> values = result.stream().map(ax -> ax.getValue().stringValue()).collect(Collectors.toSet());
assertTrue(values.contains("a"));
assertTrue(values.contains("b"));
}

@Test
public void loadsStringLiteralWithCorrectLanguageTagWhenSpecifiedOnUnspecifiedDataProperty() throws Exception {
final String propertyUri = "http://krizik.felk.cvut.cz/dataPropertyOne";
initExplicitDataPropertyStringsWithLanguageTag(propertyUri);
final Assertion assertion = Assertion.createUnspecifiedPropertyAssertion("cs", false);
final Collection<Axiom<?>> result = axiomLoader.findAxioms(descriptor(assertion));
checkLoadedAxiomsForStringValue(result, "b");
}

@Test
public void loadsStringLiteralWithCorrectLanguageTagWhenSpecifiedOnUnspecifiedAnnotationProperty()
throws Exception {
initExplicitAnnotationPropertyStringsWithLanguageTag();
final Assertion assertion = Assertion.createUnspecifiedPropertyAssertion("cs", false);
final Collection<Axiom<?>> result = axiomLoader.findAxioms(descriptor(assertion));
checkLoadedAxiomsForStringValue(result, "b");
}

@Test
public void loadsStringLiteralWithCorrectLanguageTagSpecifiedOnInferredDataProperty() throws Exception {
final String propertyUri = "http://krizik.felk.cvut.cz/dataPropertyOne";
final OWLDataProperty dp = dataFactory.getOWLDataProperty(IRI.create(propertyUri));
final Set<OWLLiteral> values = new HashSet<>();
values.add(dataFactory.getOWLLiteral("a", LANG));
values.add(dataFactory.getOWLLiteral("b", "cs"));
when(reasonerMock.getDataPropertyValues(individual, dp)).thenReturn(values);
final Assertion assertion = Assertion.createDataPropertyAssertion(URI.create(propertyUri), "cs", true);

final Collection<Axiom<?>> result = axiomLoader
.findAxioms(descriptor(Assertion.createDataPropertyAssertion(URI.create(propertyUri), true)));
checkLoadedAxiomsForStringValue(result, "a");
final Collection<Axiom<?>> result = axiomLoader.findAxioms(descriptor(assertion));
checkLoadedAxiomsForStringValue(result, "b");
}
}

0 comments on commit eef32fb

Please sign in to comment.