org.pragmaticminds.crunch.api.EvalFunctionCall Maven / Gradle / Ivy
Show all versions of crunch-api Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.pragmaticminds.crunch.api;
import org.pragmaticminds.crunch.api.annotations.ChannelValue;
import org.pragmaticminds.crunch.api.function.def.FunctionParameter;
import org.pragmaticminds.crunch.api.function.def.Signature;
import org.pragmaticminds.crunch.api.records.DataType;
import org.pragmaticminds.crunch.api.values.dates.Value;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Call to an Evaluation function.
* This call knows the context and has thus e.g. the literal parameters given in the call.
* And it contains a mapping of the channel names, the mapping is organized the following way:
* name of the channel (in the @{@link ChannelValue}-annotation
* - name of the channel in "reality"
*
* Furthermore it contains a direct reference to the evaluation function that has been called.
*
* @author julian
* Created by julian on 05.11.17
*
* @deprecated Part of the old API
*/
@Deprecated
@SuppressWarnings("squid:S1319") // Use HashMap instead of map to be serializable
public class EvalFunctionCall implements Serializable {
private static final Logger logger = LoggerFactory.getLogger(EvalFunctionCall.class);
private EvalFunctionFactory factory;
private EvalFunction evalFunction;
private HashMap literals;
private HashMap channelNamesMapping;
public EvalFunctionCall(EvalFunction evalFunction, Map literals, Map channelNamesMapping) {
this.evalFunction = evalFunction;
setLiterals(literals);
setChannelNamesMapping(channelNamesMapping);
}
public EvalFunctionCall(EvalFunctionFactory factory, Map literals, Map channelNamesMapping) {
this.factory = factory;
setLiterals(literals);
setChannelNamesMapping(channelNamesMapping);
}
public EvalFunction getEvalFunction() {
if (this.factory != null) {
try {
return factory.create();
} catch (IllegalAccessException e) {
logger.error("Exception occured", e);
return null;
}
}
return evalFunction;
}
public void setEvalFunction(EvalFunction evalFunction) {
this.evalFunction = evalFunction;
}
/*
this method has to return HashMap instead of Map to be serializable
*/
public HashMap getLiterals() {
return literals;
}
public void setLiterals(Map literals) {
this.literals = new HashMap<>(literals);
}
public Map getChannelNamesMapping() {
return channelNamesMapping;
}
public void setChannelNamesMapping(Map channelNamesMapping) {
this.channelNamesMapping = new HashMap<>(channelNamesMapping);
}
/**
* Returns a Map with the Channel names (as in the real world measurement) and the {@link DataType} extracted from the {@link Signature}
* of the Eval Function.
*
* @return Mapping of Channel Names to {@link DataType}
*/
public Map getChannelsAndTypes() {
return channelNamesMapping.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getValue, entry -> {
Optional fp = Arrays.stream(getEvalFunction().getFunctionDef().getSignature().getParameters())
.filter(functionParameter -> entry.getKey().equals(functionParameter.getName()))
.findFirst();
if (!fp.isPresent()) {
throw new IllegalStateException("Asking for a channel \"" + entry.getKey() + "\" that is not present in the functions signature!");
}
return fp.get().getDataType();
}));
}
}