Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 //----------------------------------------------------------------------------
 // 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;

Internal action: .count.

Description: counts the number of occurrences of a particular belief (pattern) in the agent's belief base.

Parameters:

  • + query (logical formula): the formula used to count literals in the belief base; is has the same syntax as the plan context.
  • +/- quantity (number): the number of occurrences of the belief.

Examples:

  • .count(a(2,_),N): counts the number of beliefs that unify with a(2,_); N unifies with this quantity.
  • .count((a(2,X)& X>10),N): counts the number of beliefs that unify with a(2,X) and X > 10; N unifies with this quantity.
  • .count(a(2,_),5): succeeds if the BB has exactly 5 beliefs that unify with a(2,_).

See also:
findall
jason.functions.Count function version
public class count extends DefaultInternalAction {
    @Override public int getMinArgs() { return 2; }
    @Override public int getMaxArgs() { return 2; }
    @Override protected void checkArguments(Term[] argsthrows JasonException {
        super.checkArguments(args); // check number of arguments
        if (!(args[0] instanceof LogicalFormula))
            throw JasonException.createWrongArgument(this,"first argument must be a formula");
    }
    @Override
    public Object execute(TransitionSystem tsUnifier unTerm[] argsthrows Exception {
        checkArguments(args);
        LogicalFormula logExpr = (LogicalFormula)args[0];
        int n = 0;
        Iterator<Unifieriu = logExpr.logicalConsequence(ts.getAg(), un);
        while (iu.hasNext()) {
            iu.next();
            n++;
        }
        return un.unifies(args[1], new NumberTermImpl(n));
    }
New to GrepCode? Check out our FAQ X