/* * Linearize.java * Copyright (C) 2004-2005, Bjorn Bringert (bringert@cs.chalmers.se) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package se.chalmers.cs.gf.oaa; import se.gu.ling.trindikit.oaa.common.*; import com.sri.oaa2.icl.*; import se.chalmers.cs.gf.GFException; import se.chalmers.cs.gf.abssyn.*; import se.chalmers.cs.gf.translate.*; import se.chalmers.cs.gf.util.Pair; import static se.chalmers.cs.gf.util.Pair.pair; import java.util.*; /** * Implements the linearization solvable. */ public class Linearize extends OAASolver { private static String solvableString = "linearize(Grammar,Lang,Tree,Text)"; private Translators translators; public Linearize(Translators translators) { super(solvableString); this.translators = translators; } public boolean solve(IclTerm goal, IclList params, IclList answers) { String grammar; IclTerm langTerm; IclTerm treeTerm; IclTerm result; try { grammar = ((IclStr)goal.getTerm(0)).toUnquotedString(); langTerm = goal.getTerm(1); treeTerm = goal.getTerm(2); result = goal.getTerm(3); } catch (ClassCastException e){ return false; } Tree tree = IclTermToTree.iclTermToTree(treeTerm); Translator translator = translators.getTranslator(grammar); if (translator == null) { System.err.println("Unknown grammar " + grammar); return false; } Collection> ps = linearize(translator, langTerm, tree); boolean success = false; for (Pair p : ps) { String lang = p.fst; String resultStr = p.snd; IclTerm linResult = new IclStr(resultStr); IclTerm r = Unifier.getInstance().unify(result, linResult); if (r != null) { System.err.println("Success: " + r); IclTerm ans = new IclStruct("linearize", new IclStr(grammar), new IclStr(lang), treeTerm, r); System.err.println("Adding answer " + ans); answers.add(ans); success = true; } } return success; } public static Collection> linearize(Translator translator, IclTerm langTerm, Tree tree) { try { if (langTerm.isStr()) { String lang = ((IclStr)langTerm).toUnquotedString(); System.err.println("Linearizing '" + tree + "' as " + lang); return Arrays.asList(pair(lang,translator.linearize(lang, tree))); } else if (langTerm.isVar()) { System.err.println("Linearizing '" + tree + "' as any language"); return translator.linearizeWithAll(tree); } else { System.err.println("Bad language " + langTerm); return new LinkedList>(); } } catch (GFException ex) { ex.printStackTrace(); return new LinkedList>(); } } }