Ignore:
Timestamp:
06/05/08 10:11:18 (16 years ago)
Author:
ra33
Message:

Added DebugFrame action
Added GetCometStats
Lots of bug fixes

Location:
trunk/src/org/expeditee/actions
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/actions/Actions.java

    r80 r86  
    4646public class Actions {
    4747
     48        private static final String INVALID_PARAMETERS_ERROR = "Invalid parameters for agent: "; //$NON-NLS-1$
     49
    4850        // the currently running agent (if there is one)
    4951        private static Agent _Agent = null;
     
    164166         * Clears out the Action and JAG Hashtables and refills them. Normally this
    165167         * is only called once when the system starts.
    166          * @return a warning message if there were any problems loading agents or actions.
     168         *
     169         * @return a warning message if there were any problems loading agents or
     170         *         actions.
    167171         */
    168172        public static Collection<String> Init() {
    169173                Collection<String> warnings = new LinkedList<String>();
    170174                Class[] classes;
    171                
     175
    172176                try {
    173177                        classes = getClasses(AGENTS_PACKAGE);
     
    327331
    328332                for (Method possible : possibles) {
    329 
    330333                        // try first with the launching item as a parameter
    331334
     
    346349                                e.printStackTrace();
    347350                        }
     351                }
     352                if (possibles.size() > 0) {
     353                        FrameGraphics.ErrorMessage("Incorrect parameters for " + mname);
     354                } else {
     355                        assert(false);
     356                        FrameGraphics.ErrorMessage(mname + " action not found");
    348357                }
    349358        }
     
    386395                        // get the constructor for the JAG class
    387396                        Constructor con = null;
    388                         Constructor constructors[] = agentClass.getConstructors();
     397                        Constructor[] constructors = agentClass.getConstructors();
    389398                        Object[] params = null;
    390399
     
    397406                                        params[0] = parameters;
    398407                                        break;
    399                                 } else if (c.getParameterTypes().length == 0 && con == null)
     408                                } else if (c.getParameterTypes().length == 0 && con == null) {
    400409                                        con = c;
     410                                }
    401411                        }
    402412
    403413                        // if there is no constructor, return
    404414                        if (con == null) {
    405                                 FrameGraphics.DisplayMessage("Invalid parametres for agent: "
     415                                FrameGraphics.DisplayMessage(INVALID_PARAMETERS_ERROR
    406416                                                + nameWithCorrectCase);
    407417                                // System.out.println("Constructor not found...");
     
    433443
    434444                } catch (ClassNotFoundException cnf) {
    435                         FrameGraphics.ErrorMessage("Error: '" + nameWithCorrectCase
     445                        FrameGraphics.ErrorMessage(nameWithCorrectCase
    436446                                        + "' is not an action or agent.");
    437447                } catch (Exception e) {
     
    540550                        }
    541551                        // convert the value to an object
    542                         Object o = Conversion.Convert(paramTypes[ind], param);
    543                         if (o == null)
     552                        try {
     553                                Object o = Conversion.Convert(paramTypes[ind], param);
     554                                if (o == null)
     555                                        return null;
     556                                objects[ind] = o;
     557                        } catch (Exception e) {
    544558                                return null;
    545                         objects[ind] = o;
     559                        }
    546560                }
    547561
  • trunk/src/org/expeditee/actions/Misc.java

    r80 r86  
    1111import java.io.IOException;
    1212import java.lang.reflect.Method;
     13import java.sql.Time;
    1314import java.util.ArrayList;
    1415import java.util.List;
     
    2223import org.expeditee.gui.FrameMouseActions;
    2324import org.expeditee.gui.TimeKeeper;
     25import org.expeditee.io.Logger;
    2426import org.expeditee.items.Item;
    2527import org.expeditee.items.Text;
     
    5456        public static void Restore() {
    5557                FrameIO.Reload();
    56 
    57                 FrameGraphics.DisplayMessage("Restoration complete.");
     58                //FrameGraphics.DisplayMessage("Restoration complete.");
    5859        }
    5960
     
    227228        }
    228229
     230        /**
     231         * Creates a new Text Object containing statistics for the current tree.
     232         */
     233        public static void GetCometStats(Frame frame) {
     234                FrameGraphics.DisplayMessage("Extracting comet stats...");
     235
     236                StringBuffer sb = new StringBuffer();
     237                Time active = frame.getCometActiveTime(true);
     238                Time dark = frame.getCometDarkTime(true);
     239                Time total = new Time(active.getTime() + dark.getTime());
     240                int length = frame.getCometLength();
     241                sb.append("CometName: ").append(frame.getName()).append('\n');
     242                sb.append("Length: ").append(length).append('\n');
     243                sb.append("            Current  Average  Total").append('\n');
     244                Time averageActive = new Time((long) (0.5 + active.getTime()
     245                                / (1.0 * length)));
     246                Time averageDark = new Time((long) (0.5 + dark.getTime()
     247                                / (1.0 * length)));
     248                Time averageTotal = new Time((long) (0.5 + total.getTime()
     249                                / (1.0 * length)));
     250                Time currentActive = SessionStats.getFrameActiveTime();
     251                Time currentDark = SessionStats.getFrameDarkTime();
     252                Time currentTotal = SessionStats.getFrameTotalTime();
     253               
     254                sb.append("ActiveTime: ").append(Logger.EasyTimeFormat(currentActive)).append(
     255                ' ').append(Logger.EasyTimeFormat(averageActive)).append(
     256                                ' ').append(Logger.EasyTimeFormat(active)).append('\n');
     257                sb.append("  DarkTime: ").append(Logger.EasyTimeFormat(currentDark)).append(
     258                ' ').append(Logger.EasyTimeFormat(averageDark)).append(
     259                                ' ').append(Logger.EasyTimeFormat(dark)).append('\n');
     260                sb.append(" TotalTime: ").append(Logger.EasyTimeFormat(currentTotal)).append(
     261                ' ').append(Logger.EasyTimeFormat(averageTotal)).append(
     262                                ' ').append(Logger.EasyTimeFormat(total));
     263                CreateTextItem(sb.toString());
     264
     265                FrameGraphics.OverwriteMessage("Comet stats complete");
     266        }
     267
    229268        public static void CreateTextItem(String itemText) {
    230269                SessionStats.CreatedText();
     
    260299        public static void GetCurrentFrameFile() {
    261300
    262                 String fileContents = FrameIO.SaveFrame(DisplayIO.getCurrentFrame());
    263 
    264                 Text text = DisplayIO.getCurrentFrame().createNewText();
     301                Frame current = DisplayIO.getCurrentFrame();
     302                current.change();
     303                String fileContents = FrameIO.SaveFrame(current);
     304
     305                Text text = current.createNewText();
    265306                // We dont want the stats to wrap at all
    266307                text.setMaxSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
     
    349390        }
    350391
    351         public static String SaveImage(BufferedImage screen, String format, String directory, String fileName){
     392        public static String SaveImage(BufferedImage screen, String format,
     393                        String directory, String fileName) {
    352394                // Check if we need to append the suffix
    353395                if (fileName.indexOf('.') < 0)
    354                         fileName += "." + format.toLowerCase();         
     396                        fileName += "." + format.toLowerCase();
    355397
    356398                try {
     
    370412                return null;
    371413        }
    372        
     414
    373415        public static String ImageFrame(Frame frame, String format, String directory) {
    374416                assert (frame != null);
    375                
     417
    376418                FrameGraphics.UpdateBuffer(frame, false);
    377419
  • trunk/src/org/expeditee/actions/Simple.java

    r80 r86  
    1414import java.util.Random;
    1515
     16import org.expeditee.agents.DefaultAgent;
    1617import org.expeditee.agents.DisplayTree;
    1718import org.expeditee.agents.WriteTree;
     
    4950import org.expeditee.stats.SessionStats;
    5051
    51 public class Simple {
     52public class Simple implements Runnable {
    5253
    5354        private static final String DEFAULT_STRING = "$s.";
     
    8788        private static final String TOKEN_SEPARATOR = " +";
    8889
    89         private static final String RUN_FRAME_ACTION = "RunFrame";
     90        public static final String RUN_FRAME_ACTION = "runframe";
     91
     92        public static final String DEBUG_FRAME_ACTION = "debugframe";
    9093
    9194        /**
     
    100103         * Expeditee
    101104         */
    102         private static boolean _acceptKeyboardInput = false;
     105        private static boolean _consumeKeyboardInput = false;
    103106
    104107        public static void ProgramFinished() {
    105108                _programsRunning--;
     109                _stop = false;
    106110        }
    107111
    108112        private static LinkedList<Character> _KeyStrokes = new LinkedList<Character>();
    109113
     114        private static boolean _stop;
     115
     116        private static boolean _step;
     117
     118        private static int _stepPause = 0;
     119
     120        private static Color _stepColor;
     121
     122        private static boolean _nextStatement;
     123
    110124        public static void KeyStroke(char c) {
     125               
     126               
    111127                _KeyStrokes.add(c);
    112128        }
     
    116132        }
    117133
    118         public static boolean acceptKeyboardInput() {
    119                 return _acceptKeyboardInput && _programsRunning > 0;
     134        public static boolean consumeKeyboardInput() {
     135                return _consumeKeyboardInput && _programsRunning > 0;
    120136        }
    121137
     
    134150                do {
    135151                        next = FrameIO.LoadNext(next);
    136                 } while (next != null
    137                                 && (next.getTitleItem() == null || !RUN_FRAME_ACTION
    138                                                 .equalsIgnoreCase(next.getTitleItem().getFirstAction())));
     152                } while (next != null && next.isTestFrame());
    139153                FrameUtils.DisplayFrame(next, true);
    140154        }
     
    144158                do {
    145159                        prev = FrameIO.LoadPrevious(prev);
    146                 } while (prev != null
    147                                 && (prev.getTitleItem() == null || !RUN_FRAME_ACTION
    148                                                 .equalsIgnoreCase(prev.getTitleItem().getFirstAction())));
     160                } while (prev != null && prev.isTestFrame());
    149161
    150162                FrameUtils.DisplayFrame(prev, true);
     
    156168                do {
    157169                        // check if its a test frame
    158                         if (next != null
    159                                         && next.getTitleItem() != null
    160                                         && RUN_FRAME_ACTION.equalsIgnoreCase(next.getTitleItem()
    161                                                         .getFirstAction())) {
     170                        if (next != null && next.isTestFrame()) {
    162171                                lastTest = next;
    163172                                break;
     
    178187        }
    179188
    180         public static void RunSimpleTests(String frameset, boolean verbose) {
     189        private String _frameset;
     190
     191        private boolean _verbose;
     192
     193        public Simple(String frameset, boolean verbose) {
     194                _frameset = frameset;
     195                _verbose = verbose;
     196        }
     197
     198        public void run() {
    181199                int testsPassed = 0;
    182200                int testsFailed = 0;
    183201
    184202                FrameIO.SaveFrame(DisplayIO.getCurrentFrame(), false);
    185                 FrameGraphics.DisplayMessage("Starting test suite: " + frameset,
     203                FrameGraphics.DisplayMessage("Starting test suite: " + _frameset,
    186204                                Color.CYAN);
    187205
    188                 // Get the next number in the inf file for the frameset
    189                 int lastFrameNo = FrameIO.getLastNumber(frameset);
    190 
    191                 // Loop through all the valid frames in the frameset
     206                // Get the next number in the inf file for the _frameset
     207                int lastFrameNo = FrameIO.getLastNumber(_frameset);
     208
     209                // Loop through all the valid frames in the _frameset
    192210                for (int i = 1; i <= lastFrameNo; i++) {
    193                         String nextFrameName = frameset + i;
     211                        String nextFrameName = _frameset + i;
    194212                        Frame nextFrame = FrameIO.LoadFrame(nextFrameName);
    195213                        if (nextFrame == null)
     
    214232                                        RunFrameAndReportError(title, new Context());
    215233                                        _programsRunning--;
    216                                         if (verbose)
     234                                        if (_verbose)
    217235                                                FrameGraphics.DisplayMessage("Test passed: "
    218236                                                                + title.toString(), Item.GREEN);
     
    223241                                        // Print out the reason for failed tests
    224242                                        FrameGraphics.LinkedErrorMessage(e.getMessage());
     243                                        if (Simple._stop) {
     244                                                Simple._stop = false;
     245                                                return;
     246                                        }
    225247                                }
    226248                        }
     
    240262        }
    241263
    242         public static void RunFrame(Item current) {
    243                 RunFrame(current, false);
    244         }
    245 
    246         public static void RunFrame(Item current, boolean acceptKeyboardInput) {
     264        public static void RunSimpleTests(String frameset, boolean verbose) {
     265                _stop = false;
     266                Thread t = new Thread(new Simple(frameset, verbose));
     267                t.setPriority(Thread.MIN_PRIORITY);
     268                t.start();
     269        }
     270
     271        private static void RunFrame(Item current, boolean acceptKeyboardInput,
     272                        boolean step, int pause, Color color) {
    247273                try {
    248                         _acceptKeyboardInput = acceptKeyboardInput;
     274                        DisplayIO.addToBack(current.getParent());
     275
     276                        _stepColor = color == null ? Color.green : color;
     277                        _stepColor = new Color(_stepColor.getRed(), _stepColor.getGreen(),
     278                                        _stepColor.getBlue(), 50);
     279                        _stepPause = pause;
     280                        _step = step;
     281                        _consumeKeyboardInput = acceptKeyboardInput;
    249282                        FrameIO.SaveFrame(DisplayIO.getCurrentFrame(), true);
    250283
     
    265298                        // _programsRunning--;
    266299                }
     300        }
     301
     302        public static void RunFrame(Item current, boolean acceptKeyboardInput) {
     303                RunFrame(current, acceptKeyboardInput, false, 0, null);
     304        }
     305
     306        public static void RunFrame(Item current) {
     307                RunFrame(current, false);
     308        }
     309
     310        /**
     311         * At present programs which accept keyboard input can not be debugged.
     312         *
     313         * @param current
     314         * @param pause
     315         */
     316        public static void DebugFrame(Item current, double pause, Color color) {
     317                RunFrame(current, false, true, (int) (pause * 1000 + 0.5), color);
     318        }
     319
     320        /**
     321         * At present programs which accept keyboard input can not be debugged.
     322         *
     323         * @param current
     324         * @param pause
     325         *            the time to pause between
     326         */
     327        public static void DebugFrame(Item current, double pause) {
     328                DebugFrame(current, pause, null);
     329        }
     330
     331        public static void DebugFrame(Item current) {
     332                DebugFrame(current, 0.2, null);
    267333        }
    268334
     
    290356
    291357                Frame child = FrameIO.LoadFrame(current.getAbsoluteLink());
     358
     359                if (_step) {
     360                        DisplayIO.setCurrentFrame(child);
     361                        DisplayIO.addToBack(child);
     362                }
     363
    292364                AgentStats.FrameExecuted();
    293365
     
    299371
    300372                // loop through non-title, non-name, non-annotation text items
    301                 List<Item> body = child.getItems();
    302                 Text item = null;
     373                List<Text> body = child.getBodyTextItems(false);
     374
     375                // if no item was found
     376                if (body.size() == 0)
     377                        throw new Exception("No code to be executed: " + current.toString());
     378
    303379                Status lastItemStatus = Status.OK;
    304                 for (Item i : body)
    305                         // Ignore items that arent statements
    306                         if (isStatement(i)) {
    307                                 item = (Text) i;
    308                                 AgentStats.ItemExecuted();
    309                                 try {
    310 
    311                                         lastItemStatus = RunItem(item, context, lastItemStatus);
    312 
    313                                         if (lastItemStatus != Status.OK) {
    314                                                 if (lastItemStatus != Status.TrueIf
    315                                                                 && lastItemStatus != Status.FalseIf) {
    316                                                         return lastItemStatus;
     380                for (Text item : body) {
     381                        AgentStats.ItemExecuted();
     382                        try {
     383                                Color oldColor = item.getBackgroundColor();
     384                                if (_step) {
     385                                        if (item.getLink() == null) {
     386                                                item.setBackgroundColor(_stepColor);
     387                                        }else{
     388                                                item.setSelectedMode(Item.SelectedMode.Normal,_stepColor);
     389                                        }
     390                                        FrameGraphics.Repaint();
     391                                }
     392                                lastItemStatus = RunItem(item, context, lastItemStatus);
     393                                if (_step) {
     394                                        if (item.getLink() != null) {
     395                                                item.setBackgroundColor(_stepColor);
     396                                                item.setSelectedMode(Item.SelectedMode.None);
     397                                                FrameGraphics.Repaint();
     398                                        }
     399                                        int timeRemaining;
     400                                        if (_stepPause < 0)
     401                                                timeRemaining = Integer.MAX_VALUE;
     402                                        else
     403                                                timeRemaining = _stepPause;
     404
     405                                        while (timeRemaining > 0 && !_nextStatement) {
     406                                                if (_stop) {
     407                                                        item.setBackgroundColor(oldColor);
     408                                                        item.setSelectedMode(SelectedMode.Normal,_stepColor);
     409                                                        throw new Exception("Program terminated");
    317410                                                }
     411                                                Thread.sleep(DefaultAgent.TIMER_RESOLUTION);
     412                                                timeRemaining -= DefaultAgent.TIMER_RESOLUTION;
    318413                                        }
    319                                 } catch (ArrayIndexOutOfBoundsException e) {
    320                                         FlagError(item);
    321                                         throw new Exception("Too few parametres: "
    322                                                         + item.toString());
    323                                 } catch (NullPointerException e) {
    324                                         FlagError(item);
    325                                         throw new Exception("Null pointer exception: "
    326                                                         + item.toString());
    327                                 } catch (RuntimeException e) {
    328                                         FlagError(item);
    329                                         throw new Exception(e.getMessage() + " " + item.toString());
    330                                 } catch (Exception e) {
    331                                         throw new Exception(e.getMessage());
    332                                 }
    333                         }
    334 
    335                 // if no item was found
    336                 if (item == null)
    337                         throw new Exception("No code to be executed: " + current.toString());
     414                                        _nextStatement = false;
     415                                        // Turn off the highlighting
     416                                        item.setBackgroundColor(oldColor);
     417                                }
     418
     419                                if (lastItemStatus != Status.OK) {
     420                                        if (lastItemStatus != Status.TrueIf
     421                                                        && lastItemStatus != Status.FalseIf) {
     422                                                return lastItemStatus;
     423                                        }
     424                                }
     425                        } catch (ArrayIndexOutOfBoundsException e) {
     426                                FlagError(item);
     427                                throw new Exception("Too few parametres: " + item.toString());
     428                        } catch (NullPointerException e) {
     429                                FlagError(item);
     430                                throw new Exception("Null pointer exception: "
     431                                                + item.toString());
     432                        } catch (RuntimeException e) {
     433                                FlagError(item);
     434                                throw new Exception(e.getMessage() + " " + item.toString());
     435                        } catch (Exception e) {
     436                                throw new Exception(e.getMessage());
     437                        }
     438                }
     439
     440                if (_step) {
     441                        DisplayIO.setCurrentFrame(current.getParent());
     442                        DisplayIO.removeFromBack();
     443                }
    338444
    339445                return Status.OK;
    340         }
    341 
    342         private static boolean isStatement(Item i) {
    343                 if (!(i instanceof Text))
    344                         return false;
    345 
    346                 Frame frame = i.getParent();
    347 
    348                 if (i == frame.getTitleItem() || i == frame.getNameItem()
    349                                 || i.isAnnotation()) {
    350                         return false;
    351                 }
    352 
    353                 return true;
    354446        }
    355447
     
    541633        private static Status RunItem(Text code, Context context,
    542634                        Status lastItemStatus) throws Exception {
     635                if (_stop) {
     636                        throw new Exception("Program terminated");
     637                }
    543638
    544639                String[] tokens = code.getProcessedCode();
     
    18051900                                                                        .StripTag(
    18061901                                                                                        ((Text) pauseItem).getFirstLine(),
    1807                                                                                         "@DisplayFramePause:"));
     1902                                                                                        "@DisplayFramePause"));
    18081903                                                } catch (NumberFormatException nfe) {
    18091904                                                }
     
    22422337                                assertExactParametreCount(tokens, 3);
    22432338                                assertVariableType(tokens[1], 1, SPointer.associationPrefix);
    2244                                 Map<String,String> map = (Map<String,String>) context.getPointers().getVariable(
    2245                                                 tokens[1]).getValue();
    2246                                 for(Map.Entry entry: map.entrySet()){
     2339                                Map<String, String> map = (Map<String, String>) context
     2340                                                .getPointers().getVariable(tokens[1]).getValue();
     2341                                for (Map.Entry entry : map.entrySet()) {
    22472342                                        String value = entry.getValue().toString();
    22482343                                        String key = entry.getKey().toString();
     
    22592354                                return Status.OK;
    22602355                        }
    2261                        
     2356
    22622357                        if (Primitives.isPrimitive(tokens[1])
    22632358                                        && Primitives.isPrimitive(tokens[2])
     
    23532448                return message.toString();
    23542449        }
     2450
     2451        public static void stop() {
     2452                _stop = true;
     2453        }
     2454
     2455        public static void nextStatement() {
     2456                _nextStatement = true;
     2457        }
    23552458}
Note: See TracChangeset for help on using the changeset viewer.