Package gr.ntua.irmos.services.map.impl

Source Code of gr.ntua.irmos.services.map.impl.callOctaveThread

package gr.ntua.irmos.services.map.impl;

import java.rmi.RemoteException;
import java.io.*;
import java.lang.reflect.*;
import java.lang.*;
import org.globus.wsrf.Resource;
import org.globus.wsrf.ResourceProperties;
import org.globus.wsrf.ResourceProperty;
import org.globus.wsrf.ResourcePropertySet;
import org.globus.wsrf.impl.ReflectionResourceProperty;
import org.globus.wsrf.impl.SimpleResourcePropertySet;
import gr.ntua.irmos.stubs.MapService_instance.AddResponse;
import gr.ntua.irmos.stubs.MapService_instance.SubtractResponse;
import gr.ntua.irmos.stubs.MapService_instance.GetValueRP;
import gr.ntua.irmos.stubs.MapService_instance.Estimate;

import java.net.URL;
import java.util.*;
//import java.io.*;
import java.net.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.w3c.dom.NamedNodeMap;
import org.apache.commons.net.ftp.FTPClient;

import dk.ange.octave.*;
import dk.ange.octave.type.*;
import dk.ange.octave.type.OctaveMatrix;
import dk.ange.octave.type.OctaveScalar;

import java.sql.*;

// SOME CONGIFURATION NEEDS TO BE MADE IN THE CODE
// THIS HAS TO DO MAINLY WITH FTP AND MYSQL DETAIL CONNECTIONS, PATHS WHERE MODELS ARE SAVED ON THE SERVER AND DIRECTORY STRUCTURE OF THE FTP SERVER

public class MapService implements Resource, ResourceProperties {

  /* Resource Property set */
  private ResourcePropertySet propSet;

  /* Resource properties */
  private int value;
  private String lastOp;

  /* Constructor. Initializes RPs */
  public MapService() throws RemoteException {
    /* Create RP set */
    this.propSet = new SimpleResourcePropertySet(
        MapQNames.RESOURCE_PROPERTIES);

    /* Initialize the RP's */
    try {
      ResourceProperty valueRP = new ReflectionResourceProperty(
          MapQNames.RP_VALUE, "Value", this);
      this.propSet.add(valueRP);
      setValue(0);

      ResourceProperty lastOpRP = new ReflectionResourceProperty(
          MapQNames.RP_LASTOP, "LastOp", this);
      this.propSet.add(lastOpRP);
      setLastOp("NONE");
    } catch (Exception e) {
      throw new RuntimeException(e.getMessage());
    }
  }

  /* Get/Setters for the RPs */
  public synchronized int getValue() {
    return value;
  }

  public synchronized void setValue(int value) {
    this.value = value;
  }

  public synchronized String getLastOp() {
    return lastOp;
  }

  public synchronized void setLastOp(String lastOp) {
    this.lastOp = lastOp;
  }

  /* Remotely-accessible operations */



  public String estimate (Estimate params) throws RemoteException {
   
     
    try {
     
      //get the ASC ID (for the model retrieval) and the high level input to the model
      String ASC_ID=params.getAscid();
      String HLinput=params.getHlinput();  
      System.out.println("Inside estimation operation...");        
         
      try {
      //Call method for retrieving xml description file (ASCD) based on asc id
      //The ASCD is necessary for retrieving inputs, outputs and ranges of them
      System.out.println("Retrieving xml file....");
      retrieveXML(ASC_ID);
           
      }catch (Exception e1){
      System.out.println("Error with transferring xml file");
      e1.printStackTrace();
    }     
   
    try {
        //Call method for extracting necessary info   
        // it automatically looks in the same dir
        String pathname="ASC_Description"+ASC_ID+".xml";//DEPENDS ON NAMING CONVENTION
               
        // Create a factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        // Use the factory to create a builder
        DocumentBuilder builder = factory.newDocumentBuilder();
        //Document doc = builder.parse(pathname);
        Document doc = builder.parse(new File(pathname));
       
        System.out.println("Opening xml file for extraction...");
        //Rule is created inside extract method
        extract(doc, ASC_ID);
       
        //DELETE FILE AFTER USE       
        File f=new File(pathname);
        boolean success = f.delete();
          if (!success)
          throw new IllegalArgumentException("Delete: deletion failed");
       
           
      }catch (Exception e) {
        System.out.println("Error with opening xml file");
         e.printStackTrace();
        }
     
    //Call method for storing resulting rules in FTP server (repository)
    try {
      storeRule(ASC_ID);
    }catch (Exception e3){
      System.out.println("Error calling storeRule method");
     
    }
     
   
    } catch (Exception ex) {
        System.err.println("Exception " + ex);
        ex.printStackTrace();
        String result2 = "ERROR";
        return result2;
    }

    String ret2="Model successfully created and stored in ASC Repository";
    return ret2;
   
  }

//helper methods from Globus example for managing resources (indirect indication of how many estimations are made)
  public synchronized AddResponse add(int a) throws RemoteException {
    value += a;
    lastOp = "ADDITION";
    return new AddResponse();
  }

  public synchronized SubtractResponse subtract(int a) throws RemoteException {
    value -= a;
    lastOp = "SUBTRACTION";

    return new SubtractResponse();
  }

  public synchronized int getValueRP(GetValueRP params) throws RemoteException {
    return value;
  }

  /* Required by interface ResourceProperties */
  public synchronized ResourcePropertySet getResourcePropertySet() {
    return this.propSet;
  }


  public void extract(Document document, String ASC_ID) throws Exception {

        //extract information from ASCD
       
        System.out.println("Step 1");
       
        ArrayList AllowedValuesList=new ArrayList();
       
        //NEED FOR DISTINCTION INSIDE ASCD, FOR INPUT PARAMETERS AND OUTPUT PARAMETERS
        NodeList inputparameterlist = document.getElementsByTagName("parameters");
        System.out.println("XML Elements: ");
        int arrayrows=inputparameterlist.getLength();
        double[][] rangesarray=new double[arrayrows][3];
        int numberOfAllowed=0;
        int enumInputs=0;
        for (int i=0; i<inputparameterlist.getLength(); i++) {
          // Get element
          Element element = (Element)inputparameterlist.item(i);
         
          NodeList children=element.getChildNodes();
          numberOfAllowed=0;
          for (int l=0; l<children.getLength(); l++) {
            Node child = children.item(l);
            //Ranges exist in rangesarray
            //First column : min (-1 if enumeration)
            //Second column: max (1 if enumeration)
            //Third column: number of discrete values for enumeration (0 for continuous)
           
            //Different processing is needed for each type of parameter (continuous, discrete etc.)
           
            //Continuous values in a limited interval identified by valueMin and valueMax
            if (child.getNodeName()=="valueMin"){
              Element minelem=(Element)child;
              rangesarray[i][0]= Integer.parseInt(minelem.getAttribute("value"));
              System.out.println("THIS IS THE min VALUE for input No"+(i+1)+": "+minelem.getAttribute("value"));
             
            }
            if (child.getNodeName()=="valueMax"){
             
              //at one point, here or in min, the array needs to be filled with zero
              // in the third column to indicate that it is a continuous variable
              Element maxelem=(Element)child;
              rangesarray[i][1]= Integer.parseInt(maxelem.getAttribute("value"));
              System.out.println("THIS IS THE max VALUE for input No"+(i+1)+": "+maxelem.getAttribute("value"));
              rangesarray[i][2]=0;
             
            }
           
            // Discrete values
            if (child.getNodeName()=="valuesAllowed"){
              numberOfAllowed=numberOfAllowed+1;
              Element allowedelem=(Element)child;
              AllowedValuesList.add(allowedelem.getAttribute("value"));
             
             
            }
           
          }
          if (numberOfAllowed!=0){
            System.out.println("Max allowed values for input parameter No"+(i+1)+" : "+numberOfAllowed);
            rangesarray[i][2]=numberOfAllowed;
            rangesarray[i][0]=-1;//default range
            rangesarray[i][1]=1;//default range
          }
         
         
        }//end of for for input parameters
        System.out.println("This is the number of inputs: "+inputparameterlist.getLength());
       
        for (int r=0; r<rangesarray.length; r++) {
         
            if (rangesarray[r][2]!=0){
                     enumInputs=enumInputs+1;
                 }
         
             for (int c=0; c<rangesarray[r].length; c++) {                
                
                 System.out.print("\t" + rangesarray[r][c]);
                
               }
               System.out.println("");
            }
       
        //TEST WITH INPUT ARRAY
        // NORMALIZATION formula from (a,b) sto (A,B)
        //y=(A-B)*x/(a-b)+(a*B-b*A)/(a-b)
        int A=-1;
        int B=1;
       
        int interval=2;//from -1 to 1
           
       
        double testarray[]={495,20,0.5,0.5};
        if (rangesarray.length!=testarray.length){
          System.out.println("Error with number of inputs");
        }else{
          for (int k=0;k<rangesarray.length; k++) {
           
            double a=rangesarray[k][0]; //min   //changed int to double
            double b=rangesarray[k][1]; //max   //changed int to double
            testarray[k]=(A-B)*testarray[k]/(a-b)+(a*B-b*A)/(a-b);
           
          }
        }
         
        for (int l=0;l<testarray.length; l++) {
          System.out.println("Normalized input for"+l+":"+testarray[l]);
        }
         
        for (int l=0;l<AllowedValuesList.size(); l++) {
          System.out.println("Allowed values for"+":"+AllowedValuesList.get(l));
        }
        System.out.println("Number of enumInputs: "+enumInputs);
       
       
       
        //ANOTHER SEGMENT SIMILAR TO THE ABOVE FOR OUTPUTS---
       
       
        //RANGES inside ranges array              
              
        
         //ACQUISITION OF DATA SET FROM HISTORICAL DATA REPOSITORY                    
        
         //Connect to DB
         Connection conn = this.connectToDB("jdbc:mysql://your_IP:your_Port/your_DB","user","password");         
        String querry = null;        
         //Get results
        
         //change querry
         querry = "SELECT * from MonitoringData WHERE ascID="+ASC_ID;
     
        ResultSet results=this.retreiveFromDB(conn,querry);
         System.out.println("Result(s): ");
        while(results.next()){
           System.out.println("Name:\t" + results.getString("some_field_of_your_DB"));          
           System.out.println("");
        }

        
         //disconnect
         this.disconnectFromDB(conn);
                
         //Creation and initialization of test array--testing code
         String unnormarray[]={"495","20","0.5","0.5"};
        String [][] a2 = new String[10][rangesarray.length];
         for (int r=0; r<a2.length; r++) {
            
             for (int c=0; c<a2[r].length; c++) {                
                 a2[r][c]=unnormarray[c];
              
               }
              
               System.out.println("");
        }
       
        for (int t=0;t<a2.length; t++) {
                //for each row of a2
                //change according column 2
                a2[t][2]= "VGA"; //Double.parseDouble("VGA");
               
             
        }
        for (int t=0;t<a2.length; t++) {
                //for each row of a2
                //change according column 2
                a2[t][1]= "slow"; //Double.parseDouble("VGA");
               
             
        }
       
        a2[5][1]="fast";
       
        a2[6][2]="EGA";
        //print final a2
        for (int r=0; r<a2.length; r++) {
            
             for (int c=0; c<a2[r].length; c++) {                
                 //a2[r][c]=unnormarray[c];
                 System.out.print("\t" + a2[r][c]);
               }
               //a2[r]=testarray;
               System.out.println("");
        }
       
            
         //END OF DUMMY MONITORING INTERFACE--RESULT IN a2   
        
        
         //TRANSFORM A2 BASED ON RANGES        
         //THE CHANGE OF THE ENUMERATED TYPES TO NORMALIZED WILL OCCUR FIRST          
         //THEN THE ENTIRE A2 WILL BE TRANSFORMED TO DOUBLE FOR THE REMAINING PROCESS
                 
        
         //10 needs to be changed according to the data received from DB
         Double [][] a2Final = new Double[10][rangesarray.length];
        
         if (rangesarray.length!=a2[1].length){
          System.out.println("Error with number of inputs");
        }else{
          for (int k=0;k<rangesarray.length; k++) {
            //for every input range
            if (rangesarray[k][2]==0){ //NOT enumeration
              double a=rangesarray[k][0]; //min  
              double b=rangesarray[k][1]; //max  
              for (int t=0;t<a2.length; t++) {
                //for each row of a2
                //change according column k
                a2Final[t][k]=(A-B)*(Double.parseDouble(a2[t][k]))/(a-b)+(a*B-b*A)/(a-b);
             
              }
            } else {//ENUMERATION
               
                //necessary for division to have double operators, int/int results in int, regardless of the result variable
                double quantum=2/(rangesarray[k][2]-1); //2 is the interval between -1 1, rangesarray[r][2] is the allowed values length, -1 is needed because n intervals equal to n+1 points
               
                int placeInList=0;
                //HERE CODE FOR FINDING PLACEINLIST, NOW HARDCODED
               
                //k shows the column, which is the number of the input
                double low_search_window=0;
                for (int index1=0;index1<k;index1++) {
                  low_search_window=rangesarray[index1][2]+low_search_window;
                }
               
                double upper_search_window=0;
                upper_search_window=low_search_window+rangesarray[k][2];
                int inside_index=0;
                //allowedvalueslist contains all the enumeration values from all inputs
                System.out.println("Low search window:"+low_search_window+"    upper search window"+upper_search_window);
                
                for (int ta=0;ta<a2.length; ta++) {
                //for each row of a2
                //change according column k
               
               
                  for (int index2=(int)low_search_window;index2<(int)upper_search_window; index2++) {
                                     
                    if (a2[ta][k].equals((String)AllowedValuesList.get(index2))){
                      placeInList=index2;
                    }                 
                   
                  }
                  a2Final[ta][k]=(-1)+(placeInList-low_search_window)*quantum;//if placeInList numbering begins from 1 subtract 1
                } //placeInList is the position in the enumeration list
                System.out.println("Processing enumeration...."+"quantum="+quantum+"...place in list="+placeInList+"number of allowed values="+rangesarray[k][2]);
            }
          }
        }
        
         System.out.println("length of a2:"+a2.length);
          
        
        //This must be dynamic
        //How many inputs will be defined in the ascd, this number must be passed
                       
        int num_inputs=inputparameterlist.getLength();;
               
        //NUM_OUTPUTS IS EQUAL TO BENCHOUTPUTLIST.SIZE     
        int num_outputs=1;   
              
        //Saving of variables and data to octave file--Parameterized by ASC_ID               
        System.out.println("Thread for creating model was launched. Wait...");     
        callOctaveThread calloct = new callOctaveThread();
        calloct.setNumInputs(num_inputs);
        calloct.setNumOutputs(num_outputs);
        calloct.setDataset(a2Final);
        calloct.setascid(Integer.parseInt(ASC_ID));
        calloct.start();
        System.out.println("Request for ASC:"+ASC_ID+" model creation initialized..");                 
        while (calloct.isAlive())
        {
            Thread.sleep(3000)
        }
       
                      
     }

  private Connection connectToDB(String url, String userName, String password){
 
  Connection conn = null;
  try
             {                       
                  Class.forName ("com.mysql.jdbc.Driver").newInstance ();
                 conn = DriverManager.getConnection (url, userName, password);
                 System.out.println ("Database connection established");
                
                 return conn;
                
             }
             catch (Exception e)
             {
                 System.err.println ("Cannot connect to database server "+e);
                 return conn;
             }
 
  }


  private boolean disconnectFromDB(Connection conn){
 
  if (conn != null)
                 {
                     try
                     {
                         conn.close ();
                         System.out.println ("Database connection terminated");
                         return true;
                     }
                     catch (Exception e) {
                     return false; }
                 }
 
  return false;
  }

  private ResultSet retreiveFromDB(Connection conn, String querry){
 
 
    if (conn != null)
                   {
                       try
                       {
   
                      Statement stat = conn.createStatement();
                      ResultSet result = stat.executeQuery(querry);

                      System.out.println ("Acquiring historical data from DB....");
                      return result;
                       }
                       catch (Exception e) {
                       return null; }
 
                 }
    return null;   
  }


  public static void retrieveXML (String ASC_ID) throws Exception {
   
   
    //Retrieve ASCD XML document--from ftp server
    //Stores it in local dir
    //FTP details hardcoded
    //Argument ASC_ID
   
    FTPClient client = new FTPClient();
    FileOutputStream fos = null;
    try {
      client.connect("your_FTP_IP");//change
      client.login("user", "password"); //change
     
     
      //DEPENDS ON NAMING CONVENTION
      String filename = "ASC_Description"+ASC_ID+".xml";
      fos = new FileOutputStream(filename);
      client.retrieveFile("/uploads/ASCDs/" + filename, fos);//DEPENDS ON PATH ON FTP SERVER
    } catch (IOException e) {
     
      System.out.println("Error connecting to ftp server for retrieving ASCD....");
      e.printStackTrace();
    }finally {
      try {
        if (fos != null) {
          fos.close();
          }
          client.disconnect();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
 
  }
 
 
  public static void storeRule (String ASC_ID) throws Exception {
   
    //Store ruleASC_ID.m to ftp server
    //FTP details hardcoded
    //Argument ASC_ID
   
    FTPClient client = new FTPClient();
    FileInputStream fis = null;
    try {
      client.connect("your_FTP_IP");
      client.login("user", "password");
     
     
      //DEPENDS ON NAMING CONVENTION for the models-here ANNs were used so it is netASC_ID.m
      String filename = "net"+ASC_ID+".m";
      String pathname="YOUR_RULE_STORING_PATH"; //DEPENDS ON WHERE THE RULES ARE STORED IN THE OCTAVE SCRIPT
      fis = new FileInputStream(pathname+filename);
      client.storeFile("/uploads/rules/"+filename, fis);//possible change according to path on FTP server
      client.logout();
    } catch (IOException e) {
     
      System.out.println("Error connecting to ftp server for storing rules....");
      e.printStackTrace();
    }finally {
      try {
        if (fis != null) {
          fis.close();
          }
          client.disconnect();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
   
  }

}

class EstimThread extends Thread {
  private String record = null;
  private String input_1 = null;
  private String input_2 = null;
 
    public EstimThread(){}
 
  public EstimThread(String input1, String input2){
    try {
      setInput_1(input1);
      //setInput_2(input2);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
   
  }

  public void run()
  {   
      Process proc;
     
      try {
      String asc_identity=getInput_1();     
      System.out.println("Creating system command for calling createmodel.m for " + asc_identity );
      String command=("sh /usr/share/octave/tests/createrule.sh"); // Depends on path and shell script name
     
      proc = Runtime.getRuntime().exec(command);
     
      //THIS PART IS NECESSARY FOR THE STABILITY OF THE OCTAVE SYSTEM TASK THAT IS EXECUTED UNDER JAVA
      //taken from http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=1
      //It is critical to capture the output and error streams with threads in order for the server not to hang
     
     
      // any error message?
      StreamGobbler errorGobbler = new
          StreamGobbler(proc.getErrorStream(), "ERROR");           
     
      // any output?
      StreamGobbler outputGobbler = new
      StreamGobbler(proc.getInputStream(), "OUTPUT");
         
      // kick them off
      errorGobbler.start();
      outputGobbler.start();
                             
      // any error???
      int exitVal = proc.waitFor();
      System.out.println("ExitValue: " + exitVal);  
     
    } catch (Exception ex) { 
       ex.printStackTrace();
       
    }
   
  }
 
  public void setRecord(String record_) {
      record = record_;
  }
 
  public String getRecord() {
      return record;
  }
 
  public void setInput_1(String input1) {
      input_1 = input1;
  }
 
  public String getInput_1() {
      return input_1;
  }
 
  public void setInput_2(String input2) {
      input_2 = input2;
  }
 
  public String getInput_2() {
      return input_2;
  }

}


class callOctaveThread extends Thread {
  private Double dataset[][];
  private int num_inputs = 0;
  private int num_outputs = 0;
  private int ASC_IDENT;
  private String record = null;
  static Object o = new Object()
 
  public callOctaveThread(){}
 
  public callOctaveThread(Double passedDataset[][], int numinputs, int numoutputs, int asc_id){
    try {
      setDataset(passedDataset);
      setNumInputs(numinputs);
      setNumOutputs(numoutputs);
      setascid(asc_id);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
   
  }

  public void run()
  {   
     
      synchronized(o){ 
      try {
     
     
        OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
        int number_of_inputs=getNumInputs();
        int number_of_outputs=getNumOutputs();
        int ASC_ID=getascid();
        Double inside_dataset[][]=getDataset();
        System.out.println("THIS IS THE NUM INPUTS INSIDE THE THREAD ...:" +number_of_inputs )
        for (int r=0; r<inside_dataset.length; r++) {
              
               for (int c=0; c<inside_dataset[r].length; c++) {                
                  
                   System.out.print("\t" + inside_dataset[r][c]);
                 }
                
                 System.out.println("");
            }
       
            //OCTAVE PART---CRITICAL FOR THREADS
       
          
        OctaveScalar num_in=new OctaveScalar(0);     
        OctaveScalar num_out=new OctaveScalar(0);
        OctaveScalar asc_id=new OctaveScalar(0);
        num_in.set(number_of_inputs);  
        num_out.set(number_of_outputs);
        asc_id.set(ASC_ID);      
        octave.put("num_inputs", num_in);
        octave.put("num_outputs", num_out);
        octave.put("asc_id", asc_id);
        int rows=inside_dataset.length;
         int columns=inside_dataset[1].length;
         OctaveMatrix arr=new OctaveMatrix(rows,columns);
       
        int octave_array_index_offset_r,octave_array_index_offset_c;
       
       
         // Pass java array to octave
        
         for (int r=1; r<=rows; r++) {
             for (int c=1; c<=columns; c++) {
                 arr.set(inside_dataset[r-1][c-1], r, c);
             }
        }
        octave.put("arr", arr);
         //End of Passing of array to octave workspace
       
        octave.eval("save params.m asc_id num_inputs num_outputs arr");
        //ALL PARAMETERS NOW IN PARAMS.M
       
        octave.close();
       
        //createmodel.m (basic octave script for creating the model)needs to be in the same dir?YES
                       
        EstimThread estim = new EstimThread();
        estim.setInput_1(Integer.toString(ASC_ID));
             
        estim.start()
       
        while (estim.isAlive())
        {
            Thread.sleep(1000)
        }
        System.out.println("Thread for model creation for ASC_ID:"+ASC_ID+" has finished...");
                       
       
    } catch (Exception ex) { 
       ex.printStackTrace();
        ex.printStackTrace();
    }
   
   }
   
  }
 
  public void setNumInputs(int numinputs) {
      num_inputs = numinputs;
  }
 
  public int getNumInputs() {
      return num_inputs;
  }
 
  public void setNumOutputs(int numoutputs) {
      num_outputs = numoutputs;
  }
 
  public int getNumOutputs() {
      return num_outputs;
  }
 
  public void setascid(int asc_id) {
      ASC_IDENT = asc_id;
  }
 
  public int getascid() {
      return ASC_IDENT;
  }
 
  public void setRecord(String record_) {
      record = record_;
  }
 
  public void setDataset(Double passedDataset[][] ) {
      dataset = passedDataset;
  }
 
  public Double[][] getDataset() {
      return dataset;
  }

}

// taken from http://www.javaworld.com/jw-12-2000/jw-1229-traps.html?page=1
class StreamGobbler extends Thread
{
    InputStream is;
    String type;
   
    StreamGobbler(InputStream is, String type)
    {
        this.is = is;
        this.type = type;
    }
   
    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null)
                System.out.println(type + ">" + line);   
            } catch (IOException ioe)
              {
                ioe.printStackTrace()
              }
    }
}
TOP

Related Classes of gr.ntua.irmos.services.map.impl.callOctaveThread

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.