Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package prerna.reactor;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Stack;
import java.util.Vector;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
import prerna.sablecc2.om.CodeBlock;
import prerna.sablecc2.om.PixelDataType;
import prerna.sablecc2.om.VarStore;
import prerna.sablecc2.om.nounmeta.NounMetadata;
public class PixelPlanner {
public static final String NOUN = "NOUN";
public static final String NOUN_TYPE = "NOUN_TYPE";
public static final String OPERATION = "OP";
// public static final String REACTOR_CLASS = "REACTOR";
public static final String TINKER_ID = "_T_ID";
public static final String TINKER_TYPE = "_T_TYPE";
public static final String TINKER_NAME = "_T_NAME";
public static final String IN_DEGREE = "IN_DEGREE";
public static final String PROCESSED = "PROCESSED";
public static final String ORDER = "ORDER";
// this is primarily the tinker graph that would be used for planning the operation
public TinkerGraph g = null;
// I need some way to have roots
private Vector roots = new Vector();
// and i need to keep track of the vars
private VarStore varStore = new VarStore();
public PixelPlanner() {
this.g = TinkerGraph.open();
addIndices();
}
public void addIndices() {
g.createIndex(PixelPlanner.TINKER_TYPE, Vertex.class);
g.createIndex(PixelPlanner.TINKER_ID, Vertex.class);
g.createIndex(PixelPlanner.TINKER_ID, Edge.class);
}
public void dropIndices() {
g.dropIndex(PixelPlanner.TINKER_TYPE, Vertex.class);
g.dropIndex(PixelPlanner.TINKER_ID, Vertex.class);
g.dropIndex(PixelPlanner.TINKER_ID, Edge.class);
}
public void dropGraph() {
this.g.clear();
this.g.close();
this.g = null;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*
* Variable methods
*/
public void addVariable(String variableName, NounMetadata value) {
varStore.put(variableName, value);
}
public NounMetadata getVariable(String variableName) {
return varStore.get(variableName);
}
public NounMetadata getVariableValue(String variableName) {
return varStore.getEvaluatedValue(variableName);
}
public boolean hasVariable(String variableName) {
return varStore.containsKey(variableName);
}
public NounMetadata removeVariable(String variableName) {
return varStore.remove(variableName);
}
public Set getVariables() {
return varStore.getKeys();
}
public void setVarStore(VarStore varStore) {
this.varStore = varStore;
}
public VarStore getVarStore() {
return this.varStore;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/*
* New planner methods for pipeline...
* TODO: see how to perform previous logic (unused currently)
* with new structure
*/
protected Vertex upsertVertex(String type, Object name) {
type = type.trim();
if(name instanceof String) {
name = name.toString().trim();
}
Vertex retVertex = findVertex(type, name);
if(retVertex == null) {
retVertex = g.addVertex(TINKER_ID, type + ":" + name, TINKER_TYPE, type, TINKER_NAME, name);
}
return retVertex;
}
private Vertex findVertex(String type, Object name) {
type = type.trim();
if(name instanceof String) {
name = name.toString().trim();
}
Vertex retVertex = null;
GraphTraversal gt = g.traversal().V().has(TINKER_ID, type + ":" + name);
if(gt.hasNext()) {
retVertex = gt.next();
}
return retVertex;
}
protected Edge upsertEdge(Vertex startV, Vertex endV, String edgeID, String type) {
GraphTraversal gt = g.traversal().E().has(TINKER_ID, edgeID);
// increase the count of the in degree on the endV
if(gt.hasNext()) {
// this is a duplicate edge, do nothing
return gt.next();
} else {
// this is a new edge
// increase the in_degree on the end vertex
if(!endV.property(IN_DEGREE).isPresent()) {
endV.property(IN_DEGREE, 1);
} else {
int newInDegree = (int) endV.value(IN_DEGREE) + 1;
endV.property(IN_DEGREE, newInDegree);
}
return startV.addEdge(edgeID, endV, TINKER_ID, edgeID, "COUNT", 1, "TYPE", type);
}
}
public void addInputs(String opName, List inputs) {
opName = opName.trim();
Vertex opVertex = upsertVertex(OPERATION, opName);
for(int inputIndex = 0; inputIndex < inputs.size(); inputIndex++) {
NounMetadata noun = inputs.get(inputIndex);
Object value = noun.getValue();
PixelDataType nounType = noun.getNounType();
if(value == null) {
value = "null";
}
// if we have a lambda, then its another operation as the input
if(nounType == PixelDataType.LAMBDA) {
Vertex inpVertex = upsertVertex(OPERATION, value);
String edgeID = value + "_" + opName;
upsertEdge(inpVertex, opVertex, edgeID, "INPUT");
} else {
Vertex inpVertex = upsertVertex(NOUN, value);
String edgeID = value + "_" + opName;
upsertEdge(inpVertex, opVertex, edgeID, "INPUT");
}
}
}
public void addOutputs(String opName, List outputs) {
opName = opName.trim();
Vertex opVertex = upsertVertex(OPERATION, opName);
for(int inputIndex = 0; inputIndex < outputs.size(); inputIndex++) {
Object value = outputs.get(inputIndex).getValue();
if(value == null) {
value = "null";
}
Vertex inpVertex = upsertVertex(NOUN, value);
String edgeID = value + "_" + opName;
upsertEdge(opVertex, inpVertex, edgeID, "OUTPUT");
}
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//**********************PROPERTY METHODS*******************************//
public void addProperty(String opName, String propertyName, Object value)
{
Vertex opVertex = upsertVertex(OPERATION, opName);
if(opVertex != null)
{
opVertex.property(propertyName, value);
}
}
public Object getProperty(String opName, String propertyName) {
Vertex opVertex = findVertex(OPERATION, opName);
if(opVertex != null) {
if(opVertex.property(propertyName).isPresent()) {
return opVertex.property(propertyName).value();
}
}
return null;
}
public boolean hasProperty(String opName, String propertyName) {
return getProperty(opName, propertyName) != null;
}
public void addNounProperty(String nounName, String propertyName, Object value)
{
Vertex nounVertex = findVertex(NOUN, nounName);
if(nounVertex != null)
{
nounVertex.property(propertyName, value);
}
}
//**********************END PROPERTY METHODS*******************************//
//**********************IMPORTANT METHODS*******************************//
// adds an operation with outputs
public void addOutputs(String opName, List outputs, IReactor.TYPE opType)
{
opName = opName.trim();
// find the vertex for each of the input
// wonder if this should be a full fledged block of java ?
Vertex opVertex = upsertVertex(OPERATION, opName);
for(int outputIndex = 0;outputIndex < outputs.size();outputIndex++)
{
Vertex outpVertex = upsertVertex(NOUN, outputs.get(outputIndex).trim());
outpVertex.property("VAR_TYPE", "QUERY"); // by default it is query
String edgeID = opName + "_" + outputs.get(outputIndex).trim();
upsertEdge(opVertex, outpVertex, edgeID, "OUTPUT");
}
}
// specify what type is the output is going to be sitting in store or is it sitting in query
// should I also specify as names here ?
public void addOutputs(String opName, List outputs, List outputTypes, IReactor.TYPE opType)
{
opName = opName.trim();
// find the vertex for each of the input
// wonder if this should be a full fledged block of java ?
Vertex opVertex = upsertVertex(OPERATION, opName);
for(int outputIndex = 0;outputIndex < outputs.size();outputIndex++)
{
Vertex outpVertex = upsertVertex(NOUN, outputs.get(outputIndex).trim());
outpVertex.property("VAR_TYPE", outputTypes.get(outputIndex)); // by default it is query
String edgeID = opName + "_" + outputs.get(outputIndex).trim();
upsertEdge(opVertex, outpVertex, edgeID, "OUTPUT");
}
}
public void addInputs(String opName, List inputs, IReactor.TYPE opType) {
addInputs(opName, inputs, null, opType);
}
// adds an operation with necessary inputs
public void addInputs(String opName, List inputs, List asInputs, IReactor.TYPE opType)
{
// find the vertex for each of the input
// wonder if this should be a full fledged block of java ?
Vertex opVertex = upsertVertex(OPERATION, opName);
opVertex.property("OP_TYPE", opType);
opVertex.property("CODE", "NONE");
opVertex.property("INPUT_ORDER", inputs);
Vector aliasVector = new Vector();
for(int inputIndex = 0;inputIndex < inputs.size();inputIndex++)
{
Vertex inpVertex = upsertVertex(NOUN, inputs.get(inputIndex).trim());
if(asInputs != null && inputIndex < asInputs.size()) {
aliasVector.add(asInputs.get(inputIndex));
} else {
aliasVector.add(inputs.get(inputIndex).trim());
}
String edgeID = inputs.get(inputIndex).trim() + "_" + opName;
// make sure we do not add the edges multiple times
upsertEdge(inpVertex, opVertex, edgeID, "INPUT");
}
opVertex.property("ALIAS", aliasVector);
}
//**********************END IMPORTANT METHODS*******************************//
public void addInputs(String opName, CodeBlock codeBlock, List inputs, IReactor.TYPE opType)
{
addInputs(opName, codeBlock, inputs, null, opType);
}
// adds a java operation string as input
// this should be more of outputs and not inputs
// face palm
// in this case the as Name should be used as the main name
// I see seems like I have accomodated for multiple as names
// no not really it is just as here
public void addInputs(String opName, CodeBlock codeBlock, List inputs, List asInputs, IReactor.TYPE opType)
{
// should this be string ?
// it should be a map block
// or it should be a reduce block
// at a minimum it should say what type of block it is
opName = opName.trim();
Vertex opVertex = upsertVertex(OPERATION, opName);
opVertex.property("OP_TYPE", opType);
opVertex.property("CODE", codeBlock);
opVertex.property("INPUT_ORDER", inputs);
List aliasVector = new Vector();
for(int inputIndex = 0;inputIndex < inputs.size();inputIndex++)
{
Vertex inpVertex = upsertVertex(NOUN, inputs.get(inputIndex).trim());
if(asInputs != null && inputIndex < asInputs.size()) {
aliasVector.add(asInputs.get(inputIndex).trim());
} else {
aliasVector.add(inputs.get(inputIndex).trim());
}
String edgeID = inputs.get(inputIndex).trim()+ "_" + opName;
upsertEdge(inpVertex, opVertex, edgeID, "INPUT");
}
opVertex.property("ALIAS", aliasVector);
}
// this runs the plan for the final output
public Iterator runMyPlan(String output)
{
// I need to take all of the stuff from gremlin and assort it into
// blocks
// Blocks can be progressively executed
// I need to stack the blocks
// everytime I find the input to be a block, I need to stack it up i.e. only the code operation
// Output <- codeBlock <- codeblock etc.
// Everytime I see a reduce operation I need to chop it off
// And So on
// while output input edges is all not null
// I need to always assimilate all the inputs
// and plan my operation
// I need a block keeper
// when there is a reduce which multiple thing depends on, I need to validatee so that it doesn't do it twice
// Just as a stage has various operations and their sequence
// I also need to keep something with all stages and its sequence
// but at the same time ensure that it is not overwriting anything
// I need to test
// Single Stage - done
// Multi Stage with reduce - done
// Multi stage with reduce
List