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

astra.statement.ModuleCall Maven / Gradle / Ivy

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

import astra.core.AbstractTask;
import astra.core.Intention;
import astra.formula.Predicate;
import astra.reasoner.util.ContextEvaluateVisitor;
import astra.reasoner.util.VariableVisitor;



public class ModuleCall extends AbstractStatement {
	String module;
	Predicate method;
	ModuleCallAdaptor adaptor;
	
	public ModuleCall(ModuleCallAdaptor adaptor) {
		this.adaptor = adaptor;
	}

	public ModuleCall(String module, String clazz, int[] data, Predicate method, ModuleCallAdaptor moduleCallAdaptor) {
		this.setLocation(clazz, data[0], data[1], data[2], data[3]);
		this.module = module;
		this.method = method;
		this.adaptor = moduleCallAdaptor;
	}

	public ModuleCall(String module, Predicate method, ModuleCallAdaptor moduleCallAdaptor) {
		this.module = module;
		this.method = method;
		this.adaptor = moduleCallAdaptor;
	}

	@Override
	public StatementHandler getStatementHandler() {
		return new AbstractStatementHandler() {
			int state = 0;
			AbstractTask task;
			Predicate action;
			
			@Override
			public boolean execute(Intention context) {
				switch (state) {
				case 0:
					task= new AbstractTask() {
						/**
						 *
						 */
						private static final long serialVersionUID = -7588983545629284558L;

						public void doTask() {
							// action = method.clone();
							// System.out.println("method: " + method);
							// System.out.println("method.clone: " + method.clone());
							action = (Predicate) method.clone().accept(new ContextEvaluateVisitor(context));
							// System.out.println("[" + context.agent.name() + "-modulecall] Action: " + action);
							VariableVisitor visitor = new VariableVisitor();
							action.accept(visitor);
							// if (context.agent.trace())
							// System.out.println("["+context.agent.name()+"-modulecall] variables: " + visitor.variables());
							executor.addUnboundVariables(visitor.variables());
							context.resetActionParams();
							try {
								// THIS IS THE PROBLEM - THE ADAPTOR IS CALLED
								// FROM HERE, AND THE CONTEXT IS RESUMED.
								// NEED A SUSPEND NOTIFY METHOD TO ALLOW AUTO ACTIONS
								// TO CIRCUMVENT THIS...
								// DEFAULT SHOULD BE TO NOT SKIP NOTIFICATION...
								
								// ALTERNATIVE IS TO SUSPEND THE ACTION (BLOCK) AND 
								// WAIT TO BE NOTIFIED
								// System.out.println("[" + ModuleCall.class.getCanonicalName()+"] about to perform: " + method);
								// System.out.println("[" + ModuleCall.class.getCanonicalName()+"] about to perform: " + action);
								// System.out.println("[" + ModuleCall.class.getCanonicalName()+"] bindings: " + context.bindings());
								if (!adaptor.invoke(context, action)) {
									context.notifyDone("Failed Action: " + module + "." + action);
								}
								// System.out.println("[" + ModuleCall.class.getCanonicalName()+"] done performing: " + method);
								// System.out.println("[" + ModuleCall.class.getCanonicalName()+"] done performing: " + action);
								
								context.applyActionParams();
								if (!adaptor.suppressNotification()) context.notifyDone(null);
							} catch (Throwable th) {
								context.notifyDone("Failed Action: " + module + "." + action, th);
							}
						}

						@Override
						public Object source() {
							return null;
						}
					};
					
					context.suspend();
					if (adaptor.inline()) {
						task.doTask();
						task = null;
						return false;
					} else {
						context.schedule(task);
						state = 1;
						return true;
					}

					// ALL ACTIONS ARE INLINE - DEFAULT TO RETURNING
					// false TO INDICATE THAT THE STATEMENT IS COMPLETED...
				case 1:
					task = null;
				}
				return false;
			}

			@Override
			public boolean onFail(Intention context) {
				return false;
			}
			
			public String toString() {
				return method.toString();
			}
			
			@Override
			public Statement statement() {
				return ModuleCall.this;
			}
		};
	}
	
	public String toString() {
		return module+"."+method.toString();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy