import { CatalogEntry , Collection , Domain , Problem } from './CatalogEntry' ; import { getJson } from '../httpUtils' ; export class PlanningDomains { public static readonly AUTHORITY = "api.planning.domains" ; public static readonly URL = "https://api.planning.domains/json/classical/" ; private parseCollection ( collectionJson ) { return new Collection ( collectionJson [ "collection_id" ] , collectionJson [ "collection_name" ] , collectionJson [ "description" ] , JSON . parse ( collectionJson [ "domain_set" ] ) ) ; } async getCollections ( ) { const url = PlanningDomains . URL + "collections" ; const jsonOutput = await getJsonResult ( url ) ; return jsonOutput . map ( ( collectionJson ) => this . parseCollection ( collectionJson ) ) ; } private parseDomain ( domainJson ) { return new Domain ( domainJson [ "domain_id" ] , domainJson [ "domain_name" ] , domainJson [ "description" ] ) ; } async getDomains ( collection ) { const url = PlanningDomains . URL + "domains/" + collection . id ; const jsonOutput = await getJsonResult ( url ) ; return jsonOutput . map ( ( domainJson ) => this . parseDomain ( domainJson ) ) . sort ( compareCatalogEntry ) ; } private parseProblem ( problemJson ) { return new Problem ( problemJson [ "problem_id" ] , problemJson [ "problem" ] , problemJson [ "domain_url" ] , problemJson [ "problem_url" ] ) ; } async getProblems ( domain ) { const url = PlanningDomains . URL + "problems/" + domain . id ; const jsonOutput = await getJsonResult ( url ) ; return jsonOutput . map ( ( problemJson ) => this . parseProblem ( problemJson ) ) . sort ( compareCatalogEntry ) ; } } async function getJsonResult ( url ) { const response = await getJson ( url ) ; checkResponseForError ( response ) ; return response [ "result" ] ; } export function checkResponseForError ( response ) { if ( response [ "error" ] ) { throw new Error ( response [ "message" ] ) ; } } function compareCatalogEntry ( a , b ) { if ( a . label && b . label && a . label < b . label ) { return - 1 ; } else if ( a . label && b . label && a . label > b . label ) { return 1 ; } else { return 0 ; } }