001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.connector.matlab;
006    
007    import graphlab.platform.core.BlackBoard;
008    import graphlab.platform.extension.Extension;
009    import graphlab.platform.extension.UnknownExtensionLoader;
010    import graphlab.platform.StaticUtils;
011    import graphlab.platform.plugin.Plugger;
012    import graphlab.plugins.commandline.Shell;
013    import graphlab.plugins.connector.ConnectorDS;
014    import graphlab.plugins.connector.ConnectorReportExtension;
015    
016    import java.io.File;
017    import java.io.FileNotFoundException;
018    import java.net.MalformedURLException;
019    import java.util.Scanner;
020    import java.util.StringTokenizer;
021    
022    public class MatlabExtensionLoader implements UnknownExtensionLoader {
023        public Extension load(File file, BlackBoard blackboard) {
024            try {
025                if ("m".equals(Plugger.getExtension(file))) {
026                    System.err.println("loding MatLab extension: " + file.getName());
027                    ConnectorDS cds = parseMatlabFile(file);
028    //            System.err.println(cds.command);
029                    if (cds == null)
030                        return null;
031                    Shell.getCurrentShell(blackboard).evaluate(cds.command);
032                    return new ConnectorReportExtension(file, blackboard, cds);
033                } else
034                    return null;
035            } catch (Exception e) {
036                e.printStackTrace();
037                return null;
038            }
039        }
040    
041        //todo: to move this method to ConnectorDS and make it a helper method for all external connections
042        protected ConnectorDS parseMatlabFile(File matlabfile) {
043            ConnectorDS ret = new ConnectorDS();
044            String args = "";
045            ret.commandName = matlabfile.getName().substring(0, matlabfile.getName().lastIndexOf("."));
046            Scanner fileScanner = new Scanner("");
047            try {
048                fileScanner = new Scanner(matlabfile);
049            }
050            catch (FileNotFoundException e) {
051                e.printStackTrace();
052            }
053            String funcBody = "new String[]{";
054    
055            String line = fileScanner.nextLine();
056    
057            //read arguments
058            int counter = 0;
059    //        while (line.startsWith("%")) {
060            if (line.contains("description:"))
061                ret.description = line.substring(line.indexOf(":") + 2, line.length());
062            else
063                return null;
064    
065            line = fileScanner.nextLine();
066            if (line.contains("args:")) {
067                args = line.substring(line.indexOf(":") + 2, line.length());
068                StringTokenizer argsTok = new StringTokenizer(args, ",");
069                ret.command = ret.commandName + "(";
070                while (argsTok.hasMoreTokens()) {
071                    String temp = argsTok.nextToken().trim();
072                    if (temp.startsWith("@")) {
073                        Scanner sc = new Scanner(temp);
074                        String clazz = sc.next().substring(1); //variable class
075                        String name = sc.next();
076                        ret.command += clazz + " " + name + " ,";
077                        sc.next(); //skips the " = "
078                        String defVal = sc.next();
079                        funcBody += "String.valueOf(" + name + "),";
080                        ret.atrs.put(name, StaticUtils.fromString(clazz, defVal));
081                        ret.shellMethodArgs.add(name);
082                        counter++;
083                    } else {
084                        funcBody += "String.valueOf(" + temp + "),";
085                    }
086                }
087            }
088            //prepare the command
089    
090            ret.command = "public Object " + (counter != 0 ? ret.command.substring(0, ret.command.length() - 1) : ret.command) + ")";
091            funcBody = funcBody.substring(0, funcBody.length() - 1) + "}";
092            try
093    
094            {
095                ret.command += "{return graphlab.plugins.connector.matlab.MatlabRunner.matlabRunner(new File(\"" + matlabfile.toURI().toURL().getPath() + "\") , " + funcBody + ");}";
096            }
097    
098            catch (MalformedURLException e) {
099                e.printStackTrace();
100            }
101    
102            return ret;
103        }
104    }