Skip to content

Commit b0751ec

Browse files
committed
Add Mapping model
1 parent 336ff38 commit b0751ec

File tree

15 files changed

+460
-354
lines changed

15 files changed

+460
-354
lines changed

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
v0.8:
2+
- Add a JSMF Mapping model, to log transformation
13
v0.7:
24
- ES6 migration
35
v0.6:

examples/ABExample/TransformationDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var JSTL = require('../../index'); var Transformation= JSTL.Transformation;
1+
var JSTL = require('../../src/index'); var Transformation= JSTL.Transformation;
22
var JSMF = require('jsmf-core'); var Model = JSMF.Model; var Class = JSMF.Class;
33

44
//Load the metamodels (in one file for the example)

examples/ArduinoML/ArduinoToCode.js

Lines changed: 84 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Authors : Nicolas Biri
1010
'use strict'
1111

1212
// model imports
13-
const JSTL = require('../../index')
13+
const JSTL = require('../../src/index')
1414
const Transformation = JSTL.Transformation
1515
const NAV = require('jsmf-magellan')
1616
const Model = require('jsmf-core').Model
@@ -24,99 +24,111 @@ const MMI = require('./MMArduinoML')
2424
const MMO = require('./MMAbstractCode')
2525

2626
// input file
27-
const input = require('./MArduinoML').switchExample
27+
const input = require('./MArduinoML').Switch
2828
const output = new Model('Out')
2929

30-
const transfo = new Transformation()
31-
32-
transfo.addRule({
33-
in: x => NAV.allInstancesFromModel(MMI.App, x),
34-
out: function(i) {
35-
var app = MMO.App.newInstance()
36-
this.assign(app, 'structural', [i])
37-
this.assign(app, 'behavioural', [i])
38-
return [app]
39-
}
30+
const arduinoToCode = new Transformation()
31+
32+
arduinoToCode.addRule({
33+
name: 'Arduino App to Code App',
34+
in: x => NAV.allInstancesFromModel(MMI.App, x),
35+
out: function(i) {
36+
const app = MMO.App.newInstance()
37+
this.assign(app, 'structural', [i])
38+
this.assign(app, 'behavioural', [i])
39+
return [app]
40+
}
4041
})
4142

42-
transfo.addRule({
43-
in: x => NAV.allInstancesFromModel(MMI.App, x),
44-
out: function(i) {
45-
var s = MMO.StructuralConcerns.newInstance()
46-
this.assign(s, 'alias', i.brick)
47-
this.assign(s, 'pinMode', i.brick)
48-
return [s]
49-
}
43+
arduinoToCode.addRule({
44+
name: 'Structural concerns generation',
45+
in: x => NAV.allInstancesFromModel(MMI.App, x),
46+
out: function(i) {
47+
const s = MMO.StructuralConcerns.newInstance()
48+
this.assign(s, 'alias', i.brick)
49+
this.assign(s, 'pinMode', i.brick)
50+
return [s]
51+
}
5052
})
5153

52-
transfo.addRule({
53-
in: x => NAV.allInstancesFromModel(MMI.Brick, x),
54-
out: function(i) {
55-
return [MMO.BrickAlias.newInstance({name: i.name, pin: i.pin})]
56-
}
54+
arduinoToCode.addRule({
55+
name: 'Brick Alias generation',
56+
in: x => NAV.allInstancesFromModel(MMI.Brick, x),
57+
out: function(i) {
58+
return [MMO.BrickAlias.newInstance({name: i.name, pin: i.pin})]
59+
}
5760
})
5861

59-
transfo.addRule({
60-
in: x => NAV.allInstancesFromModel(MMI.Sensor, x),
61-
out: function(i) {
62-
return [MMO.PinMode.newInstance({name: i.name, mode: MMO.IO.INPUT})]
63-
}
62+
arduinoToCode.addRule({
63+
name: 'Pin mode definition for sensors',
64+
in: x => NAV.allInstancesFromModel(MMI.Sensor, x),
65+
out: function(i) {
66+
return [MMO.PinMode.newInstance({name: i.name, mode: MMO.IO.INPUT})]
67+
}
6468
})
6569

66-
transfo.addRule({
67-
in: x => NAV.allInstancesFromModel(MMI.Actuator, x),
68-
out: function(i) {
69-
return [MMO.PinMode.newInstance({name: i.name, mode: MMO.IO.OUTPUT})]
70-
}
70+
arduinoToCode.addRule({
71+
name: 'Pin mode definition for actuators',
72+
in: x => NAV.allInstancesFromModel(MMI.Actuator, x),
73+
out: function(i) {
74+
return [MMO.PinMode.newInstance({name: i.name, mode: MMO.IO.OUTPUT})]
75+
}
7176
})
7277

73-
transfo.addRule({
74-
in: x => NAV.allInstancesFromModel(MMI.App, x),
75-
out: function(i) {
76-
var b = MMO.BehaviouralConcerns.newInstance()
77-
b.timeConfig = MMO.TimeConfig.newInstance({initialTime: 0, debounce: 200})
78-
this.assign(b, 'stateFunction', i.state)
79-
this.assign(b, 'mainLoop', i.initial)
80-
return [b]
81-
}
78+
arduinoToCode.addRule({
79+
name: 'Behavioural concerns generation',
80+
in: x => NAV.allInstancesFromModel(MMI.App, x),
81+
out: function(i) {
82+
const b = MMO.BehaviouralConcerns.newInstance()
83+
b.timeConfig = MMO.TimeConfig.newInstance({initialTime: 0, debounce: 200})
84+
this.assign(b, 'stateFunction', i.state)
85+
this.assign(b, 'mainLoop', i.initial)
86+
return [b]
87+
}
8288
})
8389

84-
transfo.addRule({
85-
in: x => NAV.allInstancesFromModel(MMI.State, x),
86-
out: function(i) {
87-
var t = i.transition[0]
88-
var s = MMO.StateFunction.newInstance({
89-
name: i.name,
90-
next: t.next[0].name,
91-
readOn: t.sensor[0].name,
92-
read: t.value
93-
})
94-
this.assign(s, 'write', i.action)
95-
return [s]
96-
}
90+
arduinoToCode.addRule({
91+
name: 'Generate state function',
92+
in: x => NAV.allInstancesFromModel(MMI.State, x),
93+
out: function(i) {
94+
const t = i.transition[0]
95+
const s = MMO.StateFunction.newInstance({
96+
name: i.name,
97+
next: t.next[0].name,
98+
readOn: t.sensor[0].name,
99+
read: t.value
100+
})
101+
this.assign(s, 'write', i.action)
102+
return [s]
103+
}
97104
})
98105

99-
transfo.addRule({
100-
in: x => NAV.allInstancesFromModel(MMI.State, x),
101-
out: function(i) {
102-
return [MMO.MainLoop.newInstance({ init: i.name })]
103-
}
106+
arduinoToCode.addRule({
107+
name: 'Generate main loop',
108+
in: x => NAV.allInstancesFromModel(MMI.State, x),
109+
out: function(i) {
110+
return [MMO.MainLoop.newInstance({ init: i.name })]
111+
}
104112
})
105113

106114

107-
transfo.addRule({
108-
in: x => NAV.allInstancesFromModel(MMI.Action, x),
109-
out: function(i) {
110-
return [MMO.Write.newInstance({
111-
on: i.actuator[0].name,
112-
value: i.value
113-
})]
114-
}
115+
arduinoToCode.addRule({
116+
name: 'Ganarate Writes',
117+
in: x => NAV.allInstancesFromModel(MMI.Action, x),
118+
out: function(i) {
119+
return [MMO.Write.newInstance({
120+
on: i.actuator[0].name,
121+
value: i.value
122+
})]
123+
}
115124
})
116125

117126

118127
// launch transformation
119128

120-
const log = transfo.apply(input, output, false)
129+
const mapping = arduinoToCode.apply(input, output, true)
130+
131+
module.exports = {arduinoToCode, mapping}
121132

122-
_.forEach(NAV.allInstancesFromModel(MMO.App, output), x => console.log(x.toCode()))
133+
_.forEach( NAV.allInstancesFromModel(MMO.App, output)
134+
, x => console.log(x.toCode()))

examples/ArduinoML/MArduinoML.js

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,55 @@
11
'use strict';
2-
var AML = require('./MMArduinoML')
32

4-
var Model;
3+
const AML = require('./MMArduinoML.js')
4+
const Model = require('jsmf-core').Model
55

6-
(function() {
7-
var JSMF = require('jsmf-core');
8-
Model = JSMF.Model;
9-
}).call();
106

11-
var switchExample = new Model('Switch!');
12-
switchExample.setReferenceModel(AML.ArduinoML);
13-
14-
var button = AML.Sensor.newInstance({name: 'button', pin: 9});
15-
switchExample.add(button);
16-
var led = AML.Actuator.newInstance({name: 'led', pin: 13});
17-
switchExample.add(led);
7+
const button = AML.Sensor.newInstance({name: 'button', pin: 9})
8+
const led = AML.Actuator.newInstance({name: 'led', pin: 12})
189

1910
/*
2011
* on state
2112
*/
2213

23-
var aOn = AML.Action.newInstance({value: AML.Signal.HIGH, actuator: led});
24-
switchExample.add(aOn);
25-
var tOn = AML.Transition.newInstance({value: AML.Signal.LOW, sensor: button});
26-
switchExample.add(tOn);
27-
var on = AML.State.newInstance({name: 'on', action: aOn, transition: tOn})
28-
switchExample.add(on);
14+
const aOn = AML.Action.newInstance({value: AML.Signal.HIGH, actuator: led})
15+
const tOn = AML.Transition.newInstance({value: AML.Signal.HIGH, sensor: button})
16+
17+
const on = AML.State.newInstance({name: 'on'})
18+
on.action = aOn
19+
on.transition = tOn
2920

3021
/*
3122
* off state
3223
*/
3324

34-
var aOff = AML.Action.newInstance({value: AML.Signal.LOW, actuator: led});
35-
switchExample.add(aOff);
36-
var tOff = AML.Transition.newInstance({value: AML.Signal.LOW, sensor: button});
37-
switchExample.add(tOff);
38-
var off = AML.State.newInstance({name: 'off', action: aOff, transition: tOff})
39-
switchExample.add(off);
25+
const aOff = AML.Action.newInstance({value: AML.Signal.LOW, actuator: led})
26+
const tOff = AML.Transition.newInstance({value: AML.Signal.HIGH, sensor: button})
27+
const off = AML.State.newInstance({name: 'off'})
28+
off.action = aOff
29+
off.transition = tOff
4030

4131

4232
/*
4333
* set transitions
4434
*/
45-
tOn.next = off;
46-
tOff.next = on;
35+
tOn.next = off
36+
tOff.next = on
4737

4838

4939
/*
5040
* define app
5141
*/
5242

53-
var switchApp = AML.App.newInstance({
54-
name: 'Switch!',
55-
brick: [button, led],
56-
state: [on, off],
57-
initial: off
58-
});
59-
switchExample.add(switchApp);
43+
const switchApp = AML.App.newInstance({
44+
name: 'Switch!',
45+
bricks: [button, led],
46+
states: [on, off],
47+
initial: off
48+
})
49+
50+
const Switch = new Model('Switch', AML.ArduinoML, switchApp, true)
6051

6152
module.exports = {
62-
switchExample: switchExample,
63-
switchApp: switchApp
53+
Switch: Switch,
54+
switchApp: switchApp
6455
}

0 commit comments

Comments
 (0)