/* * Translate.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 java.util.*; /** * Implements a translation solvable which includes input ranges in the output. */ public class TranslateWithRanges extends OAASolver { private static String solvableString = "translate_with_ranges(Grammar,FromLang,Input,ToLang,Output)"; private Translators translators; public TranslateWithRanges(Translators translators) { super(solvableString); this.translators = translators; } public boolean solve(IclTerm goal, IclList params, IclList answers) { String grammar; IclTerm fromLangTerm; String toLang; String input; IclTerm result; try { grammar = ((IclStr)goal.getTerm(0)).toUnquotedString(); fromLangTerm = goal.getTerm(1); input = ((IclStr)goal.getTerm(2)).toUnquotedString(); toLang = ((IclStr)goal.getTerm(3)).toUnquotedString(); result = goal.getTerm(4); } catch (ClassCastException e){ return false; } Translator translator = translators.getTranslator(grammar); if (translator == null) { System.err.println("Unknown grammar " + grammar); return false; } Collection> ps = Parse.parse(translator, fromLangTerm, input); boolean success = false; for (Pair p : ps) { try { String fromLang = p.fst; Tree tree = p.snd; String resultStr = translator.linearizeWithRanges(toLang, tree); IclTerm linResult = new IclStr(resultStr); IclTerm r = Unifier.getInstance().unify(result, linResult); if (r != null) { IclTerm ans = new IclStruct("translate_with_ranges", new IclStr(grammar), new IclStr(fromLang), new IclStr(input), new IclStr(toLang), r); System.err.println("Adding answer " + ans); answers.add(ans); success = true; } } catch (GFException ex) { ex.printStackTrace(); } } System.err.println("Returning " + success); return success; } }