Compare commits

..

7 Commits

Author SHA1 Message Date
Will Webberley
a82756ab73 updated refs to github repos 2018-07-11 13:15:04 +01:00
Will Webberley
2c527f95fd
Merge pull request #22 from jtorreggiani/multi-responses-relationship-questions
Multiple responses to relationship questions
2018-03-19 19:21:50 +00:00
Joe Torreggiani
6229aa5aaa Add test for what relationship questions 2018-03-18 00:45:49 -04:00
Joe Torreggiani
505dec2ac4 Add support for multiple responses to what relationship questions 2018-03-18 00:45:08 -04:00
Will Webberley
feb2962d36 fixed lint issues and bumped version 2017-07-27 18:31:41 +01:00
Will Webberley
7080db074a 3.0.12 2017-07-27 18:27:04 +01:00
Will Webberley
65da1e698d added gist card handlers and implemented 'gist policy' logic 2017-07-27 18:24:04 +01:00
7 changed files with 70 additions and 22 deletions

View File

@ -4,7 +4,7 @@ A pure JavaScript implementation of the ITA project's CEStore - called CENode. C
Please visit the project's [home page](http://cenode.io) for more information and for documentation. Please visit the project's [home page](http://cenode.io) for more information and for documentation.
**We recommend beginners check out the [Getting Started Guide](https://github.com/flyingsparx/CENode/wiki/Getting-Started-Guide) before continuing.** **We recommend beginners check out the [Getting Started Guide](https://github.com/willwebberley/CENode/wiki/Getting-Started-Guide) before continuing.**
## Getting started ## Getting started
@ -33,13 +33,13 @@ const CEModels = require('cenode/models'); // if requred
const node = new CENode(CEModels.core); const node = new CENode(CEModels.core);
``` ```
See the [Wiki](https://github.com/flyingsparx/CENode/wiki) for further guides and the API reference. See the [Wiki](https://github.com/willwebberley/CENode/wiki) for further guides and the API reference.
## Testing ## Testing
Clone the repository Clone the repository
``` ```
git clone git@github.com:flyingsparx/CENode.git git clone git@github.com:willwebberley/CENode.git
``` ```
Install the necessary dev dependencies. Install the necessary dev dependencies.
@ -54,7 +54,7 @@ npm test
## More Information ## More Information
See the CENode [Wiki](https://github.com/flyingsparx/CENode/wiki) for more information, guides, and the API reference. See the CENode [Wiki](https://github.com/willwebberley/CENode/wiki) for more information, guides, and the API reference.
## Licence ## Licence

View File

@ -18,7 +18,7 @@ module.exports = [
"conceptualise a ~ policy ~ P that is an entity and has the value V as ~ enabled ~ and has the agent A as ~ target ~", "conceptualise a ~ policy ~ P that is an entity and has the value V as ~ enabled ~ and has the agent A as ~ target ~",
"conceptualise a ~ tell policy ~ P that is a policy", "conceptualise a ~ tell policy ~ P that is a policy",
"conceptualise an ~ ask policy ~ P that is a policy", "conceptualise an ~ ask policy ~ P that is a policy",
"conceptualise a ~ gist policy ~ P that is a policy", "conceptualise a ~ gist policy ~ P that is a policy",
"conceptualise a ~ listen policy ~ P that is a policy", "conceptualise a ~ listen policy ~ P that is a policy",
"conceptualise a ~ listen onbehalfof policy ~ P that is a policy", "conceptualise a ~ listen onbehalfof policy ~ P that is a policy",
"conceptualise a ~ forwardall policy ~ P that is a policy and has the timestamp T as ~ start time ~ and has the value V as ~ all agents ~", "conceptualise a ~ forwardall policy ~ P that is a policy and has the timestamp T as ~ start time ~ and has the value V as ~ all agents ~",

View File

@ -1,13 +1,13 @@
{ {
"name": "cenode", "name": "cenode",
"version": "3.0.11", "version": "3.0.12",
"description": "A pure JavaScript implementation of the ITA project's CEStore - called CENode. CENode is able to understand the basic sentence types parsed by the CEStore, such as conceptualising and instance creation and modification.", "description": "A pure JavaScript implementation of the ITA project's CEStore - called CENode. CENode is able to understand the basic sentence types parsed by the CEStore, such as conceptualising and instance creation and modification.",
"homepage": "http://cenode.io", "homepage": "http://cenode.io",
"license": "Apache-2.0", "license": "Apache-2.0",
"author": "Will Webberley & Alun Preece", "author": "Will Webberley & Alun Preece",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/flyingsparx/CENode" "url": "https://github.com/willwebberley/CENode"
}, },
"files": [ "files": [
"src", "src",

View File

@ -115,16 +115,16 @@ class CardHandler {
return this.node.addSentence(`there is a ${data.response.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.response.message.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`); return this.node.addSentence(`there is a ${data.response.type} card named 'msg_{uid}' that is from the agent '${this.agent.name.replace(/'/g, "\\'")}' and is to the ${card.is_from.type.name} '${card.is_from.name.replace(/'/g, "\\'")}' and has the timestamp '{now}' as timestamp and has '${data.response.message.replace(/'/g, "\\'")}' as content and is in reply to the card '${card.name}'.`);
}, },
'gist card': (card) => { 'gist card': (card) => {
// Add sentence to any active gist policy queues // Add sentence to any active gist policy queues
for (const policy of this.node.getInstances('gist policy')) { for (const policy of this.node.getInstances('gist policy')) {
if (policy.enabled === 'true' && policy.target && policy.target.name) { if (policy.enabled === 'true' && policy.target && policy.target.name) {
const targetName = policy.target.name; const targetName = policy.target.name;
if (!(targetName in this.agent.policyHandler.unsentGistCards)) { this.agent.policyHandler.unsentGistCards[targetName] = []; } if (!(targetName in this.agent.policyHandler.unsentGistCards)) { this.agent.policyHandler.unsentGistCards[targetName] = []; }
this.agent.policyHandler.unsentGistCards[targetName].push(card); this.agent.policyHandler.unsentGistCards[targetName].push(card);
} }
} }
}, },
}; };
} }

View File

@ -86,7 +86,7 @@ class PolicyHandler {
this.node = agent.node; this.node = agent.node;
this.unsentTellCards = {}; this.unsentTellCards = {};
this.unsentAskCards = {}; this.unsentAskCards = {};
this.unsentGistCards = {}; this.unsentGistCards = {};
this.lastSuccessfulRequest = 0; this.lastSuccessfulRequest = 0;
this.handlers = { this.handlers = {
@ -105,7 +105,8 @@ class PolicyHandler {
for (const to of card.is_tos) { for (const to of card.is_tos) {
if (to.id === policy.target.id) { inCard = true; break; } if (to.id === policy.target.id) { inCard = true; break; }
} }
if (!inCard) { card.addRelationship('is to', policy.target); if (!inCard) {
card.addRelationship('is to', policy.target);
} }
data += `${card.ce}\n`; data += `${card.ce}\n`;
} }
@ -159,7 +160,7 @@ class PolicyHandler {
} }
}, },
'gist policy': (policy) => { 'gist policy': (policy) => {
// For each gist policy in place, send all currently-untold cards to each target // For each gist policy in place, send all currently-untold cards to each target
// multiple cards to be sent to one target line-separated // multiple cards to be sent to one target line-separated
if (policy.target && policy.target.name && policy.target.address) { if (policy.target && policy.target.name && policy.target.address) {
@ -174,7 +175,8 @@ class PolicyHandler {
for (const to of card.is_tos) { for (const to of card.is_tos) {
if (to.id === policy.target.id) { inCard = true; break; } if (to.id === policy.target.id) { inCard = true; break; }
} }
if (!inCard) { card.addRelationship('is to', policy.target); if (!inCard) {
card.addRelationship('is to', policy.target);
} }
data += `${card.ce}\n`; data += `${card.ce}\n`;
} }

View File

@ -282,6 +282,7 @@ class QuestionParser {
const data = t.match(/^(\bwho\b|\bwhat\b) ([a-zA-Z0-9_ ]*)/i); const data = t.match(/^(\bwho\b|\bwhat\b) ([a-zA-Z0-9_ ]*)/i);
const body = data[2].replace(/\ban\b/gi, '').replace(/\bthe\b/gi, '').replace(/\ba\b/gi, ''); const body = data[2].replace(/\ban\b/gi, '').replace(/\bthe\b/gi, '').replace(/\ba\b/gi, '');
const tokens = body.split(' '); const tokens = body.split(' ');
const uniqueResponses = new Set([]);
let instance; let instance;
for (let i = 0; i < tokens.length; i += 1) { for (let i = 0; i < tokens.length; i += 1) {
const testString = tokens.slice(tokens.length - (i + 1), tokens.length).join(' ').trim(); const testString = tokens.slice(tokens.length - (i + 1), tokens.length).join(' ').trim();
@ -295,6 +296,7 @@ class QuestionParser {
break; break;
} }
} }
if (instance) { if (instance) {
const propertyName = tokens.splice(0, tokens.length - instance.name.split(' ').length).join(' ').trim(); const propertyName = tokens.splice(0, tokens.length - instance.name.split(' ').length).join(' ').trim();
for (let i = 0; i < this.node.instances.length; i += 1) { for (let i = 0; i < this.node.instances.length; i += 1) {
@ -316,7 +318,11 @@ class QuestionParser {
property = subject.property(fixedPropertyName); property = subject.property(fixedPropertyName);
} }
if (property && property.name === instance.name) { if (property && property.name === instance.name) {
return QuestionParser.success(`${subject.name} ${fixedPropertyName} the ${property.type.name} ${property.name}.`); uniqueResponses.add(`${subject.name} ${fixedPropertyName} the ${property.type.name} ${property.name}.`);
}
const responsesArray = Array.from(uniqueResponses);
if (responsesArray.length > 0 && i === this.node.instances.length - 1) {
return QuestionParser.success(responsesArray.join(' '));
} }
} }
return QuestionParser.success(`Sorry - I don't know that property about the ${instance.type.name} ${instance.name}.`); return QuestionParser.success(`Sorry - I don't know that property about the ${instance.type.name} ${instance.name}.`);

40
test/QuestionParser.js Normal file
View File

@ -0,0 +1,40 @@
const CENode = require('../src/CENode.js');
const CEModels = require('../models/index.js');
const expect = require('expect.js');
const myName = 'User'
const PLANETS_MODEL = [
"there is a rule named 'r1' that has 'if the planet C ~ orbits ~ the star D then the star D ~ is orbited by ~ the planet C' as instruction.",
"there is a rule named 'r2' that has 'if the planet C ~ is orbited by ~ the moon D then the moon D ~ orbits ~ the planet C' as instruction.",
"conceptualise a ~ celestial body ~ C.",
"conceptualise the celestial body C ~ orbits ~ the celestial body D and ~ is orbited by ~ the celestial body E.",
"conceptualise a ~ planet ~ P that is a celestial body and is an imageable thing.",
"conceptualise a ~ star ~ S that is a celestial body.",
"there is a star named sun.",
"there is a planet named Venus that orbits the star 'sun' and has 'media/Venus.jpg' as image.",
"there is a planet named Mercury that orbits the star 'sun' and has 'media/Mercury.jpg' as image."
]
let node;
describe('CEQuestionParser', function() {
describe('What relation questions', function () {
this.timeout(2050);
before(function() {
node = new CENode(CEModels.core, PLANETS_MODEL);
node.attachAgent();
node.agent.setName('agent1');
});
it('returns the correct number of responses', (done) => {
const message = 'what orbits the sun?';
const askCard = "there is a nl card named '{uid}' that is to the agent 'agent1' and is from the individual '" + myName + "' and has the timestamp '{now}' as timestamp and has '" + message.replace(/'/g, "\\'")+"' as content.";
node.addSentence(askCard);
setTimeout(function() {
const cards = node.concepts.card.allInstances;
const card = cards[cards.length - 1];
expect(card.content).to.equal('Venus orbits the star sun. Mercury orbits the star sun.');
done();
}, 2000);
});
});
});