From 9bc5349e622cf00156b46f56a940d035e000115a Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Tue, 16 Dec 2014 10:21:26 +0000 Subject: change in the API for literals The API in the C runtime as well as in the Haskell, Python and Java binding is changed. Now instead of adding the literal callbacks to the concrete syntax you need to supply them every time when you need to parse. The main reason is: - referentially transparent API for Haskell - when we start using memory mapped files we will not be allowed to change anything in the grammar data structures. At that point the old API would be impossible to use. --- .../java/org/grammaticalframework/pgf/Concr.java | 13 +++------- .../org/grammaticalframework/pgf/ExprIterator.java | 4 ++-- .../java/org/grammaticalframework/pgf/Parser.java | 28 ++++++++++++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) (limited to 'src/runtime/java/org/grammaticalframework') diff --git a/src/runtime/java/org/grammaticalframework/pgf/Concr.java b/src/runtime/java/org/grammaticalframework/pgf/Concr.java index ca90c4466..b25a83a52 100644 --- a/src/runtime/java/org/grammaticalframework/pgf/Concr.java +++ b/src/runtime/java/org/grammaticalframework/pgf/Concr.java @@ -8,16 +8,11 @@ public class Concr { public native String getName(); public Iterable parse(String startCat, String s) throws ParseError { - return new Parser(this, startCat, s); + return new Parser(this, startCat, s, -1, null); } - public Expr parseBest(String startCat, String s) throws ParseError { - Iterator iter = Parser.parse(this, startCat, s); - if (iter.hasNext()) { - return iter.next().getExpr(); - } else { - return null; - } + public Iterable parseWithHeuristics(String startCat, String s, double heuristics, Map callbacks) throws ParseError { + return new Parser(this, startCat, s, heuristics, callbacks); } public Iterable complete(String startCat, String s, String prefix) throws ParseError { @@ -44,8 +39,6 @@ public class Concr { public native void unload(); - public native void addLiteral(String cat, LiteralCallback callback); - ////////////////////////////////////////////////////////////////// // private stuff diff --git a/src/runtime/java/org/grammaticalframework/pgf/ExprIterator.java b/src/runtime/java/org/grammaticalframework/pgf/ExprIterator.java index ffd8a45cb..c1504d9da 100644 --- a/src/runtime/java/org/grammaticalframework/pgf/ExprIterator.java +++ b/src/runtime/java/org/grammaticalframework/pgf/ExprIterator.java @@ -9,9 +9,9 @@ class ExprIterator implements Iterator { private ExprProb ep; private boolean fetched; - public ExprIterator(PGF gr, long pool, long out_pool, long ref) { + public ExprIterator(PGF gr, Pool pool, long out_pool, long ref) { this.gr = gr; - this.pool = new Pool(pool); + this.pool = pool; this.out_pool = new Pool(out_pool); this.ref = ref; this.ep = null; diff --git a/src/runtime/java/org/grammaticalframework/pgf/Parser.java b/src/runtime/java/org/grammaticalframework/pgf/Parser.java index 8892d423a..c8ec3663e 100644 --- a/src/runtime/java/org/grammaticalframework/pgf/Parser.java +++ b/src/runtime/java/org/grammaticalframework/pgf/Parser.java @@ -6,13 +6,19 @@ class Parser implements Iterable { private Concr concr; private String s; private String startCat; + private double heuristics; + private Map callbacks; private ExprIterator iter; - public Parser(Concr concr, String startCat, String s) throws ParseError { + public Parser(Concr concr, String startCat, String s, + double heuristics, + Map callbacks) throws ParseError { this.concr = concr; this.startCat = startCat; this.s = s; - this.iter = parse(concr, startCat, s); + this.heuristics = heuristics; + this.callbacks = callbacks; + this.iter = doParse(); } public Iterator iterator() { @@ -20,7 +26,7 @@ class Parser implements Iterable { // If someone has asked for a second iterator over // the same parse results then we have to parse again. try { - return parse(concr, startCat, s); + return doParse(); } catch (ParseError e) { return null; } @@ -31,5 +37,19 @@ class Parser implements Iterable { } } - static native ExprIterator parse(Concr concr, String startCat, String s) throws ParseError; + private ExprIterator doParse() throws ParseError + { + Pool pool = new Pool(); + long callbacksRef = newCallbacksMap(concr, pool); + for (Map.Entry entry : callbacks.entrySet()) { + addLiteralCallback(concr, callbacksRef, + entry.getKey(), entry.getValue(), + pool); + } + return parseWithHeuristics(concr, startCat, s, heuristics, callbacksRef, pool); + } + + static native long newCallbacksMap(Concr concr, Pool pool); + static native void addLiteralCallback(Concr concr, long callbacksRef, String cat, LiteralCallback callback, Pool pool); + static native ExprIterator parseWithHeuristics(Concr concr, String startCat, String s, double heuristics, long callbacksRef, Pool pool) throws ParseError; } -- cgit v1.2.3