import { PartialFlow } from '../pin/partial-flow' ; import { group , Group } from '../pin/group' ; import { PinMap } from '../pin/pin-map' ; import { PinLike } from '../pin/pin-like' ; import { Pin } from '../pin/pin' ; import { InputNotInSignature , OutputNotInSignature } from './errors/signature-mismatch.error' ; import { ImproperPartialFlow } from './errors/improper-partial-flow.error' ; import { Signature } from './signature' ; import { AgentLike } from './agent-like' ; /**
 *
 * The parent class for [agents](https://connective.dev/docs/agent).
 *
 */ export class Agent extends PartialFlow implements AgentLike { private _inputs : PinMap ; private _outputs : PinMap ; private _entries : Group ; private _exits : Group ; param signature constructor ( readonly signature ) { super ( ) ; this . _inputs = new PinMap ( label => this . createInput ( label ) ) ; this . _outputs = new PinMap ( label => this . createOutput ( label ) ) ; } param label returns throws public in ( label ) { return this . _inputs . get ( label . toString ( ) ) ; } param label returns throws public out ( label ) { return this . _outputs . get ( label . toString ( ) ) ; } returns public get entries ( ) : Group { if ( ! this . _entries ) { let entries = this . createEntries ( ) ; this . _entries = ( entries instanceof Group ) ? entries : group ( ... entries ) ; } return this . _entries ; } returns public get exits ( ) : Group { if ( ! this . _exits ) { let exits = this . createExits ( ) ; this . _exits = ( exits instanceof Group ) ? exits : group ( ... exits ) ; } return this . _exits ; } public get inputs ( ) : PinMap { return this . _inputs ; } public get outputs ( ) : PinMap { return this . _outputs ; } note public clear ( ) { this . _inputs . clear ( ) ; this . _outputs . clear ( ) ; return super . clear ( ) ; } param label protected 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.
     *
     */ protected checkOutput ( label ) { if ( ! this . signature . outputs . includes ( label ) ) throw new OutputNotInSignature ( label , this . signature ) ; } param label returns protected createInput ( label ) { this . checkInput ( label ) ; return new Pin ( ) ; } param label returns protected 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.
     *
     */ protected 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.
     *
     */ protected createExits ( ) { throw new ImproperPartialFlow ( this ) ; } }