1 package iotruntime.master;
3 import iotruntime.slave.IoTRelation;
5 import iotinstaller.MySQLInterface;
6 import iotinstaller.TableProperty;
7 import iotinstaller.TableSet;
8 import iotinstaller.TableRelation;
11 import java.util.Collection;
12 import java.util.Iterator;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Properties;
18 import java.lang.Class;
19 import java.lang.Integer;
20 import java.lang.reflect.*;
22 /** Class RelationInstrumenter 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,
26 * e.g. IoTRelation<ProximitySensor, LightBulb>
27 * Upon extracting information, this class can be used to create
28 * an IoTRelation object that contains a list of objects from
29 * the Relation declaration.
31 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
35 public class RelationInstrumenter {
38 * RelationInstrumenter class properties
40 private String[][] arrRelation;
41 private String[][] arrRelOther;
42 private HashMap<String, String> hmEntryTypes;
43 private TableRelation tbl;
46 private int iColsOther;
47 private String strRelationEntityName;
48 private String strRelationOtherName;
49 private boolean bVerbose;
52 * RelationInstrumenter class constants
54 private final String STR_PACKAGE_PREFIX = "iotcode.";
55 private final String STR_FIELD_ID_NAME = "ID";
58 * Class constructor #1
60 * @param strRelEntName String that contains the IoTRelation entity name in the DB, e.g. ProximitySensor
61 * @param strRelOthName String that contains the other IoTRelation entity name in the DB, e.g. LightBulb
62 * @param _bVerbose Verboseness of runtime output
64 public RelationInstrumenter(String strRelEntName, String strRelOthName, boolean _bVerbose) {
68 strRelationEntityName = strRelEntName;
69 strRelationOtherName = strRelOthName;
70 tbl = new TableRelation(strRelationEntityName, strRelationOtherName, _bVerbose);
71 iRows = tbl.getNumOfRows();
75 RuntimeOutput.print("RelationInstrumentation: Creating a Relation for "
76 + strRelationEntityName + " and " + strRelationOtherName, bVerbose);
80 * Class constructor #2
82 * @param strRelEntName String that contains the IoTRelation entity name in the DB, e.g. ProximitySensor
83 * @param strRelOthName String that contains the other IoTRelation entity name in the DB, e.g. LightBulb
84 * @param strQueryFileName String name for SQL query config file
85 * @param _bVerbose Verboseness of runtime output
87 public RelationInstrumenter(String strRelEntName, String strRelOthName, String strQueryFileName, boolean _bVerbose) {
91 strRelationEntityName = strRelEntName;
92 strRelationOtherName = strRelOthName;
93 tbl = new TableRelation(strRelationEntityName, strRelationOtherName, _bVerbose);
94 tbl.setTableRelationFromQueryFile(strQueryFileName);
95 tbl.selectRelationEntry();
96 iRows = tbl.getNumOfRows();
100 RuntimeOutput.print("RelationInstrumentation: Creating a Relation for "
101 + strRelationEntityName + " and " + strRelationOtherName, bVerbose);
106 * A method to give the object/table name of the first set
110 public String firstObjectTableName() {
112 return strRelationEntityName;
117 * A method to give the object/table name of the first set
121 public String secondObjectTableName() {
123 return strRelationOtherName;
129 * A method to give the number of columns of the first Set
131 * @param iIndex integer index
134 public int numberOfFirstCols(int iIndex) {
136 tbl.selectRelationOnFirstTable();
137 iCols = tbl.getNumOfCols(iIndex);
142 * A method to give the number of columns of the second Set
144 * @param iIndex integer index
147 public int numberOfSecondCols(int iIndex) {
149 tbl.selectRelationOnOtherTable();
150 iColsOther = tbl.getNumOfCols(iIndex);
155 * A method to give the number of rows
159 public int numberOfRows() {
166 * A method to return the entry field TYPE of the first Set based on ID
170 public String firstEntryFieldType(String sID) {
172 tbl.selectRelationOnFirstTable();
173 hmEntryTypes = tbl.getEntryTypes();
175 // Get the entry type
176 String strEntryType = hmEntryTypes.get(sID);
182 * A method to return the entry field TYPE of the first Set based on ID
186 public String secondEntryFieldType(String sID) {
188 tbl.selectRelationOnOtherTable();
189 hmEntryTypes = tbl.getEntryTypes();
191 // Get the entry type
192 String strEntryType = hmEntryTypes.get(sID);
198 * A method to return the field object ID from the first set entry pointed by certain index
200 * @param iIndex integer index
203 public String firstFieldObjectID(int iIndex) {
205 // Select the first table
206 tbl.selectRelationOnFirstTable();
208 // Get the right entry based on iIndex
209 String[] arrObjectID = tbl.getFieldObjectIDs();
210 String strID = arrObjectID[iIndex];
212 RuntimeOutput.print("RelationInstrumentation: Extracting field object ID from value..", bVerbose);
218 * A method to return the field object ID from the second set entry pointed by certain index
220 * @param iIndex integer index
223 public String secondFieldObjectID(int iIndex) {
225 // Select the second table
226 tbl.selectRelationOnOtherTable();
228 // Get the right entry based on iIndex
229 String[] arrObjectID = tbl.getFieldObjectIDs();
230 String strID = arrObjectID[iIndex];
232 RuntimeOutput.print("RelationInstrumentation: Extracting field object ID from value..", bVerbose);
238 * A method to return the field values of certain index from the first set
240 * @param iIndex integer index
243 public Object[] firstFieldValues(int iIndex) {
245 // Select the first table
246 tbl.selectRelationOnFirstTable();
247 arrRelation = tbl.getDBTable();
248 iCols = tbl.getNumOfCols(iIndex);
250 // Get the right entry based on iIndex
251 String[] arrRelEntry = arrRelation[iIndex];
253 // Fill in the params array with the Objects needed as parameters
254 // for the constructor
255 Object[] arrFirstFieldValues = new Object[iCols];
257 for(int i=0; i<iCols; i++) {
258 // MySQL column starts from 1, NOT 0
259 arrFirstFieldValues[i] = getObjectConverted(arrRelEntry[i], getClassName(tbl.getFieldType(i+1, iIndex)).getName());
262 RuntimeOutput.print("RelationInstrumentation: Extracting field values..", bVerbose);
264 return arrFirstFieldValues;
268 * A method to return the field values of certain index from second Set
270 * @param iIndex integer index
273 public Object[] secondFieldValues(int iIndex) {
275 // Select the second table
276 tbl.selectRelationOnOtherTable();
277 arrRelOther = tbl.getDBTable();
278 iColsOther = tbl.getNumOfCols(iIndex);
280 // Get the right entry based on iIndex
281 String[] arrRelEntry = arrRelOther[iIndex];
283 // Fill in the params array with the Objects needed as parameters
284 // for the constructor
285 Object[] arrSecondFieldValues = new Object[iCols];
287 for(int i=0; i<iCols; i++) {
288 // MySQL column starts from 1, NOT 0
289 arrSecondFieldValues[i] = getObjectConverted(arrRelEntry[i], getClassName(tbl.getFieldType(i+1, iIndex)).getName());
292 RuntimeOutput.print("RelationInstrumentation: Extracting field values..", bVerbose);
294 return arrSecondFieldValues;
298 * A method to return the field classes of a certain index
300 * @param iIndex integer index
303 public Class[] firstFieldClasses(int iIndex) {
305 // Select the first table
306 tbl.selectRelationOnFirstTable();
307 arrRelation = tbl.getDBTable();
308 iCols = tbl.getNumOfCols(iIndex);
310 RuntimeOutput.print("RelationInstrumentation: Extracting table " + strRelationEntityName + ".", bVerbose);
312 Class[] arrFirstFieldClasses = new Class[iCols];
314 // We start from column 1 and we skip column 0
315 // Column 0 is for ID
316 for(int i=0; i<iCols; i++) {
317 // MySQL column starts from 1, NOT 0
318 arrFirstFieldClasses[i] = getClassName(tbl.getFieldType(i+1, iIndex));
321 RuntimeOutput.print("RelationInstrumentation: Extracting field classes from field types..", bVerbose);
322 return arrFirstFieldClasses;
326 * A method to return the field classes
328 * @param iIndex integer index
331 public Class[] secondFieldClasses(int iIndex) {
333 // Select the second table
334 tbl.selectRelationOnOtherTable();
335 arrRelOther = tbl.getDBTable();
336 iCols = tbl.getNumOfCols(iIndex);
338 RuntimeOutput.print("RelationInstrumentation: Extracting table " + strRelationOtherName + ".", bVerbose);
340 Class[] arrSecondFieldClasses = new Class[iCols];
342 // We start from column 1 and we skip column 0
343 // Column 0 is for ID
344 for(int i=0; i<iCols; i++) {
345 // MySQL column starts from 1, NOT 0
346 arrSecondFieldClasses[i] = getClassName(tbl.getFieldType(i+1, iIndex));
349 RuntimeOutput.print("RelationInstrumentation: Extracting field classes from field types..", bVerbose);
350 return arrSecondFieldClasses;
354 * A helper function that gives the Class object of a particular DB data type
356 * @param strDataType String MySQL data type
359 public Class getClassName(String strDataType) {
361 if (strDataType.equals("VARCHAR")) {
363 } else if (strDataType.equals("INT")) {
371 * A helper function that returns an Object in the right format
373 * We give it input from the elements of the HashSet where we
374 * populate all the DB information for a certain Object
376 * @param obj Object to be converted
377 * @param strClassType String Java Class type
380 public Object getObjectConverted(Object obj, String strClassType) {
382 if (strClassType.equals("java.lang.String")) {
384 } else if (strClassType.equals("int")) {
385 return Integer.parseInt((String) obj);