<commit_before>/*globals define*/
/*jshint node:true, browser:true*/

/**
 * Generated by PluginGenerator 0.14.0 from webgme on Mon Apr 04 2016 15:12:25 GMT-0700 (PDT).
 */

define([
    'plugin/PluginConfig',
    'text!./metadata.json',
    'plugin/PluginBase',
    'gridlabd/meta',
    'q',
    './cola.min'
], function (
    PluginConfig,
    pluginMetadata,
    PluginBase,
    MetaTypes,
    Q) {
    'use strict';

    pluginMetadata = JSON.parse(pluginMetadata);

    /**
     * Initializes a new instance of ImportGLM.
     * @class
     * @augments {PluginBase}
     * @classdesc This class represents the plugin ImportGLM.
     * @constructor
     */
    var ImportGLM = function () {
        // Call base class' constructor.
        PluginBase.call(this);
        this.pluginMetadata = pluginMetadata;
        this.metaTypes = MetaTypes;
    };

    /**
     * Metadata associated with the plugin. Contains id, name, version, description, icon, configStructue etc.
     * This is also available at the instance at this.pluginMetadata.
     * @type {object}
     */
    ImportGLM.metadata = pluginMetadata;

    // Prototypal inheritance from PluginBase.
    ImportGLM.prototype = Object.create(PluginBase.prototype);
    ImportGLM.prototype.constructor = ImportGLM;

    ImportGLM.prototype.notify = function(level, msg) {
	var self = this;
	self.logData += msg + '\n';
	if (!self.logDebug && level == 'debug')
	    return;
	var prefix = self.projectId + '::' + self.projectName + '::' + level + '::';
	if (level=='error')
	    self.logger.error(msg);
	else if (level=='debug')
	    self.logger.debug(msg);
	else if (level=='info')
	    self.logger.info(msg);
	else if (level=='warning')
	    self.logger.warn(msg);
	self.createMessage(self.activeNode, msg, level);
	self.sendNotification(prefix+msg);
    };

    /**
     * Main function for the plugin to execute. This will perform the execution.
     * Notes:
     * - Always log with the provided logger.[error,warning,info,debug].
     * - Do NOT put any user interaction logic UI, etc. inside this method.
     * - callback always has to be called even if error happened.
     *
     * @param {function(string, plugin.PluginResult)} callback - the result callback
     */
    ImportGLM.prototype.main = function (callback) {
        // Use self to access core, project, result, logger etc from PluginBase.
        // These are all instantiated at this point.
        var self = this,
        nodeObject;

	self.runningOnClient = false;
        if (typeof WebGMEGlobal !== 'undefined') {
	    self.runningOnClient = true;
        }

        self.updateMETA(self.metaTypes);

        // Default fails
        self.result.success = false;
	self.logData = '';

	// fill this out before creating the WebGME nodes
	self.newModel = {
	    base: 'Model',
	    children: [],
	    attributes: []
	};

	var currentConfig = self.getCurrentConfig(),
	glmFileHash = currentConfig.glmFile;
	self.logDebug = currentConfig.logDebug;
	self.iterations = currentConfig.iterations;
	self.linkDistance = currentConfig.linkDistance;
	self.nodeSize = currentConfig.nodeSize;

        // Using the coreAPI to make changes.
        nodeObject = self.activeNode;

	self.blobClient.getMetadata(glmFileHash)
	    .then(function(glmMetaData) {
		var splitName = glmMetaData.name.split(".");
		var newName = "";
		for (var i=0;i<splitName.length-1;i++) {
		    newName += splitName[i];
		}
		self.modelName = newName;
		self.newModel.name = newName;
		//self.logger.error('loaded model: ' + self.modelName);
	    })
	    .then(function() {
		return self.blobClient.getObjectAsString(glmFileHash)
	    })
	    .then(function(glmFile) {
		return self.parseObjectsFromGLM(glmFile);
	    })
	    .then(function() {
		return self.layoutObjects();
	    })
	    .then(function() {
		return self.createModelArtifacts();
	    })
	    .then(function() {
		// This will save the changes. If you don't want to save;
		// exclude self.save and call callback directly from this scope.
		return self.save('ImportGLM updated model.');
	    })
	    .then(function() {
		return self.blobClient.putFile(self.modelName+'.log', self.logData);
	    })
	    .then(function(hash) {
		self.result.addArtifact(hash);
	    })
	    .then(function() {
		self.result.setSuccess(true);
		callback(null, self.result);
	    })
	    .catch(function(err) {
		return self.blobClient.putFile(self.modelName+'.log', self.logData)
		    .then(function(hash) {
			self.result.addArtifact(hash);
			self.notify('error', err);
			self.result.setSuccess(false);
			callback(err, self.result);
		    });
	    });
    };

    ImportGLM.prototype.objectToKey = function(obj) {
	return obj.base +'_' + obj.type + '_' + obj.name;
    };

    ImportGLM.prototype.nameToKey = function(name, dict) {
	var self = this;
	self.notify('debug', 'Looking up key for '+name);
	var keys = Object.keys(dict);
	var suffix = '_' + name;
	var objKeys = keys.filter(function(val) {return val.substr(-suffix.length)===suffix;});
	var objKey = '';
	if (objKeys.length)
	    objKey = objKeys[0];
	self.notify('debug', 'Got key: '+objKey + ' for ' + name);
	return objKey;
    };

    ImportGLM.prototype.nameToObject = function(name, dict) {
	var self = this;
	var objKey = self.nameToKey(name, dict);
	var p = dict[objKey];
	if (!p) { // didn't find the name
	    // map from objName (e.g. node:412) to actual name (e.g. 412)
	    name = name.replace(/\w+:/g,'');
	    objKey = self.nameToKey(name, dict);
	    p = dict[objKey];
	}
	if (!p) {
	    self.notify('error', "Couldn't get object by name: " + name);
	}
	return p;
    };

    ImportGLM.prototype.parseObjectsFromGLM = function(glmFile) {
	// fill out self.newModel
	var self = this,<commit_msg>updated code to better distinguish names for lookup<commit_after>/*globals define*/
/*jshint node:true, browser:true*/

/**
 * Generated by PluginGenerator 0.14.0 from webgme on Mon Apr 04 2016 15:12:25 GMT-0700 (PDT).
 */

define([
    'plugin/PluginConfig',
    'text!./metadata.json',
    'plugin/PluginBase',
    'gridlabd/meta',
    'q',
    './cola.min'
], function (
    PluginConfig,
    pluginMetadata,
    PluginBase,
    MetaTypes,
    Q) {
    'use strict';

    pluginMetadata = JSON.parse(pluginMetadata);

    /**
     * Initializes a new instance of ImportGLM.
     * @class
     * @augments {PluginBase}
     * @classdesc This class represents the plugin ImportGLM.
     * @constructor
     */
    var ImportGLM = function () {
        // Call base class' constructor.
        PluginBase.call(this);
        this.pluginMetadata = pluginMetadata;
        this.metaTypes = MetaTypes;
    };

    /**
     * Metadata associated with the plugin. Contains id, name, version, description, icon, configStructue etc.
     * This is also available at the instance at this.pluginMetadata.
     * @type {object}
     */
    ImportGLM.metadata = pluginMetadata;

    // Prototypal inheritance from PluginBase.
    ImportGLM.prototype = Object.create(PluginBase.prototype);
    ImportGLM.prototype.constructor = ImportGLM;

    ImportGLM.prototype.notify = function(level, msg) {
	var self = this;
	self.logData += msg + '\n';
	if (!self.logDebug && level == 'debug')
	    return;
	var prefix = self.projectId + '::' + self.projectName + '::' + level + '::';
	if (level=='error')
	    self.logger.error(msg);
	else if (level=='debug')
	    self.logger.debug(msg);
	else if (level=='info')
	    self.logger.info(msg);
	else if (level=='warning')
	    self.logger.warn(msg);
	self.createMessage(self.activeNode, msg, level);
	self.sendNotification(prefix+msg);
    };

    /**
     * Main function for the plugin to execute. This will perform the execution.
     * Notes:
     * - Always log with the provided logger.[error,warning,info,debug].
     * - Do NOT put any user interaction logic UI, etc. inside this method.
     * - callback always has to be called even if error happened.
     *
     * @param {function(string, plugin.PluginResult)} callback - the result callback
     */
    ImportGLM.prototype.main = function (callback) {
        // Use self to access core, project, result, logger etc from PluginBase.
        // These are all instantiated at this point.
        var self = this,
        nodeObject;

	self.runningOnClient = false;
        if (typeof WebGMEGlobal !== 'undefined') {
	    self.runningOnClient = true;
        }

        self.updateMETA(self.metaTypes);

        // Default fails
        self.result.success = false;
	self.logData = '';

	// fill this out before creating the WebGME nodes
	self.newModel = {
	    base: 'Model',
	    children: [],
	    attributes: []
	};

	var currentConfig = self.getCurrentConfig(),
	glmFileHash = currentConfig.glmFile;
	self.logDebug = currentConfig.logDebug;
	self.iterations = currentConfig.iterations;
	self.linkDistance = currentConfig.linkDistance;
	self.nodeSize = currentConfig.nodeSize;

        // Using the coreAPI to make changes.
        nodeObject = self.activeNode;

	self.blobClient.getMetadata(glmFileHash)
	    .then(function(glmMetaData) {
		var splitName = glmMetaData.name.split(".");
		var newName = "";
		for (var i=0;i<splitName.length-1;i++) {
		    newName += splitName[i];
		}
		self.modelName = newName;
		self.newModel.name = newName;
		//self.logger.error('loaded model: ' + self.modelName);
	    })
	    .then(function() {
		return self.blobClient.getObjectAsString(glmFileHash)
	    })
	    .then(function(glmFile) {
		return self.parseObjectsFromGLM(glmFile);
	    })
	    .then(function() {
		return self.layoutObjects();
	    })
	    .then(function() {
		return self.createModelArtifacts();
	    })
	    .then(function() {
		// This will save the changes. If you don't want to save;
		// exclude self.save and call callback directly from this scope.
		return self.save('ImportGLM updated model.');
	    })
	    .then(function() {
		return self.blobClient.putFile(self.modelName+'.log', self.logData);
	    })
	    .then(function(hash) {
		self.result.addArtifact(hash);
	    })
	    .then(function() {
		self.result.setSuccess(true);
		callback(null, self.result);
	    })
	    .catch(function(err) {
		return self.blobClient.putFile(self.modelName+'.log', self.logData)
		    .then(function(hash) {
			self.result.addArtifact(hash);
			self.notify('error', err);
			self.result.setSuccess(false);
			callback(err, self.result);
		    });
	    });
    };

    var delimeter = '/';

    ImportGLM.prototype.objectNamePrefix = function(obj) {
	return obj.base + delimeter + obj.type + delimeter;
    };

    ImportGLM.prototype.objectToKey = function(obj) {
	return this.objectNamePrefix(obj) + obj.name;
    };

    ImportGLM.prototype.nameToKey = function(name, dict) {
	var self = this;
	self.notify('debug', 'Looking up key for '+name);
	var keys = Object.keys(dict);
	var suffix = delimeter + name;
	var objKeys = keys.filter(function(val) {return val.substr(-suffix.length)===suffix;});
	var objKey = '';
	if (objKeys.length)
	    objKey = objKeys[0];
	self.notify('debug', 'Got key: '+objKey + ' for ' + name);
	return objKey;
    };

    ImportGLM.prototype.nameToObject = function(name, dict) {
	var self = this;
	var objKey = self.nameToKey(name, dict);
	var p = dict[objKey];
	if (!p) { // didn't find the name
	    // map from objName (e.g. node:412) to actual name (e.g. node_412)
	    name = name.replace(/:/g,'/');
	    objKey = self.nameToKey(name, dict);
	    p = dict[objKey];
	}
	if (!p) {
	    self.notify('error', "Couldn't get object by name: " + name);
	}
	return p;
    };

    ImportGLM.prototype.parseObjectsFromGLM = function(glmFile) {
	// fill out self.newModel
	var self = this,
{'fraction_non_alphanumeric': 0.13903927128985133, 'fraction_numerical': 0.004984102431898256, 'mean_word_length': 2.941076870978666, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3727439', 'n_tokens_mistral': 3919, 'n_tokens_neox': 3671, 'n_words': 881}