Source: chemistry/KnowledgeBase.js

/*
 KnowledgeBase methods:
    getSpecies(id)
*/
/**
 * @module
 */
define([
    'underscore',
    './Species',
    './ReactionKinetics'
], function(_, Species, Reaction) {
    var __hasProp = {}.hasOwnProperty;
    /**
     * @class KnowledgeBase
     * @param {*} speciesIn 
     * @param {*} reactionsIn 
     * @param {*} spectraIn 
     * @param {*} waterFinite 
     */
    function KnowledgeBase(speciesIn, reactionsIn, spectraIn, waterFinite) {
        var coefs, id, ids, key, parseInt10, reac, reacNew, reacSpecies, sid, sp, sp1, speciesNew, spnew, t1, t2, value, vfloat, _i, _j, _len, _len1, spectrum, band;
        this.species = [];
        this.waterFinite = waterFinite;
        for (sid in speciesIn) {
            sp = speciesIn[sid];
            spnew = {
                id: parseInt(sid, 10)
            };
            for (key in sp) {
                if (!__hasProp.call(sp, key)) continue;
                value = sp[key];
                vfloat = parseFloat(value);
                if (isNaN(vfloat)) {
                    spnew[key] = value;
                } else {
                    spnew[key] = vfloat;
                }
            }
            speciesNew = new Species(spnew, waterFinite);
            this.species.push(speciesNew);
        }
        this.reactions = [];
        for (_i = 0, _len = reactionsIn.length; _i < _len; _i++) {
            // Keep everything there - including SPECIES_REF, and KINETICS...
            const reactionData = reactionsIn[_i];
            reac = reactionData.SPECIES_REF; 
            // Add another thing here that is the SPECIES_REF part - check for "KINETICS as well"
            parseInt10 = function(x) {
                return parseInt(x, 10);
            };
            t1 = _.pluck(reac, 'id');
            t2 = t1.map(parseInt10);
            ids = _.pluck(reac, 'id').map(parseInt10);
            coefs = _.pluck(reac, 'coefficient').map(parseInt10);
            reacSpecies = [];
            for (_j = 0, _len1 = ids.length; _j < _len1; _j++) {
                id = ids[_j];
                sp1 = _.find(this.species, function(x) {
                    return x.id === id;
                });
                reacSpecies.push(sp1);
            }

            if (reactionData.hasOwnProperty("KINETICS")) {
                // If kinetics information is here, pass it to the reaction...
                const kinetics = reactionData.KINETICS;
                const orders = reac.map(x => {
                if (x.hasOwnProperty("order")) {
                    return parseFloat(x.order);
                } else {
                    // Default order of a reactant / product is 0 (no effect on rate)
                    return 0;
                }
                });
                reacNew = new Reaction(reacSpecies, coefs, kinetics, orders);
            } else {
                reacNew = new Reaction(reacSpecies, coefs);
            }
            this.reactions.push(reacNew);
        }
        for (_i = 0, _len = spectraIn.length; _i < _len; _i++) {
            spectrum = spectraIn[_i];
            sp = this.getSpecies(parseInt(spectrum.id));
            for (_j = 0, _len1 = spectrum.BAND.length; _j < _len1; _j++) {
                band = spectrum.BAND[_j];
                sp.addBand(band.wavelength, band.width, band.e);
            }
        }
    }
    KnowledgeBase.prototype.getSpecies = function(sid) {
        return _.find(this.species, function(x) {
            return x.id === sid;
        });
    };
    KnowledgeBase.prototype.getReactionsContaining = function(sp) {
        var speciesId, test1, test2;
        speciesId = sp.id;
        test1 = _.pluck(this.reactions[0].species, 'id');
        test2 = _.contains(test1, speciesId);
        return _.filter(this.reactions, function(reac) {
            return _.contains(_.pluck(reac.species, 'id'), speciesId);
        });
    };
    return KnowledgeBase;
});