From 5c4a574ddfe23d9536b0ac372200a44506167e04 Mon Sep 17 00:00:00 2001
From: alandau <alandau@cs.stanford.edu>
Date: Sun, 10 Nov 2013 15:43:17 -0800
Subject: [PATCH] Fix a couple of bugs in the SimpleSentenceForm. Add a
 regression test.

---
 src/org/ggp/base/test/AllTests.java           | 14 ++++++----
 .../ggp/base/test/SimpleSentenceFormTest.java | 28 +++++++++++++++++++
 .../util/gdl/model/SimpleSentenceForm.java    |  3 +-
 3 files changed, 38 insertions(+), 7 deletions(-)
 create mode 100644 src/org/ggp/base/test/SimpleSentenceFormTest.java

diff --git a/src/org/ggp/base/test/AllTests.java b/src/org/ggp/base/test/AllTests.java
index de5c9a1cc..95c8493b1 100644
--- a/src/org/ggp/base/test/AllTests.java
+++ b/src/org/ggp/base/test/AllTests.java
@@ -4,12 +4,14 @@
 import org.junit.runners.Suite;
 
 @RunWith(Suite.class)
-@Suite.SuiteClasses({GdlCleanerTests.class,
-                     NoTabsInRulesheetsTest.class,
-                     ProverStateMachineTests.class,
-                     StaticValidationTests.class,
-                     GameParsingTests.class
+@Suite.SuiteClasses({
+	GameParsingTests.class,
+	GdlCleanerTests.class,
+	NoTabsInRulesheetsTest.class,
+	ProverStateMachineTests.class,
+	SimpleSentenceFormTest.class,
+	StaticValidationTests.class
                      })
 public class AllTests {
-    
+
 }
diff --git a/src/org/ggp/base/test/SimpleSentenceFormTest.java b/src/org/ggp/base/test/SimpleSentenceFormTest.java
new file mode 100644
index 000000000..4a680e95d
--- /dev/null
+++ b/src/org/ggp/base/test/SimpleSentenceFormTest.java
@@ -0,0 +1,28 @@
+package org.ggp.base.test;
+
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.ggp.base.util.gdl.GdlUtils;
+import org.ggp.base.util.gdl.factory.GdlFactory;
+import org.ggp.base.util.gdl.grammar.GdlPool;
+import org.ggp.base.util.gdl.grammar.GdlSentence;
+import org.ggp.base.util.gdl.grammar.GdlTerm;
+import org.ggp.base.util.gdl.model.SimpleSentenceForm;
+import org.junit.Test;
+
+public class SimpleSentenceFormTest {
+	@Test
+	public void testFunctionNesting() throws Exception {
+		GdlSentence sentence = (GdlSentence) GdlFactory.create("(does player (combine foo (bar b b)))");
+		SimpleSentenceForm form = SimpleSentenceForm.create(sentence);
+		Assert.assertEquals(GdlPool.DOES, form.getName());
+		Assert.assertEquals(4, form.getTupleSize());
+		Assert.assertTrue(form.matches(sentence));
+
+		List<GdlTerm> tuple = GdlUtils.getTupleFromSentence(sentence);
+		Assert.assertEquals(sentence,
+				form.getSentenceFromTuple(tuple));
+	}
+}
diff --git a/src/org/ggp/base/util/gdl/model/SimpleSentenceForm.java b/src/org/ggp/base/util/gdl/model/SimpleSentenceForm.java
index 69ceeecc6..73e26024b 100644
--- a/src/org/ggp/base/util/gdl/model/SimpleSentenceForm.java
+++ b/src/org/ggp/base/util/gdl/model/SimpleSentenceForm.java
@@ -57,6 +57,7 @@ private static SimpleSentenceForm create(GdlFunction function) {
 			if (term instanceof GdlFunction) {
 				SimpleSentenceForm functionForm = create((GdlFunction) term);
 				functions.put(i, functionForm);
+				tupleSize += functionForm.getTupleSize();
 			} else {
 				tupleSize++;
 			}
@@ -185,7 +186,7 @@ private GdlFunction getFunctionFromTuple(List<? extends GdlTerm> tuple,
 			Preconditions.checkArgument(!(term instanceof GdlFunction));
 			if (functions.containsKey(i)) {
 				SimpleSentenceForm functionForm = functions.get(i);
-				functionBody.add(getFunctionFromTuple(tuple, curIndex));
+				functionBody.add(functionForm.getFunctionFromTuple(tuple, curIndex));
 				curIndex += functionForm.getTupleSize();
 			} else {
 				functionBody.add(term);