diff options
author | Stefan Radomski <github@mintwerk.de> | 2016-06-18 11:55:39 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2016-06-18 11:55:39 (GMT) |
commit | 0e0be07906a720ae54e4572d6ac0cb657424550d (patch) | |
tree | 7d48c87a9142a5dad06570ca4daf0212475d83f1 | |
parent | 84bbbd42c3480c40c0355c64899f99f8d588d5c0 (diff) | |
download | uscxml-0e0be07906a720ae54e4572d6ac0cb657424550d.zip uscxml-0e0be07906a720ae54e4572d6ac0cb657424550d.tar.gz uscxml-0e0be07906a720ae54e4572d6ac0cb657424550d.tar.bz2 |
Started to port Debugger and issue 87
31 files changed, 4917 insertions, 179 deletions
diff --git a/apps/uscxml-browser.cpp b/apps/uscxml-browser.cpp index 722b0af..a57b79b 100644 --- a/apps/uscxml-browser.cpp +++ b/apps/uscxml-browser.cpp @@ -74,7 +74,7 @@ int main(int argc, char** argv) { if (options.verbose) { StateTransitionMonitor* vm = new StateTransitionMonitor(); vm->copyToInvokers(true); - interpreter.setMonitor(vm); + interpreter.addMonitor(vm); } interpreters.push_back(interpreter); diff --git a/apps/uscxml-debugger.html b/apps/uscxml-debugger.html new file mode 100644 index 0000000..3aece02 --- /dev/null +++ b/apps/uscxml-debugger.html @@ -0,0 +1,2836 @@ +<!doctype html> +<html> +<head> + <!-- <link href="http://localhost/~sradomski/jsPlumb/font-awesome.css" rel="stylesheet"> --> + <!-- <link rel="stylesheet" href="http://localhost/~sradomski/jsPlumb/demo-all.css"> --> + <!-- <link rel="stylesheet" href="http://localhost/~sradomski/jsPlumb/demo.css"> --> + + <!-- script src="https://google-code-prettify.googlecode.com/svn/loader/prettify.js?autoload=false"></script --> + <!--script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js?autoload=false&lang=xml" defer="defer"></script--> + + <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> + <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script> + <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script> + <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/additional-methods.min.js"></script> + <script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> + + <script src="http://cdn.jsdelivr.net/ace/1.1.01/min/ace.js" charset="utf-8"></script> + <script src="http://vkbeautify.googlecode.com/files/vkbeautify.0.99.00.beta.js" charset="utf-8"></script> + <script src="http://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js" charset="utf-8"></script> + <script src="http://dean.edwards.name/base/Base.js" charset="utf-8"></script> + <!-- alternatively http://maxailloud.github.io/confirm-bootstrap/#liveDemo --> + + <script src="https://rawgithub.com/jibe914/Bootstrap-Confirmation/master/bootstrap3-confirmation.js" charset="utf-8"></script> + <!--script src="https://raw.githubusercontent.com/jibe914/Bootstrap-Confirmation/master/bootstrap3-confirmation.js" charset="utf-8"></script--> + <!-- script src="https://raw.github.com/Tavicu/bootstrap-confirmation/master/bootstrap-confirmation.js" charset="utf-8"></script --> + + + <script src="https://rawgithub.com/msurguy/ladda-bootstrap/master/dist/spin.js" charset="utf-8"></script> + <script src="https://rawgithub.com/msurguy/ladda-bootstrap/master/dist/ladda.js" charset="utf-8"></script> + <script src="https://rawgithub.com/prettycode/Object.identical.js/master/Object.identical.js" charset="utf-8"></script> + + + <!-- <link rel="stylesheet" href="jQuery-File-Upload/css/style.css"> + <link rel="stylesheet" href="jQuery-File-Upload/css/jquery.fileupload.css"> --> + + + + <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" /> + <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> + <!-- <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css"> --> + <!-- <link rel="stylesheet" href="http://twitter.github.io/typeahead.js/css/main.css" /> --> + <link rel="stylesheet" href="https://raw.githubusercontent.com/hyspace/typeahead.js-bootstrap3.less/master/typeahead.css" /> + <link rel="stylesheet" href="https://rawgithub.com/msurguy/ladda-bootstrap/master/dist/ladda-themeless.min.css" /> + + <!-- script src="jquery-ui-1.10.4.uscxml/js/jquery-1.10.2.js"></script> + <script src="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.5/select2.min.js" charset="utf-8"></script> + <link rel="stylesheet" href="http://twitter.github.io/typeahead.js/css/main.css" /> + <script src="Base.js"></script> + <script src="jquery-ui-1.10.4.uscxml/js/jquery-ui-1.10.4.custom.js"></script> + <script src="http://localhost/~sradomski/ace/ace.js" charset="utf-8"></script> + <script src="http://localhost/~sradomski/vkbeautify.0.99.00.beta.js" charset="utf-8"></script> + <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/select2/3.4.5/select2.min.css"> + + <script src='http://localhost/~sradomski/jsPlumb/jquery.min.js'></script> + <script src='http://localhost/~sradomski/jsPlumb/jquery-ui.min.js'></script> + <script src='http://localhost/~sradomski/jsPlumb/jquery.ui.touch-punch.min.js'></script> + <script src="//cdnjs.cloudflare.com/ajax/libs/ace/1.1.01/ace.js"></script> + <script src='http://localhost/~sradomski/jsPlumb/jquery.jsPlumb-1.5.5.js'></script> + <script src="http://localhost/~sradomski/ace/ace.js" charset="utf-8"></script> + <script src="http://localhost/~sradomski/jquery.dropdown.min.js" charset="utf-8"></script> + <script src="http://localhost/~sradomski/bootstrap/js/bootstrap.min.js"></script --> + + <!-- link href="jquery.dropdown.css" rel="stylesheet" / --> + <!-- <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/dark-hive/jquery-ui.css"> --> + <!-- link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" --> + + <style type="text/css"> + + body { +/* font-family: "Trebuchet MS", "Helvetica", "Arial", "Verdana", "sans-serif";*/ + } + + .windowlet { + position: absolute; + top: 3em; + left: 5em; + box-shadow: 5px 2px 19px #aaa; + -o-box-shadow: 5px 2px 19px #aaa; + -webkit-box-shadow: 5px 2px 19px #aaa; + -moz-box-shadow: 5px 2px 19px #aaa; + } + + .windowlet .panel-body { + padding: 8px; + } + + .windowlet textarea { + resize:vertical; + } + + .debug { + position:fixed !important; + width: 320px; + min-width: 250px; + } + + .debug > .panel > .panel-group { +/* overflow: hidden;*/ + margin-bottom: 0px; + } + + .debug > .panel { + margin-bottom: 0; + } + + .debug > .panel > .panel-footer { + padding: 5px 5px; + font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace; + font-size: 10px; + } + + .panel-group .panel { +/* border: 1px solid transparent;*/ + overflow: visible; + } + + .debug-header { + cursor: move; +/* padding: 10px 15px 5px 15px; +*/ } + + .subtitle { + font-size: 0.7em; + font-weight: normal; + } + + /* nested panels */ + .panel .panel .panel-heading { + padding: 8px 15px; + } + + .panel .panel-group .panel { + border-radius: 0px; + } + + .panel .panel .panel-title { + font-size: 100%; /* Override explicit font-size from bootstrap */ + } + + .panel .panel { + font-size: 90%; + } + + .panel-group .panel+.panel { + margin-top: 0px; + } + + .panel-heading { + position: relative; + } + .heading-decoration { + position: absolute; + right: 40px; + top: 4px; + } + + .heading-decoration .badge { + float: left; + margin-top: 2px; + margin-right: 5px; + } + + .panel-heading .panel-toggle:after { + /* symbol for "opening" panels */ + font-family: 'Glyphicons Halflings'; /* essential for enabling glyphicon */ + content: "\e114"; /* adjust as needed, taken from bootstrap.css */ + float: right; /* adjust as needed */ + color: grey; /* adjust as needed */ + } + .panel-heading.collapsed .panel-toggle:after { + /* symbol for "collapsed" panels */ + content: "\e080"; /* adjust as needed, taken from bootstrap.css */ + } + + /* see http://stackoverflow.com/questions/18059161/css-issue-on-twitter-typeahead-with-bootstrap-3 */ + .twitter-typeahead .tt-hint { + display: block; + height: 34px; + padding: 6px 12px; + font-size: 14px; + line-height: 1.428571429; + border: 1px solid transparent; + } + + .form-group .twitter-typeahead { + width: 100%; + } + + .messages-content textarea.messages { + font-size: 10px; + cursor: auto; + white-space: pre; + overflow: auto; + font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace; + height: 16em; + } + + ul.dropdown-menu li.server-session { + padding-left: 0.7em; + } + + ul.recent-documents span.recent-document-features { + padding-left: 0.7em; + } + + .tooltip-inner { + background-color: #333; + } + .tooltip.bottom .tooltip-arrow { + border-bottom-color: #333; + background-color: #FFF transparent; + } + + .dom { + font-size: 10px; + font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace; + color: #666; + } + + .dom .active-state { + color: #000; + } + + .dom .xpath-selected { + color: #A00; + font-weight: bold; + } + + </style> + + <script type="text/javascript"> + + var SCXMLEditor = Base.extend({ + constructor: function(params) { + $.extend(this, params); + + this.debugger = new SCXMLEditor.Debugger.USCXML({ + debuggerURL: "http://localhost:8088", + scxmlEditor: this, + }); + + this.xmlView = new SCXMLEditor.XMLView({ + destNode: $("body").find(".dom")[0], + }); + + $(this.debugger.getNode()).appendTo($("body")); + }, + + scxmlDOM: undefined, + scxmlURL: undefined, + scxmlRoot: undefined, + scxmlStates: {}, + + setDocumentNameFromURL: function(url) { + var tmp = $('<a>', { href:url } )[0]; + $(this.debugger.debugDocumentNameNode).html(tmp.pathname.split('/').pop()); + $(this.debugger.saveBreakpointsNode).closest("li").removeClass("disabled"); + }, + + loadURL: function(scxmlURL) { + this.scxmlURL = scxmlURL; + this.setDocumentNameFromURL(scxmlURL); + + if (scxmlURL in this.debugger.recentDocuments) { + // load breakpoints + if ('breakpoints' in this.debugger.recentDocuments[scxmlURL]) { + for (var index in this.debugger.recentDocuments[scxmlURL].breakpoints) { + var breakpointData = this.debugger.recentDocuments[scxmlURL].breakpoints[index]; + var breakpoint = new SCXMLEditor.Debugger.Breakpoint({ + data: breakpointData + }); + + var serializedBreakpoint = JSON.stringify(breakpoint.toWireFormat()); + var alreadyExists = false; + + // check if such a breakpoint already exists + for (var index in this.debugger.breakpoints) { + var otherBreakpoint = this.debugger.breakpoints[index]; + if (serializedBreakpoint === JSON.stringify(otherBreakpoint.toWireFormat())) { + alreadyExists = true; + break; + } + } + + if (!alreadyExists) + this.debugger.addBreakpoint(breakpoint); + } + } + } + + // get instance into scope chain + var scxmlEdit = this; + $.ajax({ + type: "GET", + dataType: "text xml", + url: scxmlURL, + success: function (dom) { + scxmlEdit.loadDOM(dom); + }, + error: function(data) { + console.log(data); + } + }); + }, + + loadDOM: function(scxmlDOM) { + this.scxmlDOM = scxmlDOM; + this.scxmlRoot = $(scxmlDOM).find("scxml")[0]; + + // notify the debugger + this.debugger.loadDOM(scxmlDOM); + + // var domText = SCXMLEditor.xmlToString(this.scxmlDOM); + // $("body").find("pre.dom").text(domText); + if (this.xmlView) { + this.xmlView.setXML(this.scxmlDOM); + this.xmlView.prettyPrint(); + } + + // add 'nested' class to states of nested scxml documents + $(this.scxmlRoot).find("scxml").find("state, final, parallel, history").addClass("nested"); + $(this.getNode()).appendTo(this.containerNode); + }, + + getNode: function() { + this.node = document.createElement('div'); + this.node.className = "scxml-editor"; + this.node.innerHTML = '\ + <div class="state-tree"></div>\ + <div class="tools"></div>\ + '; + + this.stateTreeNode = $(this.node).children("div.state-tree"); + this.toolsNode = $(this.node).children("div.tools"); + + // var rootState = new SCXMLEditor.PortletStateView({ + // scxmlState: this.scxmlRoot, + // scxmlEditor: this + // }); + // this.rootNode = rootState.getNode(); + // $(this.rootNode).appendTo(this.stateTreeNode); + // $(this.rootNode).draggable(); + + return this.node; + }, + + getAllChildStates: function() { + return $(this.scxmlRoot).find("state, parallel, final, history").not(".nested"); + }, + + getAllStateIds: function() { + var stateIds = []; + var self = this; + $.each(this.getAllChildStates(), function(index, state) { + stateIds.push(SCXMLEditor.getStateId(state)); + }); + return stateIds; + } + + }); + + SCXMLEditor.getStateId = function(state) { + return $(state).attr("id") || $(state).attr("name") || "scxml"; + } + + SCXMLEditor.getStateType = function(state) { + return state.nodeName.toLowerCase(); + } + + SCXMLEditor.xmlToString = function (xmlNode) { + var text = xmlNode.xml || (new XMLSerializer()).serializeToString(xmlNode); + return vkbeautify.xml(text); + } + + SCXMLEditor.XMLView = Base.extend({ + constructor: function(params) { + $.extend(this, params); + }, + + prettyPrint: function(destNode) { + var self = this; + + function escapeText(text) { + return $('<div/>').text(text).html(); + } + function prettyIndent(xmlNode, indentation) { + var outputText = ''; + var indenter = ''; + + for (var i = 0; i < indentation; i++) { + indenter += " "; + } + var isActive = false; + var isXPathSelected = false; + + switch(xmlNode.nodeType) { + case Node.ELEMENT_NODE: + if (xmlNode.nodeName.toLowerCase() === "state") { + var stateId = xmlNode.getAttribute("id"); + if ($.inArray(stateId, self.activeStates) >= 0) { + outputText += '<span class="active-state">'; + isActive = true; + } + } + + if (xpathNodeSet && xpathNodeSet.snapshotLength > 0) { + for (var i = 0; i < xpathNodeSet.snapshotLength; i++) { + var currNode = xpathNodeSet.snapshotItem(i); + // console.log("Comparing:"); + // console.log(currNode); + // console.log(xmlNode); + if (currNode === xmlNode) { + // console.log("currNode:"); + // console.log(currNode); + outputText += '<span class="xpath-selected">'; + isXPathSelected = true; + break; + } + } + } + + outputText += indenter; + outputText += '<' + xmlNode.nodeName; + for (var j = 0; j < xmlNode.attributes.length; j++) { + var attribute = xmlNode.attributes.item(j); + outputText += " " + attribute.nodeName + "=\"" + attribute.nodeValue + "\""; + } + if (!xmlNode.hasChildNodes()) { + outputText += ' />'; + outputText += '<br />'; + } else { + outputText += '>'; + outputText += '<br />'; + } + break; + case Node.COMMENT_NODE: + outputText += indenter; + outputText += '<!-- ' + escapeText(xmlNode.nodeValue.trim()) + ' -->'; + outputText += '<br />'; + break; + case Node.TEXT_NODE: + var text = xmlNode.nodeValue.trim(); + if (text.length > 0) { + outputText += indenter; + outputText += escapeText(text); + outputText += '<br />'; + } + break; + case Node.ATTRIBUTE_NODE: + case Node.CDATA_SECTION_NODE: + case Node.ENTITY_REFERENCE_NODE: + case Node.ENTITY_NODE: + case Node.PROCESSING_INSTRUCTION_NODE: + case Node.DOCUMENT_NODE: + case Node.DOCUMENT_TYPE_NODE: + case Node.DOCUMENT_FRAGMENT_NODE: + case Node.NOTATION_NODE: + default: + } + + var child = xmlNode.firstChild; + while(child) { + outputText += prettyIndent(child, indentation + 1); + child = child.nextSibling; + } + + switch(xmlNode.nodeType) { + case Node.ELEMENT_NODE: + if (xmlNode.hasChildNodes()) { + outputText += indenter; + outputText += '</' + xmlNode.nodeName; + outputText += '>'; + outputText += '<br />'; + } + break; + default: + } + + if (isActive) { + outputText += '</span>'; + } + if (isXPathSelected) { + outputText += '</span>'; + } + + return outputText; + } + + if (typeof this.xmlNode == "string") { + this.xmlNode = $.parseXML(this.xmlNode); + } + + // this.xpath = "//*[local-name() = \"state\"][@id=\"s0\"]/*[local-name() = \"onentry\"][1]/*[local-name() = \"foreach\"][1]"; + // this.xpath = "//*[local-name() = \"state\"][@id=\"s0\"]/*[local-name() = \"onentry\"][1]/*[local-name() = \"foreach\"][1]"; + var xpathNodeSet = undefined; + + // console.log("xpath:"); + // console.log(this.xpath); + + if (this.xpath) { + try { + xpathNodeSet = this.xmlNode.evaluate( + this.xpath, + this.xmlNode, + function (prefix) { + switch (prefix) { + case 'scxml': + default: + return 'http://www.w3.org/2005/07/scxml'; + } + return null; + }, + XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, + null); + for (var i = 0; i < xpathNodeSet.snapshotLength; i++) { + var currNode = xpathNodeSet.snapshotItem(i); + // console.log("Got node:"); + // console.log(currNode); + } + + } catch(e) { + console.log(e); + } + } + + + var xmlMarkup = '<font color=#888>' + prettyIndent(this.xmlNode, 0) + '</font>'; + + + if (destNode) { + $(destNode).html(xmlMarkup); + } else { + $(this.destNode).html(xmlMarkup); + } + }, + + setDestinationNode: function(destNode) { + this.destNode = destNode; + }, + + setXML: function(xmlNode) { + this.xmlNode = xmlNode; + }, + + setElementXPath: function(xpath) { + this.xpath = xpath; + }, + + setActiveStates: function(states) { + this.activeStates = states; + }, + }); + + // base class of all views onto a state + SCXMLEditor.StateView = Base.extend({ + constructor: function(params) { + $.extend(this, params); + this.scxmlStateType = SCXMLEditor.getStateType(this.scxmlState); + this.scxmlStateId = SCXMLEditor.getStateId(this.scxmlState); + + $(this.scxmlState).bind("DOMSubtreeModified", this.scxmlStateSubtreeModified) + }, + + node: undefined, + scxmlStateType: undefined, + scxmlStateId: undefined, + scxmlState: undefined, + + getNode: function() { + console.log("SCXMLEditor.StateView.getNode()"); + }, + + scxmlStateSubtreeModified: function() { + console.log("SCXMLEditor.StateView.scxmlStateSubtreeModified()"); + }, + + isRoot: function() { + return $(this.scxmlState).prop("tagName").toLowerCase() === "scxml"; + }, + + getDirectChildStates: function() { + return $(this.scxmlState).children("state, parallel, final, history").not(".nested"); + }, + + getAllChildStates: function() { + return $(this.scxmlState).find("state, parallel, final, history").not(".nested"); + }, + + createTransitions: function() { + + } + }); + + // view a state in a small windowlet + SCXMLEditor.ToolsStateView = SCXMLEditor.StateView.extend({ + constructor: function(params) { + this.base(params); + }, + + getNode: function() { + + var tools = this; + + this.node = document.createElement('div'); + this.node.className = 'windowlet tools ui-widget-content'; + this.node.innerHTML = '\ + <div class="ui-widget-header">' + this.getStateId() + '<span class="ui-icon ui-icon-close"></span></div>\ + '; + + $(this.node).children( ".ui-widget-header" ).children( ".ui-icon-close" ).click(function() { + $(tools.node).remove(); + }); + + $(this.node).resizable(); + $(this.node).draggable(); + $(this.node).css("position", "absolute"); + $(this.node).appendTo(this.containerNode); + + return this.node; + }, + + scxmlStateSubtreeModified: function() { + console.log("SCXMLEditor.StateView.scxmlStateSubtreeModified()"); + }, + + }); + + SCXMLEditor.PortletStateView = SCXMLEditor.StateView.extend({ + constructor: function(params) { + this.base(params); + }, + + getNode: function() { + + this.node = document.createElement('div'); + this.node.className = 'portlet'; + + this.node.innerHTML = '\ + <div class="portlet-header"><span class="stateId">' + this.scxmlStateId + '</span></div>\ + <div class="portlet-content"></div>\ + '; + + this.portletHeader = $(this.node).children("div.portlet-header")[0]; + this.portletContent = $(this.node).children("div.portlet-content")[0]; + + var scxmlState = this.scxmlState; + + var settingsButton = document.createElement('button'); + settingsButton.innerHTML = 'Settings'; + $(settingsButton).button({ + icons: { + primary: "ui-icon-gear", + secondary: "ui-icon-triangle-1-s" + }, + text: false + }); + + var settingsMenu = document.createElement('ul'); + settingsMenu.innerHTML = '\ + <li><a href="#"><span class="ui-icon ui-icon-plus"></span>Add nested State</a></li>\ + <li><a href="#"><span class="ui-icon ui-icon-arrow-4-diag"></span>Move this State</a></li>\ + <li><a href="#"><span class="ui-icon ui-icon-trash"></span>Remove this State</a></li>\ + <li>-</li>\ + <li><a href="#"><span class="ui-icon ui-icon-transfer-e-w"></span>Transitions</a></li>\ + <li><a href="#"><span class="ui-icon ui-icon-gear"></span>Executable Content</a></li>\ + <li><a href="#"><span class="ui-icon ui-icon-tag"></span>Datamodel</a></li>\ + <li><a href="#"><span class="ui-icon ui-icon-document"></span>Invokers</a></li>\ + <li><a href="#"><span class="ui-icon ui-icon-zoomin"></span>States</a></li>\ + '; + + $(settingsMenu).menu().hide(); + + $(settingsButton).click(function() { + $(settingsMenu).show().position({ + my: "left top", + at: "left top", + of: this + }); + $(settingsMenu).find("a").click(function event() { + $(settingsMenu).hide(); + }); + $(settingsMenu).mouseleave(function(event) { + $(settingsMenu).hide(); + }); + return false; + }) + + $(settingsMenu).prependTo(this.portletHeader); + $(settingsButton).prependTo(this.portletHeader); + + var childs = this.getDirectChildStates(); + var nrChilds = childs.length + 1; + + var nrCols = Math.ceil(Math.sqrt(nrChilds)); + var nrRows = Math.ceil(nrChilds / nrCols); + + // console.log(nrCols + ' / ' + nrRows); + for (var col = 0; col < nrCols; col++) { + var colNode = document.createElement('div'); + colNode.className = "column"; + + for (var row = 0; row < nrRows; row++) { + if (childs.length > col * nrRows + row) { + var child = childs[col * nrRows + row]; + var childPortlet = new SCXMLEditor.PortletStateView({ + scxmlState: child, + scxmlEditor: this.scxmlEditor + }); + $(childPortlet.getNode()).appendTo(colNode); + } + } + $(colNode).appendTo(this.portletContent); + + $(colNode).sortable({ + connectWith: ".column", + handle: ".portlet-header", + cancel: ".portlet-toggle", + placeholder: "portlet-placeholder ui-corner-all" + }); + } + + // see https://api.jqueryui.com/theming/icons/ + $(this.node) + .addClass( "ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" ) + .children( ".portlet-header" ) + .addClass( "ui-widget-header ui-corner-all" ) + .prepend( "<span class='ui-icon ui-icon-minusthick portlet-toggle'></span>") + .prepend( "<span class='ui-icon ui-icon-newwin portlet-detach'></span>") + .append( " "); + + $(this.node).children( ".portlet-header" ).children( ".portlet-toggle" ).click(function() { + var icon = $(this); + icon.toggleClass( "ui-icon-minusthick ui-icon-plusthick" ); + icon.closest( ".portlet" ).children( ".portlet-content" ).toggle(); + }); + + $(this.node).children( ".portlet-header" ).children( ".portlet-detach" ).click(function() { + var detach = $(this); + detach.toggleClass( "ui-icon-arrowthickstop-1-s ui-icon-newwin" ); + this.tools = new SCXMLEditor.ToolsStateView({ + scxmlState: scxmlState, + scxmlEditor: this.scxmlEditor + }); + $(this.tools.getNode()).appendTo($("body")); // TODO ought to be relative? + }); + return this.node; + + }, + }) + + SCXMLEditor.Debugger = Base.extend({ + constructor: function(params) { + this.breakpointsEnabled = true; + $.extend(this, params); + }, + + breakpoints: [], + currEvent: {}, + + loadDOM: function(scxmlDOM) { + // called by the editor, inherit to e.g. activate buttons + }, + + getNode: function() { + + var debug = this; + this.node = document.createElement('div'); + this.node.className = 'windowlet debug'; + $(this.node).css("position", "absolute"); + + // data-parent="#accordion" missing from <a> to show multiple panels + this.node.innerHTML = '\ + <div class="panel panel-default">\ + <div class="panel-heading debug-header">\ + <h4 class="panel-title">Debugger<span class="toolbar pull-right"></span></h4>\ + <!--span class="subtitle document-name">No Document loaded</span -->\ + </div>\ + <div class="panel-group">\ +<!-- Breakpoints -->\ + <div class="breakpoints panel panel-default">\ + <div class="panel-heading" data-toggle="collapse" data-target="#collapseBreakpoints">\ + <span class="panel-toggle"></span>\ + <h4 class="panel-title">Breakpoints<br/><span class="subtitle current-breakpoint"></span>\ + <i class="heading-decoration pull-right">\ + <div class="btn-group">\ + <button title="Deactivate all Breakpoints" class="deactivate btn btn-default btn-xs btn-primary"><span class="glyphicon glyphicon-off"></span></button>\ + </div>\ + <div class="btn-group">\ + <button title="Add a new Breakpoint" class="new btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span></button>\ + <button \ + title="Remove all Breakpoints" class="delete-all btn btn-default btn-xs"><span class="glyphicon glyphicon-remove"></span></button>\ + </div>\ + </i>\ + </h4>\ + </div>\ + <div id="collapseBreakpoints" class="breakpoints-content panel-collapse collapse">\ + </div>\ + </div>\ +<!-- Current Event -->\ + <!--div class="event panel panel-default">\ + <div class="panel-heading" data-toggle="collapse" data-target="#collapseEvent">\ + <h4 class="panel-title">Current Event\ + <i class="heading-decoration pull-right">\ + <div class="btn-group">\ + <button title="Insert a new Event" class="new btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span></button>\ + <button title="Edit current Event" class="edit btn btn-default btn-xs"><span class="glyphicon glyphicon-pencil"></span></button>\ + <button title="View Event in Window" class="view btn btn-default btn-xs"><span class="glyphicon glyphicon-search"></span></button>\ + </div>\ + </i>\ + <span class="panel-toggle"></span></h4>\ + </div>\ + <div id="collapseEvent" class="panel-collapse collapse">\ + <div class="panel-body event-content"></div>\ + </div>\ + </div-->\ +<!-- Active States -->\ + <!-- div class="states panel panel-default">\ + <div class="panel-heading" data-toggle="collapse" data-target="#collapseStates">\ + <h4 class="panel-title">Active States<span class="panel-toggle"></span></h4>\ + </div>\ + <div id="collapseStates" class="panel-collapse collapse">\ + <div class="panel-body states-content">\ + Anim pariatur cliche reprehenderit, enim eiusmod high life\ + </div>\ + </div>\ + </div -->\ +<!-- Datamodel -->\ + <div class="datamodel panel panel-default">\ + <div class="panel-heading" data-toggle="collapse" data-target="#collapseDatamodel">\ + <h4 class="panel-title">Datamodel\ + <!--i class="heading-decoration pull-right">\ + <div class="btn-group">\ + <button title="Clear Log" class="clear btn btn-default btn-xs"><span class="glyphicon glyphicon-trash"></span></button>\ + <button title="View Log in Window" class="view btn btn-default btn-xs"><span class="glyphicon glyphicon-search"></span></button>\ + </div>\ + </i-->\ + <span class="panel-toggle"></span></h4>\ + </div>\ + <div id="collapseDatamodel" class="panel-collapse collapse">\ + <div class="panel-body datamodel-content"></div>\ + </div>\ + </div>\ +<!-- Assets -->\ + <!--div class="assets panel panel-default">\ + <div class="panel-heading" data-toggle="collapse" data-target="#collapseAssets">\ + <h4 class="panel-title">Assets\ + <i class="heading-decoration pull-right">\ + <div class="btn-group">\ + <button title="New Asset" class="view btn btn-default btn-xs"><span class="glyphicon glyphicon-plus"></span></button>\ + </div>\ + </i>\ + <span class="panel-toggle"></span></h4>\ + </div>\ + <div id="collapseAssets" class="panel-collapse collapse">\ + <div class="panel-body assets-content"></div>\ + </div>\ + </div-->\ +<!-- Message -->\ + <div class="messages panel panel-default">\ + <div class="panel-heading" data-toggle="collapse" data-target="#collapseMessages">\ + <h4 class="panel-title">Messages\ + <i class="heading-decoration pull-right">\ + <span class="badge"></span>\ + <div class="btn-group">\ + <button title="Clear Log" class="clear btn btn-default btn-xs"><span class="glyphicon glyphicon-trash"></span></button>\ + <button title="Text Wrap" class="textwrap btn btn-default btn-xs"><span class="glyphicon glyphicon-align-left"></span></button>\ + </div>\ + </i>\ + <span class="panel-toggle"></span></h4>\ + </div>\ + <div id="collapseMessages" class="panel-collapse collapse in">\ + <div class="panel-body messages-content">\ + <textarea class="form-control messages" wrap="off" readonly></textarea>\ + </div>\ + </div>\ + </div>\ + </div>\ + <div class="panel-footer">\ + <span class="status-indicators"></span>\ + <span class="pull-right document-name"></span>\ + </div>\ + </div>\ + '; + + $(this.node).find("div.panel-collapse").not(".in").prev().addClass("collapsed"); + + + // get some nodes + this.debugFooterNode = $(this.node).find(".panel-footer")[0]; + this.statusIndicatorNode = $(this.node).find(".status-indicators")[0]; + this.debugDocumentNameNode = $(this.node).find(".document-name")[0]; + this.currentBreakpointNode = $(this.node).find(".current-breakpoint")[0]; + this.toolbarNode = $(this.node).find(".toolbar")[0]; + + this.messagesNode = $(this.node).find("div.messages-content")[0]; + this.messagesBadgeNode = $(this.node).find("div.messages").find("span.badge")[0]; + this.clearMessagesButton = $(this.node).find("div.messages").find("button.clear")[0]; + this.textwrapMessagesButton = $(this.node).find("div.messages").find("button.textwrap")[0]; + this.messagesTextarea = $(this.node).find("div.messages-content").find("textarea")[0]; + + this.breakpointNode = $(this.node).find("div.breakpoints-content")[0]; + this.deleteAllBreakpointsButton = $(this.node).find("div.breakpoints").find("button.delete-all")[0]; + this.disableBreakpointsButton = $(this.node).find("div.breakpoints").find("button.deactivate")[0]; + this.addNewBreakpointsButton = $(this.node).find("div.breakpoints").find("button.new")[0]; + + this.currEventNode = $(this.node).find("div.event-content")[0]; + this.insertEventButton = $(this.node).find("div.event").find("button.new")[0]; + this.editEventButton = $(this.node).find("div.event").find("button.edit")[0]; + this.viewEventButton = $(this.node).find("div.event").find("button.view")[0]; + + this.dataModelNode = $(this.node).find("div.datamodel-content")[0]; + this.clearDataModelLogButton = $(this.node).find("div.datamodel").find("button.clear")[0]; + this.viewDataModelLogButton = $(this.node).find("div.datamodel").find("button.view")[0]; + + this.initBreakpoints(); + this.initToolBar(); +// this.initCurrEvent(); + this.initDataModel(); + this.initMessages(); + + // enable tooltips + $(this.node).find("button").tooltip({ + placement: 'bottom', + container: 'body', + delay: { show: 300, hide: 100 } + }); + + $(this.node).resizable({grid: [1, 100000]}); + $(this.node).draggable({ + handle: $(this.node).find("div.debug-header") + }); + + return this.node; + }, + + initMessages: function() { + var debug = this; + + $(this.clearMessagesButton).prop('disabled', true); + + $(this.node).find('#collapseMessages') + .unbind('show.bs.collapse') + .unbind('shown.bs.collapse') + .on('show.bs.collapse', function () { + $(debug.messagesBadgeNode).html(""); // remove number from badge + }).on('shown.bs.collapse', function () { + debug.messagesTextarea.scrollTop = debug.messagesTextarea.scrollHeight; // scroll to bottom (won't work when hidden) + }); + + $(this.messagesTextarea) + .unbind('mouseenter') + .unbind('mouseleave') + .mouseenter(function () { + $(this).addClass("dont-scroll"); + }) + .mouseleave(function () { + $(this).removeClass("dont-scroll"); + }); + + $(this.clearMessagesButton) + .unbind('mouseenter') + .unbind('mouseleave') + .unbind('click') + .mouseenter(function() { + $(this).addClass("btn-warning"); + }).mouseleave(function() { + $(this).removeClass("btn-warning"); + }).click(function() { + debug.messagesTextarea.value = ""; + $(debug.messagesBadgeNode).html(""); + $(this).prop('disabled', true); + $(this).removeClass("btn-warning"); + $(this).tooltip("hide"); + return false; + }); + + $(this.textwrapMessagesButton).unbind('click') + .click(function() { + if ($(debug.messagesTextarea).attr("wrap") == "on") { + $(this).removeClass("active"); + $(debug.messagesTextarea).attr("wrap", "off"); + } else { + $(this).addClass("active"); + $(debug.messagesTextarea).attr("wrap", "on"); + } + return false; + }); + + + }, + + initToolBar: function() { + var debug = this; + + this.toolbarNode.innerHTML = '\ + <div class="btn-group">\ + <button class="control btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">\ + <span class="glyphicon glyphicon-cog"></span>\ + <span class="caret"></span>\ + </button>\ + <ul class="dropdown-menu">\ + <li><a class="load-from-url" href="#"><i class="glyphicon glyphicon-import"></i> Load Document</a></li>\ + <li><a class="save-breakpoints" href="#"><i class="glyphicon glyphicon-floppy-save"></i> Save Breakpoints</a></li>\ + </ul>\ + </div>\ + <div class="btn-group">\ + <button title="Start Execution" class="start btn btn-default btn-xs"><span class="glyphicon glyphicon-play"></span></button>\ + <div class="btn-group" data-toggle="buttons-checkbox">\ + <button title="Pause Execution" class="pause btn btn-default btn-xs"><span class="glyphicon glyphicon-pause"></span></button>\ + </div>\ + <button title="Step Execution" class="step btn btn-default btn-xs"><span class="glyphicon glyphicon-step-forward"></span></button>\ + </div>\ + ' + this.controlButtonNode = $(this.toolbarNode).children("div.btn-group").children("button.control")[0]; + this.controlDropdownNode = $(this.toolbarNode).children("div.btn-group").children("ul.dropdown-menu")[0]; + this.loadFromURLNode = $(this.toolbarNode).find("a.load-from-url")[0]; + this.saveBreakpointsNode = $(this.toolbarNode).find("a.save-breakpoints")[0]; + this.startButtonNode = $(this.toolbarNode).children("div.btn-group").children("button.start")[0]; + this.pauseButtonNode = $(this.toolbarNode).children("div.btn-group").find("button.pause")[0]; + this.stepButtonNode = $(this.toolbarNode).children("div.btn-group").children("button.step")[0]; + this.globalDropDownNode = $(this.toolbarNode).find(".dropdown-menu")[0]; + + $(this.loadFromURLNode).unbind('click').click(function() { + debug.showLoadDialog(); + return false; + }); + + $(this.saveBreakpointsNode) + .unbind('click').click(function() { + debug.recentDocuments[debug.scxmlEditor.scxmlURL].breakpoints = []; + for (var key in debug.breakpoints) { + debug.recentDocuments[debug.scxmlEditor.scxmlURL].breakpoints.push(debug.breakpoints[key].toWireFormat()); + } + localStorage.recentDocuments = JSON.stringify(debug.recentDocuments); + $(debug.controlButtonNode).effect('highlight', {color: '#3AA453'}, 600); + return false; + }) + .closest("li").addClass("disabled"); + + + $(this.startButtonNode).prop('disabled', true); + $(this.pauseButtonNode).prop('disabled', true); + $(this.stepButtonNode).prop('disabled', true); + + }, + + showLoadDialog: function() { + var debug = this; + + // do we already have a modal div? + $("body").children("div#loadFromURLModal").remove(); + + // load from URL form - modal window + this.loadFromURLModalNode = $('\ + <div class="modal fade" id="loadFromURLModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">\ + <div class="modal-dialog">\ + <form action="" id="loadFromURLModalForm">\ + <div class="modal-content">\ + <div class="modal-header">\ + <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\ + <h4 class="modal-title" id="myModalLabel">Load SCXML from URL</h4>\ + </div>\ + <div class="modal-body">\ + <div class="form-group">\ + <div class="input-group">\ + <span class="input-group-btn">\ + <button title="Recent Documents" type="button" class="btn dropdown-toggle" data-toggle="dropdown">\ + <span class="glyphicon glyphicon-list-alt"></span>\ + </button>\ + <ul class="recent-documents dropdown-menu" role="menu" />\ + </span>\ + <input id="loadFromURLModalInput" name="scxmlUrl" class="form-control" placeholder="URL of SCXML document">\ + </div>\ + </div>\ + </div>\ + <div class="modal-footer">\ + <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\ + <button type="submit" class="btn btn-primary">Load</button>\ + </div>\ + </div>\ + </form>\ + </div>\ + </div>\ + '); + + this.recentDocumentsNode = $(this.loadFromURLModalNode).find("ul.recent-documents")[0]; + $(this.recentDocumentsNode).prev().attr('disabled', true); + + this.recentDocuments = {}; + if (localStorage.recentDocuments) { + this.recentDocuments = JSON.parse(localStorage.recentDocuments); + for (var key in this.recentDocuments) { + if ((key.substring(0, 4) != "http") && (key.substring(0, 4) != "file")) { + // no idea, but it's not a document + delete this.recentDocuments[key]; + continue; + } + var recentURL = key; // not sure, but key is not available in callback + // we found a document, enable "recent" button + $(this.recentDocumentsNode).prev().attr('disabled', false); + + var recentDocumentItem = $('\ + <li><a href="#"><!-- i class="glyphicon glyphicon-floppy-open" style="margin-right: 0.7em" / -->' + key + '<span class="recent-document-features"></span></a></li>\ + '); + var recentDocumentFeatures = $(recentDocumentItem).find("span.recent-document-features"); + $(recentDocumentItem).children("a").click(function() { + $(debug.loadFromURLModalNode).find("input#loadFromURLModalInput").val(recentURL); + $(debug.recentDocumentsNode).dropdown("toggle"); + return false; + }); + if ('breakpoints' in this.recentDocuments[key] && this.recentDocuments[key].breakpoints.length > 0) { + $(recentDocumentFeatures).append('<i title="Has Breakpoints" class="glyphicon glyphicon-th-list">'); + } + + $(this.recentDocumentsNode).append(recentDocumentItem); + } + } + + $("body").append(this.loadFromURLModalNode); + + var fromURLValidator = $(this.loadFromURLModalNode).find("form#loadFromURLModalForm").validate({ // initialize the plugin + rules: { scxmlUrl: { required: true, url2: true }}, + highlight: function(element) { + $(element).closest('.form-group').removeClass('has-success').addClass('has-error'); + }, + success: function(element) { + element.text('OK!').addClass('valid').closest('.input-group').removeClass('has-error').addClass('has-success'); + }, + submitHandler: function (form) { + $(debug.loadFromURLModalNode).modal("hide"); + return true; + } + }); + + $(this.loadFromURLModalNode).on('hide.bs.modal', function(e) { + if (!fromURLValidator.valid()) { + $(debug.loadFromURLModalNode).find(".form-group").removeClass('has-success').removeClass('has-error'); + $(debug.loadFromURLModalNode).find("input#loadFromURLModalInput").val(""); + } else { + var scxmlURL = $(debug.loadFromURLModalNode).find("input#loadFromURLModalInput").val(); + if (!(scxmlURL in debug.recentDocuments)) { + debug.recentDocuments[scxmlURL] = {}; + } + localStorage.recentDocuments = JSON.stringify(debug.recentDocuments); + debug.scxmlEditor.loadURL(scxmlURL); + } + fromURLValidator.resetForm(); + }); + + $(this.loadFromURLModalNode).modal(); + + }, + + initDataModel: function() { + var debug = this; + + $(this.clearDataModelLogButton).prop('disabled', true); + $(this.viewDataModelLogButton).prop('disabled', true); + + // add functionality to buttons + $(this.clearDataModelLogButton).mouseenter(function() { + $(this).addClass("btn-warning"); + }).mouseleave(function() { + $(this).removeClass("btn-warning"); + }).click(function() { + + return false; + }); + + this.dataModelListingNode = document.createElement('div'); + + this.dataModelEditor = ace.edit(this.dataModelListingNode, {}); + this.dataModelEditor.getSession().setUseWorker(false); + this.dataModelListingNode.style.fontSize='10px'; + this.dataModelEditor.setTheme("ace/theme/tomorrow"); + this.dataModelEditor.getSession().setMode("ace/mode/javascript"); + this.dataModelEditor.setReadOnly(true); + // this.dataModelEditor.getSession().setUseWrapMode(true); + this.dataModelEditor.setShowPrintMargin(false); + this.dataModelEditor.renderer.setShowGutter(false); + // this.dataModelEditor.getSession().setValue(vkbeautify.json(initialContent)); + $(this.dataModelListingNode).css("height", ((this.dataModelEditor.session.getLength() + 10) * 1.5) + "em"); + + $(this.dataModelNode).append(this.dataModelListingNode); + $(this.dataModelNode).append('\ + <div class="input-group input-group-sm">\ + <span class="input-group-btn">\ + <button title="Quick Command" type="button" class="datamodel-suggest btn dropdown-toggle" data-toggle="dropdown">\ + <span class="glyphicon glyphicon-list-alt"></span>\ + </button>\ + <ul class="suggested-commands dropdown-menu" role="menu">\ + <li><a href="#">_event</a></li>\ + <li><a href="#">_name</a></li>\ + <li><a href="#">_sessionid</a></li>\ + <li><a href="#">_invokers</a></li>\ + <li><a href="#">_ioprocessors</a></li>\ + <li><a href="#">_x</a></li>\ + </ul>\ + </span>\ + <input type="text" class="datamodel-eval form-control">\ + <div class="input-group-btn">\ + <button title="Evaluate as String" type="button" class="datamodel-eval btn btn-default"><span class="glyphicon glyphicon-play"></span></button>\ + </div>\ + </div>\ + '); + + this.dataModelInputNode = $(this.dataModelNode).find("input.datamodel-eval")[0]; + this.dataModelInputButton = $(this.dataModelNode).find("button.datamodel-eval")[0]; + this.dataModelSuggestButton = $(this.dataModelNode).find("button.datamodel-suggest")[0]; + + $(this.dataModelSuggestButton).prop('disabled', true); + + $(this.dataModelNode).find("ul.suggested-commands").find("li") + .unbind('click').click(function() { + var expression = $(this).children("a").text(); + $(debug.dataModelInputNode).val(expression); + debug.evalOnDataModel(expression); + $(this).closest("ul").dropdown('toggle'); + return false; + }); + + $(this.dataModelInputNode) + .prop('disabled', true) + .unbind('keypress').keypress(function(e) { + if (e.which == 13) + debug.evalOnDataModel($(this).val()); + }); + + $(this.dataModelInputButton) + .prop('disabled', true) + .unbind('click').click(function() { + debug.evalOnDataModel($(debug.dataModelInputNode).val()); + return false; + }); + + // $(this.dataModelListingNode).resizable({handles: "n, s"}); + // $(this.dataModelListingNode).resizable({grid: [100000, 1], alsoResize: $(this.node)}); + // $(this.dataModelListingNode).unbind('resize').on('resize', function() { + // debug.dataModelEditor.resize(); + // }); + }, + + initBreakpoints: function() { + var debug = this; + + $(this.deleteAllBreakpointsButton) + .unbind('mouseenter') + .unbind('mouseleave') + .unbind('click'); + $(this.deleteAllBreakpointsButton).mouseenter(function() { + $(this).addClass("btn-danger"); + }).mouseleave(function() { + $(this).removeClass("btn-danger"); + }).click(function() { + return false; + }); + + $(this.deleteAllBreakpointsButton).confirmation({ + singleton: true, + container: "body", + btnOkClass: "btn btn-danger", + btnOkLabel: "Delete All", + btnOkIcon: "glyphicon glyphicon-trash", + onConfirm: function() { + while (debug.breakpoints.length > 0) { + debug.removeBreakpoint(debug.breakpoints[0]); + } + } + }); + + $(this.disableBreakpointsButton).unbind('click'); + $(this.disableBreakpointsButton).click(function() { + $(this).toggleClass("btn-primary"); + if ($(this).hasClass("btn-primary")) { + debug.enableAllBreakpoints(); + } else { + debug.disableAllBreakpoints(); + } + return false; + }); + + $(this.addNewBreakpointsButton).unbind('click'); + $(this.addNewBreakpointsButton).click(function() { + debug.addBreakpoint(); + return false; + }); + + this.breakpointNode.innerHTML = '\ + <table class="table table-hover">\ + </table>\ + '; + + for (var index in this.breakpoints) { + $(this.breakpointNode).children("table").append(this.breakpoints[index].getNode()); + } + }, + + initCurrEvent: function() { + var debug = this; + + $(this.insertEventButton).prop('disabled', true); + $(this.editEventButton).prop('disabled', true); + $(this.viewEventButton).prop('disabled', true); + + var initialContent = ''; + + var editor = ace.edit(this.currEventNode, {}); + editor.getSession().setUseWorker(false); + this.currEventNode.style.fontSize='10px'; + editor.setTheme("ace/theme/tomorrow"); + editor.getSession().setMode("ace/mode/javascript"); + editor.setReadOnly(true); + editor.setShowPrintMargin(false); + editor.renderer.setShowGutter(false); + try { + editor.getSession().setValue(vkbeautify.json(initialContent)); + } catch(e) { + editor.getSession().setValue(initialContent); + } + $(this.currEventNode).css("height", (editor.session.getLength() * 1.5) + "em"); + + this.eventViewModal = $('\ + <div class="modal fade bs-example-modal-lg" id="eventViewModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">\ + <div class="modal-dialog modal-lg">\ + <form action="" id="eventViewModalForm">\ + <div class="modal-content">\ + <div class="modal-header">\ + <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\ + <h4 class="modal-title" id="myModalLabel">View / Edit Event</h4>\ + </div>\ + <div class="modal-body">\ + <div class="editor"></div>\ + </div>\ + <div class="modal-footer">\ + <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\ + <button type="submit" class="btn btn-primary">Apply</button>\ + </div>\ + </div>\ + </form>\ + </div>\ + </div>\ + '); + + + $(this.eventViewModal).modal({ + show: false + }); + + // $(this.eventViewModal).find("div.modal-dialog").resizable(); + // $(this.eventViewModal).find("div.modal-dialog").draggable(); + + $("body").append(this.eventViewModal); + + // add functionality to buttons + $(this.viewEventButton).click(function() { + $(debug.eventViewModal).find(".editor"); + + var modalEditorNode = $(debug.eventViewModal).find(".editor")[0]; + + var modalEditor = ace.edit(modalEditorNode, {}); + modalEditorNode.style.fontSize='10px'; + // safari has the cursor somewhat off +// modalEditorNode.style.font='inherit!important'; + modalEditor.setTheme("ace/theme/tomorrow"); + modalEditor.getSession().setMode("ace/mode/javascript"); + modalEditor.setReadOnly(false); + modalEditor.setShowPrintMargin(false); + modalEditor.renderer.setShowGutter(true); + modalEditor.getSession().setValue(vkbeautify.json(initialContent)); + + $(modalEditorNode).css("min-height", (editor.session.getLength() * 1.5) + "em"); + //$(modalEditorNode).css("height", "80%"); + + $(debug.eventViewModal).modal("show"); + return false; + }); + + }, + + log: function(messages) { + var self = this; + + $(this.clearMessagesButton).prop('disabled', false); + + if (!self.messagesTextarea) { + console.log(messages); + return; + } + + function logSingleMsg(msg) { + // only show badge when when collapsed + if (!$(self.messagesNode).parent().hasClass("in")) { + var bagdeContent = parseInt($(self.messagesBadgeNode).html()) || 0; + $(self.messagesBadgeNode).html(bagdeContent + 1); + } + + if (typeof(msg) === 'string') { + self.messagesTextarea.value += msg.trim() + "\n"; + } else if (typeof(msg) === 'object') { + if ('formatted' in msg) { + self.messagesTextarea.value += msg.formatted.trim() + "\n"; + } else if('message' in msg) { + self.messagesTextarea.value += msg.message.trim() + "\n"; + } else { + try { + self.messagesTextarea.value += vkbeautify.json(msg) + "\n"; + } catch(e) { + self.messagesTextarea.value += JSON.stringify(msg).trim() + "\n"; + } + } + } else { + //not sure what to do here + self.messagesTextarea.value += msg; + } + } + + if (messages instanceof Array) { + for (var key in messages) { + logSingleMsg(messages[key]); + } + } else { + logSingleMsg(messages); + } + + // scroll to bottom + if (!$(self.messagesTextarea).hasClass("dont-scroll")) + self.messagesTextarea.scrollTop = self.messagesTextarea.scrollHeight; + }, + + // find a breakpoint by value and return its index + findBreakpoint: function(breakpoint) { + for (var index in this.breakpoints) { + var currBreakpoint = this.breakpoints[index]; + + // $("body").append("<pre>" + JSON.stringify(breakpoint.toWireFormat()) + "</pre><br />"); + // $("body").append("<pre>" + JSON.stringify(currBreakpoint.toWireFormat()) + "</pre><br />"); + if (Object.identical(breakpoint.toWireFormat(), currBreakpoint.toWireFormat())) { + return index; + } + } + return -1; + }, + + editBreakpoint: function(breakpoint) { + breakpoint.showEditDialog() + }, + + editedBreakpoint: function(breakpoint, oldData) { + // override to update your debugger + }, + + skipToBreakpoint: function(breakpoint) { + }, + + addBreakpoint: function(breakpoint) { + var debug = this; + if (breakpoint) { + breakpoint.scxmlEditor = this.scxmlEditor; + breakpoint.scxmlDebugger = this; + + if (this.findBreakpoint(breakpoint) < 0) { + // add as new breakpoint + this.breakpoints.push(breakpoint); + var breakpointNode = breakpoint.getNode(); + if (!debug.breakpointsEnabled) { + $(breakpointNode).find("button.enable").prop('disabled', true); + $(breakpointNode).find("button.skipto").prop('disabled', true); + } + $(this.breakpointNode).children("table").append(breakpointNode); + } + } else { + // show dialog to add a breakpoint + var newBreakpoint = new SCXMLEditor.Debugger.Breakpoint({ + scxmlEditor: this.scxmlEditor, + scxmlDebugger: this + }); + debug.editBreakpoint(newBreakpoint); + } + }, + + removeBreakpoint: function(breakpoint) { + var debug = this; + var breakPointIndex = this.findBreakpoint(breakpoint); + if (breakPointIndex >= 0) { + this.breakpoints.splice(breakPointIndex, 1); + $(this.breakpointNode).children("table").find("tr").slice(breakPointIndex, breakPointIndex + 1).detach(); + } + }, + + highlightBreakpoint: function(breakpoint) { + var breakPointIndex = this.findBreakpoint(breakpoint); + if (breakPointIndex < 0) + return; + + // open breakpoint collapsible + $(this.node).find("div.breakpoints").find("div.panel-collapse").addClass("in"); + + // highlight breakpoint + $(this.breakpointNode) + .children("table") + .find("tr").slice(breakPointIndex, breakPointIndex + 1) + .addClass("info"); + + }, + + unhighlightAllBreakpoints: function() { + $(this.breakpointNode) + .children("table") + .find("tr").removeClass("info"); + }, + + enableBreakpoint: function(breakpoint) { + var breakPointIndex = this.findBreakpoint(breakpoint); + if (breakPointIndex < 0) + return; + + $(this.breakpointNode) + .children("table") + .find("tr").slice(breakPointIndex, breakPointIndex + 1) + .find("button.enable").addClass('btn-primary'); + + $(this.breakpointNode) + .children("table") + .find("tr").slice(breakPointIndex, breakPointIndex + 1) + .find("button.skipto") + .prop('disabled', false); + + }, + + disableBreakpoint: function(breakpoint) { + var breakPointIndex = this.findBreakpoint(breakpoint); + if (breakPointIndex < 0) + return; + + $(this.breakpointNode) + .children("table") + .find("tr").slice(breakPointIndex, breakPointIndex + 1) + .find("button.enable").removeClass('btn-primary'); + + $(this.breakpointNode) + .children("table") + .find("tr").slice(breakPointIndex, breakPointIndex + 1) + .find("button.skipto") + .prop('disabled', true); + }, + + disableAllBreakpoints: function() { + this.breakpointsEnabled = false; + $(this.breakpointNode).find("button.enable").prop('disabled', true); + $(this.breakpointNode).find("button.skipto").prop('disabled', true); + }, + + enableAllBreakpoints: function() { + this.breakpointsEnabled = true; + + $(this.breakpointNode).find("button.enable").each(function() { + $(this).prop('disabled', false); + if ($(this).hasClass("btn-primary")) { + $(this).parent().children("button.skipto").prop('disabled', false); + } + }); + }, + + evalOnDataModel: function() { + }, + + attachDebug: function(sessionId) { + }, + + prepareDebug: function() { + this.scxmlText = SCXMLEditor.xmlToString(this.scxmlEditor.scxmlDOM); + }, + + startDebug: function() { + this.unhighlightAllBreakpoints(); + }, + + stopDebug: function() { + this.unhighlightAllBreakpoints(); + }, + + stepDebug: function() { + this.unhighlightAllBreakpoints(); + }, + + }); + + + SCXMLEditor.Debugger.Breakpoint = Base.extend({ + constructor: function(params) { + $.extend(this, params); + this.seqNr = SCXMLEditor.Debugger.Breakpoint.seqNr++; + }, + + data: {}, // all data describing this breakpoint + editNodes: {}, // html nodes of form-groups during editing - needed to retain input - TODO + + breakpointTypes: { + "categories": [ + { id: "state", label: "States" }, + { id: "event", label: "Events" }, + { id: "transition", label: "Transitions" }, + { id: "executable", label: "Executable Content" }, + { id: "invoke", label: "Invokers" }, + { id: "configuration", label: "Configuration" }, + ], + "types": [ + { id: "state-before-enter", + label: "Before Entering State", category: "state", + hint: "Stop execution <b>before</b> a given state is <b>entered</b>." + }, + { id: "state-after-enter", + label: "After Entering State", category: "state", + hint: "Stops execution <b>after</b> a given state is <b>entered</b>." + }, + { id: "state-before-exit", + label: "Before Exiting State", category: "state", + hint: "Stops execution <b>before</b> a given state was <b>exited</b>." + }, + { id: "state-after-exit", + label: "After Exiting State", category: "state", + hint: "Stops execution <b>after</b> a given state was <b>exited</b>." + }, + { id: "event-before", + label: "Before Processing Event", category: "event", + hint: "Stops execution <b>before</b> a given event is processed." + }, + { id: "transition-before", + label: "Before Taking Transition", category: "transition", + hint: "Stops execution <b>before</b> a given transition is taken." + }, + { id: "transition-after", + label: "After Taking Transition", category: "transition", + hint: "Stops execution <b>after</b> a given transition was taken." + }, + { id: "invoker-before-invoke", + label: "Before Starting Invoker", category: "invoke", + hint: "Stop execution <b>before</b> an invoker is <b>started</b>." + }, + { id: "invoker-after-invoke", + label: "After Starting Invoker", category: "invoke", + hint: "Stops execution <b>after</b> an invoker is <b>started</b>." + }, + { id: "invoker-before-cancel", + label: "Before Cancelling Invoker", category: "invoke", + hint: "Stop execution <b>before</b> an invoker is <b>cancelled</b>." + }, + { id: "invoker-after-cancel", + label: "After Cancelling Invoker", category: "invoke", + hint: "Stops execution <b>after</b> an invoker is <b>cancelled</b>." + }, + { id: "executable-before", + label: "Before Executing Content", category: "executable", + hint: "Stops <b>before</b> executing content." + }, + { id: "executable-after", + label: "After Executing Content", category: "executable", + hint: "Stops <b>after</b> executing content." + }, + { id: "microstep-before", + label: "Before Microstep", category: "configuration", + hint: "Stops execution <b>before</b> processing a microstep." + }, + { id: "microstep-after", + label: "After Microstep", category: "configuration", + hint: "Stops execution <b>after</b> after a microstep was processed." + }, + { id: "stable-on", + label: "On Stable Configuration", category: "configuration", + hint: "Stops execution when the interpreter reached a stable configuration and waits for events." + } + ] + }, + + isEnabled: function() { + return true; + }, + + toWireFormat: function() { + var jsonData = jQuery.extend(true, {}, this.data); + + if ('breakpointType' in this.data) { + var typeComponents = this.data.breakpointType.split("-"); + jsonData.subject = typeComponents[0]; + jsonData.when = typeComponents[1]; + if (typeComponents.length > 2) { + jsonData.action = typeComponents[2]; + } else { + delete jsonData.action; + } + } + delete jsonData.breakpointType; + + return jsonData; + }, + + showEditDialog: function() { + var breakpoint = this; + + // do we already have a modal div? + $("body").children("div#editBreakpointModal").remove(); + + this.editModalNode = $('\ + <div class="modal fade" id="editBreakpointModal" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">\ + <div class="modal-dialog">\ + <form action="" class="form-horizontal" role="form">\ + <div class="modal-content">\ + <div class="modal-header">\ + <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>\ + <h4 class="modal-title" id="myModalLabel"></h4>\ + </div>\ + <div class="modal-body">\ + <div class="form-group">\ + <label for="breakpointType" class="col-sm-2 control-label">Type</label>\ + <div class="col-sm-10 type-select"></div>\ + </div>\ + <div class="col-sm-2"></div><div class="col-sm-10 alert alert-info"></div>\ + </div>\ + <div class="modal-footer">\ + <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\ + <button type="submit" class="btn btn-primary">Apply</button>\ + </div>\ + </div>\ + </form>\ + </div>\ + </div>\ + '); + + $(this.editModalNode).find("div.modal-body").find("div.type-select").append(this._typeSelectNode()); + $("body").append(this.editModalNode); + + this.editTypeInfoNode = $(this.editModalNode).find("div.alert-info")[0]; + this.editTypeNode = $(this.editModalNode).find("div.modal-body")[0]; + + var stateNames = this.scxmlEditor.getAllStateIds(); + + var editTypeSelectNode = $(this.editTypeNode).find("div.type-select").children("select"); + if ('breakpointType' in this.data) { + $(editTypeSelectNode).val(this.data.breakpointType); + $(this.editModalNode).find("h4.modal-title").text("Edit Breakpoint"); + } else { + $(this.editModalNode).find("h4.modal-title").text("Add Breakpoint"); + } + + $(this.editModalNode).find("form").removeData(); // important as closures persist with dom + $(this.editModalNode).find("form").validate({ + debug: true, + highlight: function(element) { + $(element).closest('.form-group').removeClass('has-success has-warning').addClass('has-error'); + }, + success: function(element) { + var state = element.closest('.form-group').find("input[name=stateId], input[name=toStateId], input[name=fromStateId]").val(); + if (state && $.inArray(state, stateNames) < 0) { + element.text('Warning: No such state!').addClass('valid').closest('.form-group').removeClass('has-success has-error').addClass('has-warning'); + return; + } + element.text('OK!').addClass('valid').closest('.form-group').removeClass('has-error has-warning').addClass('has-success'); + }, + submitHandler: function (form) { + $(breakpoint.editModalNode).modal("hide"); + + // reset + var oldData = jQuery.extend(true, {}, breakpoint.data); + breakpoint.data = {}; + + // copy over form fields + $(breakpoint.editTypeNode).find("textarea[name], input[name], select[name]").each(function() { + var name = $(this).attr("name"); + var value = $(this).val(); + if (name.length > 0 && value.length > 0) { + breakpoint.data[name] = value; + } + }) + console.log(breakpoint.data); + if (breakpoint.node) { + breakpoint._updateLabelNode(); + breakpoint._updateConditionNode(); + } + if (oldData.length > 0) { + breakpoint.scxmlDebugger.editedBreakpoint(breakpoint, oldData); + } else { + breakpoint.scxmlDebugger.addBreakpoint(breakpoint); + } + return true; + } + }); + + $(breakpoint.editTypeNode).children(".form-group").not(':first').remove(); + $(breakpoint.editTypeNode).children("input[type=hidden]").remove(); // hidden fields + + + $(editTypeSelectNode).change(function () { + $(breakpoint.editTypeNode).children(".form-group").not(':first').remove(); + $(breakpoint.editTypeNode).children("input[type=hidden]").remove(); // hidden fields + breakpoint._updateEditTypeDOM($(editTypeSelectNode).val()); + }); + this._updateEditTypeDOM($(editTypeSelectNode).val()); + + $(this.editModalNode).modal(); + }, + + _updateEditTypeDOM: function(typeId) { + var breakpoint = this; + var breakpointType = $.grep(this.breakpointTypes["types"], function(e){ return e.id == typeId; })[0]; + this.editTypeInfoNode.innerHTML = breakpointType.hint; + + // $(this.editTypeNode).append('\ + // <input type="hidden" name="label" value="' + breakpointType.label + '" />\ + // '); + + var stateNames = this.scxmlEditor.getAllStateIds(); + + switch(true) { + case /state-.*/.test(typeId): + var editTypeStateFormGroupNode = $('\ + <div class="form-group">\ + <label for="stateId" class="col-sm-2 control-label">State</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="stateId" placeholder="The id of the state"/>\ + </div>\ + </div>\ + '); + $(this.editTypeNode).append(editTypeStateFormGroupNode); + $(editTypeStateFormGroupNode).find("input[name=stateId]") + .val('stateId' in breakpoint.data ? breakpoint.data.stateId : "") + .typeahead({local: stateNames}) + .rules('add', 'required'); + + break; + case /event-.*/.test(typeId): + var editTypeEventFormGroupNode = $('\ + <div class="form-group">\ + <label for="eventName" class="col-sm-2 control-label">Event</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="eventName" placeholder="The name or prefix of the event"/>\ + </div>\ + </div>\ + '); + $(this.editTypeNode).append(editTypeEventFormGroupNode); + $(editTypeEventFormGroupNode).find("input[name=eventName]") + .val('eventName' in breakpoint.data ? breakpoint.data.eventName : "") + .rules('add', 'required'); + break; + case /transition-.*/.test(typeId): + var editTypeTransitionFormGroupNode = $('\ + <div class="form-group">\ + <label for="transSourceId" class="col-sm-2 control-label">From State</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="transSourceId" placeholder="The id of the source state"/>\ + </div>\ + </div>\ + <div class="form-group">\ + <label for="transTargetId" class="col-sm-2 control-label">To State</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="transTargetId" placeholder="The id of the destination state"/>\ + </div>\ + </div>\ + '); + $(this.editTypeNode).append(editTypeTransitionFormGroupNode); + $(editTypeTransitionFormGroupNode).find("input[name=transSourceId]") + .val('transSourceId' in breakpoint.data ? breakpoint.data.transSourceId : "") + .typeahead({local: stateNames}); + $(editTypeTransitionFormGroupNode).find("input[name=transTargetId]") + .val('transTargetId' in breakpoint.data ? breakpoint.data.transTargetId : "") + .typeahead({local: stateNames}); + break + case /invoker-.*/.test(typeId): + var editTypeInvokerFormGroupNode = $('\ + <div class="form-group">\ + <label for="invokeId" class="col-sm-2 control-label">Invoke ID</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="invokeId" placeholder="The id of the invoker"/>\ + </div>\ + </div>\ + <div class="form-group">\ + <label for="invokeType" class="col-sm-2 control-label">Invoke Type</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="invokeType" placeholder="The type of the invoker"/>\ + </div>\ + </div>\ + '); + $(this.editTypeNode).append(editTypeInvokerFormGroupNode); + $(editTypeInvokerFormGroupNode).find("input[name=invokeId]") + .val('invokeId' in breakpoint.data ? breakpoint.data.invokeId : ""); + $(editTypeInvokerFormGroupNode).find("input[name=invokeType]") + .val('invokeType' in breakpoint.data ? breakpoint.data.invokeType : ""); + break + + case /executable-.*/.test(typeId): + var editTypeExecutableFormGroupNode = $('\ + <div class="form-group">\ + <label for="executableName" class="col-sm-2 control-label">Element Name</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="executableName" placeholder="The executable\'s element name"/>\ + </div>\ + </div>\ + <div class="form-group">\ + <label for="executableXPath" class="col-sm-2 control-label">Element XPath</label>\ + <div class="col-sm-10">\ + <input class="form-control" type="text" name="executableXPath" placeholder="The executable\'s XPath expression"/>\ + </div>\ + </div>\ + '); + $(this.editTypeNode).append(editTypeExecutableFormGroupNode); + $(editTypeExecutableFormGroupNode).find("input[name=executableName]") + .val('executableName' in breakpoint.data ? breakpoint.data.executableName : ""); + $(editTypeExecutableFormGroupNode).find("input[name=executableXPath]") + .val('executableXPath' in breakpoint.data ? breakpoint.data.executableXPath : ""); + break; + default: + break; + } + + var editTypeConditionFormGroupNode = $('\ + <div class="form-group">\ + <label for="condition" class="col-sm-2 control-label">Condition</label>\ + <div class="col-sm-10">\ + <textarea class="form-control" rows="4" style="resize:vertical;" name="condition" placeholder="An optional boolean expression evaluated on the datamodel. Breakpoint is only triggered if true." />\ + </div>\ + </div>\ + '); + $(editTypeConditionFormGroupNode).find("textarea[name=condition]") + .val('condition' in breakpoint.data ? breakpoint.data.condition : "") + + $(this.editTypeNode).append(editTypeConditionFormGroupNode); + }, + + getNode: function() { + var self = this; + this.node = $('\ + <tr>\ + <td>\ + <button title="Enable this Breakpoint" class="enable btn btn-default btn-xs btn-primary"><span class="glyphicon glyphicon-ok"></span></button><br />\ + <button title="Skip to this Breakpoint" class="skipto btn btn-default btn-xs"><span class="glyphicon glyphicon-fast-forward"></span></button>\ + </td>\ + <td width="100%">\ + <span class="breakpoint-label"></span>\ + <span class="breakpoint-condition"></span>\ + </td>\ + <td>\ + <button title="Edit this Breakpoint" class="edit btn btn-default btn-xs"><span class="glyphicon glyphicon-wrench"></span></button><br />\ + <button title="Remove this Breakpoint" class="delete btn btn-default btn-xs"><span class="glyphicon glyphicon-trash"></span></button>\ + </td>\ + </tr>\ + '); + + this.labelNode = $(this.node).find("span.breakpoint-label")[0]; + this.conditionNode = $(this.node).find("span.breakpoint-condition")[0]; + this.enableButtonNode = $(this.node).find("button.enable")[0]; + this.skipToButtonNode = $(this.node).find("button.skipto")[0]; + this.deleteButtonNode = $(this.node).find("button.delete")[0]; + this.editButtonNode = $(this.node).find("button.edit")[0]; + + $(this.enableButtonNode) + .unbind('click') + .click(function() { + if ($(this).hasClass("btn-primary")) { + self.scxmlDebugger.disableBreakpoint(self); + } else { + self.scxmlDebugger.enableBreakpoint(self); + } + return false; + }); + + $(this.deleteButtonNode) + .unbind('mouseenter') + .unbind('mouseleave') + .unbind('click') + .mouseenter(function() { + $(this).addClass("btn-danger"); + }) + .mouseleave(function() { + $(this).removeClass("btn-danger"); + }) + .click(function() { + self.scxmlDebugger.removeBreakpoint(self); + return false; + }); + + $(this.editButtonNode) + .unbind('click').click(function() { + self.showEditDialog() + return false; + }); + + $(this.skipToButtonNode) + .unbind('click').click(function() { + self.scxmlDebugger.skipToBreakpoint(self); + return false; + }); + + this._updateLabelNode(); + this._updateConditionNode(); + + return this.node; + }, + + toDescription: function() { + var wireFormat = this.toWireFormat(); + + var breakpointString = ''; + breakpointString += ('when' in wireFormat ? wireFormat.when + ' ' : ''); + breakpointString += ('action' in wireFormat ? wireFormat.action + ' ' : ''); + breakpointString += ('subject' in wireFormat ? wireFormat.subject + ' ' : ''); + + breakpointString += (wireFormat.subject === 'state' ? wireFormat.stateId + ' ' : ''); + breakpointString += (wireFormat.subject === 'event' ? wireFormat.eventName + ' ' : ''); + if (wireFormat.subject === 'transition') { + breakpointString += ('transSourceId' in wireFormat ? wireFormat.transSourceId + ' ' : '* '); + breakpointString += '⇒ '; + breakpointString += ('transTargetId' in wireFormat ? wireFormat.transTargetId + ' ' : '* '); + } + + return breakpointString; + }, + + _updateLabelNode: function() { + this.labelNode.innerHTML = 'Halt ' + this.toDescription(); + }, + + _updateConditionNode: function() { + if (this.data.condition) { + this.conditionNode.innerHTML = "<br/>But only when <code>" + this.data.condition + "</code>"; + } else { + this.conditionNode.innerHTML = ''; + } + }, + + _typeSelectNode: function() { + // establish optgroups for selecting the breakpoint type + var optGroups = {}; + for (var index in this.breakpointTypes.types) { + var bp = this.breakpointTypes.types[index]; + var optGroup = bp.category; + if (!(optGroup in optGroups)) optGroups[optGroup] = []; + optGroups[optGroup].push(bp); + } + + var breakpointTypeSelectNode = document.createElement('select'); + breakpointTypeSelectNode.className = 'form-control'; + $(breakpointTypeSelectNode).attr("name", "breakpointType"); + + for (var optGroup in this.breakpointTypes.categories) { + var cat = this.breakpointTypes.categories[optGroup]; + var optGroupNode = document.createElement('optgroup'); + $(optGroupNode).attr('label', cat.label); + + for(var type in optGroups[cat.id]) { + var breakpointType = optGroups[cat.id][type]; + var optionNode = document.createElement('option'); + $(optionNode).attr('value', breakpointType.id); + optionNode.innerHTML = breakpointType.label; + $(optionNode).appendTo(optGroupNode); + } + $(optGroupNode).appendTo(breakpointTypeSelectNode); + } + + return breakpointTypeSelectNode; + }, + + }); + SCXMLEditor.Debugger.Breakpoint.seqNr = 0; + + SCXMLEditor.Debugger.USCXML = SCXMLEditor.Debugger.extend({ + constructor: function(params) { + this.base(params); + + this.isConnected = false; // we have a connection to the uscxml server + this.isPrepared = false; // we uploaded a document + this.isRunning = false; // the interpreter on the server is running + this.isPaused = false; // the interpreter is running but paused + this.hasDOMLoaded = false; // the client has a DOM loaded + this.isAttached = false; // we are attached to a server session + this.isPolling = false; // we have a unreplied poll request standing + this.sessionId = ""; + }, + + _updateDebugControls: function() { + // all buttons are disabled per base class + $(this.startButtonNode).prop('disabled', true) + $(this.pauseButtonNode).prop('disabled', true); + $(this.stepButtonNode).prop('disabled', true); + + var status = ""; + status += (this.isConnected ? "CON " : "con "); + status += (this.isPolling ? "POL " : "pol "); + status += (this.isPrepared ? "PRP " : "prp "); + status += (this.isRunning ? "RUN " : "run "); + status += (this.isPaused ? "PAU " : "pau "); + status += (this.hasDOMLoaded ? "DOM " : "dom "); + status += (this.isAttached ? "ATC " : "atc "); + + $(this.statusIndicatorNode).html(status); + + // some sanity + if (!this.isConnected) { + $("body").find(".dom").text(""); + $(this.currentBreakpointNode).html(""); + this.isPaused = false; + this.isRunning = false; + this.isAttached = false; + this.isPrepared = false; + } + + if (!this.isRunning) { + this.isPaused = false; + $(this.currentBreakpointNode).html(""); + } + + // toggle button icons + if (this.isRunning) { // switch play and stop + $(this.startButtonNode).children("span.glyphicon").removeClass("glyphicon-play").addClass("glyphicon-stop"); + $(this.startButtonNode).prop('title', 'Stop Execution'); + } else { + $(this.startButtonNode).children("span.glyphicon").removeClass("glyphicon-stop").addClass("glyphicon-play"); + $(this.startButtonNode).prop('title', 'Start Execution'); + } + + if (!this.isPaused) { // switch pause and resume + $(this.pauseButtonNode).removeClass("active"); + $(this.pauseButtonNode).prop('title', 'Pause Execution'); + this.unhighlightAllBreakpoints(); + $(this.currentBreakpointNode).html(""); + } else { + $(this.pauseButtonNode).addClass("active"); + $(this.pauseButtonNode).prop('title', 'Resume Execution'); + } + + if (this.isConnected && (this.hasDOMLoaded || this.isAttached)) { + // are we connected, not running and have a DOM or are attached? + if (!this.isAttached) { + $(this.startButtonNode).prop('disabled', false); + } + if (this.isRunning) { + $(this.pauseButtonNode).prop('disabled', false); + $(this.stepButtonNode).prop('disabled', false); + } else { + $(this.startButtonNode).prop('disabled', false) + $(this.pauseButtonNode).prop('disabled', true); + $(this.stepButtonNode).prop('disabled', false); + } + return; + } else { + $("body").find(".dom").text(""); + } + }, + + loadDOM: function(scxmlDOM) { + this.hasDOMLoaded = true; + this._updateDebugControls(); + }, + + getNode: function() { + this.base(); + var debug = this; + + // a new menu item + $(this.controlDropdownNode).append('\ + <li role="presentation" class="divider"></li>\ + <li class="disabled"><a href="#"><i class="glyphicon glyphicon-resize-small"></i> Attach to Instance</a></li>\ + '); + + // the connect bar + $(this.node).find(".debug-header").after('\ + <div class="panel-body input-group input-group-sm">\ + <input type="text" class="form-control connect" placeholder="uSCMXL Interpreter URL">\ + <span class="input-group-btn">\ + <button title="Connect" class="btn btn-default ladda-button connect" data-spinner-color="#000" data-style="zoom-in">\ + <span class="ladda-label"><span class="ui-icon ui-icon-unlocked"></span></span></button>\ + </span>\ + </div>\ + '); + + this.connectInputNode = $(this.node).find("input.connect"); + this.connectButtonNode = $(this.connectInputNode).next().children("button.connect")[0]; + + if (this.debuggerURL) { + $(this.connectInputNode).val(this.debuggerURL); + } else { + $(this.connectInputNode).attr("placeholder", "URL of uscxml-debug"); + } + + $(this.connectInputNode).focus(function () { + $(debug.connectButtonNode) + .removeClass("btn-warning") + .find("span.ui-icon") + .removeClass("ui-icon-alert ui-icon-locked") + .addClass("ui-icon-unlocked"); + }); + + $(this.connectInputNode).unbind('keypress').keypress(function(e) { + if (e.which == 13) + $(debug.connectButtonNode).trigger('click'); + }); + + + $(this.connectButtonNode).unbind("click"); + $(this.connectButtonNode).click(function() { + debug.connectSpinner = Ladda.create(this); + debug.connectSpinner.start(); + if (debug.isConnected) { + debug.disconnect(); + } else { + debug.connect($(debug.connectInputNode).val()); + } + }); + + // debug controls + $(this.startButtonNode) + .unbind('click').click(function () { + $(this).tooltip("hide"); + if ($(this).children(".glyphicon").hasClass("glyphicon-play")) { + debug.startDebug(); + } else { + debug.stopDebug(); + } + }); + + $(this.pauseButtonNode) + .unbind('click').click(function() { + $(this).tooltip("hide"); + if (debug.isPaused) { + debug.resumeDebug(); + } else { + debug.pauseDebug(); + } + }); + + $(this.stepButtonNode) + .unbind('click').click(function() { + $(this).tooltip("hide"); + debug.stepDebug(); + }); + + this._updateDebugControls(); + + return this.node; + }, + + _indicateLockedConnection: function() { + $(this.connectButtonNode) + .removeClass("btn-warning") + .find("span.ui-icon") + .removeClass("ui-icon-alert ui-icon-unlocked") + .addClass("ui-icon-locked"); + $(this.connectInputNode).attr('disabled', true); + $(this.dataModelInputNode).prop('disabled', false); + $(this.dataModelInputButton).prop('disabled', false); + $(this.dataModelSuggestButton).prop('disabled', false); + }, + + _indicateFailedConnection: function() { + $(this.connectButtonNode) + .addClass("btn-warning") + .find("span.ui-icon") + .removeClass("ui-icon-locked ui-icon-unlocked") + .addClass("ui-icon-alert"); + }, + + _indicateUnlockedConnection: function() { + $(this.connectButtonNode) + .removeClass("btn-warning") + .find("span.ui-icon") + .removeClass("ui-icon-locked ui-icon-alert") + .addClass("ui-icon-unlocked"); + $(this.connectInputNode).attr('disabled', false); + $(this.dataModelInputNode).prop('disabled', true); + $(this.dataModelInputButton).prop('disabled', true); + $(this.dataModelSuggestButton).prop('disabled', true); + }, + + connect: function(debuggerURL) { + if (!debuggerURL || debuggerURL.length == 0) + return; + + this.debuggerURL = debuggerURL; + + var self = this; + + this.requestWithJSON('/debug/connect', { + onFailure: function(dataObj, statusString, xhrObj) { + self.isConnected = false; + self._indicateFailedConnection(); + }, + onSuccess: function(dataObj, statusString, xhrObj) { + self.isConnected = true; + self._indicateLockedConnection(); + + self.log("Client successfully connected to " + self.debuggerURL); + self.sessionId = dataObj.session; + // send all breakpoints when connected + if (self.breakpoints.length > 0) { + for (var index in self.breakpoints) { + self.addBreakpoint(self.breakpoints[index]); + } + } + // register disconnect handler + $(window).unload(function() { + self.disconnect(); + }); + // update list of sessions from server + self.updateServerSessions(); + // and start long-poller + self.pollServer(); + }, + onComplete: function(dataObj, statusString) { + self.connectSpinner.stop(); + } + }); + }, + + disconnect: function() { + if (!this.isConnected) + return; + var self = this; + + this.requestWithJSON('/debug/disconnect', { + async: false, + onComplete: function(dataObj, statusString) { + self.isConnected = false; + self.isPrepared = false; + self._indicateUnlockedConnection(); + self.connectSpinner.stop(); + + // remove all previous sessions + $(self.controlDropdownNode).children("li.server-session").remove(); + self.debuggerURL = ''; + } + }); + }, + + updateServerSessions: function() { + this.base(); + if (!this.isConnected) + return; + + var self = this; + + this.requestWithJSON('/debug/sessions', { + onSuccess: function(dataObj, statusString, xhrObj) { + $(self.controlButtonNode).effect('highlight', {color: '#4489CA'}, 600); + + // remove all previous sessions + $(self.controlDropdownNode).children("li.server-session").remove(); + for (var key in dataObj.sessions) { + $(self.controlDropdownNode).append('\ + <li class="server-session" \ + session-url="' + dataObj.sessions[key].source + '"\ + session-name="' + dataObj.sessions[key].name + '"\ + session-id="' + dataObj.sessions[key].id + '"\ + ><a href="#"><i class="glyphicon glyphicon-link"></i> ' + dataObj.sessions[key].source + '</a></li>\ + '); + } + $(self.controlDropdownNode).find("li").unbind('click').click(function() { + var session = { + sessionId: $(this).attr("session-id"), + sessionName: $(this).attr("session-name"), + sessionURL: $(this).attr("session-url"), + }; + self.attachDebug(session); + + $(self.controlDropdownNode).dropdown('toggle'); + return false; + }); + } + }); + }, + + + addBreakpoint: function(breakpoint) { + this.base(breakpoint); + if (!this.isConnected) + return; + + if (!breakpoint) // it's perfectly fine to call this without a breakpoint - will add a new one + return; + + this.requestWithJSON('/debug/breakpoint/add', { + data: breakpoint.toWireFormat() + }); + }, + + removeBreakpoint: function(breakpoint) { + this.base(breakpoint); + if (!this.isConnected) + return; + this.requestWithJSON('/debug/breakpoint/remove', { + data: breakpoint.toWireFormat() + }); + }, + + editedBreakpoint: function(breakpoint, oldData) { + this.base(breakpoint, oldData); + if (!this.isConnected) + return; + + var oldBreakpoint = new SCXMLEditor.Debugger.Breakpoint({ data : oldData }); + this.requestWithJSON('/debug/breakpoint/remove', { + data: oldBreakpoint.toWireFormat() + }); + this.requestWithJSONBlocking('/debug/breakpoint/add', { + data: breakpoint.toWireFormat() + }); + }, + + skipToBreakpoint: function(breakpoint) { + this.base(breakpoint); + if (!this.isConnected) + return; + + this.requestWithJSON('/debug/breakpoint/skipto', { + data: breakpoint.toWireFormat() + }); + + }, + + enableBreakpoint: function(breakpoint) { + this.base(breakpoint); + if (!this.isConnected) + return; + + this.requestWithJSON('/debug/breakpoint/enable', { + data: breakpoint.toWireFormat() + }); + }, + + disableBreakpoint: function(breakpoint) { + this.base(breakpoint); + if (!this.isConnected) + return; + + this.requestWithJSON('/debug/breakpoint/disable', { + data: breakpoint.toWireFormat() + }); + }, + + enableAllBreakpoints: function() { + this.base(); + if (!this.isConnected) + return; + + this.requestWithJSON('/debug/breakpoint/enable/all'); + }, + + disableAllBreakpoints: function() { + this.base(); + if (!this.isConnected) + return; + + this.requestWithJSON('/debug/breakpoint/disable/all'); + + }, + + + attachDebug: function(session) { + this.base(); + if (!this.isConnected) + return; + + var self = this; + + var data = { + attach: session.sessionId, + }; + + this.requestWithJSON('/debug/attach', { + data: data, + onSuccess: function(dataObj, statusString, xhrObj) { + self.isPrepared = true; + self.isRunning = true; + self.isAttached = true; + + if (self.scxmlEditor.xmlView) { + self.scxmlEditor.xmlView.setXML(dataObj.xml) + self.scxmlEditor.xmlView.prettyPrint(); + } + + // var domText = vkbeautify.xml(dataObj.xml); + // $("body").find("pre.dom").text(domText); + self.scxmlEditor.setDocumentNameFromURL(session.sessionURL); + self._updateDebugControls(); + } + }); + + }, + + prepareDebug: function() { + this.base(); + if (!this.isConnected) + return; + + if (this.isAttached) + return; + + var self = this; + + var data = { + url: this.scxmlEditor.scxmlURL, + xml: this.scxmlText + }; + + this.requestWithJSON('/debug/prepare', { + data: data, + onSuccess: function(dataObj, statusString, xhrObj) { + self.isPrepared = true; + } + }); + }, + + startDebug: function() { + this.base(); + if (!this.isConnected) + return; + + if (!this.isPrepared) + this.prepareDebug(); + + var self = this; + + this.requestWithJSON('/debug/start', { + onSuccess: function(dataObj, statusString, xhrObj) { + self.isRunning = true; + } + }); + }, + + stopDebug: function() { + this.base(); + if (!this.isConnected) + return; + + var self = this; + + this.requestWithJSON('/debug/stop', { + onSuccess: function(dataObj, statusString, xhrObj) { + self.isRunning = false; + self.isPrepared = false; + } + }); + }, + + pauseDebug: function() { + this.base(); + if (!this.isConnected) + return; + + var self = this; + + this.requestWithJSON('/debug/pause', { + onSuccess: function(dataObj, statusString, xhrObj) { + self.isPaused = true; + self._updateDebugControls(); + } + }); + }, + + resumeDebug: function() { + this.base(); + if (!this.isConnected) + return; + + var self = this; + + this.requestWithJSON('/debug/resume', { + onSuccess: function(dataObj, statusString, xhrObj) { + self.isPaused = false; + self._updateDebugControls(); + } + }); + }, + + stepDebug: function() { + this.base(); + if (!this.isConnected) + return; + + if (!this.isPrepared) + this.prepareDebug(); + + var self = this; + + this.requestWithJSON('/debug/step', { + onSuccess: function(dataObj, statusString, xhrObj) { + self.isRunning = true; + self.isPaused = true; + self._updateDebugControls(); + } + }); + }, + + evalOnDataModel: function(expression) { + this.base(); + if (!this.isConnected) + return; + + var self = this; + + var data = {}; + data.expression = expression; + + this.requestWithJSON('/debug/eval', { + data: data, + onSuccess: function(dataObj, statusString, xhrObj) { + var content = JSON.stringify(dataObj.eval); + self.dataModelEditor.getSession().setValue(vkbeautify.json(content)); + + }, + }); + + }, + + // server will return 'status' with JSON response, dispatch accordingly + requestWithJSON: function(url, params) { + var self = this; + if (!params) + params = {}; + + params.method = ('method' in params ? params.method : "POST"); + params.async = ('async' in params ? params.async : true); + params.onSuccess = ('onSuccess' in params ? params.onSuccess : function() {}); + params.onFailure = ('onFailure' in params ? params.onFailure : function(dataObj) { self.log(dataObj); }); + params.onComplete = ('onComplete' in params ? params.onComplete : function() {}); + params.onError = ('onError' in params ? params.onError : function() {}); + params.returnType = ('returnType' in params ? params.returnType : "json"); + params.sendType = ('sendType' in params ? params.sendType : "application/json; charset=utf-8"); + params.data = ('data' in params ? params.data : {}); + + if (params.sendType.match("^application/json")) { + if (this.sessionId.length > 0) // automatically add session + params.data.session = this.sessionId; + params.data = JSON.stringify(params.data); + } + + $.ajax({ + type: params.method, + dataType: params.returnType, + async: params.async, + data: params.data, + contentType: params.sendType, + url: this.debuggerURL + url, + success: function (dataObj, statusString, xhrObj) { + self.log("Received from " + url); + self.log(dataObj); + + if ("status" in dataObj && dataObj.status === 'success') { + params.onSuccess(dataObj, statusString, xhrObj); + } else { + params.onFailure(dataObj, statusString, xhrObj); + } + }, + error: function(xhrObj, statusString, errorString) { + self.log("Error from " + self.debuggerURL + url); + self.log(statusString + "\n\t" + errorString); + self.log(xhrObj); + + self.isConnected = false; + self._indicateFailedConnection(); + params.onError(errorString, statusString, xhrObj); + }, + complete: function(xhrObj, statusString) { + params.onComplete(xhrObj, statusString); + self._updateDebugControls(); + } + }); + }, + + handleBreakpointReturn: function(serverReturn) { + var updateViewer = false; + if ('qualified' in serverReturn) { + var qualifiedBreakpoint = new SCXMLEditor.Debugger.Breakpoint({ data : serverReturn.qualified }); + var breakpointString = 'Halted ' + qualifiedBreakpoint.toDescription(); + $(this.currentBreakpointNode).html(breakpointString); + + if ('xpath' in serverReturn.qualified) { + if (this.scxmlEditor.xmlView) { + this.scxmlEditor.xmlView.setElementXPath(serverReturn.qualified.xpath); + } + } else { + this.scxmlEditor.xmlView.setElementXPath(""); + } + } + + if ('basicStates' in serverReturn) { + if (this.scxmlEditor.xmlView) { + this.scxmlEditor.xmlView.setActiveStates(serverReturn['basicStates']); + } + } + + if (this.scxmlEditor.xmlView) { + this.scxmlEditor.xmlView.prettyPrint(); + } + + if ('breakpoint' in serverReturn) { + var breakpoint = new SCXMLEditor.Debugger.Breakpoint({ data : serverReturn.breakpoint }); + this.highlightBreakpoint(breakpoint); + } + + }, + + pollServer: function() { + this.isPolling = false; + this._updateDebugControls(); + + if (!this.isConnected) + return; + + if (this.sessionId.length == 0) + return; + + var self = this; + this.isPolling = true; + self._updateDebugControls(); + + $.ajax({ + type: "POST", + dataType: "json", + async: true, + timeout: 100000000, + data: JSON.stringify({session: this.sessionId}), + contentType: "application/json", + url: this.debuggerURL + '/debug/poll', + success: function (dataObj, statusString, xhrObj) { + try { + if ('status' in dataObj) { + if (dataObj.status !== 'success') { + self.log("Error from " + self.debuggerURL + "/debug/poll"); + self.log(dataObj); + return; + } + } + if ('replyType' in dataObj) { + //self.log("Received " + dataObj.replyType); + if (dataObj.replyType === "log") { + self.log(dataObj); + } else if (dataObj.replyType === "finished") { + self.isRunning = false; + self.isPrepared = false; + } else if (dataObj.replyType === "breakpoint") { + self.isPaused = true; + self.log(dataObj); + self.handleBreakpointReturn(dataObj); + } else { + self.log("Unhandled reply from /pollServer"); + self.log(dataObj); + } + } else { + self.log("Untyped reply from /pollServer"); + self.log(dataObj); + } + } catch(e) { + self.log("Exception when dispatching server push:"); + self.log(e); + } + // dispatch server push here + self.pollServer(); + }, + error: function(xhrObj, statusString, errorString) { + if (xhrObj.statusText === "timeout") { + self.pollServer(); // just repoll + return; + } + + self.log("Error from /pollServer"); + self.log(statusString + "\n\t" + errorString); + self.log(xhrObj); + + self.isConnected = false; + self._indicateFailedConnection(); + }, + complete: function(xhrObj, statusString) { + self._updateDebugControls(); + } + }); + + }, + + }); + + // see http://nealvs.wordpress.com/2010/07/12/how-to-compare-two-javascript-objects-for-equality/ + function deepEquals(x, y) { + for (var p in y) { + if (typeof(y[p]) !== typeof(x[p])) return false; + if ((y[p] === null) !== (x[p] === null)) return false; + switch (typeof(y[p])) { + case 'undefined': + if (typeof(x[p]) != 'undefined') return false; + break; + case 'object': + if (y[p] !== null && x[p] !== null && (y[p].constructor.toString() !== x[p].constructor.toString() || !y[p].equals(x[p]))) return false; + break; + case 'function': + if (p != 'equals' && y[p].toString() != x[p].toString()) return false; + break; + default: + if (y[p] !== x[p]) return false; + } + } + return true; + } + + $(document).ready(function() { + + + // see http://stackoverflow.com/questions/18754020/bootstrap-3-with-jquery-validation-plugin + $.validator.setDefaults({ + highlight: function(element) { + $(element).closest('.form-group').addClass('has-error'); + }, + unhighlight: function(element) { + $(element).closest('.form-group').removeClass('has-error'); + }, + errorElement: 'span', + errorClass: 'help-block', + errorPlacement: function(error, element) { + if(element.parent('.input-group').length) { + error.insertAfter(element.parent()); + } else { + error.insertAfter(element); + } + } + }); + + var scxmlEdit = new SCXMLEditor({ + containerNode: $("#scxml-editor")[0] + }); + + }); + + </script> +</head> +<body> + <div id="scxml-editor"></div> + <div class="messages"></div> + <div class="dom"></pre> +</body> +</html> diff --git a/src/bindings/swig/wrapped/WrappedInterpreterMonitor.cpp b/src/bindings/swig/wrapped/WrappedInterpreterMonitor.cpp index f066a72..a89ff54 100644 --- a/src/bindings/swig/wrapped/WrappedInterpreterMonitor.cpp +++ b/src/bindings/swig/wrapped/WrappedInterpreterMonitor.cpp @@ -30,31 +30,31 @@ using namespace XERCESC_NS; WrappedInterpreterMonitor::WrappedInterpreterMonitor() {} WrappedInterpreterMonitor::~WrappedInterpreterMonitor() {} -void WrappedInterpreterMonitor::beforeExitingState(const XERCESC_NS::DOMElement* state) { +void WrappedInterpreterMonitor::beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { std::stringstream ss; ss << *state; beforeExitingState(ATTR(state, "id"), DOMUtils::xPathForNode(state), ss.str()); } -void WrappedInterpreterMonitor::afterExitingState(const XERCESC_NS::DOMElement* state) { +void WrappedInterpreterMonitor::afterExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { std::stringstream ss; ss << *state; afterExitingState(ATTR(state, "id"), DOMUtils::xPathForNode(state), ss.str()); } -void WrappedInterpreterMonitor::beforeExecutingContent(const XERCESC_NS::DOMElement* content) { +void WrappedInterpreterMonitor::beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* content) { std::stringstream ss; ss << *content; beforeExecutingContent(TAGNAME(content), DOMUtils::xPathForNode(content), ss.str()); } -void WrappedInterpreterMonitor::afterExecutingContent(const XERCESC_NS::DOMElement* content) { +void WrappedInterpreterMonitor::afterExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* content) { std::stringstream ss; ss << *content; afterExecutingContent(TAGNAME(content), DOMUtils::xPathForNode(content), ss.str()); } -void WrappedInterpreterMonitor::beforeUninvoking(const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { +void WrappedInterpreterMonitor::beforeUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { std::stringstream ss; ss << *invoker; std::string invokeId; @@ -65,7 +65,7 @@ void WrappedInterpreterMonitor::beforeUninvoking(const XERCESC_NS::DOMElement* i beforeUninvoking(DOMUtils::xPathForNode(invoker), invokeId, ss.str()); } -void WrappedInterpreterMonitor::afterUninvoking(const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { +void WrappedInterpreterMonitor::afterUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { std::stringstream ss; ss << *invoker; std::string invokeId; @@ -76,7 +76,7 @@ void WrappedInterpreterMonitor::afterUninvoking(const XERCESC_NS::DOMElement* in afterUninvoking(DOMUtils::xPathForNode(invoker), invokeId, ss.str()); } -void WrappedInterpreterMonitor::beforeTakingTransition(const XERCESC_NS::DOMElement* transition) { +void WrappedInterpreterMonitor::beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { XERCESC_NS::DOMElement* sourceState = getSourceState(transition); const XERCESC_NS::DOMElement* root = DOMUtils::getNearestAncestor(transition, "scxml"); @@ -93,7 +93,7 @@ void WrappedInterpreterMonitor::beforeTakingTransition(const XERCESC_NS::DOMElem beforeTakingTransition(DOMUtils::xPathForNode(transition), ATTR_CAST(sourceState, "id"), targets, ss.str()); } -void WrappedInterpreterMonitor::afterTakingTransition(const XERCESC_NS::DOMElement* transition) { +void WrappedInterpreterMonitor::afterTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { XERCESC_NS::DOMElement* sourceState = getSourceState(transition); const XERCESC_NS::DOMElement* root = DOMUtils::getNearestAncestor(transition, "scxml"); @@ -110,19 +110,19 @@ void WrappedInterpreterMonitor::afterTakingTransition(const XERCESC_NS::DOMEleme afterTakingTransition(DOMUtils::xPathForNode(transition), ATTR_CAST(sourceState, "id"), targets, ss.str()); } -void WrappedInterpreterMonitor::beforeEnteringState(const XERCESC_NS::DOMElement* state) { +void WrappedInterpreterMonitor::beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { std::stringstream ss; ss << *state; beforeEnteringState(ATTR(state, "id"), DOMUtils::xPathForNode(state), ss.str()); } -void WrappedInterpreterMonitor::afterEnteringState(const XERCESC_NS::DOMElement* state) { +void WrappedInterpreterMonitor::afterEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { std::stringstream ss; ss << *state; afterEnteringState(ATTR(state, "id"), DOMUtils::xPathForNode(state), ss.str()); } -void WrappedInterpreterMonitor::beforeInvoking(const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { +void WrappedInterpreterMonitor::beforeInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { std::stringstream ss; ss << *invoker; std::string invokeId; @@ -133,7 +133,7 @@ void WrappedInterpreterMonitor::beforeInvoking(const XERCESC_NS::DOMElement* inv beforeInvoking(DOMUtils::xPathForNode(invoker), invokeId, ss.str()); } -void WrappedInterpreterMonitor::afterInvoking(const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { +void WrappedInterpreterMonitor::afterInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invoker, const std::string& invokeid) { std::stringstream ss; ss << *invoker; std::string invokeId; diff --git a/src/bindings/swig/wrapped/WrappedInterpreterMonitor.h b/src/bindings/swig/wrapped/WrappedInterpreterMonitor.h index e83c896..c5655cf 100644 --- a/src/bindings/swig/wrapped/WrappedInterpreterMonitor.h +++ b/src/bindings/swig/wrapped/WrappedInterpreterMonitor.h @@ -44,82 +44,91 @@ public: WrappedInterpreterMonitor(); virtual ~WrappedInterpreterMonitor(); - void beforeExitingState(const XERCESC_NS::DOMElement* state); + void beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); virtual void beforeExitingState(const std::string& stateId, const std::string& xpath, const std::string& stateXML) {} - void afterExitingState(const XERCESC_NS::DOMElement* state); + void afterExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); virtual void afterExitingState(const std::string& stateId, const std::string& xpath, const std::string& stateXML) {} - void beforeExecutingContent(const XERCESC_NS::DOMElement* content); + void beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* content); virtual void beforeExecutingContent(const std::string& tagName, const std::string& xpath, const std::string& contentXML) {} - void afterExecutingContent(const XERCESC_NS::DOMElement* content); + void afterExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* content); virtual void afterExecutingContent(const std::string& tagName, const std::string& xpath, const std::string& contentXML) {} - void beforeUninvoking(const XERCESC_NS::DOMElement* invoker, + void beforeUninvoking(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* invoker, const std::string& invokeid); virtual void beforeUninvoking(const std::string& xpath, const std::string& invokeid, const std::string& invokerXML) {} - void afterUninvoking(const XERCESC_NS::DOMElement* invoker, + void afterUninvoking(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* invoker, const std::string& invokeid); virtual void afterUninvoking(const std::string& xpath, const std::string& invokeid, const std::string& invokerXML) {} - void beforeTakingTransition(const XERCESC_NS::DOMElement* transition); + void beforeTakingTransition(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* transition); virtual void beforeTakingTransition(const std::string& xpath, const std::string& source, const std::list<std::string>& targets, const std::string& transitionXML) {} - void afterTakingTransition(const XERCESC_NS::DOMElement* transition); + void afterTakingTransition(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* transition); virtual void afterTakingTransition(const std::string& xpath, const std::string& source, const std::list<std::string>& targets, const std::string& transitionXML) {} - void beforeEnteringState(const XERCESC_NS::DOMElement* state); + void beforeEnteringState(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* state); virtual void beforeEnteringState(const std::string& stateId, const std::string& xpath, const std::string& stateXML) {} - void afterEnteringState(const XERCESC_NS::DOMElement* state); + void afterEnteringState(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* state); virtual void afterEnteringState(const std::string& stateId, const std::string& xpath, const std::string& stateXML) {} - void beforeInvoking(const XERCESC_NS::DOMElement* invoker, + void beforeInvoking(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* invoker, const std::string& invokeid); virtual void beforeInvoking(const std::string& xpath, const std::string& invokeid, const std::string& invokerXML) {} - void afterInvoking(const XERCESC_NS::DOMElement* invoker, + void afterInvoking(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* invoker, const std::string& invokeid); virtual void afterInvoking(const std::string& xpath, const std::string& invokeid, const std::string& invokerXML) {} - virtual void reportIssue(const InterpreterIssue& issue) {} + virtual void reportIssue(InterpreterImpl* impl, + const InterpreterIssue& issue) {} }; } diff --git a/src/uscxml/Interpreter.cpp b/src/uscxml/Interpreter.cpp index a050401..d21ec6a 100644 --- a/src/uscxml/Interpreter.cpp +++ b/src/uscxml/Interpreter.cpp @@ -220,8 +220,12 @@ void Interpreter::setActionLanguage(ActionLanguage actionLanguage) { return _impl->setActionLanguage(actionLanguage); } -void Interpreter::setMonitor(InterpreterMonitor* monitor) { - return _impl->setMonitor(monitor); +void Interpreter::addMonitor(InterpreterMonitor* monitor) { + return _impl->addMonitor(monitor); +} + +void Interpreter::removeMonitor(InterpreterMonitor* monitor) { + return _impl->removeMonitor(monitor); } std::list<InterpreterIssue> Interpreter::validate() { @@ -240,19 +244,19 @@ static void printNodeSet(const std::list<XERCESC_NS::DOMElement*> nodes) { } #endif -void StateTransitionMonitor::beforeTakingTransition(const XERCESC_NS::DOMElement* transition) { +void StateTransitionMonitor::beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { std::lock_guard<std::recursive_mutex> lock(_mutex); std::cerr << "Transition: " << uscxml::DOMUtils::xPathForNode(transition) << std::endl; } -void StateTransitionMonitor::onStableConfiguration() { +void StateTransitionMonitor::onStableConfiguration(InterpreterImpl* impl) { std::lock_guard<std::recursive_mutex> lock(_mutex); std::cerr << "Stable Config: { "; // printNodeSet(_interpreter.getConfiguration()); std::cerr << " }" << std::endl; } -void StateTransitionMonitor::beforeProcessingEvent(const uscxml::Event& event) { +void StateTransitionMonitor::beforeProcessingEvent(InterpreterImpl* impl, const uscxml::Event& event) { std::lock_guard<std::recursive_mutex> lock(_mutex); switch (event.eventType) { case uscxml::Event::INTERNAL: @@ -267,23 +271,23 @@ void StateTransitionMonitor::beforeProcessingEvent(const uscxml::Event& event) } } -void StateTransitionMonitor::beforeExecutingContent(const XERCESC_NS::DOMElement* element) { +void StateTransitionMonitor::beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* element) { std::lock_guard<std::recursive_mutex> lock(_mutex); std::cerr << "Executable Content: " << DOMUtils::xPathForNode(element) << std::endl; } -void StateTransitionMonitor::beforeExitingState(const XERCESC_NS::DOMElement* state) { +void StateTransitionMonitor::beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { std::lock_guard<std::recursive_mutex> lock(_mutex); std::cerr << "Exiting: " << (HAS_ATTR(state, "id") ? ATTR(state, "id") : DOMUtils::xPathForNode(state)) << std::endl; } -void StateTransitionMonitor::beforeEnteringState(const XERCESC_NS::DOMElement* state) { +void StateTransitionMonitor::beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { std::lock_guard<std::recursive_mutex> lock(_mutex); std::cerr << "Entering: " << (HAS_ATTR(state, "id") ? ATTR(state, "id") : DOMUtils::xPathForNode(state)) << std::endl; } -void StateTransitionMonitor::beforeMicroStep() { +void StateTransitionMonitor::beforeMicroStep(InterpreterImpl* impl) { std::lock_guard<std::recursive_mutex> lock(_mutex); std::cerr << "Config: {"; // printNodeSet(_interpreter.getConfiguration()); diff --git a/src/uscxml/Interpreter.h b/src/uscxml/Interpreter.h index 4dbefd8..0d7573c 100644 --- a/src/uscxml/Interpreter.h +++ b/src/uscxml/Interpreter.h @@ -221,7 +221,12 @@ public: /** * Attach a monitor to make more details of the interpreter observable. */ - void setMonitor(InterpreterMonitor* monitor); + void addMonitor(InterpreterMonitor* monitor); + + /** + * Remove a monitor that was attached previously. + */ + void removeMonitor(InterpreterMonitor* monitor); /** * Return the actual implementation of the Interperter. diff --git a/src/uscxml/debug/Breakpoint.cpp b/src/uscxml/debug/Breakpoint.cpp new file mode 100644 index 0000000..1e40ee3 --- /dev/null +++ b/src/uscxml/debug/Breakpoint.cpp @@ -0,0 +1,267 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#include "uscxml/debug/Breakpoint.h" +#include "uscxml/Interpreter.h" +#include "uscxml/interpreter/InterpreterImpl.h" +#include "uscxml/util/DOM.h" +#include "uscxml/util/String.h" + +namespace uscxml { + +Breakpoint::Breakpoint(const Data& data) { + enabled = true; + subject = UNDEF_SUBJECT; + when = UNDEF_WHEN; + action = UNDEF_ACTION; + + if (data.hasKey("when")) { + if (false) { + } else if (data.at("when").atom == "before") { + when = BEFORE; + } else if (data.at("when").atom == "after") { + when = AFTER; + } else if (data.at("when").atom == "on") { + when = ON; + } + } + + if (data.hasKey("action")) { + if (false) { + } else if (data.at("action").atom == "enter") { + action = ENTER; + } else if (data.at("action").atom == "exit") { + action = EXIT; + } else if (data.at("action").atom == "invoke") { + action = INVOKE; + } else if (data.at("action").atom == "cancel") { + action = UNINVOKE; + } + } + + if (data.hasKey("subject")) { + if (false) { + } else if (data.at("subject").atom == "state") { + subject = STATE; + } else if (data.at("subject").atom == "transition") { + subject = TRANSITION; + } else if (data.at("subject").atom == "stable") { + subject = STABLE; + } else if (data.at("subject").atom == "microstep") { + subject = MICROSTEP; + } else if (data.at("subject").atom == "event") { + subject = EVENT; + } else if (data.at("subject").atom == "invoker") { + subject = INVOKER; + } else if (data.at("subject").atom == "executable") { + subject = EXECUTABLE; + } + } + + if (data.hasKey("condition")) + condition = data.at("condition").atom; + + if (data.hasKey("invokeId")) + invokeId = data.at("invokeId").atom; + + if (data.hasKey("invokeType")) + invokeType = data.at("invokeType").atom; + + if (data.hasKey("eventName")) + eventName = data.at("eventName").atom; + + if (data.hasKey("executableName")) + executableName = data.at("executableName").atom; + + if (data.hasKey("executableXPath")) + executableXPath = data.at("executableXPath").atom; + + if (data.hasKey("stateId")) + stateId = data.at("stateId").atom; + + if (data.hasKey("transSourceId")) + transSourceId = data.at("transSourceId").atom; + + if (data.hasKey("transTargetId")) + transTargetId = data.at("transTargetId").atom; + +} + +Data Breakpoint::toData() const { + Data data; + + switch (subject) { + case STATE: + data.compound["subject"] = Data("state", Data::VERBATIM); + break; + case TRANSITION: + data.compound["subject"] = Data("transition", Data::VERBATIM); + break; + case STABLE: + data.compound["subject"] = Data("stable", Data::VERBATIM); + break; + case MICROSTEP: + data.compound["subject"] = Data("microstep", Data::VERBATIM); + break; + case EVENT: + data.compound["subject"] = Data("event", Data::VERBATIM); + break; + case INVOKER: + data.compound["subject"] = Data("invoker", Data::VERBATIM); + break; + case EXECUTABLE: + data.compound["subject"] = Data("executable", Data::VERBATIM); + break; + default: + break; + } + + switch (when) { + case AFTER: + data.compound["when"] = Data("after", Data::VERBATIM); + break; + case BEFORE: + data.compound["when"] = Data("before", Data::VERBATIM); + break; + case ON: + data.compound["when"] = Data("on", Data::VERBATIM); + break; + default: + break; + } + + switch (action) { + case ENTER: + data.compound["action"] = Data("enter", Data::VERBATIM); + break; + case EXIT: + data.compound["action"] = Data("exit", Data::VERBATIM); + break; + case INVOKE: + data.compound["action"] = Data("invoke", Data::VERBATIM); + break; + case UNINVOKE: + data.compound["action"] = Data("cancel", Data::VERBATIM); + break; + default: + break; + } + + if (invokeId.length() > 0) + data.compound["invokeId"] = Data(invokeId, Data::VERBATIM); + + if (invokeType.length() > 0) + data.compound["invokeType"] = Data(invokeType, Data::VERBATIM); + + if (eventName.length() > 0) + data.compound["eventName"] = Data(eventName, Data::VERBATIM); + + if (executableName.length() > 0) + data.compound["executableName"] = Data(executableName, Data::VERBATIM); + + if (executableXPath.length() > 0) { + data.compound["executableXPath"] = Data(executableXPath, Data::VERBATIM); + } + + if (element) + data.compound["xpath"] = Data(DOMUtils::xPathForNode(element, "*"), Data::VERBATIM); + + if (stateId.length() > 0) + data.compound["stateId"] = Data(stateId, Data::VERBATIM); + + if (transSourceId.length() > 0) + data.compound["transSourceId"] = Data(transSourceId, Data::VERBATIM); + + if (transTargetId.length() > 0) + data.compound["transTargetId"] = Data(transTargetId, Data::VERBATIM); + + if (condition.length() > 0) + data.compound["condition"] = Data(condition, Data::VERBATIM); + + return data; +} + +bool Breakpoint::matches(Interpreter interpreter, const Breakpoint& other) const { + // would we match the given breakpoint? + + if (subject != UNDEF_SUBJECT && + other.subject != subject) + return false; // subject does not match + + if (when != UNDEF_WHEN && + other.when != when) + return false; // time does not match + + if (action != UNDEF_ACTION && + other.action != action) + return false; // action does not match + + // when we have a qualifier it has to match + if(invokeId.length() > 0 && invokeId != other.invokeId) { + return false; + } + + if(invokeType.length() > 0 && invokeType != other.invokeType) { + return false; + } + + if(stateId.length() > 0 && stateId != other.stateId) { + return false; + } + + if(eventName.length() > 0 && !nameMatch(eventName, other.eventName)) { + return false; + } + + if(executableName.length() > 0 && executableName != other.executableName) { + return false; + } + +#if 0 + if(executableXPath.length()) { + Arabica::XPath::NodeSet<std::string> nodes; + try { + nodes = interpreter.getNodeSetForXPath(executableXPath); + } catch (...) { + return false; + } + return InterpreterImpl::isMember(other.element, nodes); + } +#endif + + if(transSourceId.length() > 0 && transSourceId != other.transSourceId) { + return false; + } + + if(transTargetId.length() > 0 && transTargetId != other.transTargetId) { + return false; + } + + if (condition.length() > 0) { + try { + interpreter.getImpl()->isTrue(condition); + } catch (...) { + return false; + } + } + + return true; +} + +}
\ No newline at end of file diff --git a/src/uscxml/debug/Breakpoint.h b/src/uscxml/debug/Breakpoint.h new file mode 100644 index 0000000..3809663 --- /dev/null +++ b/src/uscxml/debug/Breakpoint.h @@ -0,0 +1,100 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef BREAKPOINT_H_VR7K7T1X +#define BREAKPOINT_H_VR7K7T1X + +#include <string> // for string +#include "uscxml/Common.h" // for USCXML_API +#include "uscxml/Interpreter.h" +//#include "DOM/Element.hpp" // for Element +#include "uscxml/messages/Data.h" // for Data + +// forward declare +namespace XERCESC_NS { + class DOMElement; +} + +namespace uscxml { + +class USCXML_API Breakpoint { +public: + + enum When { + UNDEF_WHEN, AFTER, BEFORE, ON + }; + + enum Subject { + UNDEF_SUBJECT, STATE, TRANSITION, STABLE, MICROSTEP, EVENT, INVOKER, EXECUTABLE + }; + + enum Action { + UNDEF_ACTION, ENTER, EXIT, INVOKE, UNINVOKE + }; + + Breakpoint() { + subject = UNDEF_SUBJECT; + when = UNDEF_WHEN; + action = UNDEF_ACTION; + } + Breakpoint(const Data& data); + + // would we match the given breakpoint as well? + bool matches(Interpreter interpreter, const Breakpoint& other) const; + + Data toData() const; + + bool operator<(const Breakpoint& other) const { + return (toData() < other.toData()); + } + + operator bool() { + return (subject != UNDEF_SUBJECT || + when != UNDEF_WHEN || + action != UNDEF_ACTION); + } + + mutable bool enabled; + + When when; + Subject subject; + Action action; + + const XERCESC_NS::DOMElement* element = NULL; + + std::string invokeId; + std::string invokeType; + + std::string eventName; + + std::string executableName; + std::string executableXPath; + + std::string stateId; + std::string transSourceId; + std::string transTargetId; + + std::string condition; +}; + +} + + + +#endif /* end of include guard: BREAKPOINT_H_VR7K7T1X */ diff --git a/src/uscxml/debug/DebugSession.cpp b/src/uscxml/debug/DebugSession.cpp new file mode 100644 index 0000000..ef4d469 --- /dev/null +++ b/src/uscxml/debug/DebugSession.cpp @@ -0,0 +1,373 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#include "uscxml/debug/DebugSession.h" +#include "uscxml/debug/Debugger.h" +#include "uscxml/util/Predicates.h" + +namespace uscxml { + +void DebugSession::checkBreakpoints(const std::list<Breakpoint> qualifiedBreakpoints) { + std::list<Breakpoint>::const_iterator qualifiedBreakpointIter = qualifiedBreakpoints.begin(); + + if (!_breakpointsEnabled) + return; + + while(qualifiedBreakpointIter != qualifiedBreakpoints.end()) { + const Breakpoint& qualifiedBreakpoint = *qualifiedBreakpointIter++; + + // check if one of the user-supplied breakpoints match + bool userBreakpointMatched = false; + Data replyData; + + if (_skipTo) { + if (_skipTo.matches(_interpreter, qualifiedBreakpoint)) { + replyData.compound["breakpoint"] = _skipTo.toData(); + replyData.compound["qualified"] = qualifiedBreakpoint.toData(); + breakExecution(replyData); + _skipTo = Breakpoint(); + } + continue; + } + + std::set<Breakpoint>::const_iterator breakpointIter = _breakPoints.begin(); + while(breakpointIter != _breakPoints.end()) { + const Breakpoint& breakpoint = *breakpointIter++; + if (!breakpoint.enabled) + continue; + if (breakpoint.matches(_interpreter, qualifiedBreakpoint)) { + // do we have a condition? + + replyData.compound["breakpoint"] = breakpoint.toData(); + replyData.compound["qualified"] = qualifiedBreakpoint.toData(); + + userBreakpointMatched = true; + breakExecution(replyData); + } + } + if (_isStepping && !userBreakpointMatched) { + replyData.compound["qualified"] = qualifiedBreakpoint.toData(); + breakExecution(replyData); + + } + } +} + +void DebugSession::breakExecution(Data replyData) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + + std::list<XERCESC_NS::DOMElement*> configuration = _interpreter.getConfiguration(); + for (auto state : configuration) { + if (HAS_ATTR(state, "id")) { + replyData.compound["activeStates"].array.push_back(Data(ATTR(state, "id"), Data::VERBATIM)); + if (isAtomic(state)) { + replyData.compound["basicStates"].array.push_back(Data(ATTR(state, "id"), Data::VERBATIM)); + } + } + } + + replyData.compound["replyType"] = Data("breakpoint", Data::VERBATIM); + _debugger->pushData(shared_from_this(), replyData); + _resumeCond.wait(_mutex); +} + +Data DebugSession::debugPrepare(const Data& data) { + Data replyData; + + if (!data.hasKey("xml") && !data.hasKey("url")) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No XML or URL given", Data::VERBATIM); + return replyData; + } + + debugStop(data); + + _isAttached = false; + + if (data.hasKey("xml")) { + _interpreter = Interpreter::fromXML(data.at("xml").atom, (data.hasKey("url") ? data.at("url").atom : "")); + } else if (data.hasKey("url")) { + _interpreter = Interpreter::fromURL(data.at("url").atom); + } else { + _interpreter = Interpreter(); + } + + if (_interpreter) { + // register ourself as a monitor + _interpreter.addMonitor(_debugger); + _debugger->attachSession(_interpreter.getImpl().get(), shared_from_this()); + + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + + return replyData; +} + +Data DebugSession::debugAttach(const Data& data) { + Data replyData; + _isAttached = true; + + if (!data.hasKey("attach")) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No id to attach to given", Data::VERBATIM); + return replyData; + } + + std::string interpreterId = data.at("attach").atom; + bool interpreterFound = false; + + // find interpreter for sessionid + std::map<std::string, std::weak_ptr<InterpreterImpl> > instances = InterpreterImpl::getInstances(); + for (auto weakInstance : instances) { + + std::shared_ptr<InterpreterImpl> instance = weakInstance.second.lock(); + if (instance && instance->getSessionId() == interpreterId) { + _interpreter = instance; + _debugger->attachSession(_interpreter.getImpl().get(), shared_from_this()); + interpreterFound = true; + break; + } + } + + if (!interpreterFound) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No interpreter with given id found", Data::VERBATIM); + } else { + replyData.compound["xml"].node = _interpreter.getImpl()->getDocument(); + replyData.compound["status"] = Data("success", Data::VERBATIM); + } + + return replyData; +} + +Data DebugSession::debugDetach(const Data& data) { + Data replyData; + _isAttached = false; + + _debugger->detachSession(_interpreter.getImpl().get()); + replyData.compound["status"] = Data("success", Data::VERBATIM); + return replyData; +} + +Data DebugSession::debugStart(const Data& data) { + Data replyData; + + if (_isAttached) { + replyData.compound["reason"] = Data("Already started when attached", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } else if (!_interpreter) { + replyData.compound["reason"] = Data("No interpreter attached or loaded", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } else { + //_interpreter.start(); + assert(false); + + replyData.compound["status"] = Data("success", Data::VERBATIM); + } + + return replyData; +} + +Data DebugSession::debugStop(const Data& data) { + Data replyData; + + if (_interpreter) { + // detach from old intepreter + _debugger->detachSession(_interpreter.getImpl().get()); + } + + if (_interpreter && !_isAttached) + assert(false); + //_interpreter.stop(); + // unblock + _resumeCond.notify_all(); + + _skipTo = Breakpoint(); + replyData.compound["status"] = Data("success", Data::VERBATIM); + + // calls destructor + _interpreter = Interpreter(); + + return replyData; +} + +Data DebugSession::debugStep(const Data& data) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + + stepping(true); + _resumeCond.notify_one(); + + Data replyData; + if (_interpreter) { + // register ourself as a monitor + if (!_isRunning) + //_interpreter.start(); + assert(false); + + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + return replyData; +} + +Data DebugSession::debugResume(const Data& data) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + + stepping(false); + + Data replyData; + replyData.compound["status"] = Data("success", Data::VERBATIM); + + _resumeCond.notify_one(); + return replyData; +} + + +Data DebugSession::debugPause(const Data& data) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + + _skipTo = Breakpoint(); + stepping(true); + + Data replyData; + replyData.compound["status"] = Data("success", Data::VERBATIM); + + return replyData; +} + +Data DebugSession::skipToBreakPoint(const Data& data) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + + _skipTo = Breakpoint(data); + + Data replyData; + replyData.compound["status"] = Data("success", Data::VERBATIM); + + _resumeCond.notify_one(); + return replyData; +} + +Data DebugSession::addBreakPoint(const Data& data) { + Breakpoint breakpoint(data); + + Data replyData; + if (_breakPoints.find(breakpoint) == _breakPoints.end()) { + _breakPoints.insert(breakpoint); + replyData.compound["status"] = Data("success", Data::VERBATIM); + + } else { + replyData.compound["reason"] = Data("Breakpoint already exists", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + return replyData; +} + +Data DebugSession::removeBreakPoint(const Data& data) { + Breakpoint breakpoint(data); + + Data replyData; + if (_breakPoints.find(breakpoint) != _breakPoints.end()) { + _breakPoints.erase(breakpoint); + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["reason"] = Data("No such breakpoint", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + return replyData; +} + +Data DebugSession::enableBreakPoint(const Data& data) { + Breakpoint breakpoint(data); + + Data replyData; + if (_breakPoints.find(breakpoint) != _breakPoints.end()) { + _breakPoints.find(breakpoint)->enabled = true; + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["reason"] = Data("No such breakpoint", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + + return replyData; +} +Data DebugSession::disableBreakPoint(const Data& data) { + Breakpoint breakpoint(data); + + Data replyData; + if (_breakPoints.find(breakpoint) != _breakPoints.end()) { + _breakPoints.find(breakpoint)->enabled = false; + replyData.compound["status"] = Data("success", Data::VERBATIM); + } else { + replyData.compound["reason"] = Data("No such breakpoint", Data::VERBATIM); + replyData.compound["status"] = Data("failure", Data::VERBATIM); + } + + return replyData; +} +Data DebugSession::enableAllBreakPoints() { + Data replyData; + + _breakpointsEnabled = true; + replyData.compound["status"] = Data("success", Data::VERBATIM); + + return replyData; +} +Data DebugSession::disableAllBreakPoints() { + Data replyData; + + _breakpointsEnabled = false; + replyData.compound["status"] = Data("success", Data::VERBATIM); + + return replyData; +} + +Data DebugSession::debugEval(const Data& data) { + Data replyData; + + if (!data.hasKey("expression")) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No expression given", Data::VERBATIM); + return replyData; + } + + std::string expr = data.at("expression").atom; + + if (!_interpreter) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No interpreter running", Data::VERBATIM); + } else if (!_interpreter.getImpl()->_dataModel) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No datamodel available", Data::VERBATIM); + } else { + try { + replyData.compound["eval"] = _interpreter.getImpl()->getAsData(expr); + } catch (Event e) { + replyData.compound["eval"] = e.data; + replyData.compound["eval"].compound["error"] = Data(e.name, Data::VERBATIM); + } + replyData.compound["status"] = Data("success", Data::VERBATIM); + } + return replyData; +} + + +}
\ No newline at end of file diff --git a/src/uscxml/debug/DebugSession.h b/src/uscxml/debug/DebugSession.h new file mode 100644 index 0000000..a1ecaa0 --- /dev/null +++ b/src/uscxml/debug/DebugSession.h @@ -0,0 +1,107 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef DEBUGSESSION_H_M8YHEGV6 +#define DEBUGSESSION_H_M8YHEGV6 + +#include "uscxml/debug/Breakpoint.h" +#include "uscxml/Interpreter.h" +#include <time.h> +#include <set> +#include <thread> +#include <condition_variable> + +namespace uscxml { + +class Debugger; + +class USCXML_API DebugSession : public std::enable_shared_from_this<DebugSession> { +public: + DebugSession() { + _isRunning = false; + _isStepping = false; + _isAttached = false; + _breakpointsEnabled = true; + _markedForDeletion = false; + _debugger = NULL; + } + + void stepping(bool enable) { + _isStepping = enable; + } + + void checkBreakpoints(const std::list<Breakpoint> qualifiedBreakpoints); + + Data debugPrepare(const Data& data); + Data debugAttach(const Data& data); + Data debugDetach(const Data& data); + Data debugStart(const Data& data); + Data debugStop(const Data& data); + Data debugStep(const Data& data); + Data debugResume(const Data& data); + Data debugPause(const Data& data); + Data skipToBreakPoint(const Data& data); + Data addBreakPoint(const Data& data); + Data removeBreakPoint(const Data& data); + Data enableBreakPoint(const Data& data); + Data disableBreakPoint(const Data& data); + Data enableAllBreakPoints(); + Data disableAllBreakPoints(); + Data debugEval(const Data& data); + + void setDebugger(Debugger* debugger) { + _debugger = debugger; + } + + Interpreter getInterpreter() { + return _interpreter; + } + + void markForDeletion(bool mark) { + _markedForDeletion = mark; + } + +protected: + void breakExecution(Data replyData); + + bool _isStepping; + bool _isAttached; + bool _breakpointsEnabled; + + std::condition_variable_any _resumeCond; + std::recursive_mutex _runMutex; + std::recursive_mutex _mutex; + + std::thread* _interpreterThread = NULL; + bool _isRunning; + + bool _markedForDeletion; + Debugger* _debugger; + Interpreter _interpreter; + std::set<Breakpoint> _breakPoints; + Breakpoint _skipTo; + + friend class Debugger; +}; + + +} + + +#endif /* end of include guard: DEBUGSESSION_H_M8YHEGV6 */ diff --git a/src/uscxml/debug/Debugger.cpp b/src/uscxml/debug/Debugger.cpp new file mode 100644 index 0000000..09c21e7 --- /dev/null +++ b/src/uscxml/debug/Debugger.cpp @@ -0,0 +1,258 @@ +/** +* @file +* @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) +* @copyright Simplified BSD +* +* @cond +* This program is free software: you can redistribute it and/or modify +* it under the terms of the FreeBSD license as published by the FreeBSD +* project. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +* +* You should have received a copy of the FreeBSD license along with this +* program. If not, see <http://www.opensource.org/licenses/bsd-license>. +* @endcond +*/ + +#include "uscxml/debug/Debugger.h" +#include "uscxml/util/DOM.h" +#include "uscxml/util/Predicates.h" +#include "uscxml/debug/DebugSession.h" + +namespace uscxml { + +void Debugger::afterCompletion(InterpreterImpl* impl) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + + Data msg; + msg.compound["replyType"] = Data("finished", Data::VERBATIM); + pushData(session, msg); +} + +void Debugger::beforeCompletion(InterpreterImpl* impl) {} + +std::list<Breakpoint> Debugger::getQualifiedStateBreakpoints(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state, Breakpoint breakpointTemplate) { + std::list<Breakpoint> breakpoints; + + Breakpoint bp = breakpointTemplate; // copy base as template + bp.stateId = ATTR(state, "id"); + bp.element = state; + bp.subject = Breakpoint::STATE; + breakpoints.push_back(bp); + + return breakpoints; +} + +std::list<Breakpoint> Debugger::getQualifiedInvokeBreakpoints(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string invokeId, Breakpoint breakpointTemplate) { + std::list<Breakpoint> breakpoints; + + Breakpoint bp = breakpointTemplate; // copy base as template + bp.subject = Breakpoint::INVOKER; + bp.element = invokeElem; + bp.invokeId = invokeId; + + if (HAS_ATTR(invokeElem, "type")) { + bp.invokeType = ATTR(invokeElem, "type"); + } else if (HAS_ATTR(invokeElem, "typeexpr")) { + bp.invokeType = impl->evalAsData(ATTR(invokeElem, "typeexpr")).atom; + } + + breakpoints.push_back(bp); + + return breakpoints; +} + +std::list<Breakpoint> Debugger::getQualifiedTransBreakpoints(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition, Breakpoint breakpointTemplate) { + std::list<Breakpoint> breakpoints; + + XERCESC_NS::DOMElement* source = getSourceState(transition); + std::list<XERCESC_NS::DOMElement*> targets = getTargetStates(transition, impl->_scxml); + + for (auto target : targets) { + + Breakpoint bp = breakpointTemplate; // copy base as template + bp.element = transition; + bp.transSourceId = ATTR(source, "id"); + bp.transTargetId = ATTR(target, "id"); + bp.subject = Breakpoint::TRANSITION; + + breakpoints.push_back(bp); + } + + return breakpoints; +} + +void Debugger::beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { + handleTransition(impl, transition, Breakpoint::BEFORE); +} +void Debugger::afterTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { + handleTransition(impl, transition, Breakpoint::AFTER); +} +void Debugger::beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* execContent) { + handleExecutable(impl, execContent, Breakpoint::BEFORE); +} +void Debugger::afterExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* execContent) { + handleExecutable(impl, execContent, Breakpoint::AFTER); +} +void Debugger::beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { + handleState(impl, state, Breakpoint::BEFORE, Breakpoint::EXIT); +} +void Debugger::afterExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { + handleState(impl, state, Breakpoint::AFTER, Breakpoint::EXIT); +} +void Debugger::beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { + handleState(impl, state, Breakpoint::BEFORE, Breakpoint::ENTER); +} +void Debugger::afterEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { + handleState(impl, state, Breakpoint::AFTER, Breakpoint::ENTER); +} +void Debugger::beforeUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + handleInvoke(impl, invokeElem, invokeid, Breakpoint::BEFORE, Breakpoint::UNINVOKE); +} +void Debugger::afterUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + handleInvoke(impl, invokeElem, invokeid, Breakpoint::AFTER, Breakpoint::UNINVOKE); +} +void Debugger::beforeInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + handleInvoke(impl, invokeElem, invokeid, Breakpoint::BEFORE, Breakpoint::INVOKE); +} +void Debugger::afterInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + handleInvoke(impl, invokeElem, invokeid, Breakpoint::AFTER, Breakpoint::INVOKE); +} +void Debugger::onStableConfiguration(InterpreterImpl* impl) { + handleStable(impl, Breakpoint::ON); +} +void Debugger::beforeMicroStep(InterpreterImpl* impl) { + handleMicrostep(impl, Breakpoint::BEFORE); +} +void Debugger::afterMicroStep(InterpreterImpl* impl) { + handleMicrostep(impl, Breakpoint::AFTER); +} +void Debugger::beforeProcessingEvent(InterpreterImpl* impl, const Event& event) { + handleEvent(impl, event, Breakpoint::BEFORE); +} + +void Debugger::handleExecutable(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* execContentElem, + Breakpoint::When when) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.element = execContentElem; + breakpoint.executableName = X(execContentElem->getLocalName()).str(); + breakpoint.subject = Breakpoint::EXECUTABLE; + breakpoints.push_back(breakpoint); + + session->checkBreakpoints(breakpoints); + +} + +void Debugger::handleEvent(InterpreterImpl* impl, const Event& event, Breakpoint::When when) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.eventName = event.name; + breakpoint.subject = Breakpoint::EVENT; + breakpoints.push_back(breakpoint); + + session->checkBreakpoints(breakpoints); + +} + +void Debugger::handleStable(InterpreterImpl* impl, Breakpoint::When when) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.subject = Breakpoint::STABLE; + breakpoints.push_back(breakpoint); + + session->checkBreakpoints(breakpoints); +} + +void Debugger::handleMicrostep(InterpreterImpl* impl, Breakpoint::When when) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + std::list<Breakpoint> breakpoints; + + Breakpoint breakpoint; + breakpoint.when = when; + breakpoint.subject = Breakpoint::MICROSTEP; + breakpoints.push_back(breakpoint); + + session->checkBreakpoints(breakpoints); +} + +void Debugger::handleTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition, Breakpoint::When when) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + Breakpoint breakpointTemplate; + breakpointTemplate.when = when; + std::list<Breakpoint> qualifiedBreakpoints = getQualifiedTransBreakpoints(impl, transition, breakpointTemplate); + session->checkBreakpoints(qualifiedBreakpoints); +} + +void Debugger::handleState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state, Breakpoint::When when, Breakpoint::Action action) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + Breakpoint breakpointTemplate; + breakpointTemplate.when = when; + breakpointTemplate.action = action; + std::list<Breakpoint> qualifiedBreakpoints = getQualifiedStateBreakpoints(impl, state, breakpointTemplate); + session->checkBreakpoints(qualifiedBreakpoints); + +} + +void Debugger::handleInvoke(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeId, Breakpoint::When when, Breakpoint::Action action) { + std::shared_ptr<DebugSession> session = getSession(impl); + if (!session) + return; + if (!session->_isRunning) + return; + + Breakpoint breakpointTemplate; + breakpointTemplate.when = when; + breakpointTemplate.action = action; + std::list<Breakpoint> qualifiedBreakpoints = getQualifiedInvokeBreakpoints(impl, invokeElem, invokeId, breakpointTemplate); + session->checkBreakpoints(qualifiedBreakpoints); + +} + + +}
\ No newline at end of file diff --git a/src/uscxml/debug/Debugger.h b/src/uscxml/debug/Debugger.h new file mode 100644 index 0000000..08136d6 --- /dev/null +++ b/src/uscxml/debug/Debugger.h @@ -0,0 +1,116 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef DEBUGGERMONITOR_H_Z050WPFH +#define DEBUGGERMONITOR_H_Z050WPFH + +#include "uscxml/messages/Data.h" // for Data +#include "uscxml/messages/Event.h" // for Event +#include "uscxml/interpreter/InterpreterImpl.h" +#include "uscxml/debug/Breakpoint.h" + +namespace uscxml { + +class DebugSession; + +class USCXML_API Debugger : public InterpreterMonitor { +public: + Debugger() { + } + virtual ~Debugger() {} + + virtual void attachSession(InterpreterImpl* impl, std::shared_ptr<DebugSession> session) { + std::lock_guard<std::recursive_mutex> lock(_sessionMutex); + _sessionForInterpreter[impl] = session; + } + + virtual void detachSession(InterpreterImpl* impl) { + std::lock_guard<std::recursive_mutex> lock(_sessionMutex); + _sessionForInterpreter.erase(impl); + } + + virtual std::shared_ptr<DebugSession> getSession(InterpreterImpl* impl) { + std::lock_guard<std::recursive_mutex> lock(_sessionMutex); + if (_sessionForInterpreter.find(impl) != _sessionForInterpreter.end()) + return _sessionForInterpreter[impl]; + return std::shared_ptr<DebugSession>(); + } + + virtual void pushData(std::shared_ptr<DebugSession> session, Data pushData) = 0; + + // InterpreterMonitor + virtual void beforeProcessingEvent(InterpreterImpl* impl, const Event& event); + virtual void beforeMicroStep(InterpreterImpl* impl); + virtual void beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); + virtual void afterExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); + virtual void beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* execContent); + virtual void afterExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* execContent); + virtual void beforeUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid); + virtual void afterUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid); + virtual void beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition); + virtual void afterTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition); + virtual void beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); + virtual void afterEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); + virtual void beforeInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid); + virtual void afterInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid); + virtual void afterMicroStep(InterpreterImpl* impl); + virtual void onStableConfiguration(InterpreterImpl* impl); + virtual void beforeCompletion(InterpreterImpl* impl); + virtual void afterCompletion(InterpreterImpl* impl); + +protected: + + void handleTransition(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* transition, + Breakpoint::When when); + void handleState(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* state, + Breakpoint::When when, + Breakpoint::Action action); + void handleInvoke(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* invokeElem, + const std::string& invokeId, + Breakpoint::When when, + Breakpoint::Action action); + void handleExecutable(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* execContentElem, + Breakpoint::When when); + void handleStable(InterpreterImpl* impl, Breakpoint::When when); + void handleMicrostep(InterpreterImpl* impl, Breakpoint::When when); + void handleEvent(InterpreterImpl* impl, const Event& event, Breakpoint::When when); + + std::list<Breakpoint> getQualifiedTransBreakpoints(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* transition, + Breakpoint breakpointTemplate); + std::list<Breakpoint> getQualifiedStateBreakpoints(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* state, + Breakpoint breakpointTemplate); + std::list<Breakpoint> getQualifiedInvokeBreakpoints(InterpreterImpl* impl, + const XERCESC_NS::DOMElement* invokeElem, + const std::string invokeId, + Breakpoint breakpointTemplate); + + std::recursive_mutex _sessionMutex; + std::map<InterpreterImpl*, std::shared_ptr<DebugSession> > _sessionForInterpreter; +}; + +} + + +#endif /* end of include guard: DEBUGGERMONITOR_H_Z050WPFH */ diff --git a/src/uscxml/debug/DebuggerServlet.cpp b/src/uscxml/debug/DebuggerServlet.cpp new file mode 100644 index 0000000..2b035a6 --- /dev/null +++ b/src/uscxml/debug/DebuggerServlet.cpp @@ -0,0 +1,261 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#include "uscxml/debug/DebuggerServlet.h" +#include "uscxml/debug/DebugSession.h" +#include "uscxml/util/UUID.h" +#include <boost/algorithm/string.hpp> + +namespace uscxml { + +void DebuggerServlet::pushData(std::shared_ptr<DebugSession> session, Data pushData) { + std::cout << "trying to push " << pushData.at("replyType").atom << std::endl; + + if (!session) { + if (_sendQueues.size() > 0) // logging is not aware of its interpreter + _sendQueues.begin()->second.push(pushData); + } else { + _sendQueues[session].push(pushData); + } + + serverPushData(session); +} + +void DebuggerServlet::serverPushData(std::shared_ptr<DebugSession> session) { + if (_sendQueues[session].isEmpty()) + return; + + if (!_clientConns[session]) + return; + + Data reply = _sendQueues[session].pop(); + std::cout << "pushing " << reply.at("replyType").atom << std::endl; + returnData(_clientConns[session], reply); + _clientConns[session] = HTTPServer::Request(); +} + +void DebuggerServlet::returnData(const HTTPServer::Request& request, Data replyData) { + HTTPServer::Reply reply(request); + + if (!replyData.hasKey("status")) { + replyData.compound["status"] = Data("success", Data::VERBATIM); + } + + std::cout << "<- " << replyData << std::endl; + + reply.content = Data::toJSON(replyData); + reply.headers["Access-Control-Allow-Origin"] = "*"; + reply.headers["Content-Type"] = "application/json"; + HTTPServer::reply(reply); +} + +bool DebuggerServlet::isCORS(const HTTPServer::Request& request) { + return (request.data.at("type").atom == "options" && + request.data.at("header").hasKey("Origin") && + request.data.at("header").hasKey("Access-Control-Request-Method")); +} + +void DebuggerServlet::handleCORS(const HTTPServer::Request& request) { + HTTPServer::Reply corsReply(request); + if (request.data.at("header").hasKey("Origin")) { + corsReply.headers["Access-Control-Allow-Origin"] = request.data.at("header").at("Origin").atom; + } else { + corsReply.headers["Access-Control-Allow-Origin"] = "*"; + } + if (request.data.at("header").hasKey("Access-Control-Request-Method")) + corsReply.headers["Access-Control-Allow-Methods"] = request.data.at("header").at("Access-Control-Request-Method").atom; + if (request.data.at("header").hasKey("Access-Control-Request-Headers")) + corsReply.headers["Access-Control-Allow-Headers"] = request.data.at("header").at("Access-Control-Request-Headers").atom; + + // std::cout << "CORS!" << std::endl << request << std::endl; + HTTPServer::reply(corsReply); +} + +bool DebuggerServlet::httpRecvRequest(const HTTPServer::Request& request) { + if (!request.data.hasKey("path")) + return false; // returnError(request); + + if (isCORS(request)) { + handleCORS(request); + return true; + } + + std::cout << request.data["path"] << ": " << request.data["content"] << std::endl; + + Data replyData; + // process request that don't need a session + if (false) { + } else if (boost::istarts_with(request.data.at("path").atom, "/debug/connect")) { + processConnect(request); + return true; + } else if (boost::starts_with(request.data.at("path").atom, "/debug/sessions")) { + processListSessions(request); + return true; + } + + // get session or return error + if (false) { + } else if (!request.data.at("content").hasKey("session")) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No session given", Data::VERBATIM); + } else if (_sessionForId.find(request.data.at("content").at("session").atom) == _sessionForId.end()) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No such session", Data::VERBATIM); + } + if (!replyData.empty()) { + returnData(request, replyData); + return true; + } + + std::shared_ptr<DebugSession> session = _sessionForId[request.data.at("content").at("session").atom]; + + if (false) { + } else if (boost::starts_with(request.data.at("path").atom, "/debug/poll")) { + // save long-standing client poll + _clientConns[session] = request; + serverPushData(session); + + } else if (boost::starts_with(request.data.at("path").atom, "/debug/disconnect")) { + processDisconnect(request); + + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/enable/all")) { + replyData = session->enableAllBreakPoints(); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/disable/all")) { + replyData = session->disableAllBreakPoints(); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/skipto")) { + replyData = session->skipToBreakPoint(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/add")) { + replyData = session->addBreakPoint(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/remove")) { + replyData = session->removeBreakPoint(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/enable")) { + replyData = session->enableBreakPoint(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/breakpoint/disable")) { + replyData = session->disableBreakPoint(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/stop")) { + replyData = session->debugStop(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/prepare")) { + replyData = session->debugPrepare(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/attach")) { + replyData = session->debugAttach(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/start")) { + replyData = session->debugStart(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/step")) { + replyData = session->debugStep(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/pause")) { + replyData = session->debugPause(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/resume")) { + replyData = session->debugResume(request.data["content"]); + } else if (boost::starts_with(request.data.at("path").atom, "/debug/eval")) { + replyData = session->debugEval(request.data["content"]); + } + + if (!replyData.empty()) { + returnData(request, replyData); + return true; + } + + return true; +} + +// someone connected, create a new session +void DebuggerServlet::processConnect(const HTTPServer::Request& request) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + std::string sessionId = UUID::getUUID(); + + _sessionForId[sessionId] = std::shared_ptr<DebugSession>(new DebugSession()); + _sessionForId[sessionId]->setDebugger(this); + + Data replyData; + replyData.compound["session"] = Data(sessionId, Data::VERBATIM); + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); +} + +void DebuggerServlet::processDisconnect(const HTTPServer::Request& request) { + std::lock_guard<std::recursive_mutex> lock(_mutex); + + Data replyData; + + if (!request.data.at("content").hasKey("session")) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No session given", Data::VERBATIM); + returnData(request, replyData); + } + + std::string sessionId = request.data.at("content").at("session").atom; + + if (_sessionForId.find(sessionId) == _sessionForId.end()) { + replyData.compound["status"] = Data("failure", Data::VERBATIM); + replyData.compound["reason"] = Data("No such session", Data::VERBATIM); + } else { + replyData.compound["status"] = Data("success", Data::VERBATIM); + detachSession(_sessionForId[sessionId]->getInterpreter().getImpl().get()); + _sessionForId[sessionId]->debugStop(request.data["content"]); + _clientConns.erase(_sessionForId[sessionId]); + _sendQueues.erase(_sessionForId[sessionId]); + _sessionForId.erase(sessionId); + } + + returnData(request, replyData); +} + +void DebuggerServlet::processListSessions(const HTTPServer::Request& request) { + Data replyData; + + std::map<std::string, std::weak_ptr<InterpreterImpl> > instances = InterpreterImpl::getInstances(); + for (auto weakInstance : instances) { + + std::shared_ptr<InterpreterImpl> instance = weakInstance.second.lock(); + if (instance) { + Data sessionData; + sessionData.compound["name"] = Data(instance->getName(), Data::VERBATIM); + sessionData.compound["id"] = Data(instance->getSessionId(), Data::VERBATIM); + sessionData.compound["source"] = Data(instance->getBaseURL(), Data::VERBATIM); + sessionData.compound["xml"].node = instance->getDocument(); + + replyData.compound["sessions"].array.push_back(sessionData); + } + } + + replyData.compound["status"] = Data("success", Data::VERBATIM); + returnData(request, replyData); +} + +/* +void DebuggerServlet::send(google::LogSeverity severity, const char* full_filename, + const char* base_filename, int line, + const struct ::tm* tm_time, + const char* message, size_t message_len) { + + // _sendQueue is thread-safe, not sure about ToString though + + LogMessage msg(severity, + full_filename, + base_filename, + line, + tm_time, + std::string(message, message_len), + ToString(severity, base_filename, line, tm_time, message, message_len)); + msg.compound["replyType"] = Data("log", Data::VERBATIM); + pushData(std::shared_ptr<DebugSession>(), msg); +} +*/ + +}
\ No newline at end of file diff --git a/src/uscxml/debug/DebuggerServlet.h b/src/uscxml/debug/DebuggerServlet.h new file mode 100644 index 0000000..dc6b0ee --- /dev/null +++ b/src/uscxml/debug/DebuggerServlet.h @@ -0,0 +1,108 @@ +/** + * @file + * @author 2012-2014 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef DEBUGGERSERVLET_H_ATUMDA3G +#define DEBUGGERSERVLET_H_ATUMDA3G + +#include "uscxml/Common.h" +#include <easylogging++.h> +#include "uscxml/util/BlockingQueue.h" +#include "uscxml/server/HTTPServer.h" + +#include "uscxml/debug/Debugger.h" + +namespace uscxml { + +class USCXML_API DebuggerServlet : public Debugger, public HTTPServlet { +public: + class LogMessage : public Data { + public: +#if 0 + LogMessage(google::LogSeverity severity, const char* full_filename, + const char* base_filename, int line, + const struct ::tm* tm_time, + std::string message, std::string formatted) { + + compound["severity"] = severity; + compound["fullFilename"] = Data(full_filename, Data::VERBATIM); + compound["baseFilename"] = Data(base_filename, Data::VERBATIM); + compound["line"] = line; + compound["message"] = Data(message, Data::VERBATIM); + compound["time"] = Data(mktime((struct ::tm*)tm_time), Data::INTERPRETED); + compound["formatted"] = Data(formatted, Data::VERBATIM); + } +#endif + }; + + virtual ~DebuggerServlet() {} + + // from Debugger + virtual void addBreakpoint(const Breakpoint& breakpoint) {}; + + bool isCORS(const HTTPServer::Request& request); + void handleCORS(const HTTPServer::Request& request); + + bool httpRecvRequest(const HTTPServer::Request& request); + void setURL(const std::string& url) { + _url = url; + } + + void pushData(std::shared_ptr<DebugSession> session, Data pushData); + void returnData(const HTTPServer::Request& request, Data replyData); + + void processDisconnect(const HTTPServer::Request& request); + void processConnect(const HTTPServer::Request& request); + void processListSessions(const HTTPServer::Request& request); + +// void processDebugPrepare(const HTTPServer::Request& request); +// void processDebugAttach(const HTTPServer::Request& request); +// void processDebugStart(const HTTPServer::Request& request); +// void processDebugStop(const HTTPServer::Request& request); + +// void processDebugEval(const HTTPServer::Request& request); +// void processDebugStart(const HTTPServer::Request& request); +// void processDebugStop(const HTTPServer::Request& request); +// void processDebugStep(const HTTPServer::Request& request); +// void processDebugResume(const HTTPServer::Request& request); +// void processDebugPause(const HTTPServer::Request& request); +// void processAddBreakPoint(const HTTPServer::Request& request); +// void processRemoveBreakPoint(const HTTPServer::Request& request); +// void processPoll(const HTTPServer::Request& request); + + // Logsink + /** + virtual void send(google::LogSeverity severity, const char* full_filename, + const char* base_filename, int line, + const struct ::tm* tm_time, + const char* message, size_t message_len); +*/ +protected: + void serverPushData(std::shared_ptr<DebugSession>); + + std::string _url; + std::map<std::shared_ptr<DebugSession>, HTTPServer::Request> _clientConns; + std::map<std::shared_ptr<DebugSession>, BlockingQueue<Data> > _sendQueues; + std::map<std::string, std::shared_ptr<DebugSession> > _sessionForId; + + std::recursive_mutex _mutex; +}; + +} + +#endif /* end of include guard: DEBUGGERSERVLET_H_ATUMDA3G */ diff --git a/src/uscxml/interpreter/BasicContentExecutor.cpp b/src/uscxml/interpreter/BasicContentExecutor.cpp index 58d3eae..0f5a67f 100644 --- a/src/uscxml/interpreter/BasicContentExecutor.cpp +++ b/src/uscxml/interpreter/BasicContentExecutor.cpp @@ -330,7 +330,7 @@ void BasicContentExecutor::process(XERCESC_NS::DOMElement* block, const X& xmlPr } try { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeExecutingContent, block); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeExecutingContent, block); if (false) { } else if (iequals(tagName, xmlPrefix.str() + "raise")) { @@ -358,12 +358,12 @@ void BasicContentExecutor::process(XERCESC_NS::DOMElement* block, const X& xmlPr Event e(exc); _callbacks->enqueueInternal(e); LOG(ERROR) << exc << std::endl; - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), afterExecutingContent, block); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterExecutingContent, block); throw e; // will be catched in microstepper } - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), afterExecutingContent, block); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterExecutingContent, block); } @@ -463,18 +463,18 @@ void BasicContentExecutor::invoke(XERCESC_NS::DOMElement* element) { finalize = finalizes.front(); } - USCXML_MONITOR_CALLBACK2(_callbacks->getMonitor(), beforeUninvoking, element, invokeEvent.invokeid); + USCXML_MONITOR_CALLBACK2(_callbacks->getMonitors(), beforeUninvoking, element, invokeEvent.invokeid); _callbacks->invoke(type, source, autoForward, finalize, invokeEvent); - USCXML_MONITOR_CALLBACK2(_callbacks->getMonitor(), afterUninvoking, element, invokeEvent.invokeid); + USCXML_MONITOR_CALLBACK2(_callbacks->getMonitors(), afterUninvoking, element, invokeEvent.invokeid); } void BasicContentExecutor::uninvoke(XERCESC_NS::DOMElement* invoke) { char* invokeId = (char*)invoke->getUserData(X("invokeid")); assert(invokeId != NULL); - USCXML_MONITOR_CALLBACK2(_callbacks->getMonitor(), beforeUninvoking, invoke, invokeId); + USCXML_MONITOR_CALLBACK2(_callbacks->getMonitors(), beforeUninvoking, invoke, invokeId); _callbacks->uninvoke(invokeId); - USCXML_MONITOR_CALLBACK2(_callbacks->getMonitor(), afterUninvoking, invoke, invokeId); + USCXML_MONITOR_CALLBACK2(_callbacks->getMonitors(), afterUninvoking, invoke, invokeId); invoke->setUserData(X("invokeid"), NULL, NULL); free(invokeId); diff --git a/src/uscxml/interpreter/BasicEventQueue.cpp b/src/uscxml/interpreter/BasicEventQueue.cpp index 7505f46..3cf4daf 100644 --- a/src/uscxml/interpreter/BasicEventQueue.cpp +++ b/src/uscxml/interpreter/BasicEventQueue.cpp @@ -37,24 +37,10 @@ Event BasicEventQueue::dequeue(size_t blockMs) { if (blockMs > 0) { // block for given milliseconds or until queue is filled - std::chrono::time_point<std::chrono::system_clock> updated, now; - std::chrono::milliseconds remain; + auto endTime = std::chrono::system_clock::now() + std::chrono::milliseconds(blockMs); - if (blockMs > (std::chrono::system_clock::duration::max)().count()) { - blockMs = (std::chrono::system_clock::duration::max)().count(); - } - - updated = now = std::chrono::system_clock::now(); - remain = std::chrono::milliseconds(blockMs); - - while (remain.count() > 0 && _queue.empty()) { - _cond.wait_for(_mutex, remain); - - now = std::chrono::system_clock::now(); - - auto elapsed = now - updated; - remain -= std::chrono::duration_cast<std::chrono::milliseconds>(elapsed); - updated = now; + while (_queue.empty()) { + _cond.wait_until(_mutex, endTime); } } diff --git a/src/uscxml/interpreter/ContentExecutorImpl.h b/src/uscxml/interpreter/ContentExecutorImpl.h index e1a7e8c..0e12aff 100644 --- a/src/uscxml/interpreter/ContentExecutorImpl.h +++ b/src/uscxml/interpreter/ContentExecutorImpl.h @@ -65,7 +65,7 @@ public: virtual const Event& getCurrentEvent() = 0; /** Monitoring */ - virtual InterpreterMonitor* getMonitor() = 0; + virtual std::set<InterpreterMonitor*> getMonitors() = 0; }; diff --git a/src/uscxml/interpreter/FastMicroStep.cpp b/src/uscxml/interpreter/FastMicroStep.cpp index ea43c5d..826df93 100644 --- a/src/uscxml/interpreter/FastMicroStep.cpp +++ b/src/uscxml/interpreter/FastMicroStep.cpp @@ -89,68 +89,66 @@ FastMicroStep::~FastMicroStep() { } } -void FastMicroStep::resortStates(DOMNode* node, const X& xmlPrefix) { - if (node->getNodeType() != DOMNode::ELEMENT_NODE) - return; +void FastMicroStep::resortStates(DOMElement* element, const X& xmlPrefix) { - /** + /** initials deep histories shallow histories everything else */ - DOMElement* element = dynamic_cast<DOMElement*>(node); + DOMElement* child = element->getFirstElementChild(); + while(child) { + resortStates(child, xmlPrefix); + child = child->getNextElementSibling(); + } // shallow history states to top - DOMNode* child = element->getFirstChild(); + child = element->getFirstElementChild(); while(child) { - resortStates(child, xmlPrefix); - if (child->getNodeType() == DOMNode::ELEMENT_NODE && - TAGNAME_CAST(child) == xmlPrefix.str() + "history" && - (!HAS_ATTR(element, "type") || iequals(ATTR(element, "type"), "shallow"))) { + if (TAGNAME_CAST(child) == xmlPrefix.str() + "history" && + (!HAS_ATTR(element, "type") || iequals(ATTR(element, "type"), "shallow"))) { - DOMNode* tmp = child->getNextSibling(); + DOMElement* tmp = child->getNextElementSibling(); if (child != element->getFirstChild()) { element->insertBefore(child, element->getFirstChild()); } child = tmp; } else { - child = child->getNextSibling(); + child = child->getNextElementSibling(); } } // deep history states to top - child = element->getFirstChild(); + child = element->getFirstElementChild(); while(child) { - resortStates(child, xmlPrefix); if (child->getNodeType() == DOMNode::ELEMENT_NODE && TAGNAME_CAST(child) == xmlPrefix.str() + "history" && HAS_ATTR(element, "type") && iequals(ATTR(element, "type"), "deep")) { - DOMNode* tmp = child->getNextSibling(); + DOMElement* tmp = child->getNextElementSibling(); if (child != element->getFirstChild()) { element->insertBefore(child, element->getFirstChild()); } child = tmp; } else { - child = child->getNextSibling(); + child = child->getNextElementSibling(); } } // initial states on top of histories even - child = element->getFirstChild(); + child = element->getFirstElementChild(); while(child) { - resortStates(child, xmlPrefix); if (child->getNodeType() == DOMNode::ELEMENT_NODE && LOCALNAME_CAST(child) == "initial") { - DOMNode* tmp = child->getNextSibling(); + DOMElement* tmp = child->getNextElementSibling(); if (child != element->getFirstChild()) { element->insertBefore(child, element->getFirstChild()); } child = tmp; } else { - child = child->getNextSibling(); + child = child->getNextElementSibling(); } } } @@ -193,10 +191,9 @@ void FastMicroStep::init(XERCESC_NS::DOMElement* scxml) { } resortStates(_scxml, _xmlPrefix); - - // TODO: https://github.com/tklab-tud/uscxml/blob/master/src/uscxml/transform/ChartToC.cpp#L244 - - +// assert(false); +// throw NULL; + /** -- All things states -- */ std::list<XERCESC_NS::DOMElement*> tmp; @@ -467,7 +464,7 @@ InterpreterState FastMicroStep::step(size_t blockMs) { return USCXML_FINISHED; if (_flags & USCXML_CTX_TOP_LEVEL_FINAL) { - USCXML_MONITOR_CALLBACK(_callbacks->getMonitor(), beforeCompletion); + USCXML_MONITOR_CALLBACK(_callbacks->getMonitors(), beforeCompletion); /* exit all remaining states */ i = USCXML_NUMBER_STATES; @@ -498,7 +495,7 @@ InterpreterState FastMicroStep::step(size_t blockMs) { _flags |= USCXML_CTX_FINISHED; - USCXML_MONITOR_CALLBACK(_callbacks->getMonitor(), afterCompletion); + USCXML_MONITOR_CALLBACK(_callbacks->getMonitors(), afterCompletion); return USCXML_FINISHED; } @@ -508,7 +505,7 @@ InterpreterState FastMicroStep::step(size_t blockMs) { targetSet |= USCXML_GET_STATE(0).completion; _flags |= USCXML_CTX_SPONTANEOUS | USCXML_CTX_INITIALIZED; - USCXML_MONITOR_CALLBACK(_callbacks->getMonitor(), beforeMicroStep); + USCXML_MONITOR_CALLBACK(_callbacks->getMonitors(), beforeMicroStep); goto ESTABLISH_ENTRYSET; } @@ -520,7 +517,7 @@ InterpreterState FastMicroStep::step(size_t blockMs) { if ((_event = _callbacks->dequeueInternal())) { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeProcessingEvent, _event); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeProcessingEvent, _event); goto SELECT_TRANSITIONS; } @@ -551,13 +548,13 @@ InterpreterState FastMicroStep::step(size_t blockMs) { // we dequeued all internal events and ought to signal stable configuration if (!(_flags & USCXML_CTX_STABLE)) { - USCXML_MONITOR_CALLBACK(_callbacks->getMonitor(), onStableConfiguration); + USCXML_MONITOR_CALLBACK(_callbacks->getMonitors(), onStableConfiguration); _microstepConfigurations.clear(); _flags |= USCXML_CTX_STABLE; } if ((_event = _callbacks->dequeueExternal(blockMs))) { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeProcessingEvent, _event); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeProcessingEvent, _event); goto SELECT_TRANSITIONS; } @@ -629,7 +626,7 @@ SELECT_TRANSITIONS: return USCXML_MACROSTEPPED; } - USCXML_MONITOR_CALLBACK(_callbacks->getMonitor(), beforeMicroStep); + USCXML_MONITOR_CALLBACK(_callbacks->getMonitors(), beforeMicroStep); #ifdef USCXML_VERBOSE std::cerr << "Targets: "; @@ -793,7 +790,7 @@ ESTABLISH_ENTRYSET: while(i-- > 0) { if (BIT_HAS(i, exitSet) && BIT_HAS(i, _configuration)) { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeExitingState, USCXML_GET_STATE(i).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeExitingState, USCXML_GET_STATE(i).element); /* call all on exit handlers */ for (auto exitIter = USCXML_GET_STATE(i).onExit.begin(); exitIter != USCXML_GET_STATE(i).onExit.end(); exitIter++) { @@ -805,7 +802,7 @@ ESTABLISH_ENTRYSET: } BIT_CLEAR(i, _configuration); - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), afterExitingState, USCXML_GET_STATE(i).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterExitingState, USCXML_GET_STATE(i).element); } } @@ -814,7 +811,7 @@ ESTABLISH_ENTRYSET: i = transSet.find_first(); while(i != boost::dynamic_bitset<>::npos) { if ((USCXML_GET_TRANS(i).type & (USCXML_TRANS_HISTORY | USCXML_TRANS_INITIAL)) == 0) { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeTakingTransition, USCXML_GET_TRANS(i).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeTakingTransition, USCXML_GET_TRANS(i).element); if (USCXML_GET_TRANS(i).onTrans != NULL) { @@ -826,7 +823,7 @@ ESTABLISH_ENTRYSET: } } - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), afterTakingTransition, USCXML_GET_TRANS(i).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterTakingTransition, USCXML_GET_TRANS(i).element); } i = transSet.find_next(i); @@ -856,7 +853,7 @@ ESTABLISH_ENTRYSET: continue; } - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeEnteringState, USCXML_GET_STATE(i).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeEnteringState, USCXML_GET_STATE(i).element); BIT_SET_AT(i, _configuration); @@ -877,7 +874,7 @@ ESTABLISH_ENTRYSET: } } - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), afterEnteringState, USCXML_GET_STATE(i).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterEnteringState, USCXML_GET_STATE(i).element); /* take history and initial transitions */ for (j = 0; j < USCXML_NUMBER_TRANS; j++) { @@ -885,7 +882,7 @@ ESTABLISH_ENTRYSET: (USCXML_GET_TRANS(j).type & (USCXML_TRANS_HISTORY | USCXML_TRANS_INITIAL)) && USCXML_GET_STATE(USCXML_GET_TRANS(j).source).parent == i) { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), beforeTakingTransition, USCXML_GET_TRANS(j).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), beforeTakingTransition, USCXML_GET_TRANS(j).element); /* call executable content in transition */ if (USCXML_GET_TRANS(j).onTrans != NULL) { @@ -896,7 +893,7 @@ ESTABLISH_ENTRYSET: } } - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), afterTakingTransition, USCXML_GET_TRANS(j).element); + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), afterTakingTransition, USCXML_GET_TRANS(j).element); } } @@ -941,15 +938,17 @@ ESTABLISH_ENTRYSET: } } } - USCXML_MONITOR_CALLBACK(_callbacks->getMonitor(), afterMicroStep); + USCXML_MONITOR_CALLBACK(_callbacks->getMonitors(), afterMicroStep); // are we running in circles? if (_microstepConfigurations.find(_configuration) != _microstepConfigurations.end()) { - USCXML_MONITOR_CALLBACK1(_callbacks->getMonitor(), + InterpreterIssue issue("Reentering same configuration during microstep - possible endless loop", + NULL, + InterpreterIssue::USCXML_ISSUE_WARNING); + + USCXML_MONITOR_CALLBACK1(_callbacks->getMonitors(), reportIssue, - InterpreterIssue("Reentering same configuration during microstep - possible endless loop", - NULL, - InterpreterIssue::USCXML_ISSUE_WARNING)); + issue); } _microstepConfigurations.insert(_configuration); diff --git a/src/uscxml/interpreter/FastMicroStep.h b/src/uscxml/interpreter/FastMicroStep.h index a3cdf9c..dd58480 100644 --- a/src/uscxml/interpreter/FastMicroStep.h +++ b/src/uscxml/interpreter/FastMicroStep.h @@ -125,7 +125,7 @@ protected: private: std::list<XERCESC_NS::DOMElement*> getHistoryCompletion(const XERCESC_NS::DOMElement* state); - void resortStates(XERCESC_NS::DOMNode* node, const X& xmlPrefix); + void resortStates(XERCESC_NS::DOMElement* node, const X& xmlPrefix); bool conflictsCached(const XERCESC_NS::DOMElement* t1, const XERCESC_NS::DOMElement* t2, const XERCESC_NS::DOMElement* root); ///< overrides implementation Predicates::conflicts for speed diff --git a/src/uscxml/interpreter/InterpreterImpl.cpp b/src/uscxml/interpreter/InterpreterImpl.cpp index 0547f12..5cbae3c 100644 --- a/src/uscxml/interpreter/InterpreterImpl.cpp +++ b/src/uscxml/interpreter/InterpreterImpl.cpp @@ -69,7 +69,7 @@ void InterpreterImpl::addInstance(std::shared_ptr<InterpreterImpl> interpreterIm _instances[interpreterImpl->getSessionId()] = interpreterImpl; } -InterpreterImpl::InterpreterImpl() : _isInitialized(false), _document(NULL), _scxml(NULL), _state(USCXML_INSTANTIATED), _monitor(NULL) { +InterpreterImpl::InterpreterImpl() : _isInitialized(false), _document(NULL), _scxml(NULL), _state(USCXML_INSTANTIATED) { try { ::xercesc_3_1::XMLPlatformUtils::Initialize(); } catch (const XERCESC_NS::XMLException& toCatch) { diff --git a/src/uscxml/interpreter/InterpreterImpl.h b/src/uscxml/interpreter/InterpreterImpl.h index be33306..6d047ec 100644 --- a/src/uscxml/interpreter/InterpreterImpl.h +++ b/src/uscxml/interpreter/InterpreterImpl.h @@ -97,10 +97,14 @@ public: return _microStepper.getConfiguration(); } - void setMonitor(InterpreterMonitor* monitor) { - _monitor = monitor; + void addMonitor(InterpreterMonitor* monitor) { + _monitors.insert(monitor); } + void removeMonitor(InterpreterMonitor* monitor) { + _monitors.erase(monitor); + } + /** MicrostepCallbacks */ @@ -132,8 +136,8 @@ public: _execContent.uninvoke(invoke); } - virtual InterpreterMonitor* getMonitor() { - return _monitor; + virtual std::set<InterpreterMonitor*> getMonitors() { + return _monitors; } /** @@ -255,7 +259,9 @@ protected: friend class InterpreterIssue; friend class TransformerImpl; friend class USCXMLInvoker; - friend class SCXMLIOProcessor; + friend class SCXMLIOProcessor; + friend class DebugSession; + friend class Debugger; X _xmlPrefix; X _xmlNS; @@ -280,7 +286,7 @@ protected: std::map<std::string, IOProcessor> _ioProcs; std::map<std::string, Invoker> _invokers; std::set<std::string> _autoForwarders; - InterpreterMonitor* _monitor; + std::set<InterpreterMonitor*> _monitors; private: void setupDOM(); diff --git a/src/uscxml/interpreter/InterpreterMonitor.h b/src/uscxml/interpreter/InterpreterMonitor.h index 8dac445..ff2e7cb 100644 --- a/src/uscxml/interpreter/InterpreterMonitor.h +++ b/src/uscxml/interpreter/InterpreterMonitor.h @@ -33,14 +33,14 @@ catch (std::bad_weak_ptr e) { LOG(ERROR) << "Unclean shutdown " << std::endl; } catch (...) { LOG(ERROR) << "An exception occurred when calling " #callback " on monitors"; } \ if (_state == USCXML_DESTROYED) { throw std::bad_weak_ptr(); } -#define USCXML_MONITOR_CALLBACK(callback, function) \ -if (callback) { callback->function(); } +#define USCXML_MONITOR_CALLBACK(callbacks, function) \ +for (auto callback : callbacks) { callback->function(NULL); } -#define USCXML_MONITOR_CALLBACK1(callback, function, arg1) \ -if (callback) { callback->function(arg1); } +#define USCXML_MONITOR_CALLBACK1(callbacks, function, arg1) \ +for (auto callback : callbacks) { callback->function(NULL, arg1); } -#define USCXML_MONITOR_CALLBACK2(callback, function, arg1, arg2) \ -if (callback) { callback->function(arg1, arg2); } +#define USCXML_MONITOR_CALLBACK2(callbacks, function, arg1, arg2) \ +for (auto callback : callbacks) { callback->function(NULL, arg1, arg2); } // forward declare namespace XERCESC_NS { @@ -54,34 +54,34 @@ public: InterpreterMonitor() : _copyToInvokers(false) {} virtual ~InterpreterMonitor() {} - virtual void beforeProcessingEvent(const Event& event) {} - virtual void beforeMicroStep() {} + virtual void beforeProcessingEvent(InterpreterImpl* impl, const Event& event) {} + virtual void beforeMicroStep(InterpreterImpl* impl) {} - virtual void beforeExitingState(const XERCESC_NS::DOMElement* state) {} - virtual void afterExitingState(const XERCESC_NS::DOMElement* state) {} + virtual void beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) {} + virtual void afterExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) {} - virtual void beforeExecutingContent(const XERCESC_NS::DOMElement* execContent) {} - virtual void afterExecutingContent(const XERCESC_NS::DOMElement* execContent) {} + virtual void beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* execContent) {} + virtual void afterExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* execContent) {} - virtual void beforeUninvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} - virtual void afterUninvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} + virtual void beforeUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} + virtual void afterUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} - virtual void beforeTakingTransition(const XERCESC_NS::DOMElement* transition) {} - virtual void afterTakingTransition(const XERCESC_NS::DOMElement* transition) {} + virtual void beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) {} + virtual void afterTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) {} - virtual void beforeEnteringState(const XERCESC_NS::DOMElement* state) {} - virtual void afterEnteringState(const XERCESC_NS::DOMElement* state) {} + virtual void beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) {} + virtual void afterEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) {} - virtual void beforeInvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} - virtual void afterInvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} + virtual void beforeInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} + virtual void afterInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) {} - virtual void afterMicroStep() {} - virtual void onStableConfiguration() {} + virtual void afterMicroStep(InterpreterImpl* impl) {} + virtual void onStableConfiguration(InterpreterImpl* impl) {} - virtual void beforeCompletion() {} - virtual void afterCompletion() {} + virtual void beforeCompletion(InterpreterImpl* impl) {} + virtual void afterCompletion(InterpreterImpl* impl) {} - virtual void reportIssue(const InterpreterIssue& issue) {} + virtual void reportIssue(InterpreterImpl* impl, const InterpreterIssue& issue) {} void copyToInvokers(bool copy) { _copyToInvokers = copy; @@ -101,13 +101,13 @@ public: StateTransitionMonitor() {} virtual ~StateTransitionMonitor() {} - virtual void beforeTakingTransition(const XERCESC_NS::DOMElement* transition); - virtual void beforeExecutingContent(const XERCESC_NS::DOMElement* element); - virtual void onStableConfiguration(); - virtual void beforeProcessingEvent(const uscxml::Event& event); - virtual void beforeExitingState(const XERCESC_NS::DOMElement* state); - virtual void beforeEnteringState(const XERCESC_NS::DOMElement* state); - virtual void beforeMicroStep(); + virtual void beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition); + virtual void beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* element); + virtual void onStableConfiguration(InterpreterImpl* impl); + virtual void beforeProcessingEvent(InterpreterImpl* impl, const uscxml::Event& event); + virtual void beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); + virtual void beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state); + virtual void beforeMicroStep(InterpreterImpl* impl); protected: static std::recursive_mutex _mutex; diff --git a/src/uscxml/interpreter/MicroStepImpl.h b/src/uscxml/interpreter/MicroStepImpl.h index be9d974..cb4aec6 100644 --- a/src/uscxml/interpreter/MicroStepImpl.h +++ b/src/uscxml/interpreter/MicroStepImpl.h @@ -23,6 +23,7 @@ #include "uscxml/config.h" #include <list> +#include <set> #include <string> #include <xercesc/dom/DOM.hpp> @@ -33,6 +34,8 @@ namespace uscxml { +class InterpreterMonitor; + /** * @ingroup microstep * @ingroup callback @@ -57,7 +60,7 @@ public: virtual void uninvoke(XERCESC_NS::DOMElement* invoke) = 0; /** Monitoring */ - virtual InterpreterMonitor* getMonitor() = 0; + virtual std::set<InterpreterMonitor*> getMonitors() = 0; }; /** diff --git a/src/uscxml/util/BlockingQueue.h b/src/uscxml/util/BlockingQueue.h new file mode 100644 index 0000000..2f0ab2e --- /dev/null +++ b/src/uscxml/util/BlockingQueue.h @@ -0,0 +1,77 @@ +/** + * @file + * @author 2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de) + * @copyright Simplified BSD + * + * @cond + * This program is free software: you can redistribute it and/or modify + * it under the terms of the FreeBSD license as published by the FreeBSD + * project. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the FreeBSD license along with this + * program. If not, see <http://www.opensource.org/licenses/bsd-license>. + * @endcond + */ + +#ifndef BLOCKINGQUEUE_H_4LEVMY0N +#define BLOCKINGQUEUE_H_4LEVMY0N + +#include "uscxml/Common.h" +#include <list> +#include <condition_variable> + +namespace uscxml { + +template <class T> +class BlockingQueue { +public: + BlockingQueue() {} + virtual ~BlockingQueue() { + } + + virtual void push(const T& elem) { + std::lock_guard<std::mutex> lock(_mutex); + _queue.push_back(elem); + _cond.notify_all(); + } + + virtual void push_front(const T& elem) { + std::lock_guard<std::mutex> lock(_mutex); + _queue.push_front(elem); + _cond.notify_all(); + } + + virtual T pop() { + std::lock_guard<std::mutex> lock(_mutex); +// std::cout << "Popping from " << this << std::endl; + while (_queue.empty()) { + _cond.wait(_mutex); + } + T ret = _queue.front(); + _queue.pop_front(); + return ret; + } + + virtual void clear() { + std::lock_guard<std::mutex> lock(_mutex); + _queue.clear(); + } + + virtual bool isEmpty() { + std::lock_guard<std::mutex> lock(_mutex); + return _queue.empty(); + } + +protected: + std::mutex _mutex; + std::condition_variable_any _cond; + std::list<T> _queue; +}; + +} + +#endif /* end of include guard: BLOCKINGQUEUE_H_4LEVMY0N */ diff --git a/src/uscxml/util/DOM.h b/src/uscxml/util/DOM.h index 0b35f40..86fa813 100644 --- a/src/uscxml/util/DOM.h +++ b/src/uscxml/util/DOM.h @@ -127,17 +127,20 @@ class USCXML_API X { public : X(X const &other) { + _localForm = other._localForm; _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm); _deallocOther = true; } void operator=(X const &other) { // did we maybe leak before? + _localForm = other._localForm; _unicodeForm = XERCESC_NS::XMLString::replicate(other._unicodeForm); _deallocOther = true; } X(const XMLCh* const toTranscode) { + if (toTranscode != NULL) { // Call the private transcoding method char* tmp = XERCESC_NS::XMLString::transcode(toTranscode); @@ -149,6 +152,7 @@ public : } X(const std::string& fromTranscode) { + // Call the private transcoding method _localForm = fromTranscode; _unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode.c_str()); @@ -156,6 +160,7 @@ public : } X(const char* const fromTranscode) { + // Call the private transcoding method _localForm = fromTranscode; _unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode); @@ -163,22 +168,25 @@ public : } X(char* fromTranscode) { - // Call the private transcoding method + + // Call the private transcoding method _localForm = fromTranscode; _unicodeForm = XERCESC_NS::XMLString::transcode(fromTranscode); _deallocOther = true; } X() { + _unicodeForm = NULL; _deallocOther = false; } ~X() { + if (_deallocOther) XERCESC_NS::XMLString::release(&_unicodeForm); } - + const std::string& str() const { return _localForm; } @@ -195,7 +203,7 @@ public : operator std::string () { return _localForm; } - + protected: friend USCXML_API std::ostream& operator<< (std::ostream& os, const X& data); @@ -277,6 +285,32 @@ private: }; #endif + +#if 0 +static const X kElementScxmlName = X("scxml"); +static const X kElementStateName = X("state"); +static const X kElementParallelName = X("parallel"); +static const X kElementTransitionName = X("transition"); +static const X kElementInitialName = X("initial"); +static const X kElementFinalName = X("final"); +static const X kElementOnEntryName = X("onentry"); +static const X kElementOnExitName = X("onexit"); +static const X kElementHistoryName = X("history"); + +static const X kElementRaiseName = X("raise"); +static const X kElementIfName = X("if"); +static const X kElementElseIfName = X("elseif"); +static const X kElementElseName = X("else"); +static const X kElementForEachName = X("foreach"); +static const X kElementLogName = X("log"); + +static const X kElementDataModelName = X("datamodel"); +static const X kElementDataName = X("data"); +static const X kElementAssignName = X("assign"); +static const X kElementContentName = X("content"); +static const X kElementParamName = X("param"); +static const X kElementScriptName = X("script"); +#endif USCXML_API std::ostream& operator<< (std::ostream& os, const X& xmlString); USCXML_API std::ostream& operator<< (std::ostream& os, const XERCESC_NS::DOMNode& node); diff --git a/test/issues/test-issue87.scxml.todo b/test/issues/test-issue87.scxml.todo new file mode 100644 index 0000000..55f0b60 --- /dev/null +++ b/test/issues/test-issue87.scxml.todo @@ -0,0 +1,189 @@ +<scxml datamodel="lua" initial="Work" name="Simulator" version="1.0" xmlns="http://www.w3.org/2005/07/scxml"> + <datamodel> + <data id="PauseTimers">{ + test = 'a', + b = 25, + t_test = { + xxx = '23', + zzz = 45 + } +} + </data> + </datamodel> + <final id="Pass"/> + <final id="Fail"/> + <state id="Work" initial="Init"> + <onentry> + <log expr="'Hello from work'"/> + </onentry> + <transition event="error.*" target="Fail"/> + <transition event="quit" target="Pass"/> + <state id="Init"> + <onentry> + <log expr="'Hello from init'"/> + </onentry> + <transition event="StartExercise" target="Training"/> + </state> + <parallel id="Training"> + <transition event="StopExercise" target="Init"/> + <state id="APU"> + <invoke type="scxml"> + <content> + <scxml datamodel="lua" initial="StateShape1" name="ScxmlShape1" version="1.0" xmlns="http://www.w3.org/2005/07/scxml"> + <final id="Fail"/> + <state id="StateShape1" initial="Flash"> + <transition event="error" target="Fail"/> + <transition event="StopExercise" target="Pass"/> + <state id="Flash"> + <datamodel> + <data id="t_ApuOff">{ + dDiff = 0, + dEnter = os.clock(), + dInterval = 2000 +} + </data> + <data id="t_ApuOn">{ + dDiff = 0, + dEnter = os.clock(), + dInterval = 2000 +} + </data> + <data expr="'xxx'" id="FlashName"/> + </datamodel> + <transition event="Pause" target="OnPause"/> + <state id="ApuOn"> + <onentry> + <send delayexpr="tostring(t_ApuOn.dInterval-t_ApuOn.dDiff)..'ms'" event="DoApuOff" id="ID_DoApuOff"/> + <assign expr="os.clock()" location="t_ApuOn.dEnter"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOn.dInterval-t_ApuOn.dDiff)" label="Delay[On]"/> + </onentry> + <onexit> + <cancel sendid="ID_DoApuOff"/> + <assign expr="t_ApuOn.dDiff + (os.clock() - t_ApuOn.dEnter)*1000" location="t_ApuOn.dDiff"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOn.dDiff)" label="Elapsed[On]"/> + </onexit> + <transition event="DoApuOff" target="ApuOff"> + <assign location="t_ApuOn.dDiff" expr="0"/> + </transition> + </state> + <initial> + <transition target="ApuOff"> + <assign location="t_ApuOff.dDiff" expr="0"/> + <assign location="t_ApuOn.dDiff" expr="0"/> + </transition> + </initial> + <state id="ApuOff"> + <onentry> + <send delayexpr="tostring(t_ApuOff.dInterval-t_ApuOff.dDiff)..'ms'" event="DoApuOn" id="ID_DoApuOn"/> + <assign expr="os.clock()" location="t_ApuOff.dEnter"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOff.dInterval-t_ApuOff.dDiff)" label="Delay[Off]"/> + </onentry> + <onexit> + <cancel sendid="ID_DoApuOn"/> + <assign expr="t_ApuOff.dDiff + (os.clock() - t_ApuOff.dEnter)*1000" location="t_ApuOff.dDiff"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOff.dDiff)" label="Elapsed[Off]"/> + </onexit> + <transition event="DoApuOn" target="ApuOn"> + <assign location="t_ApuOff.dDiff" expr="0"/> + </transition> + </state> + <history id="HistoryShape1" type="deep"> + <transition target="ApuOff"/> + </history> + </state> + <state id="OnPause"> + <transition event="Resume" target="HistoryShape1"/> + </state> + </state> + <final id="Pass"/> + </scxml> + </content> + <param expr="'APU'" name="FlashName"/> + </invoke> + </state> + <state id="StateShape3"> + <state id="OnResume"> + <transition event="Pause" target="OnPause"/> + </state> + <state id="OnPause"> + <transition event="Resume" target="OnResume"/> + </state> + </state> + <state id="ENGINES"> + <invoke type="scxml"> + <content> + <scxml datamodel="lua" initial="StateShape1" name="ScxmlShape1" version="1.0" xmlns="http://www.w3.org/2005/07/scxml"> + <final id="Fail"/> + <state id="StateShape1" initial="Flash"> + <transition event="error" target="Fail"/> + <transition event="StopExercise" target="Pass"/> + <state id="Flash"> + <datamodel> + <data id="t_ApuOff">{ + dDiff = 0, + dEnter = os.clock(), + dInterval = 2000 +} + </data> + <data id="t_ApuOn">{ + dDiff = 0, + dEnter = os.clock(), + dInterval = 2000 +} + </data> + <data expr="'xxx'" id="FlashName"/> + </datamodel> + <transition event="Pause" target="OnPause"/> + <state id="ApuOn"> + <onentry> + <send delayexpr="tostring(t_ApuOn.dInterval-t_ApuOn.dDiff)..'ms'" event="DoApuOff" id="ID_DoApuOff"/> + <assign expr="os.clock()" location="t_ApuOn.dEnter"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOn.dInterval-t_ApuOn.dDiff)" label="Delay[On]"/> + </onentry> + <onexit> + <cancel sendid="ID_DoApuOff"/> + <assign expr="t_ApuOn.dDiff + (os.clock() - t_ApuOn.dEnter)*1000" location="t_ApuOn.dDiff"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOn.dDiff)" label="Elapsed[On]"/> + </onexit> + <transition event="DoApuOff" target="ApuOff"> + <assign location="t_ApuOn.dDiff" expr="0"/> + </transition> + </state> + <initial> + <transition target="ApuOff"> + <assign location="t_ApuOff.dDiff" expr="0"/> + <assign location="t_ApuOn.dDiff" expr="0"/> + </transition> + </initial> + <state id="ApuOff"> + <onentry> + <send delayexpr="tostring(t_ApuOff.dInterval-t_ApuOff.dDiff)..'ms'" event="DoApuOn" id="ID_DoApuOn"/> + <assign expr="os.clock()" location="t_ApuOff.dEnter"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOff.dInterval-t_ApuOff.dDiff)" label="Delay[Off]"/> + </onentry> + <onexit> + <cancel sendid="ID_DoApuOn"/> + <assign expr="t_ApuOff.dDiff + (os.clock() - t_ApuOff.dEnter)*1000" location="t_ApuOff.dDiff"/> + <log expr="string.format('%s=%d',FlashName,t_ApuOff.dDiff)" label="Elapsed[Off]"/> + </onexit> + <transition event="DoApuOn" target="ApuOn"> + <assign location="t_ApuOff.dDiff" expr="0"/> + </transition> + </state> + <history id="HistoryShape1" type="deep"> + <transition target="ApuOff"/> + </history> + </state> + <state id="OnPause"> + <transition event="Resume" target="HistoryShape1"/> + </state> + </state> + <final id="Pass"/> + </scxml> + </content> + <param expr="'ENGINES'" name="FlashName"/> + </invoke> + </state> + </parallel> + </state> +</scxml> diff --git a/test/src/test-lifecycle.cpp b/test/src/test-lifecycle.cpp index 14ebd94..81fc363 100644 --- a/test/src/test-lifecycle.cpp +++ b/test/src/test-lifecycle.cpp @@ -48,67 +48,67 @@ std::list<CallbackType> callBackSeq; class SequenceCheckingMonitor : public InterpreterMonitor { - virtual void beforeProcessingEvent(const Event& event) { + virtual void beforeProcessingEvent(InterpreterImpl* impl, const Event& event) { CHECK_CALLBACK_TYPE(USCXML_BEFOREPROCESSINGEVENT); } - virtual void beforeMicroStep() { + virtual void beforeMicroStep(InterpreterImpl* impl) { CHECK_CALLBACK_TYPE(USCXML_BEFOREMICROSTEP); } - virtual void beforeExitingState(const XERCESC_NS::DOMElement* state) { + virtual void beforeExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { CHECK_CALLBACK_TYPE(USCXML_BEFOREEXITINGSTATE); } - virtual void afterExitingState(const XERCESC_NS::DOMElement* state) { + virtual void afterExitingState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { CHECK_CALLBACK_TYPE(USCXML_AFTEREXITINGSTATE); } - virtual void beforeExecutingContent(const XERCESC_NS::DOMElement* element) { + virtual void beforeExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* element) { CHECK_CALLBACK_TYPE(USCXML_BEFOREEXECUTINGCONTENT); } - virtual void afterExecutingContent(const XERCESC_NS::DOMElement* element) { + virtual void afterExecutingContent(InterpreterImpl* impl, const XERCESC_NS::DOMElement* element) { CHECK_CALLBACK_TYPE(USCXML_AFTEREXECUTINGCONTENT); } - virtual void beforeUninvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + virtual void beforeUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { CHECK_CALLBACK_TYPE(USCXML_BEFOREUNINVOKING); } - virtual void afterUninvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + virtual void afterUninvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { CHECK_CALLBACK_TYPE(USCXML_AFTERUNINVOKING); } - virtual void beforeTakingTransition(const XERCESC_NS::DOMElement* transition) { + virtual void beforeTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { CHECK_CALLBACK_TYPE(USCXML_BEFORETAKINGTRANSITION); } - virtual void afterTakingTransition(const XERCESC_NS::DOMElement* transition) { + virtual void afterTakingTransition(InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { CHECK_CALLBACK_TYPE(USCXML_AFTERTAKINGTRANSITION); } - virtual void beforeEnteringState(const XERCESC_NS::DOMElement* state) { + virtual void beforeEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { CHECK_CALLBACK_TYPE(USCXML_BEFOREENTERINGSTATE); } - virtual void afterEnteringState(const XERCESC_NS::DOMElement* state) { + virtual void afterEnteringState(InterpreterImpl* impl, const XERCESC_NS::DOMElement* state) { CHECK_CALLBACK_TYPE(USCXML_AFTERENTERINGSTATE); } - virtual void beforeInvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + virtual void beforeInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { CHECK_CALLBACK_TYPE(USCXML_BEFOREINVOKING); } - virtual void afterInvoking(const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { + virtual void afterInvoking(InterpreterImpl* impl, const XERCESC_NS::DOMElement* invokeElem, const std::string& invokeid) { CHECK_CALLBACK_TYPE(USCXML_AFTERINVOKING); } - virtual void afterMicroStep() { + virtual void afterMicroStep(InterpreterImpl* impl) { CHECK_CALLBACK_TYPE(USCXML_AFTERMICROSTEP); } - virtual void onStableConfiguration() { + virtual void onStableConfiguration(InterpreterImpl* impl) { CHECK_CALLBACK_TYPE(USCXML_ONSTABLECONFIGURATION); } - virtual void beforeCompletion() { + virtual void beforeCompletion(InterpreterImpl* impl) { CHECK_CALLBACK_TYPE(USCXML_BEFORECOMPLETION); } - virtual void afterCompletion() { + virtual void afterCompletion(InterpreterImpl* impl) { CHECK_CALLBACK_TYPE(USCXML_AFTERCOMPLETION); } @@ -139,7 +139,7 @@ int main(int argc, char** argv) { try { const char* xml = "<invalid />"; Interpreter interpreter = Interpreter::fromXML(xml, ""); - interpreter.setMonitor(&mon); + interpreter.addMonitor(&mon); assert(interpreter.getState() == USCXML_INSTANTIATED); interpreter.step(); assert(false); @@ -159,7 +159,7 @@ int main(int argc, char** argv) { " <final id=\"done\" />" "</scxml>"; Interpreter interpreter = Interpreter::fromXML(xml, ""); - interpreter.setMonitor(&mon); + interpreter.addMonitor(&mon); assert(interpreter.getState() == USCXML_INSTANTIATED); interpreter.step(); assert(false); @@ -182,7 +182,7 @@ int main(int argc, char** argv) { "</scxml>"; Interpreter interpreter = Interpreter::fromXML(xml, ""); - interpreter.setMonitor(&mon); + interpreter.addMonitor(&mon); callBackSeq.push_back(USCXML_BEFOREMICROSTEP); callBackSeq.push_back(USCXML_BEFOREENTERINGSTATE); // scxml @@ -232,7 +232,7 @@ int main(int argc, char** argv) { "</scxml>"; Interpreter interpreter = Interpreter::fromXML(xml, ""); - interpreter.setMonitor(&mon); + interpreter.addMonitor(&mon); callBackSeq.push_back(USCXML_BEFOREMICROSTEP); callBackSeq.push_back(USCXML_BEFOREENTERINGSTATE); // scxml @@ -304,7 +304,7 @@ int main(int argc, char** argv) { "</scxml>"; Interpreter interpreter = Interpreter::fromXML(xml, ""); - interpreter.setMonitor(&mon); + interpreter.addMonitor(&mon); callBackSeq.push_back(USCXML_BEFOREMICROSTEP); callBackSeq.push_back(USCXML_BEFOREENTERINGSTATE); // scxml callBackSeq.push_back(USCXML_AFTERENTERINGSTATE); diff --git a/test/src/test-state-pass.cpp b/test/src/test-state-pass.cpp index ac52a0b..afda506 100644 --- a/test/src/test-state-pass.cpp +++ b/test/src/test-state-pass.cpp @@ -73,11 +73,11 @@ int main(int argc, char** argv) { // interpreter.setActionLanguage(al); StateTransitionMonitor mon; - interpreter.setMonitor(&mon); + interpreter.addMonitor(&mon); InterpreterState state = InterpreterState::USCXML_UNDEF; while(state != USCXML_FINISHED) { - state = interpreter.step(true); + state = interpreter.step(); } assert(interpreter.isInState("pass")); } catch (Event e) { diff --git a/test/src/test-stress.cpp b/test/src/test-stress.cpp index 8bd2e4a..dc54f4e 100644 --- a/test/src/test-stress.cpp +++ b/test/src/test-stress.cpp @@ -16,7 +16,7 @@ int startedAt; int lastTransitionAt; class StatusMonitor : public uscxml::InterpreterMonitor { - void beforeTakingTransition(const XERCESC_NS::DOMElement* transition) { + void beforeTakingTransition(uscxml::InterpreterImpl* impl, const XERCESC_NS::DOMElement* transition) { lastTransitionAt = time(NULL); } @@ -81,7 +81,7 @@ int main(int argc, char** argv) { LOG(INFO) << "Processing " << interpreter.getImpl()->getBaseURL(); if (interpreter) { - interpreter.setMonitor(&vm); + interpreter.addMonitor(&vm); InterpreterState state = InterpreterState::USCXML_UNDEF; int now = time(NULL); diff --git a/test/src/test-url.cpp b/test/src/test-url.cpp index aa559f3..0e1d115 100644 --- a/test/src/test-url.cpp +++ b/test/src/test-url.cpp @@ -137,7 +137,7 @@ int main(int argc, char** argv) { // assert(foo.isAbsolute());
- HTTPServer::getInstance(8099, 8100);
+ HTTPServer::getInstance(8199, 8200);
std::string exeName = argv[0];
exeName = exeName.substr(exeName.find_last_of("\\/") + 1);
diff --git a/test/src/test-validating.cpp b/test/src/test-validating.cpp index 840a105..c3bc693 100644 --- a/test/src/test-validating.cpp +++ b/test/src/test-validating.cpp @@ -31,7 +31,7 @@ public: IssueMonitor() { runtimeIssues = 0; } - void reportIssue(const InterpreterIssue& issue) { + void reportIssue(InterpreterImpl* impl, const InterpreterIssue& issue) { runtimeIssues++; } }; @@ -60,7 +60,7 @@ int main(int argc, char** argv) { IssueMonitor monitor; Interpreter interpreter = Interpreter::fromXML(xml, ""); - interpreter.setMonitor(&monitor); + interpreter.addMonitor(&monitor); while(interpreter.step() > 0) {} |