'use strict' ; import { TextDocument } from 'vscode' ; import { AbstractPlanExporter } from './PlanExporter' ; import { Happening , HappeningType , PlanStep , parser } from 'pddl-workspace' ; /** Exports .happenings file to a .plan file by pairing happenings, if possible. */ export class HappeningsPlanExporter extends AbstractPlanExporter { decimals : number ; constructor ( private happeningsDocument , private epsilon ) { super ( ) ; this . decimals = - Math . log10 ( epsilon ) ; } getDefaultPlanPath ( ) { return AbstractPlanExporter . replaceExtension ( this . happeningsDocument . uri . fsPath , '.plan' ) ; } makespan = 0 ; getPlanText ( ) { const happeningsText = this . happeningsDocument . getText ( ) ; const happeningsParser = new parser . PlanHappeningsBuilder ( this . epsilon ) ; happeningsParser . tryParseFile ( happeningsText ) ; const happenings = happeningsParser . getHappenings ( ) ; let planSteps = [ ] ; happenings . forEach ( ( happening , happeningIndex ) => { switch ( happening . getType ( ) ) { case HappeningType . INSTANTANEOUS : planSteps . push ( new PlanStep ( happening . getTime ( ) , happening . getFullActionName ( ) , false , undefined , - 1 ) ) ; break ; case HappeningType . START : const end = this . findEnd ( happenings , happening , happeningIndex + 1 ) ; const duration = end === undefined ? 0 : end . getTime ( ) - happening . getTime ( ) ; planSteps . push ( new PlanStep ( happening . getTime ( ) , happening . getFullActionName ( ) , true , duration , - 1 ) ) ; break ; } } ) ; planSteps = planSteps . sort ( this . comparePlanSteps ) ; const meta = parser . PddlPlanParser . parsePlanMeta ( happeningsText ) ; const endl = '\n' ; return parser . PddlPlanParser . getPlanMeta ( meta . domainName , meta . problemName , endl ) + endl + planSteps . map ( step => step . toPddl ( ) ) . join ( endl ) ; } findEnd ( happenings , start , fromIndex ) { return happenings . slice ( fromIndex ) . find ( h => h . belongsTo ( start ) ) ; } formatTime ( time ) { return Number ( time ) . toFixed ( this . decimals ) ; } comparePlanSteps ( h1 , h2 ) { if ( h1 . getStartTime ( ) > h2 . getStartTime ( ) ) { return 1 ; } else if ( h1 . getStartTime ( ) < h2 . getStartTime ( ) ) { return - 1 ; } return 0 ; } }