import { Decider , Decision , ImplicationProofItem , Property } from '../../types' ; import { CannotDecideError , handleCannotDecideError } from './utils' ; export interface AndDeciderInput { properties : Property [ ] ; } /**
 * Decider that decides true iff both of the provided properties evaluate to true.
 */ export class AndDecider implements Decider { private static _instance : AndDecider ; public static instance ( ) { if ( ! AndDecider . _instance ) { AndDecider . _instance = new AndDecider ( ) ; } return AndDecider . _instance ; } public async decide ( input , noCache ? ) { const decisions = await Promise . all ( input . properties . map ( ( p ) => p . decider . decide ( p . input , noCache ) . catch ( handleCannotDecideError ) ) ) ; const justification = [ ] ; let undecideable = false ; let falseDecision ; for ( const decision of decisions ) { if ( ! decision ) { undecideable = true ; continue ; } if ( ! decision . outcome ) { falseDecision = decision ; break ; } justification . push ( ... decision . justification ) ; } if ( ! ! falseDecision ) { return this . getDecision ( input , falseDecision ) ; } if ( undecideable ) { throw new CannotDecideError ( 'One of the AND deciders could not decide, and none decided false.' ) ; } return this . getDecision ( input , { outcome : true , justification } ) ; } param input param subDecision returns private getDecision ( input , subDecision ) { const justification = [ { implication : { decider : this , input , } , implicationWitness : undefined , } , ... subDecision . justification , ] ; return { outcome : subDecision . outcome , justification , } ; } }