Execute script in component

Directly trigger adaptations

This component will send each received messages to the core ModelService as KevScript.
This will have the effect of modifying the abstraction layer and so the running system.

1
2
3
4
5
6
7
8
9
10
11
12
@ComponentType(version = 1, description = "Update the current system using input messages as KevScript")
public class KevScriptServiceComp {

  @KevoreeInject
  private ModelService modelService;

  @Input
  public void in(String script) {
    // this will update the abstraction layer
    this.modelService.submitScript(script);
  }
}

Local execution

This component will execute each received messages as KevScript using an empty model as context.
Once executed, it will serialize the new model and send it to its output port named json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@ComponentType(version = 1, description = "Transform incoming KevScript messages to JSON model")
public class KevScript2ModelComp {

  @KevoreeInject
  private KevScriptService kevs;

  @Output
  private Port json;

  private KevoreeFactory factory = new DefaultKevoreeFactory();
  private JSONSerializer serializer = factory.createJSONSerializer();

  @Input
  public void kevs(String script) {
    // create an empty model
    final ContainerRoot model = factory.createContainerRoot();
    factory.root(model);
    // execute the incoming script to update the empty model
    this.kevs.execute(script, model);
    // serialize the model to JSON and send it through the output port
    this.json.send(this.serializer.serialize(model));
  }
}

Directly trigger adaptations

This component will send each received messages to the core ModelService as KevScript.
This will have the effect of modifying the abstraction layer and so the running system.

1
2
3
4
5
6
7
8
9
10
11
const AbstractComponent = require('kevoree-entities/lib/AbstractComponent');

module.exports = AbstractComponent.extend({
  toString: 'KevScriptServiceComp',
  tdef_version: 1,

  in_in(script) {
    // this will update the abstraction layer
    this.kCore.submitScript(script);
  }
});

Local execution

This component will execute each received messages as KevScript using an empty model as context.
Once executed, it will serialize the new model and send it to its output port named json

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
const AbstractComponent = require('kevoree-entities/lib/AbstractComponent');
const kevoree = require('kevoree-library');

module.exports = AbstractComponent.extend({
  toString: 'KevScript2ModelComp',
  tdef_version: 1,

  construct() {
    const factory = new kevoree.factory.DefaultKevoreeFactory();
    this.serializer = factory.createJSONSerializer();
  },

  in_kevs(script) {
    // execute the incoming script on an empty model (default behavior in js)
    this.kCore.kevs.parse(script, (err, model) => {
      if (err) {
        console.error('Something went wrong', err.message);
      } else {
        // serialize the model to JSON and send it through the output port
        this.out_json(this.serializer.serialize(model));
      }
    });
  },

  out_json() {/* noop as this function will be modified by the runtime */}
});