From a522b71809f579a211e798a73224abbf59cc68d4 Mon Sep 17 00:00:00 2001 From: Gene Date: Wed, 11 Nov 2015 21:55:06 +0800 Subject: [PATCH] Added save function (UNTESTED) Utilizes JSON. Might have to double-check the dependencies. Might even need to re-do the load/save function. Should check if file exists and is writable. --- lib/ector.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/ector.js b/lib/ector.js index 9aeecf5..ebbbcdb 100644 --- a/lib/ector.js +++ b/lib/ector.js @@ -17,6 +17,7 @@ var concept_network = require('concept-network'); var ConceptNetwork = concept_network.ConceptNetwork; var ConceptNetworkState = concept_network.ConceptNetworkState; var rwc = require('random-weighted-choice'); +var fs = require('fs'); /** * ## Ector's constructor @@ -342,6 +343,34 @@ Ector.prototype = { this.cn = new ConceptNetwork(); throw new Error('NewConceptNetwork is not a ConceptNetwork'); } + }, + + /** + * ### saveCN + * @param {string} file path and name. e.g. /home/ubuntu/node.json + * @return {string|Error} true, or an Error. + */ + saveCN : function (filePath) { + if (typeof filePath !== 'string') { + return new Error("filePath should be a string"); + } + + fs.writeFileSync(filePath, JSON.stringify(this.cn)); + return true; + }, + + /** + * ### loadCN + * @param {string} file path and name. e.g. /home/ubuntu/node.json + * @return {string|Error} true, or an Error. + */ + loadCN : function (filePath) { + if (typeof filePath !== 'string') { + return new Error("filePath should be a string"); + } + + Object.merge(this.cn, JSON.parse(fs.readFileSync(filePath, 'utf8'))); + return true; } };