Source Grep
The Source Grep example is an application based on the familiar command-line text-search utility originally written for Unix (grep). The application can be used to search inside all source files contained in a directory tree, finding all lines within the files that match a specified regular expression.
The example makes use of two state machines, srcGrep and searchDirectory (see srcgrep.js). The srcGrep machine controls the application and is started when the application runs. It has a single state, ManageProcessors, used to manage the directory searching process and collect the results of any matches that are found. The state maintains a list (dirList) of the directories that still need to be searched and manages a pool of searchDirectory state machines to do the searching work.
The pool is regulated based on its size relative to a specified maximum using threshold events emitting by the searchDirectory State Machine Factory (see: setThreshold). The events emitted by individual searchDirectory state machines in the pool are factory events and therefore ManageProcessors registers the factory as an event emitter (see fire.$regEmitter and defines the appropriate actions to listen for the events. This allows the state to receive information about any matches found ('processor.addMatch' events) and also add more directories to the list dirList if a searchDirectory machine finds a directory in its search ('processor.addDir' events).
As briefly mentioned above the searchDirectory machine is used to search all files in a specified directory, looking for lines matching the given regular expression. The machine starts by listing the contents of the directory (ReadDir). It then loops over the directory constituents, running the fs.lstat function on each (DirListingStat). If a directory is found then a factory event is emitted letting the srcGrep machine know that there is a new directory that needs searching in the future ('addDir' event). If a file is found then it is opened and searched (ReadFile -> Match -> DirListingStat). If any matches are found the machine emits an event with the details ('addMatch' event). When the complete directory has been searched the machine exits.
Example Output
$ ./bin/ignite examples/apps/srcgrep/srcgrep.js
Running examples/apps/srcgrep/srcgrep.js
run: examples/apps/srcgrep/srcgrep.js Exited with no error.
index.js
1 : /* JSMachine - A JavaScript UML State Machine Engine */
examples/web_scraper.js
2 : // In this example the JSMachine framework is used to build a simple
lib/JSM.js
2 : /** JSMachine - A JavaScript UML State Machine Engine
32 : * @param {JSMachine} State machine factory
examples/rss_feed.js
2 : // In this example JSMachine is used to build a RSS feed reader.
bin/jsmachine_modes/draw.js
1 : var JSMachine = require("../../index"),
20 : graph = new JSMachine.JSMDiagram(jsmfact) ;
bin/jsmachine_modes/run.js
1 : var JSMachine = require("../../index"),
lib/plugins/defaults/async.js
2 : /* JSMachine Plugin 'async'
examples/apps/srcgrep/srcgrep.js
159 : args: ['JSMachine', '.', new JSMFactory(searchDirectory), 4],
lib/plugins/defaults/list/detect.js
1 : /* JSMachine Plug-in 'detect'
lib/plugins/defaults/list/reduce.js
1 : /* JSMachine Plugin 'reduce'
lib/plugins/defaults/list/each.js
1 : /* JSMachine Plug-in 'each'
lib/plugins/defaults/list/filter.js
1 : /* JSMachine Plugin 'filter'
lib/plugins/defaults/list/map.js
1 : /* JSMachine Plugin 'map'
$ _
See
- source: srcgrep.js