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

astra.reasoner.node.PredicateReasonerNode Maven / Gradle / Ivy

There is a newer version: 1.4.2
Show newest version
package astra.reasoner.node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import astra.formula.Formula;
import astra.formula.Inference;
import astra.formula.Predicate;
import astra.reasoner.BeliefIterator;
import astra.reasoner.NewReasoner;
import astra.reasoner.Reasoner;
import astra.reasoner.Unifier;
import astra.reasoner.util.BindingsEvaluateVisitor;
import astra.reasoner.util.RenameVisitor;
import astra.reasoner.util.Utilities;
import astra.reasoner.util.VariableVisitor;
import astra.term.Term;

public class PredicateReasonerNode extends ReasonerNode {
    Predicate predicate;
    List options = new ArrayList();
    BeliefIterator beliefIterator;
    int index = 0;
    static int counter = 0;
    int state = 0;
    ReasonerNode node;
    VariableVisitor variableVisitor;
    Map bindings;
    int successes = 0;

    public PredicateReasonerNode(ReasonerNode parent, Predicate predicate, Map initial, boolean singleResult) {
        super(parent, singleResult);

        this.predicate = predicate;
        this.initial = initial;
    }

    @Override
    public ReasonerNode initialize(Reasoner reasoner) {
		visitor = new BindingsEvaluateVisitor(initial, reasoner.agent());
        predicate = (Predicate) predicate.accept(visitor);
        // System.out.println("Predicate: " + predicate);

        beliefIterator = new BeliefIterator(reasoner.sources(), predicate);

        return super.initialize(reasoner);
    }

    @Override
    public boolean solve(Reasoner reasoner, Stack stack) {
        if (predicate.equals(Predicate.FALSE)) {
            finished = true;
            failed=true;
            return false;
        }
        
		if (predicate.equals(Predicate.TRUE)) {
            solutions.add(initial);
            finished = true;
			return true;
		}

       switch (state) {
            case 0:
                if (!beliefIterator.hasNext()) {
                    finished = true;
                    // System.out.println("Finished [" + predicate+"," + (successes > 0) + "]: " + solutions);
                    if (successes == 0) {
                        failed = true;
                        return false;
                    }
                    return true;
                    // return !(failed = solutions.isEmpty());
                }

                Formula formula = beliefIterator.next();
                // System.out.println("formula:"  + formula);
                if (Predicate.class.isInstance(formula)) {
                    Map bindings = Unifier.unify(predicate, (Predicate) formula.accept(visitor), new HashMap(initial), reasoner.agent());
                    if (bindings == null) return true;

                    // System.out.println("[Predicate: " + predicate+ "] binding: " + bindings);
                    successes++;
                    solutions.add(bindings);
                    if (singleResult) finished = true;
                    return true;
                } else if (Inference.class.isInstance(formula)) {
                    // System.out.println("matching inference");
                    Inference inference = (Inference) formula;
                    RenameVisitor renameVisitor = new RenameVisitor("rn_" + (counter++) + "_");
                    // System.out.println("Inference: " + inference.body());
                    inference = (Inference) inference.accept(renameVisitor);
                    // System.out.println("`Renamed Inference: " + inference);

                    bindings = Unifier.unify(predicate, inference.head(), new HashMap(initial), reasoner.agent());
                    // System.out.println("Bindings: " + bindings);
                    if (bindings == null) return true;

                    // get the core variables from the predicate
                    variableVisitor = new VariableVisitor();
                    predicate.accept(variableVisitor);
                    // System.out.println(variableVisitor.variables());

                    // Apply the bindings to the body and apply resolution...
                    Formula target = (Formula) inference.body().accept(new BindingsEvaluateVisitor(bindings, reasoner.agent()));
                    // System.out.println("target:  " + target);
                    node = ((NewReasoner) reasoner).createReasonerNode(this, target, initial, true);
                    // System.out.println("Adding: " + node);
                    stack.add(node);
                    // System.out.println("Added: " + target);
                    state = 1;
                    return true;
                }
                break;
            case 1:
                // System.out.println("Failed? " + node.isFailed());
                if (node.isFailed()) {
                    // if it failed, try the next one...
                    state = 0;
                    return true;
                }

                successes++;
                for (Map b : node.solutions) {
                    // System.out.println("b=" + b);
                    // System.out.println("bindings: " + bindings);
                    solutions.add(Utilities.merge(initial, Utilities.filter(Utilities.mgu(Utilities.merge(bindings, b)), variableVisitor.variables())));
                    if (singleResult) {
                        finished = true;
                        return true;
                    }                            
                }
                state = 0;
                return true;
        }

        return true;
    }
    
    public String toString() {
        return predicate.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy