All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jason.stdlib.length Maven / Gradle / Ivy

Go to download

Jason is a fully-fledged interpreter for an extended version of AgentSpeak, a BDI agent-oriented logic programming language.

The newest version!
package jason.stdlib;

import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.InternalAction;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.ListTerm;
import jason.asSyntax.NumberTerm;
import jason.asSyntax.NumberTermImpl;
import jason.asSyntax.StringTerm;
import jason.asSyntax.Term;

/**

  

Internal action: .length.

Description: gets the length of strings or lists.

Parameters:

  • + argument (string or list): the term whose length is to be determined.
  • +/- length (number).

Examples:

  • .length("abc",X): X unifies with 3.
  • .length([a,b],X): X unifies with 2.
  • .length("a",2): false.
@see jason.stdlib.concat @see jason.stdlib.delete @see jason.stdlib.max @see jason.stdlib.member @see jason.stdlib.min @see jason.stdlib.sort @see jason.stdlib.nth @see jason.stdlib.sort @see jason.stdlib.reverse @see jason.stdlib.difference @see jason.stdlib.intersection @see jason.stdlib.union @see jason.functions.Length function version */ public class length extends DefaultInternalAction { private static InternalAction singleton = null; public static InternalAction create() { if (singleton == null) singleton = new length(); return singleton; } @Override public int getMinArgs() { return 2; } @Override public int getMaxArgs() { return 2; } @Override public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { checkArguments(args); Term l1 = args[0]; Term l2 = args[1]; NumberTerm size = null; if (l1.isList()) { ListTerm lt = (ListTerm) l1; size = new NumberTermImpl(lt.size()); } else if (l1.isString()) { StringTerm st = (StringTerm) l1; size = new NumberTermImpl(st.getString().length()); } if (size != null) { return un.unifies(l2, size); } return false; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy