ensure all lint tests pass
This commit is contained in:
parent
84b4de490c
commit
4cfd7d80d4
@ -23,7 +23,7 @@ class CEConcept {
|
||||
return;
|
||||
}
|
||||
for (const concept of node.concepts) {
|
||||
if (concept.name.toLowerCase() === name.toLowerCase()){
|
||||
if (concept.name.toLowerCase() === name.toLowerCase()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -38,7 +38,7 @@ class CEConcept {
|
||||
node.concepts.push(this);
|
||||
this.node.conceptDict[this.id] = this;
|
||||
|
||||
if (isNaN(name[0])){
|
||||
if (isNaN(name[0])) {
|
||||
const concept = this;
|
||||
Object.defineProperty(node.concepts, name.toLowerCase().replace(/ /g, '_'), {
|
||||
get() {
|
||||
@ -165,7 +165,7 @@ class CEConcept {
|
||||
if (i < this.parents.length - 1) { ce += ' and'; }
|
||||
}
|
||||
}
|
||||
let facts = [];
|
||||
const facts = [];
|
||||
const alph = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'];
|
||||
for (let i = 0; i < this.valueIds.length; i += 1) {
|
||||
if (this.valueIds[i].type === 0) {
|
||||
@ -192,7 +192,7 @@ class CEConcept {
|
||||
}
|
||||
|
||||
get ce() {
|
||||
return this.getCE();
|
||||
return this.getCE();
|
||||
}
|
||||
|
||||
get gist() {
|
||||
@ -230,7 +230,7 @@ class CEConcept {
|
||||
value.label = label;
|
||||
value.type = typeof type === 'number' ? type : type.id;
|
||||
this.valueIds.push(value);
|
||||
if (isNaN(label[0])){
|
||||
if (isNaN(label[0])) {
|
||||
Object.defineProperty(this, label.toLowerCase().replace(/ /g, '_'), {
|
||||
get() {
|
||||
return type === 0 ? 'value' : type;
|
||||
@ -246,7 +246,7 @@ class CEConcept {
|
||||
relationship.label = label;
|
||||
relationship.target = target.id;
|
||||
this.relationshipIds.push(relationship);
|
||||
if (isNaN(label[0])){
|
||||
if (isNaN(label[0])) {
|
||||
Object.defineProperty(this, label.toLowerCase().replace(/ /g, '_'), {
|
||||
get() {
|
||||
return target;
|
||||
|
@ -23,7 +23,7 @@ class CEInstance {
|
||||
return;
|
||||
}
|
||||
for (const instance of node.instances) {
|
||||
if (instance.name.toLowerCase() === name.toLowerCase() && type.id === instance.concept.id){
|
||||
if (instance.name.toLowerCase() === name.toLowerCase() && type.id === instance.concept.id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -42,7 +42,7 @@ class CEInstance {
|
||||
node.instances.push(this);
|
||||
this.node.instanceDict[this.id] = this;
|
||||
|
||||
if (isNaN(name[0])){
|
||||
if (isNaN(name[0])) {
|
||||
const instance = this;
|
||||
const helperName = name.toLowerCase().replace(/ /g, '_').replace(/'/g, '');
|
||||
Object.defineProperty(node.instances, helperName, {
|
||||
@ -106,7 +106,7 @@ class CEInstance {
|
||||
ancestorInstances.push(this.concept);
|
||||
for (const subConcept of this.subConcepts) {
|
||||
ancestorInstances.push(subConcept);
|
||||
ancestorInstances = ancestorInstances.concat(subConcept.ancestors);
|
||||
ancestorInstances = ancestorInstances.concat(subConcept.ancestors);
|
||||
}
|
||||
const properties = { values: [], relationships: [] };
|
||||
for (const ancestor of ancestorInstances) {
|
||||
@ -299,7 +299,7 @@ class CEInstance {
|
||||
}
|
||||
|
||||
get ce() {
|
||||
return this.getCE();
|
||||
return this.getCE();
|
||||
}
|
||||
|
||||
get gist() {
|
||||
|
@ -66,7 +66,7 @@ class CENode {
|
||||
getInstanceByName(name, concept) {
|
||||
if (!name) { return null; }
|
||||
for (const instance of this.instances) {
|
||||
if (instance && (concept ? concept.id === instance.concept.id : true)){
|
||||
if (instance && (concept ? concept.id === instance.concept.id : true)) {
|
||||
if (instance.name.toLowerCase() === name.toLowerCase()) {
|
||||
return instance;
|
||||
}
|
||||
@ -97,7 +97,7 @@ class CENode {
|
||||
* Returns: [obj{instance}]
|
||||
*/
|
||||
getInstances(conceptType, recurse) {
|
||||
let instanceList = [];
|
||||
const instanceList = [];
|
||||
if (!conceptType) {
|
||||
for (const instance of this.instances) {
|
||||
instanceList.push(instance);
|
||||
|
@ -159,7 +159,9 @@ class CEParser {
|
||||
}
|
||||
|
||||
modifyInstance(t, source) {
|
||||
let concept, instance, instanceName;
|
||||
let concept;
|
||||
let instance;
|
||||
let instanceName;
|
||||
if (t.match(/^the ([a-zA-Z0-9 ]*) '([^'\\]*(?:\\.[^'\\]*)*)'/i)) {
|
||||
const names = t.match(/^the ([a-zA-Z0-9 ]*) '([^'\\]*(?:\\.[^'\\]*)*)'/i);
|
||||
if (names) {
|
||||
@ -191,7 +193,6 @@ class CEParser {
|
||||
if (facts) {
|
||||
for (const fact of facts) {
|
||||
this.processInstanceFact(instance, fact, source);
|
||||
|
||||
}
|
||||
}
|
||||
return [true, t, instance];
|
||||
|
@ -33,7 +33,7 @@ class CEServer {
|
||||
this.node.attachAgent();
|
||||
this.node.agent.setName(name);
|
||||
this.handlers = {
|
||||
'GET': {
|
||||
GET: {
|
||||
'/cards': (request, response) => {
|
||||
const agentRegex = decodeURIComponent(request.url).match(/agent=(.*)/);
|
||||
const agentStr = agentRegex ? agentRegex[1] : null;
|
||||
@ -57,7 +57,7 @@ class CEServer {
|
||||
for (const concept of this.node.concepts) {
|
||||
concepts.push({
|
||||
name: concept.name,
|
||||
id: concept.id
|
||||
id: concept.id,
|
||||
});
|
||||
}
|
||||
response.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
@ -68,38 +68,38 @@ class CEServer {
|
||||
const id = idRegex ? idRegex[1] : null;
|
||||
const concept = this.node.getConceptById(id);
|
||||
if (concept) {
|
||||
const body = {name: concept.name, ce: concept.ce, parents: [], children: [], instances: [], values: [], relationships: []};
|
||||
const body = { name: concept.name, ce: concept.ce, parents: [], children: [], instances: [], values: [], relationships: [] };
|
||||
for (const parent of concept.parents) {
|
||||
body.parents.push({
|
||||
name: parent.name,
|
||||
id: parent.id
|
||||
id: parent.id,
|
||||
});
|
||||
}
|
||||
for (const child of concept.children) {
|
||||
body.children.push({
|
||||
name: child.name,
|
||||
id: child.id
|
||||
id: child.id,
|
||||
});
|
||||
}
|
||||
for (const instance of concept.instances) {
|
||||
body.instances.push({
|
||||
name: instance.name,
|
||||
id: instance.id
|
||||
id: instance.id,
|
||||
});
|
||||
}
|
||||
for (const value of concept.values){
|
||||
const name = value.concept && value.concept.name;
|
||||
const id = value.concept && value.concept.id;
|
||||
body.values.push({label: value.label, targetName: name, targetId: id});
|
||||
for (const value of concept.values) {
|
||||
const valueName = value.concept && value.concept.name;
|
||||
const valueId = value.concept && value.concept.id;
|
||||
body.values.push({ label: value.label, targetName: valueName, targetId: valueId });
|
||||
}
|
||||
for (const relationship of concept.relationships){
|
||||
body.relationships.push({label: relationship.label, targetName: relationship.concept.name, targetId: relationship.concept.id});
|
||||
for (const relationship of concept.relationships) {
|
||||
body.relationships.push({ label: relationship.label, targetName: relationship.concept.name, targetId: relationship.concept.id });
|
||||
}
|
||||
response.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
return response.end(JSON.stringify(body));
|
||||
}
|
||||
response.writeHead(404);
|
||||
response.end('Concept not found');
|
||||
return response.end('Concept not found');
|
||||
},
|
||||
'/instances': (request, response) => {
|
||||
const instances = [];
|
||||
@ -108,7 +108,7 @@ class CEServer {
|
||||
name: instance.name,
|
||||
id: instance.id,
|
||||
conceptName: instance.concept.name,
|
||||
conceptId: instance.concept.id
|
||||
conceptId: instance.concept.id,
|
||||
});
|
||||
}
|
||||
response.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
@ -127,42 +127,42 @@ class CEServer {
|
||||
synonyms: instance.synonyms,
|
||||
subConcepts: [],
|
||||
values: [],
|
||||
relationships: []
|
||||
relationships: [],
|
||||
};
|
||||
for (const concept of instance.subConcepts){
|
||||
body.subConcepts.push({name: concept.name, id: concept.id});
|
||||
for (const concept of instance.subConcepts) {
|
||||
body.subConcepts.push({ name: concept.name, id: concept.id });
|
||||
}
|
||||
for (const value of instance.values){
|
||||
const name = value.instance.name || value.instance;
|
||||
const id = value.instance.id;
|
||||
for (const value of instance.values) {
|
||||
const valueName = value.instance.name || value.instance;
|
||||
const valueId = value.instance.id;
|
||||
const conceptName = value.instance.concept && value.instance.concept.name;
|
||||
const conceptId = value.instance.concept && value.instance.concept.id;
|
||||
body.values.push({label: value.label, targetName: name, targetId: id, targetConceptName: conceptName, targetConceptId: conceptId});
|
||||
body.values.push({ label: value.label, targetName: valueName, targetId: valueId, targetConceptName: conceptName, targetConceptId: conceptId });
|
||||
}
|
||||
for (const relationship of instance.relationships){
|
||||
body.relationships.push({label: relationship.label, targetName: relationship.instance.name, targetId: relationship.instance.id, targetConceptName: relationship.instance.concept.name, targetConceptId: relationship.instance.concept.id});
|
||||
for (const relationship of instance.relationships) {
|
||||
body.relationships.push({ label: relationship.label, targetName: relationship.instance.name, targetId: relationship.instance.id, targetConceptName: relationship.instance.concept.name, targetConceptId: relationship.instance.concept.id });
|
||||
}
|
||||
response.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
return response.end(JSON.stringify(body));
|
||||
}
|
||||
response.writeHead(404);
|
||||
response.end('Concept not found');
|
||||
return response.end('Concept not found');
|
||||
},
|
||||
'/info': (request, response) => {
|
||||
const body = {recentInstances: [], recentConcepts: [], instanceCount: this.node.instances.length, conceptCount: this.node.concepts.length};
|
||||
const body = { recentInstances: [], recentConcepts: [], instanceCount: this.node.instances.length, conceptCount: this.node.concepts.length };
|
||||
const recentInstances = this.node.instances.slice(this.node.instances.length >= 10 ? this.node.instances.length - 10 : 0);
|
||||
for (const instance of recentInstances) {
|
||||
body.recentInstances.push({
|
||||
name: instance.name,
|
||||
id: instance.id,
|
||||
conceptName: instance.concept.name,
|
||||
conceptId: instance.concept.id
|
||||
conceptId: instance.concept.id,
|
||||
});
|
||||
}
|
||||
for (const concept of this.node.concepts) {
|
||||
body.recentConcepts.push({
|
||||
name: concept.name,
|
||||
id: concept.id
|
||||
id: concept.id,
|
||||
});
|
||||
}
|
||||
response.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
@ -170,15 +170,15 @@ class CEServer {
|
||||
},
|
||||
'/model': (request, response) => {
|
||||
let body = '';
|
||||
for (const concept of this.node.concepts) { body += concept.creationCE + '\n'; }
|
||||
for (const concept of this.node.concepts) { body += concept.getCE(true) + '\n'; }
|
||||
for (const instance of this.node.instances) { body += instance.creationCE + '\n'; }
|
||||
for (const instance of this.node.instances) { body += instance.getCE(true) + '\n'; }
|
||||
response.writeHead(200, { 'Content-Type': 'text/ce', 'Content-Disposition': 'attachment; filename="' + this.node.agent.name + '.ce"' });
|
||||
for (const concept of this.node.concepts) { body += `${concept.creationCE}\n`; }
|
||||
for (const concept of this.node.concepts) { body += `${concept.getCE(true)}\n`; }
|
||||
for (const instance of this.node.instances) { body += `${instance.creationCE}\n`; }
|
||||
for (const instance of this.node.instances) { body += `${instance.getCE(true)}\n`; }
|
||||
response.writeHead(200, { 'Content-Type': 'text/ce', 'Content-Disposition': `attachment; filename="${this.node.agent.name}.ce"` });
|
||||
response.end(body);
|
||||
}
|
||||
},
|
||||
},
|
||||
'POST': {
|
||||
POST: {
|
||||
'/cards': (request, response) => {
|
||||
let body = '';
|
||||
request.on('data', (chunk) => { body += chunk; });
|
||||
@ -218,9 +218,9 @@ class CEServer {
|
||||
response.writeHead(200, { 'Content-Type': 'text/ce' });
|
||||
response.end(responses.map(resp => resp.data).join('\n'));
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
'PUT': {
|
||||
PUT: {
|
||||
'/reset': (request, response) => {
|
||||
this.node.resetAll();
|
||||
response.writeHead(204);
|
||||
@ -235,8 +235,8 @@ class CEServer {
|
||||
response.writeHead(302, { Location: '/' });
|
||||
response.end();
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -247,28 +247,25 @@ class CEServer {
|
||||
const path = request.url.indexOf('?') > 1 ? request.url.slice(0, request.url.indexOf('?')) : request.url;
|
||||
if (path in this.handlers[request.method]) {
|
||||
this.handlers[request.method][path](request, response);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
response.writeHead(404);
|
||||
response.end(`404: Resource not found for method ${request.method}.`);
|
||||
}
|
||||
}
|
||||
else if (request.method === 'OPTIONS') {
|
||||
} else if (request.method === 'OPTIONS') {
|
||||
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
|
||||
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
|
||||
response.writeHead(200);
|
||||
response.end();
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
response.writeHead(405);
|
||||
response.end('405: Method not allowed on this server.');
|
||||
}
|
||||
});
|
||||
this.server.listen(this.port);
|
||||
this.server.on('error', err => {this.node = undefined;});
|
||||
this.server.on('error', () => { this.node = undefined; });
|
||||
}
|
||||
|
||||
stop (){
|
||||
stop() {
|
||||
if (this.server) {
|
||||
delete this.node;
|
||||
this.server.close();
|
||||
|
@ -172,32 +172,34 @@ class PolicyHandler {
|
||||
// Forward any cards sent to THIS agent to every other known agent
|
||||
const agents = policy.all_agents === 'true' ? this.node.getInstances('agent') : policy.targets;
|
||||
const cards = this.node.getInstances('tell card');
|
||||
if (policy.start_time && card.timestamp) {
|
||||
if (policy.start_time) {
|
||||
const startTime = policy.start_time;
|
||||
for (const card of cards) {
|
||||
let toAgent = false;
|
||||
const tos = card.is_tos;
|
||||
const from = card.is_froms[0];
|
||||
const cardTimestamp = card.timestamp.name;
|
||||
if (tos && parseInt(cardTimestamp, 10) > parseInt(startTime, 10)) {
|
||||
for (const to of tos) {
|
||||
if (to.name === this.agent.name) { // If card sent to THIS agent
|
||||
toAgent = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (toAgent) {
|
||||
// Add each other agent as a recipient (if they aren't already)
|
||||
for (const agentCheck of agents) {
|
||||
let agentIsRecipient = false;
|
||||
for (const to of tos) {
|
||||
if (to.name.toLowerCase() === agentCheck.name.toLowerCase()) {
|
||||
agentIsRecipient = true;
|
||||
break;
|
||||
}
|
||||
if (card.timestamp && card.is_froms.length) {
|
||||
let toAgent = false;
|
||||
const tos = card.is_tos;
|
||||
const from = card.is_froms[0];
|
||||
const cardTimestamp = card.timestamp.name;
|
||||
if (tos && parseInt(cardTimestamp, 10) > parseInt(startTime, 10)) {
|
||||
for (const to of tos) {
|
||||
if (to.name === this.agent.name) { // If card sent to THIS agent
|
||||
toAgent = true;
|
||||
break;
|
||||
}
|
||||
if (!agentIsRecipient && agentCheck.name.toLowerCase() !== this.agent.name.toLowerCase() && agentCheck.name.toLowerCase() !== from.name.toLowerCase()) {
|
||||
card.addRelationship('is to', agentCheck);
|
||||
}
|
||||
if (toAgent) {
|
||||
// Add each other agent as a recipient (if they aren't already)
|
||||
for (const agentCheck of agents) {
|
||||
let agentIsRecipient = false;
|
||||
for (const to of tos) {
|
||||
if (to.name.toLowerCase() === agentCheck.name.toLowerCase()) {
|
||||
agentIsRecipient = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!agentIsRecipient && agentCheck.name.toLowerCase() !== this.agent.name.toLowerCase() && agentCheck.name.toLowerCase() !== from.name.toLowerCase()) {
|
||||
card.addRelationship('is to', agentCheck);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user