net.intelie.pipes.FunctionContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes-api Show documentation
Show all versions of pipes-api Show documentation
Intelie Pipes' API classes and interfaces
package net.intelie.pipes;
import net.intelie.pipes.ast.AstNode;
import net.intelie.pipes.ast.CallNode;
import net.intelie.pipes.types.Metadata;
import java.io.Serializable;
import java.util.*;
public class FunctionContext implements Serializable {
private static final long serialVersionUID = 1L;
private final Metadata metadata;
private final Function function;
private final Map early;
private final Map late;
private final HashSet keys;
public FunctionContext(Metadata metadata) {
this(metadata, null, null, null);
}
public FunctionContext(Metadata metadata, Function function, Map early, Map late) {
this.metadata = metadata;
this.function = function;
this.early = early;
this.late = late;
this.keys = new HashSet<>();
addKeys(early);
addKeys(late);
}
public FunctionContext makeClosure(Function function, Map early, Map late) {
Map newEarly = new LinkedHashMap<>();
Map newLate = new LinkedHashMap<>();
putAll(newEarly, this.early);
putAll(newEarly, early);
putAll(newLate, this.late);
putAll(newLate, late);
return new FunctionContext(metadata, function, newEarly, newLate);
}
private void putAll(Map newLate, Map late) {
if (late != null) newLate.putAll(late);
}
public String getName() {
if (function == null) return null;
return function.name();
}
public String getDescription() {
if (function == null) return null;
return function.description();
}
public boolean sameFunctionAs(FunctionContext other) {
return this.function != null && other.function != null && this.function.equals(other.function);
}
public boolean isFunction() {
return function != null;
}
private void addKeys(Map arguments) {
if (arguments != null)
keys.addAll(arguments.keySet());
}
public AstNode resolveProperty(String name, boolean hasArgs, List args) throws PipeException {
AstNode node = getInitialNode(name);
// if (node == null && early != null)
// throw Levenshtein.makeExc(name, keys, "Cannot resolve argument '%s'.%s");
if (node == null)
return null;
if (hasArgs) {
List newArgs = new ArrayList<>();
newArgs.add(node);
newArgs.addAll(args);
node = new CallNode(null, Function.OP_GET, newArgs);
}
return node;
}
public Module makeModule() {
List functions = new ArrayList<>();
addArgs(functions, early);
addArgs(functions, late);
return new ListModule(functions);
}
private void addArgs(List functions, Map map) {
if (map != null) {
for (Map.Entry entry : map.entrySet()) {
functions.add(new AstFunction.Constant("@@arg." + entry.getKey(), entry.getValue()));
}
}
}
public FunctionContext makeChild(Metadata metadata) {
if (this.metadata.equals(metadata))
return new FunctionContext(metadata, function, early, late);
else
return new FunctionContext(metadata, function, null, late);
}
private AstNode getInitialNode(String name) {
AstNode node = null;
if (early != null) node = early.get(name);
if (node == null && late != null) node = late.get(name);
return node;
}
}