Skip to content

Commit

Permalink
refactor(core) Clearer separation between contract template and contr…
Browse files Browse the repository at this point in the history
…act instance

Signed-off-by: jeromesimeon <[email protected]>
  • Loading branch information
jeromesimeon committed Oct 28, 2021
1 parent 7bf6dae commit 5412758
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 229 deletions.
29 changes: 23 additions & 6 deletions packages/cicero-cli/lib/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class Commands {

return Commands.loadTemplate(templatePath, options)
.then((template) => {
clause = new Clause(template);
clause = Clause.fromTemplate(template);
clause.parse(sampleText, currentTime, utcOffset, samplePath);
if (outputPath) {
Logger.info('Creating file: ' + outputPath);
Expand Down Expand Up @@ -248,7 +248,7 @@ class Commands {

return Commands.loadTemplate(templatePath, options)
.then(async function (template) {
clause = new Clause(template);
clause = Clause.fromTemplate(template);
clause.setData(dataJson);
const drafted = clause.draft(options, currentTime, utcOffset);
if (outputPath) {
Expand Down Expand Up @@ -306,7 +306,7 @@ class Commands {

return Commands.loadTemplate(templatePath, options)
.then(async function (template) {
clause = new Clause(template);
clause = Clause.fromTemplate(template);
clause.parse(sampleText, currentTime, utcOffset, samplePath);
if (outputPath) {
Logger.info('Creating file: ' + outputPath);
Expand Down Expand Up @@ -384,7 +384,7 @@ class Commands {
return Commands.loadTemplate(templatePath, options)
.then(async (template) => {
// Initialize clause
clause = new Clause(template);
clause = Clause.fromTemplate(template);
if (sampleText) {
clause.parse(sampleText, currentTime, utcOffset);
} else {
Expand Down Expand Up @@ -494,7 +494,7 @@ class Commands {
return Commands.loadTemplate(templatePath, options)
.then(async (template) => {
// Initialize clause
clause = new Clause(template);
clause = Clause.fromTemplate(template);
if (sampleText) {
clause.parse(sampleText, currentTime, utcOffset);
} else {
Expand Down Expand Up @@ -571,7 +571,7 @@ class Commands {
return Commands.loadTemplate(templatePath, options)
.then((template) => {
// Initialize clause
clause = new Clause(template);
clause = Clause.fromTemplate(template);
if (sampleText) {
clause.parse(sampleText, currentTime, utcOffset);
} else {
Expand Down Expand Up @@ -650,6 +650,23 @@ class Commands {
return argv;
}

/**
* Set default params before we create an instance archive
*
* @param {object} argv the inbound argument values object
* @returns {object} a modfied argument object
*/
static validateInstantiateArgs(argv) {
argv = Commands.validateCommonArgs(argv);

if(!argv.target){
Logger.info('Using ergo as the default target for the archive.');
argv.target = 'ergo';
}

return argv;
}

/**
* Verify the template developer/author's signatures
*
Expand Down
21 changes: 20 additions & 1 deletion packages/cicero-core/src/clause.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ const TemplateInstance = require('./templateinstance.js');
* @class
*/
class Clause extends TemplateInstance {
/**
* Create an instance from a Template.
* @param {Template} template - the template for the instance
* @return {object} - the clause instance
*/
static fromTemplate(template) {
// Setup
const metadata = template.getMetadata();
const logicManager = template.getLogicManager();
const grammar = template.getParserManager().getTemplate();

return new Clause(
metadata.getTemplateType(),
metadata.getIdentifier(),
logicManager,
grammar,
metadata.getRuntime(),
template,
);
}
}

module.exports = Clause;
module.exports = Clause;
21 changes: 20 additions & 1 deletion packages/cicero-core/src/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ const TemplateInstance = require('./templateinstance.js');
* @class
*/
class Contract extends TemplateInstance {
/**
* Create an instance from a Template.
* @param {Template} template - the template for the instance
* @return {object} - the contract instance
*/
static fromTemplate(template) {
// Setup
const metadata = template.getMetadata();
const logicManager = template.getLogicManager();
const grammar = template.getParserManager().getTemplate();

return new Contract(
metadata.getTemplateType(),
metadata.getIdentifier(),
logicManager,
grammar,
metadata.getRuntime(),
template,
);
}
}

module.exports = Contract;
module.exports = Contract;
53 changes: 2 additions & 51 deletions packages/cicero-core/src/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const stringify = require('json-stable-stringify');
const LogicManager = require('@accordproject/ergo-compiler').LogicManager;
const TemplateLoader = require('./templateloader');
const TemplateSaver = require('./templatesaver');
const Util = require('./util');

/**
* A template for a legal clause or contract. A Template has a template model, request/response transaction types,
Expand Down Expand Up @@ -79,23 +80,7 @@ class Template {
* @returns {ClassDeclaration} the template model for the template
*/
getTemplateModel() {

let modelType = 'org.accordproject.contract.Contract';

if(this.getMetadata().getTemplateType() !== 0) {
modelType = 'org.accordproject.contract.Clause';
}
const templateModels = this.getIntrospector().getClassDeclarations().filter((item) => {
return !item.isAbstract() && Template.instanceOf(item,modelType);
});

if (templateModels.length > 1) {
throw new Error(`Found multiple instances of ${modelType} in ${this.metadata.getName()}. The model for the template must contain a single asset that extends ${modelType}.`);
} else if (templateModels.length === 0) {
throw new Error(`Failed to find an asset that extends ${modelType} in ${this.metadata.getName()}. The model for the template must contain a single asset that extends ${modelType}.`);
} else {
return templateModels[0];
}
return Util.getContractModel(this.logicManager, this.getMetadata().getTemplateType());
}

/**
Expand Down Expand Up @@ -139,7 +124,6 @@ class Template {
return this.getMetadata().getVersion();
}


/**
* Returns the description for this template
* @return {String} the description of this template
Expand Down Expand Up @@ -325,15 +309,6 @@ class Template {
return this.logicManager;
}

/**
* Provides access to the Introspector for this template. The Introspector
* is used to reflect on the types defined within this template.
* @return {Introspector} the Introspector for this template
*/
getIntrospector() {
return this.logicManager.getIntrospector();
}

/**
* Provides access to the Factory for this template. The Factory
* is used to create the types defined in this template.
Expand Down Expand Up @@ -483,30 +458,6 @@ class Template {
return this.getScriptManager().getAllScripts().length > 0;
}

/**
* Check to see if a ClassDeclaration is an instance of the specified fully qualified
* type name.
* @internal
* @param {ClassDeclaration} classDeclaration The class to test
* @param {String} fqt The fully qualified type name.
* @returns {boolean} True if classDeclaration an instance of the specified fully
* qualified type name, false otherwise.
*/
static instanceOf(classDeclaration, fqt) {
// Check to see if this is an exact instance of the specified type.
if (classDeclaration.getFullyQualifiedName() === fqt) {
return true;
}
// Now walk the class hierachy looking to see if it's an instance of the specified type.
let superTypeDeclaration = classDeclaration.getSuperTypeDeclaration();
while (superTypeDeclaration) {
if (superTypeDeclaration.getFullyQualifiedName() === fqt) {
return true;
}
superTypeDeclaration = superTypeDeclaration.getSuperTypeDeclaration();
}
return false;
}
}

module.exports = Template;
Loading

0 comments on commit 5412758

Please sign in to comment.