@connectv/core
Version:
agent-based reactive programming library for typescript/javascript
159 lines • 5.12 kB
JavaScript
import { PartialFlow } from '../pin/partial-flow';
import { group, Group } from '../pin/group';
import { PinMap } from '../pin/pin-map';
import { Pin } from '../pin/pin';
import { InputNotInSignature, OutputNotInSignature } from './errors/signature-mismatch.error';
import { ImproperPartialFlow } from './errors/improper-partial-flow.error';
/**
*
* The parent class for [agents](https://connective.dev/docs/agent).
*
*/
export class Agent extends PartialFlow {
/**
*
* @param signature the [signature](https://connective.dev/docs/agent#signature)
* of the agent. Must be determined either by instantiators or sub-classes.
*
*/
constructor(signature) {
super();
this.signature = signature;
this._inputs = new PinMap(label => this.createInput(label));
this._outputs = new PinMap(label => this.createOutput(label));
}
/**
*
* @param label
* @returns the input pin corresponding to given label
* @throws an error if given label is not allowed by the agent's
* [signature](https://connective.dev/docs/agent#signature).
*
*/
in(label) { return this._inputs.get(label.toString()); }
/**
*
* @param label
* @returns the output pin corresponding to given label
* @throws an error if given label is not allowed by the agent's
* [signature](https://connective.dev/docs/agent#signature).
*
*/
out(label) { return this._outputs.get(label.toString()); }
/**
*
* @returns the entry pins for this agent for it to behave as a partial flow.
* You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection).
*
*/
get entries() {
if (!this._entries) {
let entries = this.createEntries();
this._entries = (entries instanceof Group) ? entries : group(...entries);
}
return this._entries;
}
/**
*
* @returns the exit pins for this agent for it to behave as a partial flow.
* You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection).
*
*/
get exits() {
if (!this._exits) {
let exits = this.createExits();
this._exits = (exits instanceof Group) ? exits : group(...exits);
}
return this._exits;
}
get inputs() { return this._inputs; }
get outputs() { return this._outputs; }
/**
*
* @note an Agent's `.clear()` also clears up
* input and output pins.
*
*/
clear() {
this._inputs.clear();
this._outputs.clear();
return super.clear();
}
/**
*
* Checks if given input label matches the agent's
* [signature](https://connective.dev/docs/agent#signature).
*
* Override this to change how validation of input labels occurs.
*
* @param label the input label to be validated
*
*/
checkInput(label) {
if (!this.signature.inputs || !this.signature.inputs.includes(label))
throw new InputNotInSignature(label, this.signature);
}
/**
*
* Checks if given output label matches the agent's
* [signature](https://connective.dev/docs/agent#signature).
*
* Override this to change how validation of output labels occurs.
*
*/
checkOutput(label) {
if (!this.signature.outputs.includes(label))
throw new OutputNotInSignature(label, this.signature);
}
/**
*
* Validates given label and creates the corresponding input pin.
*
* Override this to change how an input pin is created.
*
* @param label
* @returns the corresponding input pin.
*
*/
createInput(label) {
this.checkInput(label);
return new Pin();
}
/**
*
* Validates given label and creates the corresponding output pin.
*
* Override this to change how an output pin is created.
*
* @param label
* @returns the corresponding output pin.
*
*/
createOutput(label) {
this.checkOutput(label);
return new Pin();
}
/**
*
* Override this to specify which pins should be considered as entries of this agent as a `PartialFlow`.
* You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection).
* If not overriden, the agent will be considered an improper patial flow and an error will be thrown
* when used as one.
*
*/
createEntries() {
throw new ImproperPartialFlow(this);
}
/**
*
* Override this to specify which pins should be considered as exits of this agent as a `PartialFlow`.
* You can read more about partial flows [here](https://connective.dev/docs/agent#implicit-connection).
* If not overriden, the agent will be considered an improper patial flow and an error will be thrown
* when used as one.
*
*/
createExits() {
throw new ImproperPartialFlow(this);
}
}
//# sourceMappingURL=agent.js.map