jason.stdlib.concat Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jason Show documentation
Show all versions of jason Show documentation
Jason is a fully-fledged interpreter for an extended version of AgentSpeak, a BDI agent-oriented logic programming language.
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.ListTerm;
import jason.asSyntax.StringTerm;
import jason.asSyntax.StringTermImpl;
import jason.asSyntax.Term;
/**
Internal action: .concat
.
Description: concatenates strings or lists.
Parameters:
- + arg[0] ... + arg[n-1] (any term): the terms to be concatenated.
- +/- arg[n]: the result of the concatenation.
Parameters that are not string are concatenated using the toString method of
their class.
Examples:
-
.concat("a","b",X)
: X
unifies with "ab".
-
.concat("a","b","a")
: false.
-
.concat("a b",1,a,X)
: X
unifies with "a b1a".
-
.concat("a", "b", "c", "d", X)
: X
unifies with "abcd".
-
.concat([a,b,c],[d,e],[f,g],X)
: X
unifies with [a,b,c,d,e,f,g]
.
Note: this internal action does not implement backtrack. You if need backtrack, you can add
and use the following rules in your code:
concat([ ], L, L).
concat([H|T], L, [H|M]) :- concat(T, L, M).
@see jason.stdlib.delete
@see jason.stdlib.length
@see jason.stdlib.member
@see jason.stdlib.sort
@see jason.stdlib.substring
@see jason.stdlib.nth
@see jason.stdlib.max
@see jason.stdlib.min
@see jason.stdlib.reverse
@see jason.stdlib.difference
@see jason.stdlib.intersection
@see jason.stdlib.union
*/
public class concat extends DefaultInternalAction {
private static InternalAction singleton = null;
public static InternalAction create() {
if (singleton == null)
singleton = new concat();
return singleton;
}
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
if (args[0].isList()) {
// list concat
if (!args[args.length-1].isVar() && !args[args.length-1].isList()) {
throw new JasonException("Last argument of concat '"+args[args.length-1]+"'is not a list nor a variable.");
}
ListTerm result = (ListTerm)args[0].clone();
for (int i=1; i