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.commandline;
006    
007    import bsh.EvalError;
008    import bsh.Interpreter;
009    import graphlab.graph.atributeset.GraphAttrSet;
010    import graphlab.graph.graph.GraphModel;
011    import graphlab.platform.core.BlackBoard;
012    import graphlab.platform.core.Listener;
013    import graphlab.plugins.commandline.commands.EdgeCommands;
014    import graphlab.plugins.commandline.commands.GraphCommands;
015    import graphlab.plugins.commandline.commands.NativeCommands;
016    import graphlab.plugins.commandline.commands.VertexCommands;
017    import graphlab.plugins.commandline.parsers.InwardCommandParser;
018    import graphlab.plugins.main.GraphData;
019    import graphlab.ui.UIUtils;
020    
021    import java.util.HashMap;
022    
023    /**
024     * @author Mohamad Ali Rostami
025     * @email ma.rostami@yahoo.com
026     */
027    
028    public class Shell {
029        public static final String event = UIUtils.getUIEventKey("RunShell");
030        static final String NAME = "SHELL BLKBRD";
031        int newvar_maker = 0;
032        GraphData data;
033        Interpreter main_interpreter;
034        public ShellConsole ext_console;
035        BlackBoard bb;
036        String evaluations;
037        public HashMap<String, Class> code_completion_dictionary = new HashMap<String, Class>();
038        InwardCommandParser parser;
039    
040        public Shell(BlackBoard blackBoard) {
041            data = new GraphData(blackBoard);
042            this.bb = blackBoard;
043        }
044    
045        public Object evaluate(String s) {
046            System.out.println(s);
047            evaluations += s;
048            try {
049                return main_interpreter.eval(s);
050            } catch (EvalError evalError) {
051                evalError.printStackTrace();
052            }
053            return null;
054        }
055    
056        public void addCodeCompletionDictionary(String s, Class c) {
057            code_completion_dictionary.put(s, c);
058        }
059    
060        public Object evaluateCommand(String s, String name, String abbr) {
061            return parser.evaluateCommand(s, name, abbr);
062        }
063    
064        public void set_variable(String s, Object o) {
065            try {
066                main_interpreter.set(s, o);
067            } catch (EvalError evalError) {
068                evalError.printStackTrace();
069            }
070        }
071    
072        public Object get(String s) {
073            try {
074                return main_interpreter.get(s);
075            } catch (EvalError evalError) {
076                evalError.printStackTrace();
077            }
078            return null;
079        }
080    
081        public String getEvaluations() {
082            return evaluations;
083        }
084    
085        public String newVariable() {
086            newvar_maker++;
087            return "_newvar_" + newvar_maker;
088        }
089    
090    
091        public void performJob(String name) {
092    
093            bb.addListener(GraphAttrSet.name, new Listener() {
094                public void keyChanged(String key, Object value) {
095                    GraphModel gm = bb.getData(GraphAttrSet.name);
096                    try {
097                        main_interpreter.set(gm.getLabel(), gm);
098                    } catch (EvalError evalError) {
099                        evalError.printStackTrace();
100                    }
101                }
102            });
103            evaluations += "clr(){console.clear();}";
104            evaluations = "import graphlab.graph.graph.GraphModel;" + evaluations;
105            evaluations = "import graphlab.graph.graph.VertexModel;" + evaluations;
106    
107            ext_console = (ShellConsole) UIUtils.getComponent(bb, "ShellSideBar");
108            ext_console.shell = this;
109            final ShellConsole console = ext_console;
110            main_interpreter = new Interpreter(console);
111            parser = new InwardCommandParser(main_interpreter, this);
112            parser.addCommands(new GraphCommands(bb));
113    //        parser.addCommands(new ShellServerCommands(bb));
114            parser.addCommands(new VertexCommands(bb));
115            parser.addCommands(new EdgeCommands(bb));
116            parser.addCommands(new NativeCommands(bb));
117            evaluations = InwardCommandParser.evaluations;
118            ShellCodeCompletion code_completion = new ShellCodeCompletion(main_interpreter, parser.commands, parser.abbrs, code_completion_dictionary);
119            console.setNameCompletion(code_completion);
120    
121            new Thread() {
122                public void run() {
123                    try {
124                        main_interpreter.set("abbreviations", parser.abbrs);
125                        main_interpreter.set("code_completion_dictionary", code_completion_dictionary);
126                        main_interpreter.set("evaluations", evaluations);
127                        main_interpreter.set("console", console);
128                        main_interpreter.set("blackboard", bb);
129                        parser.abbrs.put("_clr", "clr");
130    //                    main_interpreter.set("me", Shell.this);
131                        main_interpreter.eval(evaluations);
132                    }
133                    catch (EvalError evalError) {
134                        evalError.printStackTrace();
135                    }
136                    main_interpreter.run();
137    
138                }
139            }.start();
140    
141            //for not printing 'by niemeyer'
142           new Thread() {
143    
144                public void run() {
145                    try {
146                        Thread.sleep(1000);
147                        console.clear();
148                        console.print("bsh % ");
149                    } catch (InterruptedException e) {
150                        e.printStackTrace();
151                    }
152                }
153            }.start();
154        }
155    
156        /**
157         * @param b
158         * @return the available shell for b. (normally the working shell of application)
159         */
160        public static Shell getCurrentShell(BlackBoard b) {
161            return b.getData(NAME);
162        }
163    
164    }