
prerna.om.PixelList Maven / Gradle / Ivy
The newest version!
package prerna.om;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.google.gson.Gson;
import prerna.sablecc2.om.task.options.TaskOptions;
import prerna.util.gson.GsonUtility;
public class PixelList implements Iterable {
private static final Logger logger = LogManager.getLogger(PixelList.class);
// private AtomicInteger counter = new AtomicInteger(0);
private List pixelList = null;
private transient Map idToIndexHash = null;
// frameName -> list of pixels
private transient Map> frameDependency = null;
// panel -> layer -> list of pixels
private transient Map>> taskDependency = null;
public PixelList() {
this(500);
}
public PixelList(int capacity) {
// keeping the initial capacity initially at 500
capacity = capacity > 500 ? capacity : 500;
this.pixelList = new Vector<>(capacity);
this.idToIndexHash = new ConcurrentHashMap<>(capacity);
int subCapacity = capacity / 10;
this.frameDependency = new ConcurrentHashMap<>(subCapacity);
this.taskDependency = new ConcurrentHashMap<>(subCapacity);
}
/**
* Add a pixel directly to the list
* @param p
*/
public void addPixel(Pixel p) {
pixelList.add(p);
idToIndexHash.put(p.getId(), this.pixelList.size()-1);
syncLastPixel();
// increment the counter
// counter.getAndIncrement();
}
public void syncLastPixel() {
// now store the frame dependency
Pixel p = pixelList.get(pixelList.size()-1);
Pixel pixelToUtilize = p;
// TODO: should keep track of which steps are code execution
// so we dont loop through all the pixels before trying to consolidate
// can we consolidate the pixels?
if(p.isCodeExecution() && p.isUserScript() && pixelList.size()>1) {
// will try to consolidate even if there are visualization pixels
// that are not data based
int counter = 2;
LAST_DATA_LOOP : while(pixelList.size() - counter >= 0) {
Pixel prevPixel = pixelList.get(pixelList.size()-counter);
counter++;
if(prevPixel.isCodeExecution() && prevPixel.isUserScript()
&& p.getLanguage() == prevPixel.getLanguage()
&& ( p.getLanguage() == Variable.LANGUAGE.R
|| p.getLanguage() == Variable.LANGUAGE.PYTHON )
) {
// we can combine these!
String prevCodeExecution = prevPixel.getCodeExecuted();
String newCodeExecution = p.getCodeExecuted();
String combined = prevCodeExecution + "\n" + newCodeExecution;
StringBuilder newPixelString = new StringBuilder();
if(p.getLanguage() == Variable.LANGUAGE.R) {
newPixelString.append("R(\"");
} else {
newPixelString.append("Py(\"");
}
newPixelString.append(combined);
newPixelString.append(" \");");
prevPixel.setPixelString(newPixelString.toString());
prevPixel.setCodeDetails(true, combined, prevPixel.getLanguage(), true);
// now remove p from the list
pixelList.remove(pixelList.size()-1);
// and change the pixelToUtilize reference
pixelToUtilize = prevPixel;
break LAST_DATA_LOOP;
}
// if this is a data operation
// we need to break out of this loop
if(prevPixel.isDataOperation()) {
break LAST_DATA_LOOP;
}
}
}
// store frame output
Set frameOutputs = p.getFrameOutputs();
for(String frameName : frameOutputs) {
if(frameDependency.containsKey(frameName)) {
frameDependency.get(frameName).addLast(pixelToUtilize);
} else {
LinkedList dll = new LinkedList<>();
dll.addFirst(p);
frameDependency.put(frameName, dll);
}
}
// now we also want to keep track of querying / filtering
Set frameInputs = p.getFrameInputs();
for(String frameName : frameInputs) {
// do not double count..
if(!frameOutputs.contains(frameName)) {
if(frameDependency.containsKey(frameName)) {
// make sure we are not adding the same reference twice
if(frameDependency.get(frameName).getLast() != pixelToUtilize) {
frameDependency.get(frameName).addLast(pixelToUtilize);
}
} else {
logger.info("Super weird... this is a frame input for pixel but doesn't exist as a previous frame output");
LinkedList dll = new LinkedList<>();
dll.addFirst(pixelToUtilize);
frameDependency.put(frameName, dll);
}
}
}
// we also want to keep track of the tasks on each panel
List listTaskOptions = p.getTaskOptions();
for(TaskOptions taskOption : listTaskOptions) {
Set panelIds = taskOption.getPanelIds();
for(String panelId : panelIds) {
String layerId = taskOption.getPanelLayerId(panelId);
if(layerId == null) {
layerId = "0";
}
Map> panelMap = null;
if(taskDependency.containsKey(panelId)) {
panelMap = taskDependency.get(panelId);
} else {
panelMap = new ConcurrentHashMap<>();
taskDependency.put(panelId, panelMap);
}
if(panelMap.containsKey(layerId)) {
// make sure we are not adding the same reference twice
if(panelMap.get(layerId).getLast() != pixelToUtilize) {
panelMap.get(layerId).addLast(pixelToUtilize);
}
} else {
LinkedList dll = new LinkedList<>();
dll.addFirst(pixelToUtilize);
panelMap.put(layerId, dll);
}
}
}
}
/**
* Add a pixel step to the recipe
* @param pixelString
* @return
*/
public Pixel addPixel(String pixelString) {
List pixelRecipe = new Vector<>();
pixelRecipe.add(pixelString);
List pList = addPixel(pixelRecipe);
return pList.get(0);
}
/**
* Add the pixel at a specific location in the recipe
* @param index
* @param pixelString
* @return
*/
public Pixel addPixel(int index, String pixelString) {
List pixelRecipe = new Vector<>();
pixelRecipe.add(pixelString);
List pList = addPixel(pixelRecipe);
return pList.get(0);
}
/**
* Add a list of pixel steps to the recipe
* @param pixelRecipe
* @return
*/
public List addPixel(List pixelRecipe) {
synchronized(this) {
List subset = new Vector<>();
for(int i = 0; i < pixelRecipe.size(); i++) {
int intVal = this.pixelList.size();//counter.getAndIncrement();
String uid = intVal + "";// + "__" + UUID.randomUUID().toString();
Pixel pixel = new Pixel(uid, pixelRecipe.get(i));
this.addPixel(pixel);
// return the pixel object that was added
subset.add(pixel);
}
return subset;
}
}
public Pixel get(int index) {
return pixelList.get(index);
}
public List getPixelRecipe() {
List pixelRecipe = new Vector<>(pixelList.size());
for(Pixel p : pixelList) {
pixelRecipe.add(p.getPixelString());
}
return pixelRecipe;
}
public List getPixelIds() {
List pixelIds = new Vector<>(pixelList.size());
for(Pixel p : pixelList) {
pixelIds.add(p.getId());
}
return pixelIds;
}
public List getNonMetaPixelIds() {
List pixelIds = new Vector<>(pixelList.size());
for(Pixel p : pixelList) {
if(!p.isMeta()) {
pixelIds.add(p.getId());
}
}
return pixelIds;
}
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy