-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecipe.js
More file actions
726 lines (591 loc) · 22.8 KB
/
Copy pathrecipe.js
File metadata and controls
726 lines (591 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
// utility to search jsonified pvs property descriptions
//
//we want to split the string but keep a record of the tokens
function splitMulti(str, tokens){
for(var i = 0; i < tokens.length; i++) {
var tok = '&t'+tokens[i]+'&t';
str = str.split(tokens[i]).join(tok);
}
str = str.split('&t');
return str;
}
function RcpSelector() {
this.selectors = [];
this.Parse = function(exprs,callback) {
for(var i=0;i<exprs.length; i++) {
expr = exprs[i];
//console.log('parsing '+expr);
var elems = splitMulti(expr.trim(),['+','^',' ']);
//console.log(elems);
var compiled = this.parseSelector(elems);
this.selectors.push({select:compiled, action:callback,expression:exprs});
}
};
this.parseSelector = function(elems) {
//console.log('3parsing '+elems);
if (elems.length < 2)
return this.parseSelectorExpression(elems[0]);
var idx = elems.length - 1;
var res2 = this.parseSelectorExpression(elems[idx]);
var res1 = this.parseSelector(elems.slice(0,idx-1));
return this.cmdeval2([res1,{op:elems[idx-1]},res2]);
}
this.parseSelectorExpression = function(expr) {
//console.log('2parsing '+expr);
var elems = splitMulti(expr.trim(),['%','=','>','+','<','~','!','^',' ','|','*']);
//console.log(elems);
return this.parseSelectorElements(elems);
};
this.parseSelectorElements = function(elems) {
// and for elem, break down into accessors
var cmds = [];
var atv = '';
var isattr = false;
var cmd = undefined; // default
var pitok = true;
elems.forEach(function(elem) {
var istok = /[><+:~=!^%* ]/g.test(elem);
//console.log(elem+' istok='+istok);
//console.log(elem+' pitok='+pitok);
if (istok) {
cmd = { op: elem};
cmds.push( cmd );
} else {
var accs = splitMulti(elem.trim(),['[',']','#','$','*']);
//console.log('accs='+accs);
for(var i=0; i<accs.length; i++) {
var acc = accs[i];
//console.log(acc+' '+pitok);
switch(acc) {
case '' : continue;
case '$':
if (pitok===false) cmds.push({op:'and'});
cmd = { op: 'c', ac: 'class' , val:accs[++i].toLowerCase() };
cmds.push( cmd );
break;
case '#':
if (pitok===false) cmds.push({op:'and'});
cmd = { op: 'i', ac: 'id' , val:accs[++i] };
cmds.push( cmd);
break;
case '*':
if (pitok===false) cmds.push({op:'and'});
cmd = { op: 'e', ac: '*' };
cmds.push( cmd);
break;
case '[':
if (pitok==false) cmds.push({op:'and'});
isattr = true;
cmd = { op: '?', ac: 'properties' , val:accs[++i].toLowerCase() };
cmds.push( {op: '('} );
cmds.push( cmd);
break;
case ']':
isattr = false;
cmds.push( {op: ')'} );
break;
default:
if (isattr) {
cmd = { op: '?', ac:'var', val: acc.toLowerCase() };
cmds.push( cmd);
}
else {
cmd = { op: 'e', ac:'type', val: acc.toLowerCase() };
cmds.push( cmd);
}
break;
}
pitok = false;
}
}
pitok = istok;
});
//console.log(cmds);
return this.cmdeval(cmds);
}
class result {
constructor(r,s) {
this._result = r;
this._score = s;
}
get result() { return this._result; }
set result(value) { this._result = value;}
get score() { return this._score; }
set score(value) { this._score = value; }
}
var compare = (function(op,arg1,arg2) {
return function(c) {
var lhs = arg1(c);
var rhs = arg2(c);
//console.log('run op '+op+' for '+c.id+' against '+lhs+' and '+rhs);
switch(op){
case 'and': // and
return (rhs != undefined) && (lhs != undefined) ? new result(c,lhs.score+rhs.score) : undefined;
break;
case '=': // equals
//console.log('testing '+lhs.result+'='+rhs.result);
return ((rhs != undefined) && (lhs != undefined) && (lhs.result === rhs.result)) ? new result(c,lhs.score+rhs.score) : undefined;
break;
case '!': // not equals
return ((rhs != undefined) && (lhs != undefined) && (lhs.result != rhs.result)) ? new result(c,lhs.score+rhs.score) : undefined;
break;
case '~': // similar(contains string)
//console.log('testing '+lhs.result+'~'+rhs.result);
return ((rhs != undefined) && (lhs != undefined) &&
(lhs.result != undefined) && (rhs.result != undefined) &&
(lhs.result.includes(rhs.result))) ? new result(c,lhs.score+rhs.score) : undefined;
break;
case '<': // less than
//console.log('testing '+lhs.result+'<'+rhs.result);
if ((rhs != undefined) && (lhs != undefined)) {
var lf = parseFloat(lhs.result);
var rf = parseFloat(rhs.result);
return (lf < rf) ? new result(c,lhs.score+rhs.score) : undefined;
} else
return undefined;
break;
case '>': // greater than
//console.log('testing '+lhs.result+'>'+rhs.result);
if ((rhs != undefined) && (lhs != undefined)) {
var lf = parseFloat(lhs.result);
var rf = parseFloat(rhs.result);
return (lf > rf) ? new result(c,lhs.score+rhs.score) : undefined;
} else
return undefined;
break;
case '^': // acendant (is parent of)
var p = c.parent;
while (p != undefined) {
lhs = arg1(p);
if ((rhs != undefined) && (lhs != undefined))
return new result(c,lhs.score+rhs.score);
p = p.parent;
}
return undefined;
break;
case ' ': // immediate child of
var p = c.parent;
if (p != undefined) {
lhs = arg1(p);
rhs = arg2(c);
if ((rhs != undefined) && (lhs != undefined))
return new result (c,lhs.score+rhs.score);
}
return undefined;
case '+': // peer of
var p = c.peers;
var pcalc = undefined;
if (p != undefined) {
for(var i=0;i<p.length;i++) {
var peer = p[i];
lhs = arg1(peer);
if ((rhs != undefined) && (lhs != undefined)) {
pcalc = c;
break;
}
};
}
return pcalc != undefined ? new result(pcalc,lhs.score+rhs.score) : undefined;
default:
return undefined;
break;
}
};
});
var exec = (function(cmd) {
return function(entity) {
//console.log('exec '+cmd.op); //+' on '+JSON.stringify(entity));
switch(cmd.op) {
case 'e': // eval
//console.log('eval '+cmd.ac+'='+cmd.val);
if (entity != undefined &&
entity.attr(cmd.ac) != undefined &&
entity.attr(cmd.ac) === cmd.val) return new result(entity,0x0100);
break;
case 'c': // class
// note class (today) is assumed to be a single string; this could be
// extended to supporting multiple values (like html) making this
// an array
//console.log('eval '+entity.class+'='+cmd.val);
if (entity != undefined &&
entity.class === cmd.val) return new result(entity,0x0010);
break;
case 'i': // eval
//console.log('eval '+entity.id+'='+cmd.val);
if (entity != undefined &&
entity.id === cmd.val) return new result(entity,0x1000);
break;
case '*': // eval
//console.log('eval '+entity.id+'='+cmd.val);
if (entity != undefined) return new result(entity,0x0001);
break;
case '?': // evaluate a query/value
//console.log('query '+cmd.ac+' with '+cmd.val);
//a value query just returns the value - could be a number/string
if (cmd.ac === 'var') return new result(cmd.val,0x0100);
//otherwise we check if the specified item (property) exists
//AND it is not empty
if (entity != undefined &&
entity.attr(cmd.ac) != undefined) {
var cv = entity.property(cmd.val);
if (cv != undefined && cv.length > 0)
return new result(cv,0x1000);
}
return undefined;
break;
default:
break;
}
return undefined;
};
});
this.cmdeval = function(subcmds) {
//console.log('processing1 '+subcmds);
var subfinal = undefined;
var idx = 2;
if (subcmds[0].op === '(') {
if (subcmds[2].op === ')') {
subfinal = exec(subcmds[1]);
idx = 4;
} else {
subfinal = compare(subcmds[2].op,
exec(subcmds[1]),
exec(subcmds[3]));
idx=6;
}
} else {
subfinal = exec(subcmds[0]);
}
if (subcmds.length < idx)
return subfinal;
var final = compare(subcmds[idx-1].op,
subfinal,
this.cmdeval(subcmds.slice(idx)));
return final;
}
this.cmdeval2 = function(subcmds) {
//console.log('processing2 '+JSON.stringify(subcmds));
var subfinal = subcmds[0];
var idx = 2;
if (subcmds.length < idx)
return subfinal;
var final = compare(subcmds[idx-1].op,
subfinal,
this.cmdeval2(subcmds.slice(idx)));
return final;
}
// takes a cmd structure and builds an optimal function 'ladder' which
// will evaluate each cmd.op until they either all pass (calls callback)
// or one of them fails.
this.compile = function(cmds) {
// we should check first, but there should always be an odd number
if (cmds.length % 2 == 0) {
// even number
console.log('error - even number of commands');
return undefined;
}
if (cmds.length === 1)
return exec(cmds[0]);
return cmdeval(cmds);
};
this.eval = function(entity) {
// run through each selector command set ...
this.selectors.forEach(function(selector) {
// each is a list of commands that evaluate to true/false
var runaction = selector.select(entity);
if (runaction != undefined) {
//debug
//console.log('yes action for',entity.path,runaction.score,selector.expression,entity.comp.properties.name);
if (selector.action != undefined)
selector.action(entity, runaction.score);
} else {
//debug
//console.log('no action for',entity.path,selector.expression);
}
})
};
};
// for each entity selected, keep a running tally of the 'instruction'
// associated with the entity. The 'score' that is calculated from the
// specificity calculation is used to sort the instructions. We present
// only the highest score
var todo = [];
var select = (function(selected) {
return function(entity,score) {
var eres = todo[entity.path];
if (eres === undefined) {
eres = {}; //selected;
}
//debug
//console.log('updating',eres,'for',selected);
//console.log('score',score);
var depth = entity.path.split('/').length - 1;
// treat each instruction as a separate scorable item
for (var a in selected) {
// we keep the item with the highest score
var escore = (eres[a] == undefined) ? selected[a].score : eres[a].score;
//debug
//console.log('comparing',score,escore,'for attribute',a);
if (score > escore || eres[a] == undefined) {
var value = selected[a].value;
/*
if (value!=undefined && value.startsWith('@')) {
var replacer = value.slice(1);
switch(replacer) {
case 'depth':value = depth; break;
case 'path': value = entity.path; break;
default:
value = entity.property(replacer);
break;
}
}
*/
if (value!=undefined) {
//this could be a simple literal value, or can include
//lookup tokens e.g. iam_[name] where [name] will replace
//with the value of the property 'name'
//look for the pattern [something] and replace with
//values that we look up from the referenced property
var reps = [...value.matchAll(/\[(\w*)\]/g)];
// we need to replace the old values with new values
var oldvalue = value;
//iterate through the results
reps.forEach(function(rep) {
for(var ri=1;ri<rep.length;ri++) {
var rp = rep[ri];
var replacer = rp;
switch(replacer) {
case 'depth':value = depth; break;
case 'path': value = entity.path; break;
default:
value = entity.property(replacer);
break;
}
oldvalue = oldvalue.replace(rep[0],value!=undefined?value:"");
}
value = oldvalue;
})
}
eres[a] = { score :score, value:value };
//q: should we add the properties to the live structure - this could
// allow subsequent queries to leverage previous query results
if (entity.inst == undefined)
entity.inst = {};
if (entity.inst.properties == undefined)
entity.inst.properties = {};
entity.inst.properties[a] = value;
}
//debug
//console.log('updated',entity.path,a,eres[a]);
}
todo[entity.path] = eres;
//debug
//console.log('tally',entity.path,eres)
};
});
var fs = require('fs');
// our recipe file, to parse. Recipes are of the form
// selector, selector { instruction, instruction }
// and each selector can comprise of #id$class[property] declarations
var fi = process.argv[2];
fs.readFile(fi, 'utf8', function(err, data) {
if (err) throw err;
//console.log(data);
// break out the selectors from the instructions...
var exprs = splitMulti(data,['{','}']);
//console.log('exprs',exprs);
var p = new RcpSelector();
for (var i=0; i<exprs.length; i+=4) {
if (exprs[i].trim().length > 0) {
// clean up the selector declarations
var expr = exprs[i].trim().replace(/ +/g,' ');
// split by comma (effectively an OR)
var elist = expr.split(',');
//console.log('evaluating',expr,elist);
// parse the selector and associate the replacesulting actions
p.Parse(elist, select(parseActions(exprs[i+2])));
}
}
selectData(p);
});
function parseActions(fi) {
var alist = fi.trim().replace(/\r\n/g,'').replace(/ +/g,' ').split(',');
var actions = {};
// split action=value as it will be the value that gets scored
// e.g. i might have color=red and color=blue and blue will win
alist.forEach(function(a) {
var expr = a.split('=');
var lhs = expr[0].trim();
var rhs = expr[1] != undefined ? expr[1].trim() : undefined;
if (lhs != undefined && lhs.length > 0)
actions[expr[0].trim()] = { score:0, value:rhs };
});
return actions;
}
function selectData(p) {
var fi = process.argv[3];
var pvs = JSON.parse(fs.readFileSync(fi,'utf8'));
// includes accessor functions for navigating structure
class node {
constructor(n,e) {
this.comp = pvs.components[n.cid];
if (n.par) {
this.par = pvs.components[n.par];
this.inst = this.par.children[n.idx];
}
this.ptr = n;
this.path = e;
this.clss = (this.comp != undefined && this.comp.shape != undefined) ? 'part' : 'asm';
}
attr(a) { return this.comp[a]; }
property(p) {
if (this.inst != undefined &&
this.inst.properties != undefined &&
this.inst.properties[p] != undefined) {
return this.inst.properties[p];
}
else
return this.comp.properties[p];
}
//we'll have the #id return the (unique) instance path
get id() { return this.path; }
//$class is a single string at present; we could extend this to be more
//like html and support multiple values, but not in this first example
get class() { return this.clss; }
get parent() {
if (this.path === undefined || this.path === '/')
return undefined; // this is the root
var c = this.path.substring(0,this.path.lastIndexOf('/'));
if (c === undefined || c.length === 0)
c = '/';
return new node(pvs.idmap[c],c);
}
get peers() {
//
if (this.par != undefined) {
// walk up to parent
var c = this.path.substring(0,this.path.lastIndexOf('/'));
if (c === undefined || c.length === 0)
c = '/';
var peers = [];
var par = this.par;
var path = this.path;
//console.log('collecting peers for '+this.path);
var parnode = this.par;
if (parnode.children != undefined) parnode.children.forEach(function(kid) {
var np = c.endsWith('/') ? c + kid.vid : c + '/' + kid.vid;
if (np != path) {
var pp = new node(pvs.idmap[np],np);
//console.log('adding peer '+np);
peers.push(pp);
}
});
// we could probably cache this against the parent i.e. peers === parent.children
return peers;
}
return undefined;
}
}
// lets record how long this takes (ms)
var t0 = new Date().getTime();
// we iterate over every item
for (c in pvs.idmap) {
p.eval(new node(pvs.idmap[c],c));
};
// stop the clock...
var t1 = new Date().getTime() - t0;
// write out the results
//console.log(todo);
writeAsXml(todo);
// display some stats e.g. count how many items in the results list
var tdl=0;for(var i in todo) { tdl+=1; };
//console.log('('+tdl+') in '+t1+'ms');
}
function writeAsXml(list) {
//console.log(list);
//recursively write nodes
// first, we need to build tree structure
var xml = { id:'' };
var map = {};
for(a in list) {
//we build a node structure from the path
path = a;
var lidx = path.lastIndexOf('/');
var id = path.slice(lidx+1);
var depth = path.split('/').length - 1;
//console.log('leaf',path);
map[path] = {id:id, depth:depth, children:{}, properties:list[a]}; // store the properties
//console.log(path,map[path])
var kid = map[path];
while (path.length > 1) {
path = path.slice(0,lidx);
depth = path.split('/').length -1;
lidx = path.lastIndexOf('/');
id = path.slice(lidx+1);
if (path.length==0) { path='/'; id='' }
//console.log('parent',path,id);
if (map[path] == undefined)
map[path] = { id:id, depth:depth, children:{}};
if (map[path].children[kid.id] == undefined)
map[path].children[kid.id] = kid;
//console.log('map',map[path]);
kid = map[path];
}
};
//debug
//console.log(map);
// and traverse from the root ("/")
function writeNode(node,indent) {
var space=' ';
console.log(space.repeat(indent),'<comp' + (node.id.length > 0 ? (' id="'+node.id+'">'):'>'));
if (node.properties!=undefined) {
//console.log(node.properties);
//new syntax here : we can have property=value, or property:group=value
//for the latter, the groupname is used to create the the propertygroup, otherwise we use a default
//first of all, work out the groups we need
var groups = {};
var gname;
//console.log(node.properties);
for(prop in node.properties) {
var grp = prop.split(':');
if (grp.length==1) gname = 'testgroup';
else gname = grp[1];
if (groups[gname] == undefined)
groups[gname]=[];
groups[gname].push(prop);
}
//console.log('groups',groups);
// now iterate the group loops
for(group in groups) {
console.log(space.repeat(indent+2),'<propertygroup name="'+group+'">');
props = groups[group];
for(pidx in props) {
var prop = props[pidx];
var propgrp = prop.split(':');
var pp = node.properties[prop];
if (pp.value != undefined) {
var value = pp.value.replace(/__+/g,' ');
if (value != 'ignore') console.log(space.repeat(indent+4),'<property name="'+propgrp[0]+'" value="'+value+'" />')
}
}
console.log(space.repeat(indent+2),'</propertygroup>');
}
/*
console.log(space.repeat(indent+2),'<propertygroup name="testgroup">');
for(prop in node.properties) {
// replace __ with space
var value = node.properties[prop].value.replace(/__+/g,' ');
if (value != 'ignore') console.log(space.repeat(indent+4),'<property name="'+prop+'" value="'+value+'" />')
}
console.log(space.repeat(indent+2),'</propertygroup>');
*/
}
for(kids in node.children) writeNode(node.children[kids], indent+2);
console.log(space.repeat(indent),'</comp>');
}
var root = map['/'];
console.log('<?xml version="1.0" encoding="UTF-8"?>');
console.log('<pvs_properties>');
writeNode(root,0);
console.log('</pvs_properties>');
}