|
From: <jom...@us...> - 2008-07-10 15:52:27
|
Revision: 1350
http://jason.svn.sourceforge.net/jason/?rev=1350&view=rev
Author: jomifred
Date: 2008-07-10 08:52:25 -0700 (Thu, 10 Jul 2008)
Log Message:
-----------
add two new functions: sum and average
one new IA: term2string
Modified Paths:
--------------
trunk/applications/jason-team/src/team-os.xml
trunk/release-notes.txt
trunk/src/jason/asSyntax/directives/FunctionRegister.java
trunk/src/jason/stdlib/package.html
Added Paths:
-----------
trunk/src/jason/functions/Average.java
trunk/src/jason/functions/Sum.java
trunk/src/jason/stdlib/term2string.java
Modified: trunk/applications/jason-team/src/team-os.xml
===================================================================
--- trunk/applications/jason-team/src/team-os.xml 2008-07-06 18:04:48 UTC (rev 1349)
+++ trunk/applications/jason-team/src/team-os.xml 2008-07-10 15:52:25 UTC (rev 1350)
@@ -85,7 +85,7 @@
</goal>
<goal id="goto_near_unvisited" ds="go to the near unvisited location inside the area of the group" type="maintenance"/>
<goal id="share_seen_cows" ds="share seen cows with other agents in the scheme" type="maintenance"/>
- <goal id="follow_leader" ds="follows the leader of the scheme/group" type="maintenance"/>
+ <goal id="follow_leader" ds="follow the leader of the scheme/group" type="maintenance"/>
</plan>
</goal>
@@ -105,11 +105,12 @@
<goal id="herd_cows" >
<plan operator="parallel">
<goal id="recruit" ds="recruit more herdboys depending on the size of the cows cluster" type="maintenance"/>
- <goal id="release_boys" ds="if the number of agents too much, release some boyd" type="maintenance"/>
+ <goal id="release_boys" ds="if the group has
+ too much boys, release some" type="maintenance"/>
<goal id="define_formation" ds="compute the ideal location of each member of the group and share this information with them" type="maintenance"/>
<goal id="be_in_formation" ds="go to the place allocated to the agent in the formation" type="maintenance"/>
<goal id="share_seen_cows" ds="share seen cows with other agents in the scheme" type="maintenance"/>
- <goal id="change_to_exploring" ds="verify if necessary to finish the herding group and create the exploring groups" type="maintenance"/>
+ <goal id="change_to_exploring" ds="verify if it is necessary to finish the herding group and create the exploring group" type="maintenance"/>
</plan>
</goal>
Modified: trunk/release-notes.txt
===================================================================
--- trunk/release-notes.txt 2008-07-06 18:04:48 UTC (rev 1349)
+++ trunk/release-notes.txt 2008-07-10 15:52:25 UTC (rev 1350)
@@ -1,4 +1,18 @@
-------------
+version 1.1.3
+-------------
+
+New internal actions
+. .term2string: transform term into strings and vice-versa.
+
+
+New functions
+. .math.sum: sums a list of numbers
+. .math.average: returns the average of a list of numbers
+
+
+
+-------------
version 1.1.2
-------------
Modified: trunk/src/jason/asSyntax/directives/FunctionRegister.java
===================================================================
--- trunk/src/jason/asSyntax/directives/FunctionRegister.java 2008-07-06 18:04:48 UTC (rev 1349)
+++ trunk/src/jason/asSyntax/directives/FunctionRegister.java 2008-07-10 15:52:25 UTC (rev 1350)
@@ -6,12 +6,14 @@
import jason.asSyntax.Pred;
import jason.asSyntax.StringTerm;
import jason.functions.Abs;
+import jason.functions.Average;
import jason.functions.Length;
import jason.functions.Max;
import jason.functions.Min;
import jason.functions.Random;
import jason.functions.Round;
import jason.functions.Sqrt;
+import jason.functions.Sum;
import jason.functions.ceil;
import jason.functions.e;
import jason.functions.floor;
@@ -39,6 +41,8 @@
addFunction(Abs.class);
addFunction(Max.class);
addFunction(Min.class);
+ addFunction(Sum.class);
+ addFunction(Average.class);
addFunction(Length.class);
addFunction(Random.class);
addFunction(Round.class);
Added: trunk/src/jason/functions/Average.java
===================================================================
--- trunk/src/jason/functions/Average.java (rev 0)
+++ trunk/src/jason/functions/Average.java 2008-07-10 15:52:25 UTC (rev 1350)
@@ -0,0 +1,50 @@
+package jason.functions;
+
+import jason.JasonException;
+import jason.asSemantics.DefaultArithFunction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSyntax.ListTerm;
+import jason.asSyntax.NumberTerm;
+import jason.asSyntax.Term;
+
+/**
+<p>Function: <b><code>math.average(L)</code></b>: returns the average of all values of L.
+
+<p>Examples:<ul>
+<li> <code>math.average([1,3])</code>: returns 2.</li>
+<li> <code>math.average([])</code>: fail.</li>
+</ul>
+
+@author Jomi
+
+@see jason.functions.Min
+@see jason.functions.Max
+@see jason.functions.Sum
+
+*/
+public class Average extends DefaultArithFunction {
+
+ public String getName() {
+ return "math.average";
+ }
+
+ @Override
+ public double evaluate(TransitionSystem ts, Term[] args) throws Exception {
+ if (args[0].isList()) {
+ double sum = 0;
+ int n = 0;
+ for (Term t: (ListTerm)args[0])
+ if (t.isNumeric()) {
+ sum += ((NumberTerm)t).solve();
+ n++;
+ }
+ return sum / n;
+ }
+ throw new JasonException(getName()+" is not implemented for type '"+args[0]+"'.");
+ }
+
+ @Override
+ public boolean checkArity(int a) {
+ return a == 1;
+ }
+}
Added: trunk/src/jason/functions/Sum.java
===================================================================
--- trunk/src/jason/functions/Sum.java (rev 0)
+++ trunk/src/jason/functions/Sum.java 2008-07-10 15:52:25 UTC (rev 1350)
@@ -0,0 +1,48 @@
+package jason.functions;
+
+import jason.JasonException;
+import jason.asSemantics.DefaultArithFunction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSyntax.ListTerm;
+import jason.asSyntax.NumberTerm;
+import jason.asSyntax.Term;
+
+/**
+<p>Function: <b><code>math.sum(L)</code></b>: sums all values of L.
+
+<p>Examples:<ul>
+<li> <code>math.sum([1,3])</code>: returns 4.</li>
+<li> <code>math.sum([3,a,"s",5])</code>: returns 8.</li>
+<li> <code>math.sum([])</code>: returns 0.</li>
+</ul>
+
+@author Jomi
+
+@see jason.functions.Min
+@see jason.functions.Max
+@see jason.functions.Average
+
+*/
+public class Sum extends DefaultArithFunction {
+
+ public String getName() {
+ return "math.sum";
+ }
+
+ @Override
+ public double evaluate(TransitionSystem ts, Term[] args) throws Exception {
+ if (args[0].isList()) {
+ double sum = 0;
+ for (Term t: (ListTerm)args[0])
+ if (t.isNumeric())
+ sum += ((NumberTerm)t).solve();
+ return sum;
+ }
+ throw new JasonException(getName()+" is not implemented for type '"+args[0]+"'.");
+ }
+
+ @Override
+ public boolean checkArity(int a) {
+ return a == 1;
+ }
+}
Modified: trunk/src/jason/stdlib/package.html
===================================================================
--- trunk/src/jason/stdlib/package.html 2008-07-06 18:04:48 UTC (rev 1349)
+++ trunk/src/jason/stdlib/package.html 2008-07-10 15:52:25 UTC (rev 1350)
@@ -83,6 +83,7 @@
<li>{@link jason.stdlib.reverse}: reverse strings. </li>
<li>{@link jason.stdlib.substring}: test substrings of strings. </li>
<li>{@link jason.stdlib.string}: check whether an argument is a string.</li>
+ <li>{@link jason.stdlib.term2string}: convert terms to strings and vice-versa.</li>
</ul>
<h2>Execution control</h2>
Added: trunk/src/jason/stdlib/term2string.java
===================================================================
--- trunk/src/jason/stdlib/term2string.java (rev 0)
+++ trunk/src/jason/stdlib/term2string.java 2008-07-10 15:52:25 UTC (rev 1350)
@@ -0,0 +1,73 @@
+
+package jason.stdlib;
+
+import jason.JasonException;
+import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.InternalAction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSemantics.Unifier;
+import jason.asSyntax.DefaultTerm;
+import jason.asSyntax.StringTerm;
+import jason.asSyntax.StringTermImpl;
+import jason.asSyntax.Term;
+
+/**
+ <p>Internal action: <b><code>.term2string(T,S)</code></b>.
+
+ <p>Description: converts the term T into a string S and vice-versa.
+
+ <p>Parameters:<ul>
+ <li>-/+ T (any term).<br/>
+ <li>-/+ S (a string).<br/>
+ </ul>
+
+ <p>Examples:<ul>
+ <li> <code>.substring(b,"b")</code>: true.
+ <li> <code>.substring(b,X)</code>: unifies X to "b".
+ <li> <code>.substring(X,"b")</code>: unified X to b.
+ </ul>
+
+ @see jason.stdlib.concat
+ @see jason.stdlib.delete
+ @see jason.stdlib.length
+ @see jason.stdlib.reverse
+
+*/
+public class term2string extends DefaultInternalAction {
+
+ private static InternalAction singleton = null;
+ public static InternalAction create() {
+ if (singleton == null)
+ singleton = new term2string();
+ return singleton;
+ }
+
+ @Override
+ public Object execute(TransitionSystem ts, final Unifier un, final Term[] args) throws Exception {
+ try {
+ // case 1, no vars
+ if (!args[0].isVar() && args[1].isString()) {
+ return args[0].toString().equals( ((StringTerm)args[1]).getString() );
+ }
+
+ // case 2, second is var
+ if (!args[0].isVar() && args[1].isVar()) {
+ return un.unifies(new StringTermImpl(args[0].toString()), args[1]);
+ }
+
+ // case 3, first is var
+ if (args[0].isVar() && args[1].isString()) {
+ return un.unifies(args[0], DefaultTerm.parse( ((StringTerm)args[1]).getString() ));
+ }
+
+ throw new JasonException("invalid case of term2string");
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new JasonException("The internal action 'term2string' has not received two arguments.");
+ } catch (JasonException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new JasonException("Error in internal action 'term2string': " + e, e);
+ }
+ }
+}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|