/* pddl2pi.pi, by Neng-Fa Zhou, August 2014. convert directories of PDDL instances into Picat. Usage: picat pddl2pi The current directory must contain subdirectories of PDDL instances, and each PDDL instance file must have the extension name ".pddl". */ import os,util. main => List = listdir("."), foreach(D in List, D !== ".", D !== "..",directory(D)) println(D), PDDLInsL = [Ins : Ins in listdir(D), append(_,".pddl",Ins), Ins != "domain.pddl"], PicatD = to_lowercase(D), OutFD = open(PicatD++".pi",write), BenchL = [Ins1.replace('.','_').replace('-','_') : Ins in PDDLInsL,(append(Ins1,".pddl",Ins)->true;Ins1=Ins)], printf(OutFD,"all(Benches) => \n"), printf(OutFD," Benches=%w.\n\n",BenchL), foreach(Ins in PDDLInsL) println(Ins), convert(D++"/"++Ins,OutFD) end, close(OutFD) end. convert(InFile,OutFD) => (((append(_,['/'|InFile1],InFile);append(_,['\\'|InFile1],InFile)), not member('/',InFile1), not member('\\',InFile1)) -> true ; InFile1 = InFile ), InFile2 = [Char1 : Char in InFile1, ((Char=='.';Char=='-') -> Char1='_'; Char1=Char)], (append(MainInFile2,"_pddl",InFile2) -> true; MainInFile2=InFile2), InFD = open(InFile), read_tokens(InFD,Tokens0), hyphon_to_underscore(Tokens0,Tokens1), printf(OutFD,"%w =>%n",MainInFile2), parse_prob(OutFD,Tokens1), print(OutFD," pddl(Init,Goal).\n\n"). read_tokens(InFD,Tokens) => read_tokens(InFD,read_picat_token(InFD),Tokens). read_tokens(InFD,end_of_file,Tokens) => Tokens=[], close(InFD). read_tokens(InFD,';',Tokens) => Char = read_char(InFD), while (Char !== '\n', Char !== end_of_file) Char := read_char(InFD) end, read_picat_token(InFD) = NextToken, read_tokens(InFD,NextToken,Tokens). read_tokens(InFD,Token,Tokens) => Tokens=[Token|Tokens1], read_picat_token(InFD) = NextToken, read_tokens(InFD,NextToken,Tokens1). parse_prob(_OutFD,[]) => true. parse_prob(OutFD,['(',':','INIT'|Tokens]) => parse_prob(OutFD,['(',':',init|Tokens]). parse_prob(OutFD,['(',':',init|Tokens]) => print(OutFD," Init = $[\n"), parse_init(OutFD,Tokens,Tokens1), print(OutFD,"],\n"), parse_prob(OutFD,Tokens1). parse_prob(OutFD,['(',':','GOAL'|Tokens]) => parse_prob(OutFD,['(',':',goal|Tokens]). parse_prob(OutFD,['(',':',goal|Tokens]) => print(OutFD," Goal = $[\n"), parse_goal(OutFD,Tokens,_), print(OutFD,"],\n"). parse_prob(OutFD,[_|Tokens]) => parse_prob(OutFD,Tokens). parse_init(OutFD,['(',Name|Tokens],TokensR) => print(OutFD," "),write_name(OutFD,Name), parse_fact_first_arg(OutFD,Tokens,Tokens1), parse_init_rest(OutFD,Tokens1,TokensR). parse_init(_OutFD,Tokens,_TokensR) => throw($token_expected('(',Tokens)). parse_init_rest(_OutFD,[')'|Tokens],TokensR) => TokensR=Tokens. parse_init_rest(OutFD,['(',Name|Tokens],TokensR) => print(OutFD,','),nl(OutFD), print(OutFD," "),write_name(OutFD,Name), parse_fact_first_arg(OutFD,Tokens,Tokens1), parse_init_rest(OutFD,Tokens1,TokensR). parse_goal(OutFD,['(',and|Tokens],TokensR) => parse_goal(OutFD,Tokens,TokensR). parse_goal(OutFD,['(','AND'|Tokens],TokensR) => parse_goal(OutFD,Tokens,TokensR). parse_goal(OutFD,['(',Name|Tokens],TokensR) => print(OutFD," "),write_name(OutFD,Name), parse_fact_first_arg(OutFD,Tokens,Tokens1), parse_goal_rest(OutFD,Tokens1,TokensR). parse_goal_rest(_OutFD,[')'|Tokens],TokensR) => TokensR=Tokens. parse_goal_rest(OutFD,['(',and|Tokens],TokensR) => parse_goal_rest(OutFD,Tokens,TokensR). parse_goal_rest(OutFD,['(','AND'|Tokens],TokensR) => parse_goal_rest(OutFD,Tokens,TokensR). parse_goal_rest(OutFD,['(',Name|Tokens],TokensR) => print(OutFD,','),nl(OutFD), print(OutFD," "),write_name(OutFD,Name), parse_fact_first_arg(OutFD,Tokens,Tokens1), parse_goal_rest(OutFD,Tokens1,TokensR). parse_fact_first_arg(_OutFD,[')'|Tokens],TokensR) => TokensR = Tokens. parse_fact_first_arg(OutFD,['(',Arg,')'|Tokens],TokensR) => print(OutFD,'('), write_name(OutFD,Arg), parse_fact_args(OutFD,Tokens,TokensR). parse_fact_first_arg(OutFD,['(',Arg|Tokens],TokensR) => print(OutFD,'('), write_name(OutFD,Arg),print(OutFD,'('), parse_fact_first_arg(OutFD,Tokens,Tokens1), parse_fact_args(OutFD,Tokens1,TokensR). parse_fact_first_arg(OutFD,[Arg|Tokens],TokensR) => print(OutFD,'('), write_name(OutFD,Arg), parse_fact_args(OutFD,Tokens,TokensR). parse_fact_args(OutFD,[')'|Tokens],TokensR) => print(OutFD,")"), TokensR = Tokens. parse_fact_args(OutFD,[Arg|Tokens],TokensR) => print(OutFD,','), write_name(OutFD,Arg), parse_fact_args(OutFD,Tokens,TokensR). write_name(OutFD,Name),atom(Name) => atom_chars(Name) = Chars, printf(OutFD,"%s",Chars.to_lowercase()). write_name(OutFD,Name) => print(OutFD,Name). hyphon_to_underscore([A,'-',B,C|Tokens0],Tokens1),atom(A),integer(B),atom(C) => Str = (A.to_string()++"_"++B.to_string()++C.to_string()), hyphon_to_underscore([Str.to_atom()|Tokens0],Tokens1). hyphon_to_underscore([A,'-',B|Tokens0],Tokens1) => Str = (A.to_string()++"_"++B.to_string()), hyphon_to_underscore([Str.to_atom()|Tokens0],Tokens1). hyphon_to_underscore([A|Tokens0],Tokens1) => Tokens1=[A|Tokens2], hyphon_to_underscore(Tokens0,Tokens2). hyphon_to_underscore([],Tokens1) => Tokens1=[].