org/objectweb/proactive/ext/scilab/SciEngineWorker.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.scilab;
00032 
00033 import java.io.BufferedWriter;
00034 import java.io.File;
00035 import java.io.FileWriter;
00036 import java.io.IOException;
00037 import java.io.Serializable;
00038 import java.util.ArrayList;
00039 
00040 import javasci.SciData;
00041 import javasci.Scilab;
00042 
00043 import org.apache.log4j.Logger;
00044 import org.objectweb.proactive.core.util.log.Loggers;
00045 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00046 
00047 public class SciEngineWorker implements Serializable{
00048         private SciTask sciTask; 
00049         private SciResult sciResult;
00050         private long dateStart;
00051         private long dateEnd;
00052         private boolean isStateFull = false;
00053         private static Logger logger = ProActiveLogger.getLogger(Loggers.SCILAB_TASK);
00054         private static SciEngineWorker sciEngineWorker;
00055         
00056         
00057         public SciEngineWorker(){
00058                 Scilab.init();
00059         }
00060         
00066         public SciResult execute(SciTask sciTask){
00067                 logger.debug("->SciEngineTask In:execute:" + sciTask.getId());
00068                 this.sciTask = sciTask;
00069                 this.sciResult = new  SciResult(sciTask.getId());
00070                 
00071                 this.setListDataToScilab();
00072                 
00073                 this.dateStart = System.currentTimeMillis();
00074                 try{    
00075                         if(executeJob()){
00076                                 getListDataToScilab();
00077                                 this.sciResult.setState(SciResult.SUCCESS);
00078                         }else{
00079                                 this.sciResult.setState(SciResult.ABORT);
00080                                 System.out.println("->SciEngine test:execute\n");
00081                         }
00082                 }catch(Exception e){
00083                         e.printStackTrace();
00084                         this.sciResult.setState(SciResult.ABORT);
00085                 }
00086                 
00087                 this.dateEnd = System.currentTimeMillis();
00088                 this.sciResult.setTimeExecution(this.dateEnd - this.dateStart);
00089                 return sciResult;
00090         }
00091 
00096         private void setListDataToScilab(){
00097                 ArrayList listData = this.sciTask.getListDataIn();
00098         
00099                 SciData data;
00100                 for (int i = 0; i < listData.size(); i++) {
00101                         data = (SciData) listData.get(i);
00102                         Scilab.sendData(data);
00103                 }
00104         }
00105         
00110         private void clearScilab(){
00111                 ArrayList listData = this.sciTask.getListDataOut();
00112                 SciData data;
00113                 Scilab.exec("clearglobal();");
00114                 for (int i = 0; i < listData.size(); i++) {
00115                         data = (SciData) listData.get(i);
00116                         Scilab.exec("clear " + data.getName() + ";");
00117                 }
00118         }
00119                 
00124         private void getListDataToScilab(){
00125                 ArrayList listData = this.sciTask.getListDataOut();
00126                 SciData data;
00127                 for (int i = 0; i < listData.size(); i++) {
00128                         data = (SciData) listData.get(i);
00129                         data = Scilab.receiveDataByName(data.getName());
00130                         this.sciResult.add(data);       
00131                 }
00132                 
00133                 if(!isStateFull){
00134                         clearScilab();
00135                 }
00136         }
00137         
00141         private boolean executeJob(){
00142                 String job = sciTask.getJobInit() + "\n" + sciTask.getJob();
00143                 BufferedWriter out;
00144                 File temp;
00145                 boolean isValid;
00146                 try {
00147                         temp = File.createTempFile("scilab", ".sce");
00148                         temp.deleteOnExit();
00149                         out = new BufferedWriter(new FileWriter(temp));
00150                         out.write(job);
00151                         out.close();
00152                         logger.debug("->SciEngineTask:executeJob:test1:" + temp.getAbsolutePath());
00153                         isValid = Scilab.exec("exec(''"+temp.getAbsolutePath()+"'');");
00154                 } catch (IOException e) {
00155                         isValid = false;
00156                 }
00157                 
00158                 return isValid;
00159         }
00160         
00161         public synchronized static SciResult executeTask(SciTask sciTask){
00162                 if(sciEngineWorker == null){
00163                         sciEngineWorker = new SciEngineWorker();
00164                 }
00165                 return sciEngineWorker.execute(sciTask);
00166         }
00167         
00168         public void exit(){
00169                 System.exit(0);
00170         }
00171 
00172 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1