From 887b2ab8c0377d938171af7f04b40b75d414b1c5 Mon Sep 17 00:00:00 2001 From: Sida Wang Date: Mon, 16 Jan 2017 00:10:32 -0800 Subject: [PATCH] passing tests again, fixed selection behavior --- src/edu/stanford/nlp/sempre/ILUtils.java | 3 +- .../interactive/actions/ActionExecutor.java | 5 ++ .../actions/ActionExecutorTest.java | 88 +++++++++++-------- .../sempre/interactive/actions/FlatWorld.java | 1 + 4 files changed, 59 insertions(+), 38 deletions(-) diff --git a/src/edu/stanford/nlp/sempre/ILUtils.java b/src/edu/stanford/nlp/sempre/ILUtils.java index c190cef..c87165f 100644 --- a/src/edu/stanford/nlp/sempre/ILUtils.java +++ b/src/edu/stanford/nlp/sempre/ILUtils.java @@ -252,7 +252,8 @@ public final class ILUtils { BlockFn b = new BlockFn(mode); b.init(LispTree.proto.parseFromString("(a block)")); return new Rule("$Action", Lists.newArrayList("$Action", "$Action"), b); - } + } + public static Derivation combine(List children, ActionFormula.Mode mode) { // stop double blocking if (children.size() == 1) { diff --git a/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutor.java b/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutor.java index 6868665..b7c7d1e 100644 --- a/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutor.java +++ b/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutor.java @@ -106,8 +106,13 @@ public class ActionExecutor extends Executor { } else if (f.mode == ActionFormula.Mode.forset) { // mostly deprecated Set selected = toSet(processSetFormula(f.args.get(0), world)); + Set prevSelected = world.selected; + world.selected = toItemSet(selected); performActions((ActionFormula)f.args.get(1), world); + + world.selected = prevSelected; + world.merge(); } else if (f.mode == ActionFormula.Mode.foreach) { Set selected = toItemSet(toSet(processSetFormula(f.args.get(0), world))); Set prevSelected = world.selected; diff --git a/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutorTest.java b/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutorTest.java index ef4cfdd..02ee44c 100644 --- a/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutorTest.java +++ b/src/edu/stanford/nlp/sempre/interactive/actions/ActionExecutorTest.java @@ -23,14 +23,14 @@ public class ActionExecutorTest { LogInfo.begin_track("formula: %s", formula); executor.opts.FlatWorldType = "BlocksWorld"; Executor.Response response = executor.execute(Formulas.fromLispTree(LispTree.proto.parseFromString(formula)), context); - + NaiveKnowledgeGraph graph = (NaiveKnowledgeGraph)context.graph; String wallString = ((StringValue)graph.triples.get(0).e1).value; String jsonStr = ((StringValue)response.value).value; LogInfo.logs("Start:\t%s", wallString); LogInfo.logs("Result:\t%s", jsonStr); LogInfo.end_track(); - + if (checker != null) { if (!checker.test(FlatWorld.fromContext("BlocksWorld", getContext(jsonStr)))) { LogInfo.end_track(); @@ -44,11 +44,11 @@ public class ActionExecutorTest { String strigify2 = Json.writeValueAsStringHard(blocks); // some parsing issue inside lisptree parser return ContextValue.fromString(String.format("(context (graph NaiveKnowledgeGraph ((string \"%s\") (name b) (name c))))", strigify2)); } - + public Predicate selectedSize(int n) { return x -> {LogInfo.logs("Got %d, expected %d", x.selected().size(), n); return x.selected().size()==n;}; } - + @Test public void testJoin() { String defaultBlocks = "[[1,1,1,\"Green\",[]],[1,2,1,\"Blue\",[]],[2,2,1,\"Red\",[]],[3,2,2,\"Yellow\",[]]]"; ContextValue context = getContext(defaultBlocks); @@ -67,27 +67,27 @@ public class ActionExecutorTest { } @Test public void testSpecialSets() { - String defaultBlocks = "[[1,1,1,\"Green\",[\"S\"]],[1,2,1,\"Blue\",[\"S\"]],[2,2,1,\"Red\",[\"S\"]],[2,2,2,\"Yellow\",[]]]"; - ContextValue context = getContext(defaultBlocks); - LogInfo.begin_track("testSpecial"); - runFormula(executor, "(: select nothing)", context, selectedSize(0)); - runFormula(executor, "(: select *)", context, selectedSize(4)); - runFormula(executor, "(: select this)", context, selectedSize(3)); - runFormula(executor, "(: select (and this nothing))", context, selectedSize(0)); - runFormula(executor, "(: select (not this))", context, selectedSize(1)); + String defaultBlocks = "[[1,1,1,\"Green\",[\"S\"]],[1,2,1,\"Blue\",[\"S\"]],[2,2,1,\"Red\",[\"S\"]],[2,2,2,\"Yellow\",[]]]"; + ContextValue context = getContext(defaultBlocks); + LogInfo.begin_track("testSpecial"); + runFormula(executor, "(: select nothing)", context, selectedSize(0)); + runFormula(executor, "(: select *)", context, selectedSize(4)); + runFormula(executor, "(: select this)", context, selectedSize(3)); + runFormula(executor, "(: select (and this nothing))", context, selectedSize(0)); + runFormula(executor, "(: select (not this))", context, selectedSize(1)); - LogInfo.end_track(); + LogInfo.end_track(); } - + @Test public void testMerge() { { - String defaultBlocks = "[[1,1,1,\"Green\",[]],[1,2,1,\"Blue\",[]],[2,2,1,\"Red\",[]],[2,2,2,\"Yellow\",[]]]"; - ContextValue context = getContext(defaultBlocks); - LogInfo.begin_track("testMerge"); - runFormula(executor, "(: select (or (color red) (color blue)))", context, selectedSize(2)); - runFormula(executor, "(: select (and (color red) (color blue)))", context, selectedSize(0)); - runFormula(executor, "(: select (not (color red)))", context, selectedSize(3)); - runFormula(executor, "(: select (not *))", context, selectedSize(0)); + String defaultBlocks = "[[1,1,1,\"Green\",[]],[1,2,1,\"Blue\",[]],[2,2,1,\"Red\",[]],[2,2,2,\"Yellow\",[]]]"; + ContextValue context = getContext(defaultBlocks); + LogInfo.begin_track("testMerge"); + runFormula(executor, "(: select (or (color red) (color blue)))", context, selectedSize(2)); + runFormula(executor, "(: select (and (color red) (color blue)))", context, selectedSize(0)); + runFormula(executor, "(: select (not (color red)))", context, selectedSize(3)); + runFormula(executor, "(: select (not *))", context, selectedSize(0)); } LogInfo.end_track(); } @@ -95,23 +95,37 @@ public class ActionExecutorTest { String defaultBlocks = "[[1,1,1,\"Green\",[]],[1,2,1,\"Blue\",[]],[2,2,1,\"Red\",[]],[2,2,3,\"Yellow\",[]]]"; ContextValue context = getContext(defaultBlocks); LogInfo.begin_track("testBasicActions"); - runFormula(executor, "(:s (: select *) (: remove))", context, x -> x.allitems.size() == 0); - runFormula(executor, "(:s (: select (row (number 1))) (: add red top) (: add red top))", context, x -> x.allitems.size() == 8); + runFormula(executor, "(:s (: select *) (: remove))", context, x -> real(x.allitems).size() == 0); + runFormula(executor, "(:s (: select (row (number 1))) (: add red top) (: add red top))", context, x -> real(x.allitems).size() == 8); runFormula(executor, "(:for * (: remove))", context, x -> real(x.allitems).size() == 0 ); runFormula(executor, "(:foreach * (: remove))", context, x -> real(x.allitems).size() == 0); runFormula(executor, "(:s (: select (or (color red) (color orange))) (: remove))", context, x -> real(x.allitems).size() == 3); runFormula(executor, "(:foreach (or (color red) (color orange)) (:loop (number 5) (: add red top)))", - context, x -> x.allitems.size() == 9); + context, x -> x.allitems.size() == 9); runFormula(executor, "(:for (or (color red) (color blue)) (:loop (number 5) (:s (: move left) (: move right) (: move left))))", context, null); - + LogInfo.end_track(); } - + private Set real(Set all) { return all.stream().filter(c -> ((Block)c).color != CubeColor.Fake).collect(Collectors.toSet()); } - + @Test public void testRemove() { + // this is a green stick + String defaultBlocks = "[[1,1,1,\"Green\",[]],[1,1,2,\"Green\",[\"S\"]],[1,1,3,\"Red\",[\"S\"]],[1,1,4,\"Green\",[]]]"; + ContextValue context = getContext(defaultBlocks); + LogInfo.begin_track("testMoreActions"); + runFormula(executor, "(: remove)", context, + x -> real(x.allitems).size() == 2 && x.selected.size() == 2 ); + runFormula(executor, "(:for * (: remove))", context, + x -> x.selected.size() == 2 && real(x.allitems).size() == 0); + runFormula(executor, "(:for (color green) (: remove))", context, + x -> x.selected.size() == 2 && real(x.allitems).size() == 1); + + LogInfo.end_track(); + } + @Test public void testMoreActions() { // this is a green stick String defaultBlocks = "[[1,1,1,\"Green\",[]],[1,1,2,\"Green\",[]],[1,1,3,\"Green\",[]],[1,1,4,\"Green\",[]]]"; @@ -121,13 +135,13 @@ public class ActionExecutorTest { x -> x.allitems.stream().allMatch(c -> ((Block)c).color == CubeColor.Fake) ); runFormula(executor, "(:for * (:for (call veryx bot) (:loop (number 2) (:s (: add red left) (: select (call adj top))))))", context, x -> x.allitems.size() == 6); - runFormula(executor, "(:s (:for * (: select)) (: select (call veryx bot selected)) (: remove selected) )", context, x -> real(x.allitems).size() == 3); + runFormula(executor, "(:s (: select *) (: select (call veryx bot selected)) (: remove selected) )", context, x -> real(x.allitems).size() == 3); // x -> x.selected().iterator().next().get("height") == new Integer(3) runFormula(executor, "(:loop (count (color green)) (: add red left *))", context, x -> x.allitems.size() == 20); - LogInfo.end_track(); + LogInfo.end_track(); } - + @Test public void troubleCases() { // this is a green stick String defaultBlocks = "[[1,1,1,\"Green\",[\"S\"]],[1,1,2,\"Green\",[]],[1,1,3,\"Green\",[]],[1,1,4,\"Green\",[]]]"; @@ -139,7 +153,7 @@ public class ActionExecutorTest { runFormula(executor, " (: select (call adj top this))", context, selectedSize(1)); LogInfo.end_track(); } - + @Test public void testFake() { // this is a green stick String defaultBlocks = "[[1,1,0,\"Fake\",[\"S\"]]]"; @@ -154,7 +168,7 @@ public class ActionExecutorTest { runFormula(executor, "(:loop (number 3) (: select (or this (call adj top this))))", context, selectedSize(4)); LogInfo.end_track(); } - + @Test public void testIsolation() { // this is a green stick String defaultBlocks = "[[1,1,1,\"Green\",[\"S\"]],[1,1,2,\"Green\",[]],[1,1,3,\"Green\",[]],[1,1,4,\"Green\",[]]]"; @@ -166,7 +180,7 @@ public class ActionExecutorTest { runFormula(executor, "(:s (:isolate this (:loop (number 5) (: add red top))) (: select (color red)))", context, selectedSize(5)); LogInfo.end_track(); } - + @Test public void testUpdate() { String defaultBlocks = "[[1,1,1,\"Green\",[\"S\"]],[1,1,2,\"Red\",[]],[1,1,3,\"Green\",[]],[1,1,4,\"Green\",[]]]"; ContextValue context = getContext(defaultBlocks); @@ -177,7 +191,7 @@ public class ActionExecutorTest { context, selectedSize(1)); LogInfo.end_track(); } - + @Test public void testScoping() { // this is a green stick String defaultBlocks = "[[1,1,1,\"Green\",[\"S\"]],[1,1,2,\"Green\",[]],[1,1,3,\"Green\",[]],[1,1,4,\"Green\",[]]]"; @@ -188,14 +202,14 @@ public class ActionExecutorTest { runFormula(executor, "(:blkr (: select (call adj top this)))", context, x -> (Integer)x.selected.iterator().next().get("height")==2); - + runFormula(executor, "(:s (:blk (: add red here ) (: select (call adj top this)) (: add red here )) (: select (color red)))", context, x -> (Integer)x.selected.size()==2); runFormula(executor, "(:s (:blkr (: add red here ) (: select (call adj top this)) (: add red here )) (: select (color red)))", context, x -> (Integer)x.selected.size()==2); LogInfo.end_track(); } - - + + } \ No newline at end of file diff --git a/src/edu/stanford/nlp/sempre/interactive/actions/FlatWorld.java b/src/edu/stanford/nlp/sempre/interactive/actions/FlatWorld.java index 9c54846..a0fb878 100644 --- a/src/edu/stanford/nlp/sempre/interactive/actions/FlatWorld.java +++ b/src/edu/stanford/nlp/sempre/interactive/actions/FlatWorld.java @@ -36,6 +36,7 @@ public abstract class FlatWorld { } // general actions, flatness means these actions can be performed on allitems public void remove(Set selected) { + allitems = new HashSet<>(allitems); allitems.removeAll(selected); // this.selected.removeAll(selected); }