All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prerna.algorithm.impl.specific.tap.SysOptUtilityMethods Maven / Gradle / Ivy

There is a newer version: 4.3.1
Show newest version
/*******************************************************************************
 * Copyright 2015 Defense Health Agency (DHA)
 *
 * If your use of this software does not include any GPLv2 components:
 * 	Licensed 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.
 * ----------------------------------------------------------------------------
 * If your use of this software includes any GPLv2 components:
 * 	This program is free software; you can redistribute it and/or
 * 	modify it under the terms of the GNU General Public License
 * 	as published by the Free Software Foundation; either version 2
 * 	of the License, or (at your option) any later version.
 *
 * 	This program is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU General Public License for more details.
 *******************************************************************************/
package prerna.algorithm.impl.specific.tap;

import java.util.ArrayList;

import prerna.engine.api.IEngine;
import prerna.engine.api.ISelectStatement;
import prerna.engine.api.ISelectWrapper;
import prerna.rdf.engine.wrappers.WrapperManager;
import prerna.util.Utility;

public final class SysOptUtilityMethods {

	
	public static String makeBindingString(String type,ArrayList vals) {
		String bindings = "";
		for(String val : vals)
		{
			bindings+= "()";
		}
		return bindings;
	}
	
	/**
	 * Runs a query on a specific engine to make a list of systems to report on
	 * @param engineName 	String containing the name of the database engine to be queried
	 * @param query 		String containing the SPARQL query to run
	 */
	public static Object runSingleResultQuery(IEngine engine, String query){
		if(!query.isEmpty()) {
			ISelectWrapper wrapper = null;
			try {
				wrapper = WrapperManager.getInstance().getSWrapper(engine, query);
				String[] names = wrapper.getVariables();
				while (wrapper.hasNext()) {
					ISelectStatement sjss = wrapper.next();
					return sjss.getVar(names[0]);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if(wrapper != null) {
					wrapper.cleanUp();
				}
			}
		}
		return null;
	}
	
	/**
	 * Runs a query on a specific engine to make a list of systems to report on
	 * @param engineName 	String containing the name of the database engine to be queried
	 * @param query 		String containing the SPARQL query to run
	 */
	public static ArrayList runListQuery(IEngine engine, String query){
		ArrayList list = new ArrayList();
		if(!query.isEmpty()) {
			ISelectWrapper wrapper = null;
			try {
				wrapper = WrapperManager.getInstance().getSWrapper(engine, query);
				String[] names = wrapper.getVariables();
				while (wrapper.hasNext()) {
					ISelectStatement sjss = wrapper.next();
					list.add((String) sjss.getVar(names[0]));
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if(wrapper != null) {
					wrapper.cleanUp();
				}
			}
		}
		return list;
	}
	
//	/** TODO remove
//	 * Gets the list of all capabilities for a selected functional area
//	 * @param sparqlQuery 		String containing the query to get all capabilities for a selected functional area
//	 * @return capabilities		Vector containing list of all capabilities for a selected functional area
//	 */
//	public static Vector getList(IEngine engine, String type, String sparqlQuery)
//	{
//		Vector retList=new Vector();
//		try{
//			EntityFiller filler = new EntityFiller();
//			filler.engineName = engine.getEngineName();
//			filler.type = type;
//			filler.setExternalQuery(sparqlQuery);
//			filler.run();
//			Vector names = filler.nameVector;
//			for (int i = 0;i runQuery (IEngine engine, String query){
		ArrayList list = new ArrayList();
		ISelectWrapper wrapper = Utility.processQuery(engine, query);

		String[] names = wrapper.getVariables();
		int numVals = names.length;
		while (wrapper.hasNext()) {
			ISelectStatement statement = wrapper.next();
			Object[] row = new Object[numVals];
			int i = 0;
			for(i = 0; i rowNames,ArrayList colNames) {
		ISelectWrapper wrapper = null;
		try {
			wrapper = WrapperManager.getInstance().getSWrapper(engine, query);
			// get the bindings from it
			String[] names = wrapper.getVariables();
			// now get the bindings and generate the data
			while(wrapper.hasNext())
			{
				ISelectStatement sjss = wrapper.next();
				Object rowName = sjss.getVar(names[0]);
				Object colName = sjss.getVar(names[1]);

				int rowIndex = rowNames.indexOf(rowName);
				if(rowIndex>-1)
				{
					int colIndex = colNames.indexOf(colName);
					if(colIndex>-1)
					{
						matrix[rowIndex][colIndex] = 1;
					}
				}

			}
		} catch (Exception e1) {
			e1.printStackTrace();
		} finally {
			if(wrapper != null) {
				wrapper.cleanUp();
			}
		}

		return matrix;
	}

	
	public static ArrayList inBothLists(ArrayList list1,ArrayList list2) {
		ArrayList duplicates = new ArrayList();
		
		int size1 = list1.size();
		for(int i=0; i removeDuplicates(ArrayList list) {
		ArrayList retList = new ArrayList();
		for (String entry : list) {
			if (!retList.contains(entry))
				retList.add(entry);
		}
		return retList;
	}
	
	public static ArrayList deepCopy(ArrayList list)
	{
		ArrayList retList = new ArrayList();
		for(String element : list)
		{
			retList.add(element);
		}
		return retList;
	}
	

	public static String convertToString(ArrayList list)
	{
		String printString = "";
		for(String entry : list) {
			printString += entry+", ";
		}
		return printString;
	}
	

	public static String convertToStringIfZero(ArrayList sysList,double[] listToCheck)
	{
		String ret = "";
		for(int i=0;i convertToStringIfNonZero(ArrayList sysList,int[] listToCheck)
	{
		ArrayList nonZeroList = new ArrayList();
		
		for(int i=0;i0)
				nonZeroList.add(sysList.get(i));
		return nonZeroList;
	}	

	public static int[][] createEmptyIntMatrix(int row,int col) {
		int[][] matrix = new int[row][col];
		for(int x=0;x