passing tests again, fixed selection behavior

This commit is contained in:
Sida Wang 2017-01-16 00:10:32 -08:00
parent 36eca0e73c
commit 887b2ab8c0
4 changed files with 59 additions and 38 deletions

View File

@ -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<Derivation> children, ActionFormula.Mode mode) {
// stop double blocking
if (children.size() == 1) {

View File

@ -106,8 +106,13 @@ public class ActionExecutor extends Executor {
} else if (f.mode == ActionFormula.Mode.forset) {
// mostly deprecated
Set<Object> selected = toSet(processSetFormula(f.args.get(0), world));
Set<Item> 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<Item> selected = toItemSet(toSet(processSetFormula(f.args.get(0), world)));
Set<Item> prevSelected = world.selected;

View File

@ -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<FlatWorld> 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<Item> real(Set<Item> 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();
}
}

View File

@ -36,6 +36,7 @@ public abstract class FlatWorld {
}
// general actions, flatness means these actions can be performed on allitems
public void remove(Set<Item> selected) {
allitems = new HashSet<>(allitems);
allitems.removeAll(selected);
// this.selected.removeAll(selected);
}