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

astra.statement.BeliefUpdate Maven / Gradle / Ivy

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

import astra.core.Intention;
import astra.formula.Predicate;
import astra.reasoner.util.ContextEvaluateVisitor;
import astra.term.Term;

public class BeliefUpdate extends AbstractStatement {
	public static final char ADDITION = '+';
	public static final char DELETION = '-';
	
	Predicate predicate;
	char op;
	
	public BeliefUpdate(char op, Predicate predicate) {
		this.op = op;
		this.predicate = predicate;
	}

	public BeliefUpdate(char op, String clazz, int[] data, Predicate predicate) {
		setLocation(clazz, data[0], data[1], data[2], data[3]);
		this.op = op;
		this.predicate = predicate;
	}

	@Override
	public StatementHandler getStatementHandler() {
		return new AbstractStatementHandler() {

			@Override
			public boolean execute(Intention context) {
				ContextEvaluateVisitor visitor = new ContextEvaluateVisitor(context, true);

				// System.out.println("Adding/removing belief");
				// construct belief to be added...
				Term[] terms = new Term[predicate.size()];
				for (int i=0; i < terms.length; i++) {
					terms[i] = (Term) predicate.getTerm(i).accept(visitor);
				}
				
				Predicate belief = new Predicate(predicate.predicate(), terms);
				// System.out.println("belief: " + belief);
				// System.out.println("op: " + op);
				
				if (op == ADDITION) {
					context.addBelief(belief);
				} else if (op == DELETION) {
					// System.out.println("Removing...");
					context.removeBelief(belief);
				}
				
				return false;
			}

			@Override
			public boolean onFail(Intention context) {
				return false;
			}

			@Override
			public Statement statement() {
				return BeliefUpdate.this;
			}
			
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy