| 
     
      
      
      From: <bo...@us...> - 2009-03-24 22:57:23
      
     
   | 
Revision: 1479
          http://jason.svn.sourceforge.net/jason/?rev=1479&view=rev
Author:   bordini
Date:     2009-03-24 22:57:14 +0000 (Tue, 24 Mar 2009)
Log Message:
-----------
Allowed empty list as suffix/prefix/sublist. Changed documentation to remove notes on empty list not being considered, and added these 3 internal actions in stdlib/package.html. Fixed StdLibTest.java to include [] as possible result in backtracking.
Modified Paths:
--------------
    trunk/examples/blocks-world/agent_g.asl
    trunk/src/jason/stdlib/package.html
    trunk/src/jason/stdlib/prefix.java
    trunk/src/jason/stdlib/sublist.java
    trunk/src/jason/stdlib/suffix.java
    trunk/src/test/StdLibTest.java
Modified: trunk/examples/blocks-world/agent_g.asl
===================================================================
--- trunk/examples/blocks-world/agent_g.asl	2009-03-24 11:47:30 UTC (rev 1478)
+++ trunk/examples/blocks-world/agent_g.asl	2009-03-24 22:57:14 UTC (rev 1479)
@@ -9,7 +9,7 @@
 
 allowed(X,Y) :- clear(X) & clear(Y) & not on(X,Y).
 
-// an ad-hoc implementation of the GOAL a_goal construct for this problem
+// an ad-hoc implementation of the GOAL "a_goal" construct for this problem
 a_goal(S,G) :- .member(S1,S) & .suffix(G,S1).
 
 /* Initial goals */
Modified: trunk/src/jason/stdlib/package.html
===================================================================
--- trunk/src/jason/stdlib/package.html	2009-03-24 11:47:30 UTC (rev 1478)
+++ trunk/src/jason/stdlib/package.html	2009-03-24 22:57:14 UTC (rev 1479)
@@ -69,6 +69,9 @@
   <li>{@link jason.stdlib.min min}: minimum value of a lists. </li>
   <li>{@link jason.stdlib.sort sort}: sort lists. </li>
   <li>{@link jason.stdlib.list list}: check whether an argument is a list.</li>
+  <li>{@link jason.stdlib.suffix suffix}: suffixes of a list. </li>
+  <li>{@link jason.stdlib.prefix prefix}: prefixes of a list. </li>
+  <li>{@link jason.stdlib.sublist sublist}: sublists of a list. </li>
 
   <li>{@link jason.stdlib.difference difference}: difference of sets. </li>
   <li>{@link jason.stdlib.intersection intersection}: intersection of sets. </li>
Modified: trunk/src/jason/stdlib/prefix.java
===================================================================
--- trunk/src/jason/stdlib/prefix.java	2009-03-24 11:47:30 UTC (rev 1478)
+++ trunk/src/jason/stdlib/prefix.java	2009-03-24 22:57:14 UTC (rev 1479)
@@ -7,9 +7,7 @@
 import jason.asSemantics.Unifier;
 import jason.asSyntax.ListTerm;
 import jason.asSyntax.Term;
-
 import java.util.Iterator;
-import java.util.List;
 
 /**
 
@@ -30,9 +28,9 @@
 
   <li> <code>.prefix([a],[a,b,c])</code>: true.</li>
   <li> <code>.prefix([b,c],[a,b,c])</code>: false.</li>
-  <li> <code>.prefix(X,[a,b,c])</code>: unifies X with any suffix of the list, i.e., [a,b,c], [a,b], and [a], in this order;
-                                        note that this is different from what its usual implementation in logic programming would result;
-                                        also, the empty list is not generated as a possibly prefix. </li>
+  <li> <code>.prefix(X,[a,b,c])</code>: unifies X with any prefix of the list, i.e., [a,b,c], [a,b], [a], and [] in this order;
+                                        note that this is different from what its usual implementation in logic programming would result,
+                                        where the various prefixes are returned in increasing lengths instead.</li>
 
   </ul>
 
@@ -43,6 +41,8 @@
   @see jason.stdlib.max
   @see jason.stdlib.min
   @see jason.stdlib.reverse
+  @see jason.stdlib.suffix
+  @see jason.stdlib.sublist
 
   @see jason.stdlib.difference
   @see jason.stdlib.intersection
@@ -51,7 +51,11 @@
 */
 public class prefix extends DefaultInternalAction {
     
-    private static InternalAction singleton = null;
+    /**
+	 * 
+	 */
+	private static final long serialVersionUID = -4736810884249871078L;
+	private static InternalAction singleton = null;
     public static InternalAction create() {
         if (singleton == null) 
             singleton = new prefix();
@@ -84,6 +88,7 @@
 		
         return new Iterator<Unifier>() {
             Unifier c = null; // the current response (which is an unifier)
+            boolean triedEmpty = false;
             
             public boolean hasNext() {
                 if (c == null) // the first call of hasNext should find the first response 
@@ -102,11 +107,18 @@
                 while (!list.isEmpty()) {
                     c = un.clone();
                     if (c.unifiesNoUndo(sublist, list.clone())) {
-    					((List)list).remove(list.size()-1);
+    					list.removeLast();
                         return; // found another sublist, c is the current response
 					}
-					((List)list).remove(list.size()-1);
+					list.removeLast();
                 }
+                if (!triedEmpty) {
+                	triedEmpty = true;
+                	c = un.clone();
+                    if (c.unifiesNoUndo(sublist, list.clone())) {
+                        return; // found another sublist, c is the current response
+					}                	
+                }
                 c = null; // no more sublists found 
             }
 
Modified: trunk/src/jason/stdlib/sublist.java
===================================================================
--- trunk/src/jason/stdlib/sublist.java	2009-03-24 11:47:30 UTC (rev 1478)
+++ trunk/src/jason/stdlib/sublist.java	2009-03-24 22:57:14 UTC (rev 1479)
@@ -7,9 +7,7 @@
 import jason.asSemantics.Unifier;
 import jason.asSyntax.ListTerm;
 import jason.asSyntax.Term;
-
 import java.util.Iterator;
-import java.util.List;
 
 /**
 
@@ -35,9 +33,8 @@
   <li> <code>.sublist([b,c],[a,b,c])</code>: true.</li>
   <li> <code>.sublist([d],[a,b,c])</code>: false.</li>
   <li> <code>.sublist([a,c],[a,b,c])</code>: false.</li>
-  <li> <code>.sublist(X,[a,b,c])</code>: unifies X with any suffix of the list, i.e., [a,b,c], [a,b], [a], [b,c], [b], [c] in this order;
-                                         note that this is not the order in which its usual implementation would return in logic programming;
-                                         also, the empty list is not generated as a possible sublist.</li>
+  <li> <code>.sublist(X,[a,b,c])</code>: unifies X with any sublist of the list, i.e., [a,b,c], [a,b], [a], [b,c], [b], [c], and [] in this order;
+                                         note that this is not the order in which its usual implementation would return in logic programming (see note on .prefix).</li>
 
   </ul>
 
@@ -48,6 +45,8 @@
   @see jason.stdlib.max
   @see jason.stdlib.min
   @see jason.stdlib.reverse
+  @see jason.stdlib.prefix
+  @see jason.stdlib.suffix
 
   @see jason.stdlib.difference
   @see jason.stdlib.intersection
@@ -56,7 +55,11 @@
 */
 public class sublist extends DefaultInternalAction {
     
-    private static InternalAction singleton = null;
+    /**
+	 * 
+	 */
+	private static final long serialVersionUID = -1725808189703510112L;
+	private static InternalAction singleton = null;
     public static InternalAction create() {
         if (singleton == null) 
             singleton = new sublist();
@@ -90,6 +93,7 @@
         return new Iterator<Unifier>() {
             Unifier c = null; // the current response (which is an unifier)
             ListTerm list = listOutter.cloneLT();  // used in the inner loop
+            boolean triedEmpty = false;
             
             public boolean hasNext() {
                 if (c == null) // the first call of hasNext should find the first response 
@@ -109,14 +113,21 @@
                 	while (!list.isEmpty()) {
                 		c = un.clone();
                 		if (c.unifiesNoUndo(sublist, list.clone())) {
-                			((List)list).remove(list.size()-1);
+                			list.remove(list.size()-1);
                 			return; // found another sublist, c is the current response
                 		}
-                		((List)list).remove(list.size()-1);
+                		list.remove(list.size()-1);
                 	}
-            		((List)listOutter).remove(0);
+            		listOutter.remove(0);
             		list = listOutter.cloneLT();
                 }
+                if (!triedEmpty) {
+                	triedEmpty = true;
+                	c = un.clone();
+                    if (c.unifiesNoUndo(sublist, list.clone())) {
+                        return; // found another sublist, c is the current response
+					}                	
+                }
                 c = null; // no more sublists found 
             }
 
Modified: trunk/src/jason/stdlib/suffix.java
===================================================================
--- trunk/src/jason/stdlib/suffix.java	2009-03-24 11:47:30 UTC (rev 1478)
+++ trunk/src/jason/stdlib/suffix.java	2009-03-24 22:57:14 UTC (rev 1479)
@@ -7,9 +7,7 @@
 import jason.asSemantics.Unifier;
 import jason.asSyntax.ListTerm;
 import jason.asSyntax.Term;
-
 import java.util.Iterator;
-import java.util.List;
 
 /**
 
@@ -30,8 +28,7 @@
 
   <li> <code>.suffix([c],[a,b,c])</code>: true.</li>
   <li> <code>.suffix([a,b],[a,b,c])</code>: false.</li>
-  <li> <code>.suffix(X,[a,b,c])</code>: unifies X with any suffix of the list, i.e., [a,b,c], [b,c], and [c], in this order;
-                                        note that the empty list is not returned as a possible suffix.</li>
+  <li> <code>.suffix(X,[a,b,c])</code>: unifies X with any suffix of the list, i.e., [a,b,c], [b,c], [c], and [] in this order.</li>
 
   </ul>
 
@@ -42,6 +39,8 @@
   @see jason.stdlib.max
   @see jason.stdlib.min
   @see jason.stdlib.reverse
+  @see jason.stdlib.prefix
+  @see jason.stdlib.sublist
 
   @see jason.stdlib.difference
   @see jason.stdlib.intersection
@@ -50,7 +49,11 @@
 */
 public class suffix extends DefaultInternalAction {
     
-    private static InternalAction singleton = null;
+    /**
+	 * 
+	 */
+	private static final long serialVersionUID = 2463927564326061873L;
+	private static InternalAction singleton = null;
     public static InternalAction create() {
         if (singleton == null) 
             singleton = new suffix();
@@ -83,7 +86,7 @@
 		
         return new Iterator<Unifier>() {
             Unifier c = null; // the current response (which is an unifier)
-            
+            boolean triedEmpty = false;
             public boolean hasNext() {
                 if (c == null) // the first call of hasNext should find the first response 
                     find();
@@ -101,11 +104,18 @@
                 while (!list.isEmpty()) {
                     c = un.clone();
                     if (c.unifiesNoUndo(sublist, list.clone())) {
-						((List)list).remove(0);
+						list.remove(0);
                         return; // found another sublist, c is the current response
 					}
-					((List)list).remove(0);
+					list.remove(0);
                 }
+                if (!triedEmpty) {
+                	triedEmpty = true;
+                	c = un.clone();
+                    if (c.unifiesNoUndo(sublist, list.clone())) {
+                        return; // found another sublist, c is the current response
+					}                	
+                }
                 c = null; // no more sublists found 
             }
 
Modified: trunk/src/test/StdLibTest.java
===================================================================
--- trunk/src/test/StdLibTest.java	2009-03-24 11:47:30 UTC (rev 1478)
+++ trunk/src/test/StdLibTest.java	2009-03-24 22:57:14 UTC (rev 1479)
@@ -379,11 +379,12 @@
         Term tx = ASSyntax.parseTerm("X");
         u = new Unifier();
         i = (Iterator<Unifier>)new jason.stdlib.prefix().execute(null, u, new Term[] { tx, l1 });
-        assertTrue(iteratorSize(i) == 3);
+        assertTrue(iteratorSize(i) == 4);
         i = (Iterator<Unifier>)new jason.stdlib.prefix().execute(null, u, new Term[] { tx, l1 });
         assertEquals(i.next().get("X").toString(),"[a,b,c]");
         assertEquals(i.next().get("X").toString(),"[a,b]");
         assertEquals(i.next().get("X").toString(),"[a]");
+        assertEquals(i.next().get("X").toString(),"[]");
         assertFalse(i.hasNext());
 
     }
@@ -429,11 +430,12 @@
         Term tx = ASSyntax.parseTerm("X");
         u = new Unifier();
         i = (Iterator<Unifier>)new jason.stdlib.suffix().execute(null, u, new Term[] { tx, l1 });
-        assertTrue(iteratorSize(i) == 3);
+        assertTrue(iteratorSize(i) == 4);
         i = (Iterator<Unifier>)new jason.stdlib.suffix().execute(null, u, new Term[] { tx, l1 });
         assertEquals(i.next().get("X").toString(),"[a,b,c]");
         assertEquals(i.next().get("X").toString(),"[b,c]");
         assertEquals(i.next().get("X").toString(),"[c]");
+        assertEquals(i.next().get("X").toString(),"[]");
         assertFalse(i.hasNext());
 
     }
@@ -530,7 +532,7 @@
         Term tx = ASSyntax.parseTerm("X");
         u = new Unifier();
         i = (Iterator<Unifier>)new jason.stdlib.sublist().execute(null, u, new Term[] { tx, l1 });
-        assertTrue(iteratorSize(i) == 6);
+        assertTrue(iteratorSize(i) == 7);
         i = (Iterator<Unifier>)new jason.stdlib.sublist().execute(null, u, new Term[] { tx, l1 });
         assertEquals(i.next().get("X").toString(),"[a,b,c]");
         assertEquals(i.next().get("X").toString(),"[a,b]");
@@ -538,6 +540,7 @@
         assertEquals(i.next().get("X").toString(),"[b,c]");
         assertEquals(i.next().get("X").toString(),"[b]");
         assertEquals(i.next().get("X").toString(),"[c]");
+        assertEquals(i.next().get("X").toString(),"[]");
         assertFalse(i.hasNext());
 
     }
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |