<commit_before>/**
 * Created by <NAME> on 4/8/2014.
 */

define(['plugin/PluginConfig',
    'plugin/PluginBase',
    'plugin/PluginResult',
    'json2xml'], function (PluginConfig, PluginBase, PluginResult, Json2Xml) {

    'use strict';

    var LogicGatesExporterPlugin = function () {
        PluginBase.call(this);

        this.objectToVisit = 1; // number of objects that have to be visited, visiting the selected node makes it start with 1
        this.diagramPath = "";
        this.modelID = 0;
        this.wiresToAdd = [];
        this.gatesToAdd = [];
        this.circuits = [];
        this.components = {};
        this.idLUT = {};
        this.childrenLUT = {};

        this.GATE_TYPES = {
            "Not": true,
            "Buffer": true,
            "And": true,
            "Or": true,
            "Nand": true,
            "Nor": true,
            "Xor": true,
            "Xnor": true,
            "NumericInput": true,
            "NumericOutput": true,
            "UserOutput": true,
            "UserInput": true,
            "Clock": true
        };

        this.COMPLEX = {
            "And": true,
            "Or": true,
            "Nand": true,
            "Nor": true,
            "Xor": true,
            "Xnor": true
        };

        this.WIRE_TYPES = {
            "OutputPort2InputPort": true,
            "UserInput2InputPort": true,
            "OutputPort2UserOutput": true,
            "UserInputBase2UserOutput": true,
            "PortBase2UserIOBase": true,
            "UserIOBase2PortBase": true,
            "UserIOBase2UserIOBase": true
        };

        this.outputFiles = {};

        // debugging use
        this.gates = [];
        this.wires = [];
    };

    LogicGatesExporterPlugin.prototype = Object.create(PluginBase.prototype);
    LogicGatesExporterPlugin.prototype.constructor = LogicGatesExporterPlugin;

    LogicGatesExporterPlugin.prototype.getName = function () {
        return 'LogicGatesExporter';
    };

    LogicGatesExporterPlugin.prototype.main = function (callback) {
        var self = this,
            core = self.core,
            selectedNode = self.activeNode;

        if (!selectedNode) {
            self.result.setSuccess(false);
            callback('selectedNode is not defined', pluginResult);
            return;
        }

        core.loadChildren(selectedNode, function (err, childNodes) {
            self.visitObject(err, childNodes, core, callback);
        });
    };

    LogicGatesExporterPlugin.prototype.visitObject = function (err, childNodes, core, callback) {
        var self = this,
            i,
            child,
            gmeID,
            parentPath,
            baseNode,
            metaType, // get child's base META Type
            parentClass,
            parentMeta,
            isComplex,
            isGate,
            isWire,
            pluginResult,
            artifact;

        self.objectToVisit += childNodes.length; // all child objects have to be visited
        for (i = 0; i < childNodes.length; i += 1) {

            child = childNodes[i];
            gmeID = core.getPath(child);
            parentPath = core.getPath(core.getParent(child));
            // baseClass = core.getBase(child);
            //metaType = baseClass ? core.getAttribute(baseClass, 'name') : ""; // get child's base META Type
            baseNode = self.getMetaType(child);
            metaType = baseNode ? core.getAttribute(baseNode, 'name') : "";
            parentClass = core.getBase(core.getParent(child));
//            parentClass = self.getMetaType(core.getParent(child));
            parentMeta = parentClass ? core.getAttribute(parentClass, 'name') : "";
            isComplex = self.COMPLEX[metaType];
            isGate = self.GATE_TYPES[metaType] && parentMeta === "LogicCircuit";
            isWire = self.WIRE_TYPES[metaType] && parentMeta === "LogicCircuit";

            if (isGate) {

                // if key not exist already, add key; otherwise ignore
                if (!self.idLUT.hasOwnProperty(gmeID)) {
                    // PC: addGate needs to be defined and called as an asynchronous function. see PC10
                    self.addGate(child, metaType, isComplex, parentPath, function (err) {console.log("we just added a gate from visitObject() and " + err)});
                }

            } else if (isWire) {

                self.wiresToAdd.push(child);

            } else if (metaType === 'InputPort') {

                if (!self.childrenLUT.hasOwnProperty(parentPath)) {

                    self.childrenLUT[parentPath] = [];
                }
                self.childrenLUT[parentPath].push(gmeID);
            }

            core.loadChildren(childNodes[i], function (err, childNodes) {
                self.visitObject(err, childNodes, core, callback);
            });
        }

        self.objectToVisit -= 1; // another object was just visited

        if (self.objectToVisit === 0) {
            // all objects have been visited
            // had to do it this way because we need to wait for all the gates to be added before adding the wires to avoid problems async causes
            for (i = 0; i < self.wiresToAdd.length; i += 1) {
                self.addWire(self.wiresToAdd[i], function (err) {console.log("We are adding a wire")});
            }
            self.createObjectFromDiagram();
            artifact = self.blobClient.createArtifact('logicGateExporterOutput');
            artifact.addFiles(self.outputFiles, function (err, hashes) {
                // TODO: error handling
                if (err) {
                    callback(null, null);
                    return;
                }
                self.blobClient.saveAllArtifacts(function (err, hashes) {
                    self.result.addArtifact(hashes[0]);
                    callback(null, pluginResult);
                });
            });
        }
    };

    LogicGatesExporterPlugin.prototype.addWire = function (nodeObj, callback) {
        var self = this,
            core = self.core,
            src = core.getPointerPath(nodeObj, "src"),
            dst = core.getPointerPath(nodeObj, "dst"),
            srcID,
            dstID,
            srcPort = 0,
            dstPort = 0,<commit_msg>GH-5 Saving artifacts generated from plugin<commit_after>/**
 * Created by <NAME> on 4/8/2014.
 */

define(['plugin/PluginConfig',
    'plugin/PluginBase',
    'json2xml'], function (PluginConfig, PluginBase, Json2Xml) {

    'use strict';

    var LogicGatesExporterPlugin = function () {
        PluginBase.call(this);

        this.objectToVisit = 1; // number of objects that have to be visited, visiting the selected node makes it start with 1
        this.diagramPath = "";
        this.modelID = 0;
        this.wiresToAdd = [];
        this.circuits = [];
        this.components = {};
        this.idLUT = {};
        this.childrenLUT = {};

        this.GATE_TYPES = {
            "Not": true,
            "Buffer": true,
            "And": true,
            "Or": true,
            "Nand": true,
            "Nor": true,
            "Xor": true,
            "Xnor": true,
            "NumericInput": true,
            "NumericOutput": true,
            "UserOutput": true,
            "UserInput": true,
            "Clock": true
        };

        this.COMPLEX = {
            "And": true,
            "Or": true,
            "Nand": true,
            "Nor": true,
            "Xor": true,
            "Xnor": true
        };

        this.WIRE_TYPES = {
            "OutputPort2InputPort": true,
            "UserInput2InputPort": true,
            "OutputPort2UserOutput": true,
            "UserInputBase2UserOutput": true,
            "PortBase2UserIOBase": true,
            "UserIOBase2PortBase": true,
            "UserIOBase2UserIOBase": true
        };

        this.outputFiles = {};

        // debugging use
        this.gates = [];
    };

    LogicGatesExporterPlugin.prototype = Object.create(PluginBase.prototype);
    LogicGatesExporterPlugin.prototype.constructor = LogicGatesExporterPlugin;

    LogicGatesExporterPlugin.prototype.getName = function () {
        return 'LogicGatesExporter';
    };

    LogicGatesExporterPlugin.prototype.main = function (callback) {
        var self = this,
            core = self.core,
            selectedNode = self.activeNode;

        if (!selectedNode) {
            self.result.setSuccess(false);
            callback('selectedNode is not defined', self.result);
            return;
        }

        core.loadChildren(selectedNode, function (err, childNodes) {
            self.visitObject(err, childNodes, core, callback);
        });
    };

    LogicGatesExporterPlugin.prototype.visitObject = function (err, childNodes, core, callback) {
        var self = this,
            i,
            child,
            gmeID,
            parentPath,
            baseNode,
            metaType, // get child's base META Type
            parentClass,
            parentMeta,
            isComplex,
            isGate,
            isWire,
            fileKeys,
            nbrOfFiles,
            artifact;

        self.objectToVisit += childNodes.length; // all child objects have to be visited
        for (i = 0; i < childNodes.length; i += 1) {

            child = childNodes[i];
            gmeID = core.getPath(child);
            parentPath = core.getPath(core.getParent(child));
            // baseClass = core.getBase(child);
            //metaType = baseClass ? core.getAttribute(baseClass, 'name') : ""; // get child's base META Type
            baseNode = self.getMetaType(child);
            metaType = baseNode ? core.getAttribute(baseNode, 'name') : "";
            parentClass = core.getBase(core.getParent(child));
//            parentClass = self.getMetaType(core.getParent(child));
            parentMeta = parentClass ? core.getAttribute(parentClass, 'name') : "";
            isComplex = self.COMPLEX[metaType];
            isGate = self.GATE_TYPES[metaType] && parentMeta === "LogicCircuit";
            isWire = self.WIRE_TYPES[metaType] && parentMeta === "LogicCircuit";

            if (isGate) {

                // if key not exist already, add key; otherwise ignore
                if (!self.idLUT.hasOwnProperty(gmeID)) {
                    // PC: addGate needs to be defined and called as an asynchronous function. see PC10
                    self.addGate(child, metaType, isComplex, parentPath, function (err) {console.log("we just added a gate from visitObject() and " + err)});
                }

            } else if (isWire) {

                self.wiresToAdd.push(child);

            } else if (metaType === 'InputPort') {

                if (!self.childrenLUT.hasOwnProperty(parentPath)) {

                    self.childrenLUT[parentPath] = [];
                }
                self.childrenLUT[parentPath].push(gmeID);
            }

            core.loadChildren(childNodes[i], function (err, childNodes) {
                self.visitObject(err, childNodes, core, callback);
            });
        }

        self.objectToVisit -= 1; // another object was just visited

        if (self.objectToVisit === 0) {
            // all objects have been visited
            // had to do it this way because we need to wait for all the gates to be added before adding the wires to avoid problems async causes
            for (i = 0; i < self.wiresToAdd.length; i += 1) {
                self.addWire(self.wiresToAdd[i], function (err) {console.log("We are adding a wire")});
            }

            self.createObjectFromDiagram();
            artifact = self.blobClient.createArtifact('LogicGatesExporterOutput');
            fileKeys = Object.keys(self.outputFiles);

            for (i = 0; i < fileKeys.length; i += 1) {
                artifact.addFile(fileKeys[i], this.outputFiles[fileKeys[i]], function (err, hash) {
                    nbrOfFiles -= 1;
                    if (nbrOfFiles === 0) {
                        if (err) {
                            callback('Something went wrong when adding files: ' + err, self.result);
                            return;
                        }
                        self.blobClient.saveAllArtifacts(function (err, hashes) {
                            if (err) {
                                callback(err, self.result);
                                return;
                            }
                            self.result.addArtifact(hashes[0]);
                            self.logger.info('Artifacts are saved here: ' + hashes.toString());
                            self.result.setSuccess(true);
                            callback(null, self.result);
                        });
                    }
                });
            }
        }
    };

    LogicGatesExporterPlugin.prototype.addWire = function (nodeObj, callback) {
        var self = this,
            core = self.core,
            src = core.getPointerPath(nodeObj, "src"),
            dst = core.getPointerPath(nodeObj, "dst"),
            srcID,
            dstID,
            srcPort = 0,
            dstPort = 0,
{'fraction_non_alphanumeric': 0.11082356523062871, 'fraction_numerical': 0.004795250418632973, 'mean_word_length': 1.6121272365805168, 'pattern_counts': {'":': 52, '<': 10, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5831009', 'n_tokens_mistral': 3663, 'n_tokens_neox': 3482, 'n_words': 808}