Skip to content

Commit

Permalink
Merge pull request #1161 from Informatievlaanderen/feat/default-bucke…
Browse files Browse the repository at this point in the history
…t-geospatial

feat: add defaultRelation
  • Loading branch information
pj-cegeka authored Feb 14, 2024
2 parents 2245caa + 1f4ca1c commit 76106e9
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations;

import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION;

public interface RelationsAttributer {
default TreeRelation getDefaultRelation(Fragment childFragment) {
return new TreeRelation("", childFragment.getFragmentId(), "", "", GENERIC_TREE_RELATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Set;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE_ROOT;

public class GeospatialFragmentationStrategy extends FragmentationStrategyDecorator {
Expand Down Expand Up @@ -45,7 +46,13 @@ public void addMemberToFragment(Fragment parentFragment, String memberId, Model

List<Fragment> fragments = tiles
.stream()
.map(tile -> fragmentCreator.getOrCreateTileFragment(parentFragment, tile, rootTileFragment)).toList();
.map(tile -> {
if (tile.equals(DEFAULT_BUCKET_STRING)) {
return fragmentCreator.getOrCreateTileFragment(parentFragment, tile, parentFragment);
} else {
return fragmentCreator.getOrCreateTileFragment(parentFragment, tile, rootTileFragment);
}
}).toList();

fragments
.parallelStream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.exceptions.MissingFragmentValueException;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations.RelationsAttributer;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.connected.BoundingBox;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.converter.BoundingBoxConverter;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.converter.TileConverter;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.model.Tile;

import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.*;

public class GeospatialRelationsAttributer {
public class GeospatialRelationsAttributer implements RelationsAttributer {

public TreeRelation getRelationToParentFragment(Fragment childFragment) {
String targetWKT = getWKT(childFragment);
Expand All @@ -27,4 +28,5 @@ private String getWKT(Fragment currentFragment) {
BoundingBox currentBoundingBox = new BoundingBox(currentTile);
return BoundingBoxConverter.toWKT(currentBoundingBox);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.repository.FragmentRepository;

import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE;
import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;

public class TileFragmentRelationsAttributer {

private final GeospatialRelationsAttributer relationsAttributer = new GeospatialRelationsAttributer();
Expand All @@ -14,7 +17,8 @@ public TileFragmentRelationsAttributer(FragmentRepository fragmentRepository) {
}

public void addRelationsFromRootToBottom(Fragment rootFragment, Fragment tileFragments) {
TreeRelation relationToParentFragment = relationsAttributer.getRelationToParentFragment(tileFragments);
boolean isDefaultFragment = tileFragments.getValueOfKey(FRAGMENT_KEY_TILE).orElse("").equals(DEFAULT_BUCKET_STRING);
TreeRelation relationToParentFragment = isDefaultFragment ? relationsAttributer.getDefaultRelation(tileFragments) : relationsAttributer.getRelationToParentFragment(tileFragments);
if (!rootFragment.containsRelation(relationToParentFragment)) {
rootFragment.addRelation(relationToParentFragment);
fragmentRepository.saveFragment(rootFragment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.List;
import java.util.Set;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE_ROOT;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -72,6 +73,24 @@ void when_MemberIsAddedToFragment_GeospatialFragmentationIsApplied() {
any(), any(), any(Observation.class));
verifyNoMoreInteractions(decoratedFragmentationStrategy);
}
@Test
void when_MemberIsAddedToDefaultFragment_GeospatialFragmentationIsApplied() {
Member member = mock(Member.class);

when(geospatialBucketiser.bucketise(member.id(), member.model())).thenReturn(Set.of(DEFAULT_BUCKET_STRING));
Fragment defaultTileFragment = PARENT_FRAGMENT.createChild(new FragmentPair(FRAGMENT_KEY_TILE, DEFAULT_BUCKET_STRING));
when(fragmentCreator.getOrCreateTileFragment(PARENT_FRAGMENT, DEFAULT_BUCKET_STRING,
PARENT_FRAGMENT))
.thenReturn(defaultTileFragment);

geospatialFragmentationStrategy.addMemberToFragment(PARENT_FRAGMENT, member.id(),
member.model(), mock(Observation.class));

verify(decoratedFragmentationStrategy,
times(1)).addMemberToFragment(eq(defaultTileFragment),
any(), any(), any(Observation.class));
verifyNoMoreInteractions(decoratedFragmentationStrategy);
}

private Fragment mockCreationGeospatialFragment(String tile) {
Fragment tileFragment = PARENT_FRAGMENT.createChild(new FragmentPair(FRAGMENT_KEY_TILE, tile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.util.List;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION;
import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -48,6 +50,19 @@ void when_TileFragmentsAreCreated_RelationsBetweenRootAndCreatedFragmentsAreAdde
times(1)).saveFragment(rootFragment);
}

@Test
void when_DefaultFragmentIsCreated_RelationsBetweenRootAndCreatedFragmentIsAdded() {
Fragment rootFragment = createTileFragment("0/0/0");
Fragment tileFragment = createTileFragment(DEFAULT_BUCKET_STRING);
TreeRelation expectedRelation = new TreeRelation("", tileFragment.getFragmentId(), "", "", GENERIC_TREE_RELATION);

tileFragmentRelationsAttributer.addRelationsFromRootToBottom(
rootFragment, tileFragment);

assertTrue(rootFragment.containsRelation(expectedRelation));
verify(fragmentRepository,
times(1)).saveFragment(rootFragment);
}
private Fragment createTileFragment(String tile) {
return PARENT_FRAGMENT.createChild(new FragmentPair(FRAGMENT_KEY_TILE,
tile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.List;
import java.util.Optional;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.geospatial.constants.GeospatialConstants.FRAGMENT_KEY_TILE_ROOT;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -23,6 +24,7 @@ class GeospatialFragmentCreatorTest {
private static final FragmentPair timebasedPair = new FragmentPair("year", "2023");
private static final FragmentPair geoRootPair = new FragmentPair(FRAGMENT_KEY_TILE, FRAGMENT_KEY_TILE_ROOT);
private static final FragmentPair geoPair = new FragmentPair(FRAGMENT_KEY_TILE, "15/101/202");
private static final FragmentPair defaultPair = new FragmentPair(FRAGMENT_KEY_TILE, DEFAULT_BUCKET_STRING);

private FragmentRepository fragmentRepository;
private GeospatialFragmentCreator geospatialFragmentCreator;
Expand Down Expand Up @@ -106,12 +108,50 @@ void when_RootFragmentDoesNotExist_RetrievedRootFragmentIsReturned() {
when(fragmentRepository.retrieveFragment(rootFragment.getFragmentId()))
.thenReturn(Optional.of(rootFragment));

Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment,
FRAGMENT_KEY_TILE_ROOT, rootFragment);
Fragment returnedFragment = geospatialFragmentCreator.getOrCreateRootFragment(fragment,
FRAGMENT_KEY_TILE_ROOT);

assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, geoRootPair)),
returnedFragment.getFragmentId());
verify(fragmentRepository, times(1)).retrieveFragment(rootFragment.getFragmentId());
verifyNoMoreInteractions(fragmentRepository);
}
@Test
void when_DefaultFragmentDoesNotExist_NewDefaultFragmentIsCreatedAndSaved() {
Fragment fragment = new Fragment(new LdesFragmentIdentifier(
VIEW_NAME, List.of(timebasedPair)));
Fragment rootFragment = fragment
.createChild(geoRootPair);
Fragment defaultFragment = fragment
.createChild(defaultPair);
when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId())).thenReturn(Optional.empty());

Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment,
DEFAULT_BUCKET_STRING, rootFragment);

assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, defaultPair)),
returnedFragment.getFragmentId());
verify(fragmentRepository, times(1)).retrieveFragment(defaultFragment.getFragmentId());
verify(fragmentRepository, times(1)).saveFragment(returnedFragment);
}

@Test
void when_DefaultFragmentDoesNotExist_RetrievedDefaultFragmentIsReturned() {
Fragment fragment = new Fragment(new LdesFragmentIdentifier(
VIEW_NAME, List.of(timebasedPair)));
Fragment rootFragment = fragment
.createChild(geoRootPair);
Fragment defaultFragment = fragment
.createChild(defaultPair);
when(fragmentRepository.retrieveFragment(defaultFragment.getFragmentId()))
.thenReturn(Optional.of(defaultFragment));

Fragment returnedFragment = geospatialFragmentCreator.getOrCreateTileFragment(fragment,
DEFAULT_BUCKET_STRING, rootFragment);

assertEquals(new LdesFragmentIdentifier(VIEW_NAME, List.of(timebasedPair, defaultPair)),
returnedFragment.getFragmentId());
verify(fragmentRepository, times(1)).retrieveFragment(defaultFragment.getFragmentId());
verifyNoMoreInteractions(fragmentRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.FragmentationService.LDES_SERVER_CREATE_FRAGMENTS_COUNT;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.metrics.MetricsConstants.FRAGMENTATION_STRATEGY;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.metrics.MetricsConstants.VIEW;
Expand Down Expand Up @@ -35,7 +36,11 @@ public Fragment getOrCreateFragment(Fragment parentFragment, String reference, F
.retrieveFragment(child.getFragmentId())
.orElseGet(() -> {
fragmentRepository.saveFragment(child);
relationsAttributer.addRelationsFromRootToBottom(rootFragment, child);
if (reference.equals(DEFAULT_BUCKET_STRING)) {
relationsAttributer.addDefaultRelation(parentFragment, child);
} else {
relationsAttributer.addRelationsFromRootToBottom(rootFragment, child);
}
logFragmentation(parentFragment, child);
return child;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.exceptions.MissingFragmentValueException;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations.RelationsAttributer;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.repository.FragmentRepository;
import org.apache.jena.datatypes.xsd.XSDDatatype;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.TREE;

public class ReferenceFragmentRelationsAttributer {
public class ReferenceFragmentRelationsAttributer implements RelationsAttributer {

public static final String TREE_REFERENCE_EQUALS_RELATION = TREE + "EqualToRelation";

Expand All @@ -25,10 +26,17 @@ public ReferenceFragmentRelationsAttributer(FragmentRepository fragmentRepositor
}

public void addRelationsFromRootToBottom(Fragment rootFragment, Fragment referenceFragments) {
TreeRelation relationToParentFragment = getRelationToParentFragment(referenceFragments);
if (!rootFragment.containsRelation(relationToParentFragment)) {
rootFragment.addRelation(relationToParentFragment);
fragmentRepository.saveFragment(rootFragment);
saveRelation(rootFragment, getRelationToParentFragment(referenceFragments));
}

public void addDefaultRelation(Fragment rootFragment, Fragment fragment) {
saveRelation(rootFragment, getDefaultRelation(fragment));
}

private void saveRelation(Fragment fragment, TreeRelation relation) {
if (!fragment.containsRelation(relation)) {
fragment.addRelation(relation);
fragmentRepository.saveFragment(fragment);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Optional;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.ServerConstants.DEFAULT_BUCKET_STRING;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.reference.ReferenceFragmentationStrategyWrapper.DEFAULT_FRAGMENTATION_KEY;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.reference.fragmentation.ReferenceFragmentCreator.FRAGMENT_KEY_REFERENCE_ROOT;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -29,6 +30,7 @@ class ReferenceFragmentCreatorTest {
private static final FragmentPair timebasedPair = new FragmentPair("year", "2023");
private static final FragmentPair referenceRootPair = new FragmentPair(DEFAULT_FRAGMENTATION_KEY, FRAGMENT_KEY_REFERENCE_ROOT);
private static final FragmentPair referencePair = new FragmentPair(DEFAULT_FRAGMENTATION_KEY, RDF.type.getURI());
private static final FragmentPair defaultPair = new FragmentPair(DEFAULT_FRAGMENTATION_KEY, DEFAULT_BUCKET_STRING);

private ReferenceFragmentCreator referenceFragmentCreator;

Expand Down Expand Up @@ -63,7 +65,7 @@ void when_ReferenceFragmentDoesNotExist_NewReferenceFragmentIsCreatedAndSaved()
}

@Test
void when_ReferenceFragmentDoesNotExist_RetrievedReferenceFragmentIsReturned() {
void when_ReferenceFragmentDoesExist_RetrievedReferenceFragmentIsReturned() {
Fragment fragment = new Fragment(new LdesFragmentIdentifier(viewName, List.of(timebasedPair)));
Fragment rootFragment = fragment.createChild(referenceRootPair);
Fragment tileFragment = fragment.createChild(referencePair);
Expand Down Expand Up @@ -111,5 +113,39 @@ void when_RootFragmentDoesNotExist_RetrievedRootFragmentIsReturned() {
verify(fragmentRepository, times(1)).retrieveFragment(rootFragment.getFragmentId());
verifyNoMoreInteractions(fragmentRepository);
}
@Test
void when_DefaultFragmentDoesNotExist_DefaultFragmentIsCreatedAndSaved() {
Fragment fragment = new Fragment(new LdesFragmentIdentifier(viewName, List.of(timebasedPair)));
Fragment rootFragment = fragment.createChild(referenceRootPair);
LdesFragmentIdentifier tileFragmentId = fragment.createChild(defaultPair).getFragmentId();

when(fragmentRepository.retrieveFragment(tileFragmentId)).thenReturn(Optional.empty());

Fragment childFragment = referenceFragmentCreator.getOrCreateFragment(fragment, DEFAULT_BUCKET_STRING, rootFragment);

assertThat(new LdesFragmentIdentifier(viewName, List.of(timebasedPair, defaultPair)))
.isEqualTo(childFragment.getFragmentId());
verify(fragmentRepository,
times(1)).retrieveFragment(tileFragmentId);
verify(fragmentRepository,
times(1)).saveFragment(childFragment);
}

@Test
void when_DefaultFragmentDoesExist_RetrievedDefaultFragmentIsReturned() {
Fragment fragment = new Fragment(new LdesFragmentIdentifier(viewName, List.of(timebasedPair)));
Fragment rootFragment = fragment.createChild(referenceRootPair);
Fragment tileFragment = fragment.createChild(defaultPair);

when(fragmentRepository.retrieveFragment(tileFragment.getFragmentId())).thenReturn(Optional.of(tileFragment));

Fragment childFragment = referenceFragmentCreator.getOrCreateFragment(fragment, DEFAULT_BUCKET_STRING, rootFragment);

assertThat(new LdesFragmentIdentifier(viewName, List.of(timebasedPair, defaultPair)))
.isEqualTo(childFragment.getFragmentId());
verify(fragmentRepository,
times(1)).retrieveFragment(tileFragment.getFragmentId());
verifyNoMoreInteractions(fragmentRepository);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import be.vlaanderen.informatievlaanderen.ldes.server.domain.model.TreeRelation;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.entities.Fragment;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.relations.RelationsAttributer;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentation.repository.FragmentRepository;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.config.TimeBasedConfig;
import be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.constants.Granularity;
Expand All @@ -12,10 +13,9 @@
import java.util.HashMap;
import java.util.Map;

import static be.vlaanderen.informatievlaanderen.ldes.server.domain.constants.RdfConstants.GENERIC_TREE_RELATION;
import static be.vlaanderen.informatievlaanderen.ldes.server.fragmentisers.timebasedhierarchical.constants.TimeBasedConstants.TREE_INBETWEEN_RELATION;

public class TimeBasedRelationsAttributer {
public class TimeBasedRelationsAttributer implements RelationsAttributer {

private final FragmentRepository fragmentRepository;

Expand All @@ -33,17 +33,17 @@ public void addInBetweenRelation(Fragment parentFragment, Fragment childFragment
childFragment.getFragmentId(),
timestamp.asString(), timestamp.getType(),
TREE_INBETWEEN_RELATION);
if (!parentFragment.containsRelation(parentChildRelation)) {
parentFragment.addRelation(parentChildRelation);
fragmentRepository.saveFragment(parentFragment);
}
saveRelation(parentFragment, parentChildRelation);
}

public void addDefaultRelation(Fragment parentFragment, Fragment childFragment) {
TreeRelation parentChildRelation = new TreeRelation("", childFragment.getFragmentId(), "", "", GENERIC_TREE_RELATION);
if (!parentFragment.containsRelation(parentChildRelation)) {
parentFragment.addRelation(parentChildRelation);
fragmentRepository.saveFragment(parentFragment);
saveRelation(parentFragment, getDefaultRelation(childFragment));
}

private void saveRelation(Fragment fragment, TreeRelation relation) {
if (!fragment.containsRelation(relation)) {
fragment.addRelation(relation);
fragmentRepository.saveFragment(fragment);
}
}

Expand Down

0 comments on commit 76106e9

Please sign in to comment.