Skip to content
Permalink
Browse files

Personality service added.

773 unit tests green.
  • Loading branch information...
Orbifold committed Jul 7, 2017
1 parent 2ba3991 commit 7ca9a09179dfbf0457af2cf97d9dc20521362002
@@ -26,7 +26,7 @@ class StorageBase extends ServiceBase {
* Returns whether the given collection exists.
* @param collectionName
*/
collectionExists(collectionName){
collectionExists(collectionName) {
throw new Error("This method should be but is not implemented by the storage.");
}

@@ -80,16 +80,28 @@ class StorageBase extends ServiceBase {

/**
* Return a convenience class calling this storage.
*
*
* @param {StorageDomainBase} domainStorage An instance of StorageDomainBase.
* @returns {StorageProxy} A proxy to the underlying storage.
*
*
* @memberOf StorageBase
*/
getProxy(domainStorage) {
return new StorageProxy(this, domainStorage);
}

/**
* Removes data satisfying the given criteria.
*
* @param {StorageDomainBase} domainStorage An instance of StorageDomainBase.
* @returns {StorageProxy} A proxy to the underlying storage.
*
* @memberOf StorageBase
*/
remove(specs = {}, collectionName) {
throw new Error("This method should be but is not implemented by the storage.");
}

/**
* Closes the connection to the underlying storage system.
*
@@ -275,7 +275,7 @@ const aliases = [
//-------------------------------------------------------------------
{
what: "add person * => add>person> *",
rex: /^\s?add\sperson(\s|\s?:|\s?>)?/gi,
rex: /^\s?add\sperson(\s|\s?:|\s?>)/gi,
with: "add>person>"
},
{
@@ -0,0 +1,124 @@
const utils = require('../../utils'),
path = require('path'),
_ = require('lodash');
const ServiceBase = require("../../Framework/ServiceBase");
const StorageDomainBase = require("../../Framework/StorageDomainBase");
/**
* Storage specific to the Personality service.
*
* @class PersonalityStorage
* @extends {StorageDomainBase}
*/
class PersonalityStorage extends StorageDomainBase {

init(instantiator) {
super.init(instantiator);
return this.createCollections(
{
collectionName: "Personality",
schema: {
userId: String,
personalities: [{
name: String,
value: Number
}]
}
});
}

/**
* Adds the given personality or increases the count of the personality.
* @param name {string} The name of the personality.
* @param userId {String} The user id.
* @return {Promise} Does not return anything.
*/
addPersonality(userId, name) {
const that = this;
name = name.toLowerCase();
return this.Personality.findOne({'userId': userId}).then(function(found) {
if(utils.isDefined(found)) {
const item = _.find(found.personalities, {name: name});
if(item) {
item.value = item.value + 1;
} else {
found.personalities.push({
name: name,
value: 1
});
}
that.Personality.upsert(found, {'userId': userId});
} else {
that.Personality.insert({
userId: userId,
personalities: [
{
name: name,
value: 1
}]
});
}
});
}

/**
* Returns the personality of the specified user.
* @param userId {String} The user id.
* @returns {Promise<T>}
*/
getPersonalitySpectrum(userId) {
return this.Personality.findOne({'userId': userId}).then(function(found) {
if(_.isNil(found)) {
return [];
} else {
return found.personalities;
}
});

}


/**
* Returns all the personality value of a specific name.
* @param userId {string} The user id.
* @param name {string} The name of the personality.
* @returns {Promise<number>}
*/
getPersonalityValue(userId, name) {
return this.getPersonalitySpectrum(userId).then(function(p) {
const found = _.find(p, {name: name.toLowerCase()});
if(_.isNil(found)) {
return 0;
} else {
return found.value;
}
});
}

/**
* Remove the personality of the specified user.
* @param userId
* @returns {Promise.<T>}
*/
clearAllUserPersonality(userId) {
return this.Personality.remove({userId: userId});
}

/**
* Removes the specified personality name.
* @param userId
* @param name
* @return {Promise}
*/
clearUserPersonality(userId, name) {
const that = this;
return this.Personality.findOne({'userId': userId}).then(function(found) {
if(utils.isDefined(found)) {
_.remove(found.personalities, function(x) {
return x.name.toLowerCase() === name.toLowerCase();
});
that.Personality.upsert(found, {'userId': userId});
}
});
}
}
module.exports = PersonalityStorage;
@@ -0,0 +1,55 @@
const utils = require('../../utils'),
path = require('path'),
_ = require('lodash');
const ServiceBase = require("../../Framework/ServiceBase");
const StorageDomainBase = require("../../Framework/StorageDomainBase");
const PersonalityStorage = require("./PersonalityStorage");
/**
* Manages the personalization of users and the engine.
*
* @class Personalization
* @extends {ServiceBase}
* @see {@tutorial Personalization}
*/
class Personality extends ServiceBase {
/**
* Creates an instance of Personality.
* @param {any} settings
*
* @memberOf Personality
*/
constructor(settings) {
super();
this.pluginName = "personality";
}

/**
* @inheritdoc
*/
init(instantiator) {
super.init(instantiator);
this.personality = new PersonalityStorage(this.storage);
return this.personality.init(instantiator);
}

addPersonality(name, ctx) {
return this.personality.addPersonality(ctx.userId, name);
}

getPersonalitySpectrum(ctx) {
return this.personality.getPersonalitySpectrum(ctx.userId);
}

getPersonalityValue(name, ctx) {
return this.personality.getPersonalityValue(ctx.userId, name);
}

clearAllUserPersonality(ctx) {
return this.personality.getPersonalityValue(ctx.userId);
}

clearUserPersonality(name, ctx) {
return this.personality.clearUserPersonality(ctx.userId, name);
}
}
module.exports = Personality;
@@ -8,16 +8,16 @@ const waitFor = require('asyncawait/await');
const PersonalizationStorage = require("./PersonalizationStorage");
/**
* Manages the personalization of users and the engine.
*
*
* @class Personalization
* @extends {ServiceBase}
* @see {@tutorial Personalization}
*/
class Personalization extends ServiceBase {
/**
* Creates an instance of Personalization.
* @param {any} settings
*
* @param {any} settings
*
* @memberOf Personalization
*/
constructor(settings) {
@@ -71,6 +71,7 @@ class Personalization extends ServiceBase {
getEnginePersonalization(key) {
return this.personalization.getEnginePersonalization(key);
}

/**
* Adds a personalization key-value pair.
*
@@ -103,6 +103,9 @@ class QTL extends ServiceBase {
setPersonalization(key, value){
return that.services.personalization.addPersonalization(key, value, ctx);
},
setPersonality( value){
return that.services.personality.add(key, value, ctx);
},
capture(key, value){
value = Language.capture(key, value);
return that.services.personalization.addPersonalization(key, value, ctx);
@@ -97,6 +97,7 @@ module.exports = {
"Graph",
"Topics",
"Personalization",
"Personality",
"Pipeline",
"Oracle",
"Identity",
@@ -3,34 +3,34 @@ const Qwiery = require("../lib"),
path = require("path"),
_ = require('lodash');


exports.aha = function(test) {
const qwiery = new Qwiery(
{
defaultApp: "MyApp",
apps: [
{
name: "MyApp",
id:"MyApp",
pipeline: [
{
processMessage: function(session, context) {
context.services.unknowns.inspect(session.Input.Raw);
return session;
}
}
]
}
],
plugins: [
"Unknowns"
]
}
);
qwiery.ask("The legendary king of Scotland was an abricedarity.", {return: "plain"}).then(function() {
test.done();
});
};
//
// exports.aha = function(test) {
// const qwiery = new Qwiery(
// {
// defaultApp: "MyApp",
// apps: [
// {
// name: "MyApp",
// id:"MyApp",
// pipeline: [
// {
// processMessage: function(session, context) {
// context.services.unknowns.inspect(session.Input.Raw);
// return session;
// }
// }
// ]
// }
// ],
// plugins: [
// "Unknowns"
// ]
// }
// );
// qwiery.ask("The legendary king of Scotland was an abricedarity.", {return: "plain"}).then(function() {
// test.done();
// });
// };

// exports.dummy = function(test) {
//
@@ -50,6 +50,7 @@ const qwiery = new Qwiery({
"Oracle",
"Pipeline",
"Personalization",
"Personality",
"Topics"],
coreInterpreters: [
"Alias",
@@ -308,8 +309,8 @@ exports.whoareyou = function(test) {
test.ok(utils.isDefined(found), "The topic was found.");

if(utils.isDefined(personality)) {
personality.getUserPersonality(ctx).then(function(pers) {
test.ok(utils.isDefined(pers["Curious"]), "The personality was found.");
personality.getPersonalityValue("Curious", ctx).then(function(count) {
test.ok(count > 0, "The personality was found.");
test.done();
});
} else {
@@ -327,9 +328,8 @@ exports.thepersonality = function(test) {
let found = _.find(topics, {Type: "personality"});
test.ok(utils.isDefined(found), "The perso was found.");
if(utils.isDefined(personality)) {
personality.getUserPersonality(ctx).then(function(pers) {
found = _.find(pers, {Type: perso});
test.ok(utils.isDefined(pers[perso]), "The personality was found.");
personality.getPersonalityValue(perso, ctx).then(function(count) {
test.ok(count > 0, "The personality was found.");
test.done();
});
} else {

0 comments on commit 7ca9a09

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.