
uk.gov.gchq.gaffer.data.element.function.TupleToElements Maven / Gradle / Ivy
/*
* Copyright 2019-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.gov.gchq.gaffer.data.element.function;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import uk.gov.gchq.gaffer.commonutil.iterable.StreamIterable;
import uk.gov.gchq.gaffer.data.element.Edge;
import uk.gov.gchq.gaffer.data.element.Element;
import uk.gov.gchq.gaffer.data.element.Entity;
import uk.gov.gchq.gaffer.data.element.IdentifierType;
import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.impl.predicate.Exists;
import uk.gov.gchq.koryphe.tuple.Tuple;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;
import static uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition.DESTINATION;
import static uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition.DIRECTED;
import static uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition.GROUP;
import static uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition.SOURCE;
import static uk.gov.gchq.gaffer.data.element.function.ElementTupleDefinition.VERTEX;
/**
* A {@code TupleToElements} is a Function which generates Elements from a Tuple.
*/
@Since("1.21.0")
@Summary("Generates Elements from a Tuple")
@JsonPropertyOrder(alphabetic = true)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class TupleToElements extends KorypheFunction, Iterable> implements Serializable {
private static final long serialVersionUID = -6793331642118688901L;
private final List elements = new ArrayList<>();
private boolean useGroupMapping = false;
@Override
public Iterable apply(final Tuple tuple) {
return new StreamIterable<>(() -> elements.stream().map(e -> createElement(tuple, e)).filter(new Exists()));
}
private Element createElement(final Tuple tuple, final ElementTupleDefinition elementDef) {
requireNonNull(elementDef.get(GROUP), GROUP + " is required");
Element element = null;
final String group;
if (useGroupMapping) {
group = (String) getField(GROUP, elementDef, tuple);
} else {
group = elementDef.getGroup();
}
if (elementDef.containsKey(VERTEX)) {
final Object vertex = getField(VERTEX, elementDef, tuple);
if (nonNull(vertex) && nonNull(group)) {
element = new Entity(group, vertex);
}
} else {
final Object source = getField(SOURCE, elementDef, tuple);
final Object destination = getField(DESTINATION, elementDef, tuple);
Object directed = getField(DIRECTED, elementDef, tuple);
directed = isNull(directed) || Boolean.TRUE.equals(directed) || (directed instanceof String && Boolean.parseBoolean((String) directed));
if (nonNull(source) && nonNull(destination)) {
element = new Edge(group, source, destination, (boolean) directed);
}
}
if (nonNull(element)) {
for (final Map.Entry entry : elementDef.entrySet()) {
final IdentifierType id = IdentifierType.fromName(entry.getKey());
final Object field = getField(entry.getValue(), tuple);
if (id == null && field != null && !field.equals("")) {
element.putProperty(entry.getKey(), field);
}
}
}
return element;
}
private Object getField(final String key, final ElementTupleDefinition elementDef, final Tuple tuple) {
return getField(elementDef.get(key), tuple);
}
private Object getField(final Object value, final Tuple tuple) {
if (null == value) {
return null;
}
if (value instanceof String) {
return tuple.get(((String) value));
}
return value;
}
public List getElements() {
return elements;
}
public void setElements(final List elements) {
this.elements.clear();
this.elements.addAll(elements);
}
public TupleToElements element(final ElementTupleDefinition elementDef) {
elements.add(elementDef);
return this;
}
public TupleToElements elements(final List elementDef) {
elements.addAll(elementDef);
return this;
}
public boolean getUseGroupMapping() {
return useGroupMapping;
}
public void setUseGroupMapping(final boolean useGroupMapping) {
this.useGroupMapping = useGroupMapping;
}
public TupleToElements useGroupMapping(final boolean useGroupMapping) {
setUseGroupMapping(useGroupMapping);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy