astra.reasoner.CartagoPropertyNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of astra-cartago Show documentation
Show all versions of astra-cartago Show documentation
Base project for ASTRA applications
package astra.reasoner;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;
import astra.cartago.CartagoProperty;
import astra.formula.Formula;
import astra.reasoner.node.ReasonerNode;
import astra.reasoner.util.BindingsEvaluateVisitor;
import astra.term.Term;
public class CartagoPropertyNode extends ReasonerNode {
CartagoProperty property;
Queue options = new LinkedList<>();
int state = 0;
public CartagoPropertyNode(ReasonerNode parent, CartagoProperty property, Map initial, boolean singleResult) {
super(parent, singleResult);
this.property = property;
this.initial = initial;
}
@Override
public ReasonerNode initialize(Reasoner reasoner) {
visitor = new BindingsEvaluateVisitor(initial, reasoner.agent());
property = (CartagoProperty) property.accept(visitor);
for (Queryable source : reasoner.sources()) {
source.addMatchingFormulae(options, property);
}
return super.initialize(reasoner);
}
@Override
public boolean solve(Reasoner reasoner, Stack stack) {
while (!options.isEmpty()) {
Formula formula = options.poll();
Map bindings = Unifier.unify(property, (CartagoProperty) formula.accept(visitor), new HashMap(initial), reasoner.agent());
if (bindings != null) {
solutions.add(bindings);
if (singleResult) {
finished = true;
}
return true;
}
}
finished = true;
return !(failed = solutions.isEmpty());
}
}