How to use @accordproject/cicero-engine - 5 common examples

To help you get started, we’ve selected a few @accordproject/cicero-engine examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github accordproject / cicero / packages / cicero-server / app.js View on Github external
app.post('/trigger/:template/:data', async function (req, httpResponse, next) {
    console.log('Template: ' + req.params.template);
    console.log('Clause: ' + req.params.data);
    try {
        const template = await Template.fromDirectory(`${process.env.CICERO_DIR}/${req.params.template}`);
        const data = fs.readFileSync(`${process.env.CICERO_DIR}/${req.params.template}/${req.params.data}`);
        const clause = new Clause(template);
        if(req.params.data.endsWith('.json')) {
            clause.setData(JSON.parse(data.toString()));
        }
        else {
            clause.parse(data.toString());
        }
        const engine = new Engine();
        let result;
        if(Object.keys(req.body).length === 2 &&
           Object.prototype.hasOwnProperty.call(req.body,'request') &&
           Object.prototype.hasOwnProperty.call(req.body,'state')) {
            result = await engine.trigger(clause, req.body.request, req.body.state);
        } else {
            // Add empty state in input, remove it on output
            const state = { '$class' : 'org.accordproject.cicero.contract.AccordContractState', 'stateId' : 'ehlo' };
            result = await engine.trigger(clause, req.body, state);
            delete result.state;
        }
        httpResponse.send(result);
    }
    catch(err) {
        return next(err);
    }
github accordproject / cicero / packages / cicero-cli / lib / commands.js View on Github external
static invoke(templatePath, samplePath, clauseName, paramsPath, statePath, currentTime, options) {
        let clause;
        const sampleText = fs.readFileSync(samplePath, 'utf8');
        const paramsJson = JSON.parse(fs.readFileSync(paramsPath, 'utf8'));

        const engine = new Engine();
        return Commands.loadTemplate(templatePath, options)
            .then(async (template) => {
                // Initialize clause
                clause = new Clause(template);
                clause.parse(sampleText, currentTime);

                let stateJson;
                if(!fs.existsSync(statePath)) {
                    Logger.warn('A state file was not provided, initializing state. Try the --state flag or create a state.json in the root folder of your template.');
                    const initResult = await engine.init(clause, currentTime);
                    stateJson = initResult.state;
                } else {
                    stateJson = JSON.parse(fs.readFileSync(statePath, 'utf8'));
                }

                return engine.invoke(clause, clauseName, paramsJson, stateJson, currentTime);
github accordproject / cicero / packages / cicero-cli / lib / commands.js View on Github external
static trigger(templatePath, samplePath, requestsPath, statePath, currentTime, options) {
        let clause;
        const sampleText = fs.readFileSync(samplePath, 'utf8');
        let requestsJson = [];

        for (let i = 0; i < requestsPath.length; i++) {
            requestsJson.push(JSON.parse(fs.readFileSync(requestsPath[i], 'utf8')));
        }

        const engine = new Engine();
        return Commands.loadTemplate(templatePath, options)
            .then(async (template) => {
                // Initialize clause
                clause = new Clause(template);
                clause.parse(sampleText, currentTime);

                let stateJson;
                if(!fs.existsSync(statePath)) {
                    Logger.warn('A state file was not provided, initializing state. Try the --state flag or create a state.json in the root folder of your template.');
                    const initResult = await engine.init(clause, currentTime);
                    stateJson = initResult.state;
                } else {
                    stateJson = JSON.parse(fs.readFileSync(statePath, 'utf8'));
                }

                // First execution to get the initial response
github accordproject / cicero / packages / cicero-cli / lib / commands.js View on Github external
static initialize(templatePath, samplePath, currentTime, options) {
        let clause;
        const sampleText = fs.readFileSync(samplePath, 'utf8');

        const engine = new Engine();
        return Commands.loadTemplate(templatePath, options)
            .then((template) => {
                // Initialize clause
                clause = new Clause(template);
                clause.parse(sampleText, currentTime);

                return engine.init(clause, currentTime);
            })
            .catch((err) => {
                Logger.error(err.message);
            });
    }
github accordproject / cicero / packages / cicero-test / lib / steps.js View on Github external
Before(function () {
    setDefaultTimeout(40 * 1000);

    this.engine = new Engine();
    this.currentTime = '1970-01-01T00:00:00Z';
    this.state = null;
    this.clause = null;
    this.request = null;
    this.answer = null;
});

@accordproject/cicero-engine

Cicero Engine - Node.js VM based implementation of Accord Protcol Template Specification execution

Apache-2.0
Latest version published 2 years ago

Package Health Score

51 / 100
Full package analysis

Popular @accordproject/cicero-engine functions