astra.core.BeliefStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of astra-interpreter Show documentation
Show all versions of astra-interpreter Show documentation
Core interpreter artifact for the ASTRA Language
package astra.core;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import astra.formula.Formula;
import astra.formula.Predicate;
import astra.reasoner.Queryable;
/**
* Container for storing beliefs.
*
* Beliefs are organised into lists of formulae that have the same predicate through the use of
* a Map.
*
* @author Rem
*
*/
public class BeliefStore {
private Map> store = new HashMap>();
int size = 0;
public boolean addBelief(Predicate belief) {
List list = store.get(belief.id());
if (list == null) {
list = new LinkedList();
store.put(belief.id(), list);
}
for (Formula element : list) {
if (element.equals(belief)) return false;
}
list.add(belief);
size++;
return true;
}
public boolean containsBelief(Predicate belief) {
List list = store.get(belief.id());
if (list == null) return false;
for (Formula element : list) {
if (element.equals(belief)) return true;
}
return false;
}
public boolean removeBelief(Predicate belief) {
List list = store.get(belief.id());
if (list == null) return false;
for (Formula element : list) {
if (element.equals(belief)) {
list.remove(element);
size--;
return true;
}
}
return false;
}
public List beliefs() {
List list = new LinkedList();
for (List l : store.values()) {
list.addAll(l);
}
return list;
}
public void clear() {
store.clear();
}
public void addMatchingBeliefs(Queue queue, Predicate formula) {
List list = store.get(formula.id());
if (list != null) queue.addAll(list);
}
public Iterator iterator(Formula formula) {
if (formula instanceof Predicate) {
List list = store.get(((Predicate) formula).id());
return list == null ? Queryable.EMPTY_LIST.iterator():list.iterator();
}
return Queryable.EMPTY_LIST.iterator();
}
public int size() {
return size;
}
}