|
From: <jom...@us...> - 2015-10-08 08:25:36
|
Revision: 1846
http://sourceforge.net/p/jason/svn/1846
Author: jomifred
Date: 2015-10-08 08:25:34 +0000 (Thu, 08 Oct 2015)
Log Message:
-----------
include internal action lower_case, upper_case, and include
Modified Paths:
--------------
trunk/release-notes.txt
trunk/src/jason/asSemantics/Agent.java
trunk/src/jason/asSyntax/directives/Include.java
trunk/src/jason/stdlib/package.html
Added Paths:
-----------
trunk/src/jason/stdlib/include.java
trunk/src/jason/stdlib/lower_case.java
trunk/src/jason/stdlib/upper_case.java
Modified: trunk/release-notes.txt
===================================================================
--- trunk/release-notes.txt 2015-09-24 15:08:01 UTC (rev 1845)
+++ trunk/release-notes.txt 2015-10-08 08:25:34 UTC (rev 1846)
@@ -20,8 +20,9 @@
- .asserta: inserts a belief (or rule) in the begin of the belief base (can be used in prolog like rules)
- .assertz: inserts a belief (or rule) in the end of the belief base (can be used in prolog like rules)
- .relevant_rules: gets rules with a specified head
+- .lower_case and upper_case for strings
+- .include: to load an asl source code at run time
-
New Tutorial on BDI (see doc/index.html)
---------------------------
Modified: trunk/src/jason/asSemantics/Agent.java
===================================================================
--- trunk/src/jason/asSemantics/Agent.java 2015-09-24 15:08:01 UTC (rev 1845)
+++ trunk/src/jason/asSemantics/Agent.java 2015-10-08 08:25:34 UTC (rev 1846)
@@ -173,8 +173,10 @@
initAg();
load(asSrc);
}
-
- /** parse and load the agent code, asSrc may be null */
+
+ private boolean initialLoadAlreadyDone = false;
+
+ /** parse and load the initial agent code, asSrc may be null */
public void load(String asSrc) throws JasonException {
// set the agent
try {
@@ -201,42 +203,52 @@
if (getPL().hasMetaEventPlans())
getTS().addGoalListener(new GoalListenerForMetaEvents(getTS()));
- addInitialBelsFromProjectInBB();
+ if (!initialLoadAlreadyDone) // to avoid loading it twice
+ addInitialBelsFromProjectInBB();
addInitialBelsInBB();
- addInitialGoalsFromProjectInBB();
+ if (!initialLoadAlreadyDone)
+ addInitialGoalsFromProjectInBB();
addInitialGoalsInTS();
fixAgInIAandFunctions(this); // used to fix agent reference in functions used inside includes
}
- // load kqml plans at the end of the ag PL
- Config c = Config.get();
- if (c.getKqmlPlansFile().equals(Message.kqmlDefaultPlans)) {
- // load default implementation
- // if KQML functor is not changed
- if (c.getKqmlFunctor().equals(Message.kqmlReceivedFunctor)) {
- String file = Message.kqmlDefaultPlans.substring(Message.kqmlDefaultPlans.indexOf("/"));
- if (JasonException.class.getResource(file) != null) {
- setASLSrc("kqmlPlans.asl");
- parseAS(JasonException.class.getResource(file));
- } else {
- logger.warning("The kqmlPlans.asl was not found!");
- }
- }
- } else {
- // load from specified file
- try {
- parseAS(new File(c.getKqmlPlansFile()));
- } catch (Exception e) {
- logger.warning("Error reading kqml semantic plans. "+e+". from file "+c.getKqmlPlansFile());
- }
- }
+ if (!initialLoadAlreadyDone) // to avoid loading it twice
+ loadKqmlPlans();
+
setASLSrc(asSrc);
+ initialLoadAlreadyDone = true;
} catch (Exception e) {
logger.log(Level.SEVERE, "Error creating customised Agent class!", e);
throw new JasonException("Error creating customised Agent class! - " + e);
}
}
+
+ public void loadKqmlPlans() {
+ // load kqml plans at the end of the ag PL
+ Config c = Config.get();
+ if (c.getKqmlPlansFile().equals(Message.kqmlDefaultPlans)) {
+ // load default implementation
+ // if KQML functor is not changed
+ if (c.getKqmlFunctor().equals(Message.kqmlReceivedFunctor)) {
+ String file = Message.kqmlDefaultPlans.substring(Message.kqmlDefaultPlans.indexOf("/"));
+ if (JasonException.class.getResource(file) != null) {
+ setASLSrc("kqmlPlans.asl");
+ parseAS(JasonException.class.getResource(file));
+ } else {
+ logger.warning("The kqmlPlans.asl was not found!");
+ }
+ }
+ } else {
+ // load from specified file
+ try {
+ parseAS(new File(c.getKqmlPlansFile()));
+ } catch (Exception e) {
+ logger.warning("Error reading kqml semantic plans. "+e+". from file "+c.getKqmlPlansFile());
+ }
+ }
+ }
+
/** @deprecated Prefer the initAg method with only the source code of the agent as parameter.
*
* A call of this method like
Modified: trunk/src/jason/asSyntax/directives/Include.java
===================================================================
--- trunk/src/jason/asSyntax/directives/Include.java 2015-09-24 15:08:01 UTC (rev 1845)
+++ trunk/src/jason/asSyntax/directives/Include.java 2015-10-08 08:25:34 UTC (rev 1846)
@@ -93,6 +93,10 @@
aslSourcePath = sp;
}
+ public List<String> getSourcePaths() {
+ return aslSourcePath;
+ }
+
/** fix path of the asl code based on aslSourcePath, also considers code from a jar file (if urlPrefix is not null) */
public static String checkPathAndFixWithSourcePath(String f, List<String> srcpath, String urlPrefix) {
if (urlPrefix == null || urlPrefix.length() == 0) {
Added: trunk/src/jason/stdlib/include.java
===================================================================
--- trunk/src/jason/stdlib/include.java (rev 0)
+++ trunk/src/jason/stdlib/include.java 2015-10-08 08:25:34 UTC (rev 1846)
@@ -0,0 +1,77 @@
+//----------------------------------------------------------------------------
+// Copyright (C) 2003 Rafael H. Bordini, Jomi F. Hubner, et al.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// To contact the authors:
+// http://www.inf.ufrgs.br/~bordini
+// http://www.das.ufsc.br/~jomi
+//
+//----------------------------------------------------------------------------
+
+package jason.stdlib;
+
+import jason.JasonException;
+import jason.asSemantics.Agent;
+import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSemantics.Unifier;
+import jason.asSyntax.StringTerm;
+import jason.asSyntax.Term;
+import jason.asSyntax.directives.DirectiveProcessor;
+import jason.asSyntax.directives.Include;
+
+/**
+ <p>Internal action: <b><code>.include</code></b>.
+
+ <p>Description: loads an .asl file, i.e., includes beliefs, goals, and plans from a file.
+
+ <p>Parameters:<ul>
+ <li>+ the file (string): the file name.<br/>
+ </ul>
+
+ <p>Examples:<ul>
+ <li> <code>.include("x.asl")</code>.</li>
+ </ul>
+
+ */
+public class include extends DefaultInternalAction {
+
+ @Override public int getMinArgs() { return 1; }
+ @Override public int getMaxArgs() { return 1; }
+
+ @Override protected void checkArguments(Term[] args) throws JasonException {
+ super.checkArguments(args); // check number of arguments
+ if (!args[0].isString())
+ throw JasonException.createWrongArgument(this,"first argument must be a string.");
+ }
+
+ @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
+ checkArguments(args);
+
+ Agent ag = ts.getAg();
+ String originalSrc = ag.getASLSrc(); // the source file that has the include directive
+ String file = ((StringTerm)args[0]).getString();
+
+ file = Include.checkPathAndFixWithSourcePath(
+ file,
+ ((Include)DirectiveProcessor.getDirective("include")).getSourcePaths(),
+ null);
+
+ ts.getAg().load( file );
+ ts.getAg().setASLSrc(originalSrc);
+ return true;
+ }
+}
Added: trunk/src/jason/stdlib/lower_case.java
===================================================================
--- trunk/src/jason/stdlib/lower_case.java (rev 0)
+++ trunk/src/jason/stdlib/lower_case.java 2015-10-08 08:25:34 UTC (rev 1846)
@@ -0,0 +1,57 @@
+
+package jason.stdlib;
+
+import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.InternalAction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSemantics.Unifier;
+import jason.asSyntax.StringTerm;
+import jason.asSyntax.StringTermImpl;
+import jason.asSyntax.Term;
+
+/**
+ <p>Internal action: <b><code>.lower_case(S1,S2)</code></b>.
+
+ <p>Description: converts the string S1 into lower case S2.
+
+ <p>Parameters:<ul>
+ <li>+ S1 (a term). The term representation as a string will be used.<br/>
+ <li>-/+ S2 (a string).<br/>
+ </ul>
+
+ <p>Examples:<ul>
+ <li> <code>.lower_case("CArtAgO",X)</code>: unifies X with "cartago".
+ </ul>
+
+ @see jason.stdlib.concat
+ @see jason.stdlib.delete
+ @see jason.stdlib.length
+ @see jason.stdlib.reverse
+
+*/
+public class lower_case extends DefaultInternalAction {
+
+ private static InternalAction singleton = null;
+ public static InternalAction create() {
+ if (singleton == null)
+ singleton = new lower_case();
+ return singleton;
+ }
+
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 2; }
+
+ @Override
+ public Object execute(TransitionSystem ts, final Unifier un, final Term[] args) throws Exception {
+ checkArguments(args);
+
+ String arg = null;
+ if (args[0].isString())
+ arg = ((StringTerm)args[0]).getString();
+ else
+ arg = args[0].toString();
+ arg = arg.toLowerCase();
+ return un.unifies(new StringTermImpl(arg), args[1]);
+ }
+}
+
Modified: trunk/src/jason/stdlib/package.html
===================================================================
--- trunk/src/jason/stdlib/package.html 2015-09-24 15:08:01 UTC (rev 1845)
+++ trunk/src/jason/stdlib/package.html 2015-10-08 08:25:34 UTC (rev 1846)
@@ -93,6 +93,8 @@
<li>{@link jason.stdlib.substring substring}: test substrings of strings. </li>
<li>{@link jason.stdlib.string string}: check whether an argument is a string.</li>
<li>{@link jason.stdlib.term2string term2string}: convert terms to strings and vice-versa.</li>
+ <li>{@link jason.stdlib.lower_case lower_case}: lower case strings.</li>
+ <li>{@link jason.stdlib.upper_case upper_case}: upper case strings.</li>
</ul>
<h2>Execution control</h2>
@@ -137,6 +139,8 @@
<li>{@link jason.stdlib.range range}: backtrack values in a range (used in <b>for</b>).</li>
<li>{@link jason.stdlib.random random}: produces random numbers.</li>
+
+ <li>{@link jason.stdlib.include include}: loads a source code at run time.</li>
</ul>
</body>
Added: trunk/src/jason/stdlib/upper_case.java
===================================================================
--- trunk/src/jason/stdlib/upper_case.java (rev 0)
+++ trunk/src/jason/stdlib/upper_case.java 2015-10-08 08:25:34 UTC (rev 1846)
@@ -0,0 +1,56 @@
+
+package jason.stdlib;
+
+import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.InternalAction;
+import jason.asSemantics.TransitionSystem;
+import jason.asSemantics.Unifier;
+import jason.asSyntax.StringTerm;
+import jason.asSyntax.StringTermImpl;
+import jason.asSyntax.Term;
+
+/**
+ <p>Internal action: <b><code>.upper_case(S1,S2)</code></b>.
+
+ <p>Description: converts the string S1 into upper case S2.
+
+ <p>Parameters:<ul>
+ <li>+ S1 (a term). The term representation as a string will be used.<br/>
+ <li>-/+ S2 (a string).<br/>
+ </ul>
+
+ <p>Examples:<ul>
+ <li> <code>.upper_case("CArtAgO",X)</code>: unifies X with "CARTAGO".
+ </ul>
+
+ @see jason.stdlib.concat
+ @see jason.stdlib.delete
+ @see jason.stdlib.length
+ @see jason.stdlib.reverse
+
+*/
+public class upper_case extends DefaultInternalAction {
+
+ private static InternalAction singleton = null;
+ public static InternalAction create() {
+ if (singleton == null)
+ singleton = new upper_case();
+ return singleton;
+ }
+
+ @Override public int getMinArgs() { return 2; }
+ @Override public int getMaxArgs() { return 2; }
+
+ @Override
+ public Object execute(TransitionSystem ts, final Unifier un, final Term[] args) throws Exception {
+ checkArguments(args);
+ String arg = null;
+ if (args[0].isString())
+ arg = ((StringTerm)args[0]).getString();
+ else
+ arg = args[0].toString();
+ arg = arg.toUpperCase();
+ return un.unifies(new StringTermImpl(arg), args[1]);
+ }
+}
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|