1 package iotruntime.master;
3 import iotruntime.slave.IoTSet;
5 import iotinstaller.MySQLInterface;
6 import iotinstaller.TableProperty;
7 import iotinstaller.TableSet;
11 import java.util.HashMap;
12 import java.util.HashSet;
14 import java.util.Properties;
16 import java.util.Arrays;
18 import java.lang.Class;
19 import java.lang.Integer;
20 import java.lang.reflect.*;
22 /** Class SetInstrumenter helps instrument the bytecode.
23 * This class should extract information from the database
24 * Input is the name of the device/entity extracted from the
25 * generic Set class in the bytecode, e.g. IoTSet<ProximitySensor>.
26 * Upon extracting information, this class can be used to create
27 * an IoTSet object that contains a list of objects from
28 * the Set declaration.
30 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
34 public class SetInstrumenter {
37 * SetInstrumenter class properties
39 private String[][] arrSet;
40 private HashMap<String, String> hmEntryTypes;
44 private String strSetEntityName;
45 private boolean bVerbose;
48 * SetInstrumenter class constants
50 private static final String STR_PACKAGE_PREFIX = "iotcode.";
51 private static final String STR_FIELD_ID_NAME = "ID";
56 * @param strSetEntName String that contains the IoTSet entity name in the DB, e.g. ProximitySensor
57 * @param strQueryFileName String name for SQL query config file
58 * @param strObjectID String ID to select the right device ID in IoTDeviceAddress table
59 * @param _bVerbose Verboseness of runtime output
61 public SetInstrumenter(String strSetEntName, String strQueryFileName, String strObjectID, boolean _bVerbose) {
63 strSetEntityName = strSetEntName;
64 tbl = new TableSet(strSetEntName, _bVerbose);
65 tbl.setTableSetFromQueryFile(strQueryFileName, strObjectID);
67 arrSet = tbl.getDBTable();
68 hmEntryTypes = tbl.getEntryTypes();
69 iRows = tbl.getNumOfRows();
72 RuntimeOutput.print("SetInstrumentation: Creating a Set for " + strSetEntityName, bVerbose);
77 * A method to give the object/table name
81 public String getObjTableName() {
83 return strSetEntityName;
88 * A method to give the number of columns
90 * @param iIndex integer index
93 public int numberOfCols(int iIndex) {
95 iCols = tbl.getNumOfCols(iIndex);
100 * A method to give the number of rows
104 public int numberOfRows() {
111 * A method to return the field object ID from entry pointed by certain index
113 * @param iIndex integer index
116 public String fieldObjectID(int iIndex) {
118 // Get the value of that field
119 String[] arrObjectID = tbl.getFieldObjectIDs();
120 String strID = arrObjectID[iIndex];
122 RuntimeOutput.print("RelationInstrumentation: Extracting field object ID from value..", bVerbose);
128 * A method to return the entry type name from an entry pointed by its ID
130 * @param sID device/entry ID
133 public String fieldEntryType(String sID) {
135 // Get the entry type
136 String strEntryType = hmEntryTypes.get(sID);
138 RuntimeOutput.print("RelationInstrumentation: Extracting entry type from entry..", bVerbose);
144 * A method to return the field values of certain index
146 * @param iIndex integer index
149 public Object[] fieldValues(int iIndex) {
151 iCols = tbl.getNumOfCols(iIndex);
152 // Get the right entry based on iIndex
153 String[] arrSetEntry = arrSet[iIndex];
154 Object[] arrFieldValues = new Object[iCols];
156 for(int i=0; i<iCols; i++) {
157 // MySQL column starts from 1, NOT 0
158 arrFieldValues[i] = getObjectConverted(arrSetEntry[i], getClassName(tbl.getFieldType(i+1, iIndex)).getName());
161 RuntimeOutput.print("SetInstrumentation: Extracting field values..", bVerbose);
163 return arrFieldValues;
167 * A method to return the field classes of certain index
169 * @param iIndex integer index
172 public Class[] fieldClasses(int iIndex) {
174 iCols = tbl.getNumOfCols(iIndex);
175 // Get the right entry set based on iIndex
176 RuntimeOutput.print("SetInstrumentation: Extracting table " + strSetEntityName + ".", bVerbose);
177 Class[] arrFieldClasses = new Class[iCols];
179 for(int i=0; i<iCols; i++) {
180 // MySQL column starts from 1, NOT 0
181 arrFieldClasses[i] = getClassName(tbl.getFieldType(i+1, iIndex));
184 RuntimeOutput.print("SetInstrumentation: Extracting field classes from field types..", bVerbose);
185 return arrFieldClasses;
189 * A helper function that gives the Class object of a particular DB data type
191 * @param strDataType String MySQL data type
194 public Class getClassName(String strDataType) {
196 if (strDataType.equals("VARCHAR")) {
198 } else if (strDataType.equals("INT")) {
206 * A helper function that returns an Object in the right format
208 * We give it input from the elements of the HashSet where we
209 * populate all the DB information for a certain Object
211 * @param obj Object to be converted
212 * @param strClassType String Java Class type
215 public Object getObjectConverted(Object obj, String strClassType) {
217 if (strClassType.equals("java.lang.String")) {
218 // We use String "true" or "false" as booleans in MySQL
219 String strObj = (String) obj;
220 if (strObj.equals("true") || strObj.equals("false")) {
221 // Check if this is a boolean
222 return Boolean.parseBoolean(strObj);
224 // Return just the string if it's not a boolean
227 } else if (strClassType.equals("int")) {
228 return Integer.parseInt((String) obj);