
prerna.om.OldInsight Maven / Gradle / Ivy
The newest version!
package prerna.om;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.h2.jdbc.JdbcClob;
import org.openrdf.model.Literal;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParseException;
import org.openrdf.sail.inferencer.fc.ForwardChainingRDFSInferencer;
import org.openrdf.sail.memory.MemoryStore;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import prerna.ds.QueryStruct;
import prerna.ds.TinkerFrame;
import prerna.engine.api.IDatabaseEngine;
import prerna.engine.api.ISelectStatement;
import prerna.engine.api.ISelectWrapper;
import prerna.engine.impl.rdf.InMemorySesameEngine;
import prerna.masterdatabase.utility.MasterDatabaseUtility;
import prerna.project.api.IProject;
import prerna.rdf.engine.wrappers.WrapperManager;
import prerna.rdf.query.builder.QueryBuilderData;
import prerna.ui.components.api.IPlaySheet;
import prerna.ui.components.playsheets.datamakers.DataMakerComponent;
import prerna.ui.components.playsheets.datamakers.FilterTransformation;
import prerna.ui.components.playsheets.datamakers.IDataMaker;
import prerna.ui.components.playsheets.datamakers.ISEMOSSAction;
import prerna.ui.components.playsheets.datamakers.ISEMOSSTransformation;
import prerna.ui.components.playsheets.datamakers.JoinTransformation;
import prerna.ui.components.playsheets.datamakers.PKQLTransformation;
import prerna.util.Constants;
import prerna.util.Utility;
@Deprecated
public class OldInsight extends Insight {
/*
* THIS IS FOR LEGACY INSIGHTS
* IF YOU NEED TO USE THIS CLASS...
* YOU ARE PROBABLY DOING SOMETHING WRONG
*/
private static final Logger logger = LogManager.getLogger(OldInsight.class);
public static final transient String COMP = "Component";
public static final transient String POST_TRANS = "PostTrans";
public static final transient String PRE_TRANS = "PreTrans";
public static final transient String ACTION = "Action";
@Deprecated
private String engineId;
@Deprecated
private String engineName;
private transient IDatabaseEngine mainEngine;
private transient IProject mainProject; // the main engine where the insight is stored
private transient IDatabaseEngine makeupEngine; // the in-memory engine created to store the data maker components and transformations for the insight
private transient IPlaySheet playSheet; // the playsheet for the insight
private transient Map dataTableAlign; // the data table align for the insight corresponding to the playsheet
private transient Gson gson = new Gson();
private transient Boolean append = false; // currently used to distinguish when performing overlay in gdm data maker
private transient String dataMakerName; // the name of the data maker
private transient List dmComponents; // the list of data maker components in order for creation of insight
private transient Map> paramHash; // the parameters selected by user for filtering on insights
private transient List insightParameters; // the SEMOSSParam objects for the insight
private String uiOptions;
protected String layout;
private transient IDataMaker dataMaker; // defines how to make the data for the insight
public OldInsight() {
}
/**
* Constructor for the insight
* REQUIRES PROJECT AND APP TO HAVE SAME ID
* @param mainEngine The main engine which holds the insight
* @param dataMakerName The name of the data maker
* @param layout The layout to view the insight
*/
public OldInsight(IDatabaseEngine mainEngine, String dataMakerName, String layout){
this.mainEngine = mainEngine;
// NEED THE ID TO BE THE SAME IN THIS SITUATION!!!
// the main engine has the same id as the main project
this.mainProject = Utility.getProject(mainEngine.getEngineId());
this.dataMakerName = dataMakerName;
this.layout = layout;
}
/**
* Constructor for the insight
* REQUIRES PROJECT AND APP TO HAVE SAME ID
* @param mainProject The main project which holds the insight
* @param dataMakerName The name of the data maker
* @param layout The layout to view the insight
*/
public OldInsight(IProject mainProject, String dataMakerName, String layout){
this.mainProject = mainProject;
// NEED THE ID TO BE THE SAME IN THIS SITUATION!!!
// the main engine has the same id as the main project
this.mainEngine = Utility.getDatabase(mainProject.getProjectId());
this.dataMakerName = dataMakerName;
this.layout = layout;
}
public OldInsight(String dataMakerName, String layout) {
this.dataMakerName = dataMakerName;
this.layout = layout;
}
/**
* Exposed another constructor for creating insights to be stored in some MHS custom playsheets
* @param playsheet
*/
public OldInsight(IPlaySheet playsheet){
this.playSheet = playsheet;
}
/**
* Setter for boolean to see if the insight is an append for gdm
* @param append
*/
public void setAppend(Boolean append){
this.append = append;
}
/**
* Getter for boolean to see if the insight is an append for gpm
* @return
*/
public Boolean getAppend(){
return this.append;
}
/**
* Takes the input stream for the N-Triples string to create the insight makeup database
* @param insightMakeup
*/
public void setMakeup(InputStream insightMakeup) {
if(insightMakeup != null){
this.makeupEngine = createMakeupEngine(insightMakeup);
} else{
logger.error("Invalid insight. No insight makeup available");
}
}
/**
* Takes the input stream for the N-Triples string to create the insight makeup database
* @param insightMakeup
*/
public void setMakeup(String insightMakeup) {
if(insightMakeup != null){
this.makeupEngine = createMakeupEngine(insightMakeup);
} else{
logger.error("Invalid insight. No insight makeup available");
}
}
/**
* Sets the parameters the user has selected for the insight
* @param paramHash
*/
public void setParamHash(Map> paramHash){
this.paramHash = paramHash;
}
/**
* Gets the makeup engine that contains the insight makeup
* @return
*/
public IDatabaseEngine getMakeupEngine() {
return this.makeupEngine;
}
/**
* Append the parameter filters to the correct component
* Each parameter stores the component and filter number that it is affecting
*/
public void appendParamsToDataMakerComponents() {
if(insightParameters != null) {
for(int i = 0; i < insightParameters.size(); i++) {
SEMOSSParam p = insightParameters.get(i);
String pName = p.getName();
String componentFilterId = p.getComponentFilterId();
String[] split = componentFilterId.split(":");
int compNum = Integer.parseInt(split[0].replace(COMP, ""));
int filterNum = Integer.parseInt(split[1].replace(PRE_TRANS, ""));
Map> newPHash = new Hashtable>();
newPHash.put(pName, paramHash.get(pName));
// logic inside setParamHash in each component to append this information to a filter pre-transformation
// the filter transformation either appends it to the metamodel or fills a query
dmComponents.get(compNum).setParamHash(newPHash, filterNum);
}
}
}
/**
* Getter for the selected parameter values
* @return
*/
public Map> getParamHash() {
return this.paramHash;
}
/**
* Setter for the SEMOSSParam objects for the insight
* @param insightParameters
*/
public void setInsightParameters(List insightParameters) {
this.insightParameters = insightParameters;
}
/**
* Getter for the SEMOSSParam objects for the insight
* @return
*/
public List getInsightParameters() {
return this.insightParameters;
}
/**
* Generates an in-memory database based on the N-Triples makeup input stream for the insight
* @param nTriples The inputstream holding the N-Triples string
* @return
*/
public InMemorySesameEngine createMakeupEngine(String nTriples) {
return createMakeupEngine(new ByteArrayInputStream(nTriples.getBytes()));
}
/**
* Generates an in-memory database based on the N-Triples makeup input stream for the insight
* @param nTriples The inputstream holding the N-Triples string
* @return
*/
public InMemorySesameEngine createMakeupEngine(InputStream nTriples)
{
// generate the rc
RepositoryConnection rc = null;
try {
Repository myRepository = new SailRepository(new ForwardChainingRDFSInferencer(new MemoryStore()));
myRepository.initialize();
rc = myRepository.getConnection();
rc.add(nTriples, "semoss.org", RDFFormat.NTRIPLES);
} catch(RuntimeException ignored) {
logger.error(Constants.STACKTRACE, ignored);
} catch (RDFParseException rpe) {
logger.error(Constants.STACKTRACE, rpe);
} catch (RepositoryException re) {
logger.error(Constants.STACKTRACE, re);
} catch (IOException e) {
logger.error(Constants.STACKTRACE, e);
}
// set the rc in the in-memory engine
InMemorySesameEngine myEng = new InMemorySesameEngine();
myEng.setRepositoryConnection(rc);
return myEng;
}
/**
* Process the make-up engine storing the -Triples information to get the data maker components and transformations
* @param makeupEng
* @return
*/
public List digestNTriples(IDatabaseEngine makeupEng){
logger.debug("Creating data component array from makeup engine");
List dmCompVec = new Vector<>();
String countQuery = "SELECT (COUNT(DISTINCT(?Component)) AS ?Count) WHERE {?Component a . BIND('x' AS ?x) } GROUP BY ?x";
ISelectWrapper countss = WrapperManager.getInstance().getSWrapper(makeupEng, countQuery);
Integer total = 0;
while(countss.hasNext()){
ISelectStatement countst = countss.next();
total = (int) Double.parseDouble(countst.getVar("Count")+"");
logger.debug(" THERE ARE " + total + " COMPONENTS IN THIS INSIGHT ");
}
//TODO: need to make sure preTrans, postTrans, and actions are all ordered
String theQuery = "SELECT ?Component ?Engine ?Query ?Metamodel ?DataMakerType ?PreTrans ?PostTrans ?Actions WHERE { {?Component a } {?EngineURI a } {?EngineURI ?Engine } {?Component ?EngineURI} OPTIONAL {?Component ?Query} OPTIONAL {?Component ?Metamodel} {?Component ?Order} OPTIONAL {?Component ?PreTrans} OPTIONAL {?Component ?PostTrans} OPTIONAL {?Component ?Actions} } ORDER BY ?Order";
int idx = -1;
ISelectWrapper ss = WrapperManager.getInstance().getSWrapper(makeupEng, theQuery);
String curComponent = null;
// Keeping a set of pre/post/action's that exist on each component such that they are not added twice when number of pre/post/actions is not the same
Set preTransSet = new HashSet<>();
Set postTransSet = new HashSet<>();
Set actionSet = new HashSet<>();
while(ss.hasNext()){
ISelectStatement st = ss.next();
String component = st.getVar("Component")+"";
String newComponent = COMP + component;
if(!newComponent.equals(curComponent)){
String engine = st.getVar("Engine")+"";
engine = MasterDatabaseUtility.testDatabaseIdIfAlias(engine);
String query = st.getVar("Query")+"";
String metamodelString = st.getVar("Metamodel")+"";
logger.debug(engine + " ::::::: " + component +" ::::::::: " + query + " :::::::::: " + metamodelString);
DataMakerComponent dmc = null;
// old insights store information in a query string while new insights store the metamodel information to construct the query
if (!query.isEmpty()) {
dmc = new DataMakerComponent(engine, query);
dmCompVec.add(dmc);
}
else if (!metamodelString.isEmpty()){
logger.info("trying to get QueryBuilderData object");
QueryBuilderData metamodelData = gson.fromJson(metamodelString, QueryBuilderData.class);
QueryStruct qsData = null;
if(metamodelData.getRelTriples() == null){
qsData = gson.fromJson(metamodelString, QueryStruct.class);
if(qsData.getSelectors() == null){
logger.info("failed to get QueryBuilderData.... this must be a legacy insight with metamodel data. Setting metamodel data into QueryBuilderData");
Hashtable dataHash = gson.fromJson(metamodelString, new TypeToken>() {}.getType());
metamodelData = new QueryBuilderData(dataHash);
}
}
if(qsData == null) {
qsData = metamodelData.getQueryStruct(true);
}
dmc = new DataMakerComponent(engine, qsData);
dmCompVec.add(dmc);
}
curComponent = COMP + component;
if (dmc != null) {
dmc.setId(curComponent);
}
idx++;
}
Object preTransURI = st.getRawVar("PreTrans");
Object postTransURI = st.getRawVar("PostTrans");
Object actionsURI = st.getRawVar("Actions");
// run queries to get information on each transformation/action and append to dmc list
if(preTransURI!=null && !preTransSet.contains(preTransURI + "")){
preTransSet.add(preTransURI + "");
addPreTrans(dmCompVec.get(idx), makeupEng, preTransURI, curComponent);
}
if(postTransURI!=null && !postTransSet.contains(postTransURI + "")){
postTransSet.add(postTransURI + "");
addPostTrans(dmCompVec.get(idx), makeupEng, postTransURI, curComponent);
}
if(actionsURI!=null && !actionSet.contains(actionsURI + "")) {
actionSet.add(actionsURI + "");
addAction(dmCompVec.get(idx), makeupEng, actionsURI, curComponent);
}
}
return dmCompVec;
}
/**
* Add PreTransformation to the DataMakerComponent
* @param dmc The DataMakerComponent to add the preTransformation
* @param makeupEng The makeupEngine containing the N-Triples information regarding the insight
* @param preTrans The preTransformation URI
* @param compId The name of the component to make sure the preTransforamtion has the correct id
*/
private void addPreTrans(DataMakerComponent dmc, IDatabaseEngine makeupEng, Object preTrans, String compId){
logger.info("adding pre trans :::: " + preTrans);
Map props = getProperties(preTrans+"", makeupEng);
String type = props.get(ISEMOSSTransformation.TYPE) + "";
logger.info("TRANS TYPE IS " + Utility.cleanLogString(type));
ISEMOSSTransformation trans = Utility.getTransformation(this.mainEngine, type);
logger.info("pre trans properties :::: " + Utility.cleanLogString(props.toString()));
trans.setProperties(props);
trans.setId(compId + ":" + PRE_TRANS + Utility.getInstanceName(preTrans + ""));
dmc.addPreTrans(trans);
}
/**
* Add PostTransformation to the DataMakerComponent
* @param dmc The DataMakerComponent to add the preTransformation
* @param makeupEng The makeupEngine containing the N-Triples information regarding the insight
* @param postTrans The postTransformation URI
* @param compId The name of the component to make sure the postTransformation has the correct id
*/
private void addPostTrans(DataMakerComponent dmc, IDatabaseEngine makeupEng, Object postTrans, String compId){
logger.info("adding post trans :::: " + postTrans);
Map props = getProperties(postTrans+"", makeupEng);
String type = props.get(ISEMOSSTransformation.TYPE) + "";
logger.info("TRANS TYPE IS " + Utility.cleanLogString(type));
ISEMOSSTransformation trans = Utility.getTransformation(this.mainEngine, type);
logger.info("post trans properties :::: " + Utility.cleanLogString(props.toString()));
trans.setProperties(props);
trans.setId(compId + ":" + POST_TRANS + Utility.getInstanceName(postTrans + ""));
dmc.addPostTrans(trans);
}
/**
* Add Action to the DataMakerComponent
* @param dmc The DataMakerComponent to add the preTransformation
* @param makeupEng The makeupEngine containing the N-Triples information regarding the insight
* @param preTrans The Action URI
* @param compId The name of the component to make sure the Action has the correct id
*/
private void addAction(DataMakerComponent dmc, IDatabaseEngine makeupEng, Object action, String compId){
logger.info("adding action :::: " + action);
Map props = getProperties(action+"", makeupEng);
String type = props.get(ISEMOSSAction.TYPE) + "";
logger.info("TRANS TYPE IS " + type);
ISEMOSSAction actionObj = Utility.getAction(this.mainEngine, type);
logger.info("action properties :::: " + Utility.cleanLogString(props.toString()));
actionObj.setProperties(props);
actionObj.setId(compId + ":" + ACTION + Utility.getInstanceName(action + ""));
dmc.addAction(actionObj);
}
/**
* Get the properties associated with a transformation or an action
* @param uri The URI for the transformation or action
* @param makeupEng The makeupEngine containing the N-Triples information regarding the insight
* @return
*/
private Map getProperties(String uri, IDatabaseEngine makeupEng){
String propQuery = "SELECT ?Value WHERE { BIND(<" +
uri +
"> AS ?obj) {?obj ?Value}}";
logger.info("Running query to get properties: " + Utility.cleanLogString(propQuery));
ISelectWrapper wrap = WrapperManager.getInstance().getSWrapper(makeupEng, propQuery);
Map retMap = new HashMap<>();
if(wrap.hasNext()){ // there should only be one prop map associated with each transformation or action
ISelectStatement ss = wrap.next();
String jsonPropMap = ss.getVar("Value") + "";
logger.info(Utility.cleanLogString(jsonPropMap));
retMap = gson.fromJson(jsonPropMap, Map.class);
}
if(wrap.hasNext()){
logger.error("More than one prop map has shown up for uri ::::: " + Utility.cleanLogString(uri));
logger.error("Need to find reason why/how it was stored this way...");
}
return retMap;
}
/**
* Get the data maker object from the dataMakerString
* If cannot find specified dataMaker, get the playSheet and see if is supposed to be the data maker
* @return
*/
@Override
public IDataMaker getDataMaker() {
if(this.dataMaker == null){
if(this.dataMakerName != null && !this.dataMakerName.isEmpty()) {
this.dataMaker = Utility.getDataMaker(this.mainEngine, this.dataMakerName);
} else {
if(this.playSheet == null){
this.playSheet = getPlaySheet();
}
if(this.playSheet != null) {
if (this.playSheet instanceof IDataMaker){
this.dataMaker = (IDataMaker) this.playSheet;
}
else {
this.dataMaker = this.playSheet.getDefaultDataMaker();
this.playSheet.setDataMaker(this.dataMaker);
}
}
}
this.dataMaker.setUserId(this.getUserId());
}
return this.dataMaker;
}
/**
* Setter for the data maker
* @param dataMaker
*/
@Override
public void setDataMaker(IDataMaker dataMaker) {
this.dataMaker = dataMaker;
if(this.dataMaker.getUserId() == null) {
this.dataMaker.setUserId(this.getUserId());
}
this.dataMakerName = dataMaker.getDataMakerName();
}
/**
* Getter for the DataMakerComponents
* To prevent unnecessary creation of the dmComponents list, we call digestNTriples if it is null
* @return
*/
public List getDataMakerComponents() {
if(this.dmComponents == null && this.makeupEngine != null){
this.dmComponents = digestNTriples(this.makeupEngine);
} else if(this.dmComponents == null) {
this.dmComponents = new Vector<>();
}
return this.dmComponents;
}
/**
* Setter for the DataMakerComponents of the insight
* @param dmComponents
*/
public void setDataMakerComponents(Vector dmComponents) {
this.dmComponents = dmComponents;
}
/**
* Getter for the data table align used for view
* @return
*/
public Map getDataTableAlign() {
return this.dataTableAlign;
}
/**
* Setter for the data table align used for the view
* Primary used when no data table align exists and need to get it based on the playsheet
* @param dataTableAlign
*/
public void setDataTableAlign(Map dataTableAlign) {
this.dataTableAlign = dataTableAlign;
}
/**
* Setter for the data table align used for the view
* Primarily used when data table align is coming from rdbms insight
* @param dataTableAlignJSON
*/
public void setDataTableAlign(String dataTableAlignJSON) {
if(dataTableAlignJSON != null && !dataTableAlignJSON.isEmpty()){
logger.info("Setting json dataTableAlign " + Utility.cleanLogString(dataTableAlignJSON));
this.dataTableAlign = gson.fromJson(dataTableAlignJSON, Map.class);
} else {
logger.info("data table align is empty");
}
}
/**
* Gets the playsheet based on the layout string in the insight rdbms
* @return
*/
public IPlaySheet getPlaySheet(){
if(this.playSheet == null){
if(this.dataMaker != null && this.dataMaker instanceof IPlaySheet){
return (IPlaySheet) this.dataMaker;
}
this.playSheet = Utility.getPlaySheet(this.mainEngine, this.layout);
if(playSheet != null){
// to keep playsheet ID and insight ID in sync
this.playSheet.setQuestionID(this.insightId);
this.playSheet.setDataMaker(getDataMaker());
}
else {
logger.error("Broken insight... cannot get playsheet :: " + Utility.cleanLogString(this.layout));
}
}
return this.playSheet;
}
/**
* Setter for the playsheet of the insight
* @param playSheet
*/
public void setPlaySheet(IPlaySheet playSheet){
this.playSheet = playSheet;
if(this.playSheet != null) {
// to keep playsheet ID and insight ID in sync
this.playSheet.setQuestionID(insightId);
}
}
/**
* Getter for the data maker name
* @return
*/
@Override
public String getDataMakerName() {
if(this.dataMaker == null) {
return this.dataMakerName;
} else {
return this.dataMaker.getClass().getSimpleName();
}
}
public String getOutput() {
return this.layout;
}
public void setOutput(String output) {
this.layout = output;
}
/**
* Process a data maker component on the insight
* @param component
*/
public void processDataMakerComponent(DataMakerComponent component) {
int lastComponent = this.getDataMakerComponents().size();
String compId = COMP + lastComponent;
component.setId(compId);
List preTrans = component.getPreTrans();
for(int i = 0; i < preTrans.size(); i++) {
preTrans.get(i).setId(compId + ":" + PRE_TRANS + i);
}
List postTrans = component.getPostTrans();
for(int i = 0; i < postTrans.size(); i++) {
postTrans.get(i).setId(compId + ":" + POST_TRANS + i);
}
List actions = component.getActions();
for(int i = 0; i < actions.size(); i++) {
actions.get(i).setId(compId + ":" + ACTION + i);
}
DataMakerComponent componentCopy = component.copy();
getDataMaker().processDataMakerComponent(componentCopy);
getDataMakerComponents().add(component);
}
/**
* Process a list of post transformation on the last data maker component stored in cmComponents
* @param postTrans The list of post transformation to run
* @param dataMaker Additional dataMakers if required by the transformation
*/
public void processPostTransformation(List postTrans, IDataMaker... dataMaker) throws RuntimeException {
DataMakerComponent dmc = getLastComponent();
List postTransCopy = new Vector(postTrans.size());
for(ISEMOSSTransformation trans : postTrans) {
postTransCopy.add(trans.copy());
}
getDataMaker().processPostTransformations(dmc, postTransCopy, dataMaker);
int lastPostTrans = dmc.getPostTrans().size() - 1;
for(int i = 0; i < postTrans.size(); i++) {
postTrans.get(i).setId(dmc.getId() + ":" + POST_TRANS + (++lastPostTrans));
dmc.addPostTrans(postTrans.get(i));
}
}
public DataMakerComponent getLastComponent() {
if(getDataMakerComponents().isEmpty()){
DataMakerComponent empty = new DataMakerComponent(Constants.LOCAL_MASTER_DB, Constants.EMPTY);
this.dmComponents.add(empty);
}
return getDataMakerComponents().get(this.dmComponents.size() - 1);
}
// /**
// * Process a list of actions on the last data maker component stored in cmComponents
// * @param postTrans The list of actions to run
// * @param dataMaker Additional dataMakers if required by the actions
// */
// public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy