Search in sources :

Example 16 with FacetValue

use of nl.knaw.huygens.timbuctoo.search.FacetValue in project timbuctoo by HuygensING.

the class RelatedDatableRangeFacetDescription method filter.

@Override
@SuppressWarnings("unchecked")
public void filter(GraphTraversal<Vertex, Vertex> graphTraversal, List<FacetValue> facets) {
    Optional<FacetValue> first = facets.stream().filter(facetValue -> Objects.equals(facetValue.getName(), facetName)).findFirst();
    if (!first.isPresent()) {
        return;
    }
    FacetValue facetValue = first.get();
    if (!(facetValue instanceof DateRangeFacetValue)) {
        return;
    }
    // pad the strings to make them parsable
    String lowerLimitString = Strings.padStart("" + ((DateRangeFacetValue) facetValue).getLowerLimit(), 8, '0').substring(0, 4);
    String upperLimitString = Strings.padStart("" + ((DateRangeFacetValue) facetValue).getUpperLimit(), 8, '0').substring(0, 4);
    try {
        Range<Date> range = Range.closed(FILTER_FORMAT.parse(lowerLimitString), FILTER_FORMAT.parse(upperLimitString));
        graphTraversal.where(__.bothE(relations).otherV().has(propertyName, P.test((o1, o2) -> {
            Datable datable = getDatable("" + o1);
            if (!datable.isValid()) {
                return false;
            }
            Range<Date> range1 = (Range<Date>) o2;
            return range1.contains(datable.getFromDate()) || range1.contains(datable.getToDate());
        }, range)));
    } catch (ParseException e) {
        LOG.error("Cannot parse date", e);
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Logger(org.slf4j.Logger) Date(java.util.Date) Range(com.google.common.collect.Range) DatableRangeFacetGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.DatableRangeFacetGetter) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) Objects(java.util.Objects) Strings(com.google.common.base.Strings) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Datable(nl.knaw.huygens.timbuctoo.model.Datable) Optional(java.util.Optional) ParseException(java.text.ParseException) P(org.apache.tinkerpop.gremlin.process.traversal.P) RelatedPropertyValueGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.RelatedPropertyValueGetter) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) ParseException(java.text.ParseException) Range(com.google.common.collect.Range) Date(java.util.Date) Datable(nl.knaw.huygens.timbuctoo.model.Datable)

Example 17 with FacetValue

use of nl.knaw.huygens.timbuctoo.search.FacetValue in project timbuctoo by HuygensING.

the class AltNameFacetDescription method filter.

@Override
public void filter(GraphTraversal<Vertex, Vertex> graphTraversal, List<FacetValue> facetValues) {
    Optional<FacetValue> first = facetValues.stream().filter(facetValue -> Objects.equals(facetValue.getName(), facetName)).findFirst();
    if (!first.isPresent()) {
        return;
    }
    FacetValue facetValue = first.get();
    if (!(facetValue instanceof ListFacetValue)) {
        return;
    }
    List<String> values = ((ListFacetValue) facetValue).getValues();
    if (values.isEmpty()) {
        return;
    }
    graphTraversal.where(__.<String>has(propertyName, P.test((o1, o2) -> o1 instanceof String && o2 instanceof List && ((List<?>) o2).stream().anyMatch(value -> ((String) o1).contains("\"" + value + "\"")), values)));
}
Also used : Logger(org.slf4j.Logger) ListFacetGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.ListFacetGetter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) IOException(java.io.IOException) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ListFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue) Objects(java.util.Objects) VertexProperty(org.apache.tinkerpop.gremlin.structure.VertexProperty) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) Lists(com.google.common.collect.Lists) AltNames(nl.knaw.huygens.timbuctoo.model.AltNames) Optional(java.util.Optional) P(org.apache.tinkerpop.gremlin.process.traversal.P) ListFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) ListFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue) List(java.util.List)

Example 18 with FacetValue

use of nl.knaw.huygens.timbuctoo.search.FacetValue in project timbuctoo by HuygensING.

the class DerivedListFacetDescription method filter.

@Override
public void filter(GraphTraversal<Vertex, Vertex> graphTraversal, List<FacetValue> facets) {
    Optional<FacetValue> first = facets.stream().filter(facetValue -> Objects.equals(facetValue.getName(), facetName)).findFirst();
    if (!first.isPresent()) {
        return;
    }
    FacetValue facetValue = first.get();
    if (!(facetValue instanceof ListFacetValue)) {
        return;
    }
    List<String> values = ((ListFacetValue) facetValue).getValues();
    if (values.isEmpty()) {
        return;
    }
    graphTraversal.where(__.bothE(relations).otherV().bothE(relationNames).otherV().values(propertyName).map(value -> parser.parse((String) value.get())).is(within(values)));
}
Also used : Objects(java.util.Objects) PropertyParser(nl.knaw.huygens.timbuctoo.search.description.PropertyParser) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) ListFacetGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.ListFacetGetter) Optional(java.util.Optional) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) P.within(org.apache.tinkerpop.gremlin.process.traversal.P.within) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) DerivedPropertyValueGetter(nl.knaw.huygens.timbuctoo.search.description.facet.helpers.DerivedPropertyValueGetter) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ListFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) ListFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue) ListFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue)

Example 19 with FacetValue

use of nl.knaw.huygens.timbuctoo.search.FacetValue in project timbuctoo by HuygensING.

the class ChangeRangeFacetDescriptionTest method filterAddsAFilterThatChecksIfTheDateIsBetweenTheUpperAndLowerLimit.

@Test
public void filterAddsAFilterThatChecksIfTheDateIsBetweenTheUpperAndLowerLimit() {
    GraphTraversal<Vertex, Vertex> traversal = newGraph().withVertex(v -> v.withTimId("id1").withProperty(PROPERTY_NAME, serializedChangeWithDate("20150101"))).withVertex(v -> v.withTimId("id2").withProperty(PROPERTY_NAME, serializedChangeWithDate("10000302"))).withVertex(v -> v.withTimId("id3").withProperty(PROPERTY_NAME, serializedChangeWithDate("21000302"))).build().traversal().V();
    List<FacetValue> facets = Lists.newArrayList(new DateRangeFacetValue(FACET_NAME, 20000101, 20160101));
    instance.filter(traversal, facets);
    assertThat(traversal.toList(), contains(likeVertex().withTimId("id1")));
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Instant(java.time.Instant) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ZoneId(java.time.ZoneId) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) Lists(com.google.common.collect.Lists) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) VertexMatcher.likeVertex(nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex) LocalDate(java.time.LocalDate) DateTimeFormatter(java.time.format.DateTimeFormatter) Change(nl.knaw.huygens.timbuctoo.model.Change) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) Before(org.junit.Before) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexMatcher.likeVertex(nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Test(org.junit.Test)

Example 20 with FacetValue

use of nl.knaw.huygens.timbuctoo.search.FacetValue in project timbuctoo by HuygensING.

the class DatableRangeFacetDescriptionTest method filterIncludesTheVerticesWhereOnlyTheEndFallsInTheRange.

@Test
public void filterIncludesTheVerticesWhereOnlyTheEndFallsInTheRange() {
    GraphTraversal<Vertex, Vertex> traversal = newGraph().withVertex(v -> v.withTimId("id1").withProperty(PROPERTY_NAME, asSerializedDatable("2000/2015"))).withVertex(v -> v.withTimId("id2").withProperty(PROPERTY_NAME, asSerializedDatable("0015-01"))).withVertex(v -> v.withTimId("id3").withProperty(PROPERTY_NAME, asSerializedDatable("0190-01"))).build().traversal().V();
    List<FacetValue> facetValues = Lists.newArrayList(new DateRangeFacetValue(FACET_NAME, 20140101L, 20160224L));
    instance.filter(traversal, facetValues);
    assertThat(traversal.toList(), contains(likeVertex().withTimId("id1")));
}
Also used : Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) List(java.util.List) Lists(com.google.common.collect.Lists) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) VertexMatcher.likeVertex(nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TestGraphBuilder.newGraph(nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph) Before(org.junit.Before) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) VertexMatcher.likeVertex(nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) FacetValue(nl.knaw.huygens.timbuctoo.search.FacetValue) DateRangeFacetValue(nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue) Test(org.junit.Test)

Aggregations

FacetValue (nl.knaw.huygens.timbuctoo.search.FacetValue)34 List (java.util.List)32 GraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal)31 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)31 Lists (com.google.common.collect.Lists)27 TestGraphBuilder.newGraph (nl.knaw.huygens.timbuctoo.util.TestGraphBuilder.newGraph)25 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)25 Matchers.containsInAnyOrder (org.hamcrest.Matchers.containsInAnyOrder)25 Test (org.junit.Test)25 Before (org.junit.Before)23 Matchers.contains (org.hamcrest.Matchers.contains)22 VertexMatcher.likeVertex (nl.knaw.huygens.timbuctoo.util.VertexMatcher.likeVertex)19 ListFacetValue (nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.ListFacetValue)18 DateRangeFacetValue (nl.knaw.huygens.timbuctoo.server.mediatypes.v2.search.DateRangeFacetValue)16 PropertyParser (nl.knaw.huygens.timbuctoo.search.description.PropertyParser)9 Objects (java.util.Objects)7 Optional (java.util.Optional)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 BDDMockito.given (org.mockito.BDDMockito.given)7 Mockito.mock (org.mockito.Mockito.mock)7