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

astra.reasoner.CartagoPropertyNode Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
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());
    }

    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy