in xsl decl labels(*ptr, name="defs_by_labels") alias call-template {
with "hmi_element", "$hmi_element";
with "labels"{text *ptr};
decl optional_labels(*ptr) alias - {
/* TODO add some per label xslt variable to check if exist */
decl warning_labels(*ptr) alias - {
with "mandatory","'warn'";
decl _activable(*level) alias - {
const "activity" labels("/active /inactive") {
with "mandatory"{text *level};
const "has_activity","string-length($activity)>0";
| has_activity: «$has_activity»,
decl activable() alias - {
decl optional_activable() alias - {
decl activable_labels(*ptr) alias - {
with "subelements","'active inactive'";
in xsl decl widget_desc(%name, match="widget[@type='%name']", mode="widget_desc") alias template {
in xsl decl widget_class(%name, *clsname="%nameWidget", match="widget[@type='%name']", mode="widget_class") alias template {
| class `text **clsname` extends Widget{
in xsl decl widget_defs(%name, match="widget[@type='%name']", mode="widget_defs") alias template {
// all widget potentially has a "disabled" labeled element
const "disability" optional_labels("/disabled");
const "has_disability","string-length($disability)>0";
in xsl decl widget_page(%name, match="widget[@type='%name']", mode="widget_page") alias template {
decl gen_index_xhtml alias - {
template "svg:*", mode="hmi_widgets" {
const "widget", "func:widget(@id)";
const "args" foreach "$widget/arg" > "«func:escape_quotes(@value)»"`if "position()!=last()" > ,`
const "indexes" foreach "$widget/path" {
if "position()!=last()" > ,
const "variables" foreach "$widget/path" {
warning > Widget «$widget/@type» id="«$eltid»" : No match for path "«@value»" in HMI tree
when "@type = 'PAGE_LOCAL'"
when "@type = 'HMI_LOCAL'"
> hmi_local_index("«@value»")
error > Internal error while processing widget's non indexed HMI tree path : unknown type
> minmax:["«@min»", "«@max»"]
if "position()!=last()" > ,
const "enable_expr" choose{
when "$widget/@enable_expr"
| "«@id»": new «$widget/@type»Widget ("«@id»",«$freq»,[«$args»],[«$variables»],«$enable_expr»,{
foreach "$widget/path" { // nested loop to map assignments to varnum
const "varid","generate-id()";
if "@assign" foreach "$widget/path[@assign]" if "$varid = generate-id()" {
| "«@assign»":«position()-1» `if "position()!=last()" > ,`
foreach "$widget/path" { // nested loop to map assignments to varnum
const "varid","generate-id()";
when "@assign" foreach "$widget/path[@assign]" if "$varid = generate-id()" {
if "position()!=last()" > ,\n
if "$widget/@enable_expr" {
| compute_enable: function(value, oldval, varnum) {
foreach "$widget/path[@assign]" {
| let «@assign» = this.var_assignments[«position()-1»];
| if(«@assign» == undefined) break;
| result = «$widget/@enable_expr»;
apply "$widget", mode="widget_defs" with "hmi_element",".";
| })`if "position()!=last()" > ,`
emit "preamble:local-variable-indexes" {
var last_remote_index = hmitree_types.length - 1;
var next_available_index = hmitree_types.length;
let cookies = new Map(document.cookie.split("; ").map(s=>s.split("=")));
foreach "$parsed_widgets/widget[starts-with(@type,'VarInit')]"{
if "count(path) != 1" error > VarInit «@id» must have only one variable given.
if "path/@type != 'PAGE_LOCAL' and path/@type != 'HMI_LOCAL'" error > VarInit «@id» only applies to HMI variable.
when "@type = 'VarInitPersistent'" > cookies.has("«path/@value»")?cookies.get("«path/@value»"):«arg[1]/@value»
otherwise > «arg[1]/@value»
if "position()!=last()" > ,
const persistent_locals = new Set([
foreach "$parsed_widgets/widget[@type='VarInitPersistent']"{
| "«path/@value»"`if "position()!=last()" > ,`
var persistent_indexes = new Map();
var cache = hmitree_types.map(_ignored => undefined);
function page_local_index(varname, pagename){
let pagevars = hmi_locals[pagename];
if(pagevars == undefined){
new_index = next_available_index++;
hmi_locals[pagename] = {[varname]:new_index};
let result = pagevars[varname];
if(result != undefined) {
new_index = next_available_index++;
pagevars[varname] = new_index;
let defaultval = local_defaults[varname];
if(defaultval != undefined) {
cache[new_index] = defaultval;
if(persistent_locals.has(varname))
persistent_indexes.set(new_index, varname);
function hmi_local_index(varname){
return page_local_index(varname, "HMI_LOCAL");
emit "preamble:widget-base-class" {
var pending_widget_animates = [];
function _hide(elt, placeholder){
if(elt.parentNode != null)
placeholder.parentNode.removeChild(elt);
function _show(elt, placeholder){
placeholder.parentNode.insertBefore(elt, placeholder);
function set_activity_state(eltsub, state){
if(eltsub.active_elt != undefined){
if(eltsub.active_elt_placeholder == undefined){
eltsub.active_elt_placeholder = document.createComment("");
eltsub.active_elt.parentNode.insertBefore(eltsub.active_elt_placeholder, eltsub.active_elt);
(state?_show:_hide)(eltsub.active_elt, eltsub.active_elt_placeholder);
if(eltsub.inactive_elt != undefined){
if(eltsub.inactive_elt_placeholder == undefined){
eltsub.inactive_elt_placeholder = document.createComment("");
eltsub.inactive_elt.parentNode.insertBefore(eltsub.inactive_elt_placeholder, eltsub.inactive_elt);
((state || state==undefined)?_hide:_show)(eltsub.inactive_elt, eltsub.inactive_elt_placeholder);
frequency = 10; /* FIXME arbitrary default max freq. Obtain from config ? */
constructor(elt_id, freq, args, variables, enable_expr, members){
this.element_id = elt_id;
this.element = id(elt_id);
[this.indexes, this.variables_options] = (variables.length>0) ? zip(...variables) : [[],[]];
this.indexes_length = this.indexes.length;
this.enable_expr = enable_expr;
this.enable_state = true;
this.enable_displayed_state = true;
Object.keys(members).forEach(prop => this[prop]=members[prop]);
this.lastapply = this.indexes.map(() => undefined);
this.inhibit = this.indexes.map(() => undefined);
this.pending = this.indexes.map(() => undefined);
this.bound_uninhibit = this.uninhibit.bind(this);
this.lastdispatch = this.indexes.map(() => undefined);
this.deafen = this.indexes.map(() => undefined);
this.incoming = this.indexes.map(() => undefined);
this.bound_undeafen = this.undeafen.bind(this);
this.forced_frequency = freq;
let forced = this.forced_frequency;
if(forced !== undefined){
once every 10 seconds : 10s
let unit = forced.slice(-1);
this.frequency = factor ? 1/(factor * Number(forced.slice(0,-1)))
if(typeof(init) == "function"){
this.enable_state = false;
this.enable_displayed_state = false;
for(let child of Array.from(this.element.children)){
let label = child.getAttribute("inkscape:label");
this.enabled_elts.push(child);
this.element.removeChild(child);
for(let i = 0; i < this.indexes_length; i++) {
/* flush updates pending because of inhibition */
let inhibition = this.inhibit[i];
if(inhibition != undefined){
clearTimeout(inhibition);
this.lastapply[i] = undefined;
let deafened = this.deafen[i];
if(deafened != undefined){
this.lastdispatch[i] = undefined;
let index = this.get_variable_index(i);
subscribers(index).delete(this);
this.relativeness = undefined;
sub(new_offset, relativeness, container_id){
this.offset = new_offset;
this.relativeness = relativeness;
this.container_id = container_id ;
/* add this's subsribers */
for(let i = 0; i < this.indexes_length; i++) {
let index = this.get_variable_index(i);
if(index == undefined) continue;
subscribers(index).add(this);
for(let i = 0; i < this.indexes_length; i++) {
/* dispatch current cache in newly opened page widgets */
let realindex = this.get_variable_index(i);
if(realindex == undefined) continue;
let cached_val = cache[realindex];
if(cached_val != undefined)
this.feed_data_for_dispatch(cached_val, cached_val, i);
get_variable_index(varnum) {
let index = this.indexes[varnum];
if(typeof(index) == "string"){
index = page_local_index(index, this.container_id);
if(this.relativeness[varnum]){
undershot(new_val, min) {
clip_min_max(index, new_val) {
let minmax = this.variables_options[index].minmax;
if(minmax !== undefined && typeof new_val == "number") {
let [min,max] = minmax.map(token => {
const num = Number(token);
if(!isNaN(num) && isFinite(num)){
let idx = this.assignment_idx[token];
return this.var_assignments[idx];
this.undershot(new_val, min);
this.overshot(new_val, max);
change_hmi_value(index, opstr) {
let realindex = this.get_variable_index(index);
if(realindex == undefined) return undefined;
let old_val = cache[realindex];
let new_val = eval_operation_string(old_val, opstr);
new_val = this.clip_min_max(index, new_val);
return apply_hmi_value(realindex, new_val);
_apply_hmi_value(index, new_val) {
let realindex = this.get_variable_index(index);
if(realindex == undefined) return undefined;
new_val = this.clip_min_max(index, new_val);
return apply_hmi_value(realindex, new_val);
this.inhibit[index] = undefined;
let new_val = this.pending[index];
this.pending[index] = undefined;
return this.apply_hmi_value(index, new_val);
apply_hmi_value(index, new_val) {
if(this.inhibit[index] == undefined){
let min_interval = 1000/this.frequency;
let lastapply = this.lastapply[index];
if(lastapply == undefined || now > lastapply + min_interval){
this.lastapply[index] = now;
return this._apply_hmi_value(index, new_val);
let elapsed = now - lastapply;
this.pending[index] = new_val;
this.inhibit[index] = setTimeout(this.bound_uninhibit, min_interval - elapsed, index);
this.pending[index] = new_val;
new_hmi_value(index, value, oldval) {
// TODO avoid searching, store index at sub()
for(let i = 0; i < this.indexes_length; i++) {
let refindex = this.get_variable_index(i);
if(refindex == undefined) continue;
this.feed_data_for_dispatch(value, oldval, i);
this.deafen[index] = undefined;
let [new_val, old_val] = this.incoming[index];
this.incoming[index] = undefined;
this.lastdispatch[index] = Date.now();
this.do_dispatch(new_val, old_val, index);
if(this.enable_state != enabled){
this.enable_state = enabled;
if(this.enable_state && !this.enable_displayed_state){
for(let child of this.enabled_elts){
this.element.appendChild(child);
if(this.disabled_elt && this.disabled_elt.parentNode != null)
this.element.removeChild(this.disabled_elt);
this.enable_displayed_state = true;
}else if(!this.enable_state && this.enable_displayed_state){
for(let child of this.enabled_elts){
if(child.parentNode != null)
this.element.removeChild(child);
this.element.appendChild(this.disabled_elt);
this.enable_displayed_state = false;
// once disabled activity display is lost
this.activity_displayed_state = undefined;
feed_data_for_dispatch(value, oldval, varnum) {
if(this.dispatch || this.enable_expr){
if(this.deafen[varnum] == undefined){
let min_interval = 1000/this.frequency;
let lastdispatch = this.lastdispatch[varnum];
if(lastdispatch == undefined || now > lastdispatch + min_interval){
this.lastdispatch[varnum] = now;
this.do_dispatch(value, oldval, varnum)
let elapsed = now - lastdispatch;
this.incoming[varnum] = [value, oldval];
this.deafen[varnum] = setTimeout(this.bound_undeafen, min_interval - elapsed, varnum);
this.incoming[varnum] = [value, oldval];
do_dispatch(value, oldval, varnum) {
if(this.enable_expr || this.dispatch){
let idx = this.varnum_assignments[varnum];
this.var_assignments[idx] = value;
this.dispatch(value, oldval, varnum);
if(idx != undefined && this.enable_expr) try {
this.compute_enable(value, oldval, varnum);
// inhibit widget animation when disabled
if(!this.enable_expr || this.enable_state){
if(this.animate != undefined)
this.pending_animate = false;
if(!this.pending_animate){
pending_widget_animates.push(this);
this.pending_animate = true;
if(this.activity_displayed_state != this.activity_state){
set_activity_state(this.activable_sub, this.activity_state);
this.activity_displayed_state = this.activity_state;
const "excluded_types", "str:split('Page VarInit VarInitPersistent')";
// Key to filter unique types
key "TypesKey", "widget", "@type";
emit "declarations:hmi-classes" {
const "used_widget_types", """$parsed_widgets/widget[
generate-id() = generate-id(key('TypesKey', @type)) and
not(@type = $excluded_types)]""";
apply "$used_widget_types", mode="widget_class";
template "widget", mode="widget_class" {
class «@type»Widget extends Widget{
/* empty class, as «@type» widget didn't provide any */
warning > «@type» widget is used in SVG but widget type is not declared
const "included_ids","$parsed_widgets/widget[not(@type = $excluded_types) and not(@id = $discardable_elements/@id)]/@id";
const "page_ids","$parsed_widgets/widget[@type = 'Page']/@id";
const "hmi_widgets","$hmi_elements[@id = $included_ids]";
const "page_widgets","$hmi_elements[@id = $page_ids]";
const "result_widgets","$result_svg_ns//*[@id = $hmi_widgets/@id]";
emit "declarations:hmi-elements" {
apply "$hmi_widgets | $page_widgets", mode="hmi_widgets";
function "defs_by_labels" {
param "mandatory","'yes'";
param "subelements","/..";
const "widget_type","@type";
foreach "str:split($labels)" {
const "absolute", "starts-with(., '/')";
const "name","substring(.,number($absolute)+1)";
const "widget","$result_widgets[@id = $hmi_element/@id]";
const "elt","($widget//*[not($absolute) and @inkscape:label=$name] | $widget/*[$absolute and @inkscape:label=$name])[1]";
const "errmsg" > «$widget_type» widget (id=«$widget_id») must have a «$name» element
when "$mandatory='yes'" {
// otherwise produce nothing
| «$name»_elt: id("«$elt/@id»"),
foreach "str:split($subelements)" {
const "subelt","$elt/*[@inkscape:label=$subname][1]";
when "not($subelt/@id)" {
const "errmsg" > «$widget_type» widget (id=«$widget_id») must have a «$name»/«$subname» element
when "$mandatory='yes'" {
| /* missing «$name»/«$subname» element */
| "«$subname»_elt": id("«$subelt/@id»")`if "position()!=last()" > ,`
def "func:escape_quotes" {
// have to use a python string to enter escaped quote
// const "frstln", "string-length($frst)";
when !"contains($txt,'\"')"! {
result !"concat(substring-before($txt,'\"'),'\\\"',func:escape_quotes(substring-after($txt,'\"')))"!;