EdgeToTemporalEdge.java

  1. /*
  2.  * Copyright © 2014 - 2021 Leipzig University (Database Research Group)
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *     http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package org.gradoop.temporal.model.impl.functions.tpgm;

  17. import org.apache.flink.api.common.functions.MapFunction;
  18. import org.apache.flink.api.java.functions.FunctionAnnotation;
  19. import org.gradoop.common.model.api.entities.Edge;
  20. import org.gradoop.common.model.api.entities.EdgeFactory;
  21. import org.gradoop.common.model.impl.id.GradoopId;
  22. import org.gradoop.temporal.model.api.functions.TimeIntervalExtractor;
  23. import org.gradoop.temporal.model.impl.pojo.TemporalEdge;

  24. /**
  25.  * Initializes a {@link TemporalEdge} from a {@link Edge} instance by setting either
  26.  * default temporal information or, if a timeIntervalExtractor is given, by the extracted time information.
  27.  *
  28.  * @param <E> The (non-temporal) edge type.
  29.  */
  30. @FunctionAnnotation.ForwardedFields("id;sourceId;targetId;label;properties;graphIds")
  31. public class EdgeToTemporalEdge<E extends Edge> implements MapFunction<E, TemporalEdge> {
  32.   /**
  33.    * The user defined timestamp extractor.
  34.    */
  35.   private TimeIntervalExtractor<E> timeIntervalExtractor;
  36.   /**
  37.    * Reuse this instance to reduce instantiations.
  38.    */
  39.   private TemporalEdge reuse;

  40.   /**
  41.    * Creates an instance of the TemporalEdgeFromNonTemporal map function. The temporal instance
  42.    * will have default temporal information.
  43.    *
  44.    * @param elementFactory factory that is responsible for creating a temporal edge instance
  45.    */
  46.   public EdgeToTemporalEdge(EdgeFactory<TemporalEdge> elementFactory) {
  47.     this.reuse = elementFactory.createEdge(GradoopId.NULL_VALUE, GradoopId.NULL_VALUE);
  48.   }

  49.   /**
  50.    * Creates an instance of the TemporalEdgeFromNonTemporal map function. The temporal instance
  51.    * will have valid times extracted from the non-temporal instance by the given timeIntervalExtractor.
  52.    *
  53.    * @param elementFactory factory that is responsible for creating a temporal edge instance
  54.    * @param timeIntervalExtractor the extractor instance fetches the validFrom and validTo values
  55.    */
  56.   public EdgeToTemporalEdge(
  57.     EdgeFactory<TemporalEdge> elementFactory,
  58.     TimeIntervalExtractor<E> timeIntervalExtractor) {
  59.     this(elementFactory);
  60.     this.timeIntervalExtractor = timeIntervalExtractor;
  61.   }

  62.   @Override
  63.   public TemporalEdge map(E value) throws Exception {
  64.     reuse.setId(value.getId());
  65.     reuse.setLabel(value.getLabel());
  66.     reuse.setSourceId(value.getSourceId());
  67.     reuse.setTargetId(value.getTargetId());
  68.     reuse.setProperties(value.getProperties());
  69.     reuse.setGraphIds(value.getGraphIds());
  70.     if (timeIntervalExtractor != null) {
  71.       reuse.setValidTime(timeIntervalExtractor.map(value));
  72.     }
  73.     return reuse;
  74.   }
  75. }