
jason.stdlib.setof 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.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.ListTerm;
import jason.asSyntax.ListTermImpl;
import jason.asSyntax.Literal;
import jason.asSyntax.LogicalFormula;
import jason.asSyntax.Term;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class setof extends DefaultInternalAction {
@Override public int getMinArgs() { return 3; }
@Override public int getMaxArgs() { return 3; }
@Override public Term[] prepareArguments(Literal body, Unifier un) {
return body.getTermsArray(); // we do not need to clone nor to apply for this internal action
}
@Override protected void checkArguments(Term[] args) throws JasonException {
super.checkArguments(args); // check number of arguments
if (! (args[1] instanceof LogicalFormula))
throw JasonException.createWrongArgument(this,"second argument must be a formula");
}
@Override
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
checkArguments(args);
Term var = args[0];
LogicalFormula logExpr = (LogicalFormula)args[1];
Set all = new TreeSet();
Iterator iu = logExpr.logicalConsequence(ts.getAg(), un);
while (iu.hasNext()) {
Unifier nu = iu.next();
Term vl = var.clone();
vl.apply(nu);
all.add(vl);
}
return un.unifies(args[2], setToList(all));
}
// copy the set to a new list
private ListTerm setToList(Set set) {
ListTerm result = new ListTermImpl();
ListTerm tail = result;
for (Term t: set)
tail = tail.append(t.clone());
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy