Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Jan 13, 2022
2 parents 1de69f2 + dc50981 commit 56ff8f6
Show file tree
Hide file tree
Showing 1,211 changed files with 10,842 additions and 5,867 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
**/target
**/*.iml
**/.idea
.gradle
**/results
**/testResults
**/example02-jopa-owlapi/repository.owl
Expand Down
7 changes: 7 additions & 0 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# JOPA - Release Notes

## 0.18.0 - January 13, 2022
- Implemented full support for Java 8 date/time API. It is now the preferred way of representing temporal data (#95).
- Do not validate participation constraints on inferred attributes on persist (#98).
- Support mapping lexical form of literals with explicit datatype (#96).
- Allow retrieving entity classes mapping the specified class IRI from `Metamodel` (#99).
- Dependency updates - RDF4J 3.7.4, test dependencies, plugins.

## 0.17.2 - September 27, 2021
- Reload SPARQL-based attribute values when object is retrieved from cache to ensure up-to-date results (#93).
- Support referencing other entity attributes in SPARQL-based attribute queries (#94).
Expand Down
4 changes: 1 addition & 3 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@
- [ ] Add a `@Context` annotation, which would specify that a field/entity should always be loaded from the specified context.
- This could be overwritten with a descriptor passed to EM
- [ ] Add support for `orphanRemoval` attribute in object properties
- [ ] Modify OntoDriver API to support Fetch joins
- [ ] Allow static (annotation, attribute of `@OWLDataProperty`) specification of language of String attributes.
This can be overridden on EM operation level (in descriptor).


## Low Priority

- [ ] How to enhance query results with transactional changes? Sesame
- [ ] How to enhance query results with transactional changes? Sesame driver
- First check how SQL queries in JPA behave
- [ ] Add possibility to generate integrity constraints from the object model
- [ ] When OWL2Java generates classes and they already exist, rewrite only the fields and getters and setters, keep any other code intact
Expand All @@ -26,7 +25,6 @@
## Research Topics

- [ ] Data integrity violation checks. E.g. when an entity is removed, there should be a check whether it is referenced from another entity
- [ ] Define schema for persistence.xml for JOPA
- [ ] Research whether we could replace aspectj with cglib-generated proxies

### Currently in Progress
Expand Down
26 changes: 26 additions & 0 deletions datatype/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>jopa-all</artifactId>
<groupId>cz.cvut.kbss.jopa</groupId>
<version>0.18.0</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>datatype</artifactId>
<packaging>jar</packaging>
<name>Datatype mapping</name>
<description>Datatype mapping module.</description>

<dependencies>

<dependency>
<groupId>cz.cvut.kbss.jopa</groupId>
<artifactId>ontodriver-api</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (C) 2022 Czech Technical University in Prague
*
* 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.
*
* 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
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.datatype;


import cz.cvut.kbss.ontodriver.model.Literal;

import java.util.Optional;

/**
* Maps RDF literals to Java objects.
*/
public interface DatatypeMapper {

/**
* Maps the specified RDF literal to a suitable basic (in terms of JOPA OOM) Java type.
*
* @param literal Literal to map
* @return Mapped value wrapped in an {@code Optional}, empty Optional if no suitable mapping can be found
*/
Optional<Object> map(Literal literal);
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
/**
* Copyright (C) 2020 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
* details. You should have received a copy of the GNU General Public License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
* Copyright (C) 2022 Czech Technical University in Prague
*
* 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.
*
* 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
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.utils;
package cz.cvut.kbss.jopa.datatype;

import cz.cvut.kbss.jopa.exception.UnsupportedTypeTransformation;
import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
import cz.cvut.kbss.jopa.datatype.exception.DatatypeMappingException;
import cz.cvut.kbss.jopa.datatype.exception.UnsupportedTypeTransformationException;
import cz.cvut.kbss.ontodriver.model.LangString;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;

/**
* Utility containing some basic mappings for conversion between types.
* Utility containing transformation rules for selected basic types.
*/
public class DatatypeTransformer {

Expand Down Expand Up @@ -63,9 +67,11 @@ private DatatypeTransformer() {
try {
return ((URI) value).toURL();
} catch (MalformedURLException e) {
throw new OWLPersistenceException("Unable to transform URI to URL.", e);
throw new DatatypeMappingException("Unable to transform URI to URL.", e);
}
});
map.put(new Pair(BigInteger.class, Integer.class), value -> ((BigInteger) value).intValueExact());
map.put(new Pair(BigInteger.class, Long.class), value -> ((BigInteger) value).longValueExact());
return map;
}

Expand All @@ -76,8 +82,10 @@ private DatatypeTransformer() {
* @param targetType The type to which the specified value should be converted
* @param <T> Target type
* @return Value as the target type
* @throws UnsupportedTypeTransformationException If the specified value cannot be transformed to the specified target type
*/
public static <T> T transform(Object value, Class<T> targetType) {
Objects.requireNonNull(targetType);
if (value == null) {
return null;
}
Expand All @@ -93,19 +101,25 @@ public static <T> T transform(Object value, Class<T> targetType) {
return targetType.cast(TRANSFORMERS.get(p).apply(value));
}
final Optional<T> result = tryConversionUsingConstructor(value, targetType);
return result.orElseThrow(() -> new UnsupportedTypeTransformation(
return result.orElseThrow(() -> new UnsupportedTypeTransformationException(
String.format("Cannot transform value %s of type %s to target type %s.", value, value.getClass(),
targetType)));
}

private static <T> Optional<T> tryConversionUsingConstructor(Object value, Class<T> targetType) {
try {
final Constructor<T> ctor = targetType.getDeclaredConstructor(value.getClass());
return Optional.of(targetType.cast(ctor.newInstance(value)));
} catch (NoSuchMethodException e) {
final Constructor<?>[] ctors = targetType.getDeclaredConstructors();
for (Constructor<?> c : ctors) {
if (c.getParameterCount() != 1) {
continue;
}
if (c.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
return Optional.of(targetType.cast(c.newInstance(value)));
}
}
return Optional.empty();
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
throw new OWLPersistenceException("Unable to transform value using target type constructor.", e);
throw new DatatypeMappingException("Unable to transform value using target type constructor.", e);
}
}

Expand All @@ -124,20 +138,16 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Pair)) {
if (o == null || getClass() != o.getClass()) {
return false;
}

Pair pair = (Pair) o;

return sourceType.equals(pair.sourceType) && targetType.equals(pair.targetType);
}

@Override
public int hashCode() {
int result = sourceType.hashCode();
result = 31 * result + targetType.hashCode();
return result;
return Objects.hash(sourceType, targetType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (C) 2022 Czech Technical University in Prague
*
* 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.
*
* 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
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.datatype;

import java.time.*;
import java.util.Objects;

/**
* Utility class for transformation between various date/time representations.
*/
public class DateTimeUtil {

/**
* System timezone offset used for transforming local datetime values to offset ones
*/
public static final ZoneOffset SYSTEM_OFFSET = ZoneId.systemDefault().getRules().getOffset(LocalDateTime.now());

private DateTimeUtil() {
throw new AssertionError();
}

/**
* Transforms the specified {@link LocalDateTime} to an {@link OffsetDateTime}, using the system's default offset.
*
* @param value Datetime to transform
* @return Offset datetime
*/
public static OffsetDateTime toDateTime(LocalDateTime value) {
Objects.requireNonNull(value);
return OffsetDateTime.of(value.toLocalDate(), value.toLocalTime(), SYSTEM_OFFSET);
}

/**
* Transforms the specified {@link Instant} to an {@link OffsetDateTime}.
* <p>
* The instant is taken to be at the UTC timezone.
*
* @param value Instant value to transform
* @return Offset datetime
*/
public static OffsetDateTime toDateTime(Instant value) {
return Objects.requireNonNull(value).atOffset(ZoneOffset.UTC);
}

/**
* Transforms the specified {@link LocalTime} to an {@link OffsetTime}, using the system's default offset.
*
* @param value Time to transform
* @return Offset time
*/
public static OffsetTime toTime(LocalTime value) {
return OffsetTime.of(Objects.requireNonNull(value), SYSTEM_OFFSET);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (C) 2022 Czech Technical University in Prague
*
* 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.
*
* 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
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.datatype.exception;

/**
* Indicates that the mapping has failed.
* <p>
* For example, if the literal value is not valid (e.g., an integer literal that cannot be parsed to Java {@link Integer}).
*/
public class DatatypeMappingException extends RuntimeException {

public DatatypeMappingException(String message) {
super(message);
}

public DatatypeMappingException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2022 Czech Technical University in Prague
*
* 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.
*
* 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
* details. You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cz.cvut.kbss.jopa.datatype.exception;

/**
* Thrown when it is not possible to execute a type transformation.
*/
public class UnsupportedTypeTransformationException extends DatatypeMappingException {

public UnsupportedTypeTransformationException(String message) {
super(message);
}

public UnsupportedTypeTransformationException(String message, Throwable cause) {
super(message, cause);
}
}
Loading

0 comments on commit 56ff8f6

Please sign in to comment.