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

net.sf.gluebooster.java.booster.basic.math.planning.Planning Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.math.planning;

import java.awt.Frame;
import java.util.Collection;
import java.util.Hashtable;

import net.sf.gluebooster.java.booster.basic.container.BoostedNode;
import net.sf.gluebooster.java.booster.basic.container.BoostedNodeUtils;
import net.sf.gluebooster.java.booster.basic.math.Operation;
import net.sf.gluebooster.java.booster.basic.math.graph.BoostedNodeGraph;
import net.sf.gluebooster.java.booster.basic.math.graph.GraphDisplayConfiguration;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.math.Condition;
import net.sf.gluebooster.java.booster.essentials.meta.HasName;
import net.sf.gluebooster.java.booster.essentials.objects.BoostedObject;

/**
 * Configuration of (one step of) the planning.
 *
 */
public class Planning extends BoostedObject {

	/**
	 * The generated plan.
	 */
	private Plan plan;

	/**
	 * The environment of the planning.
	 */
	private PlannerEnv env;


	public Planning() {

	}

	public Planning(Plan plan, PlannerEnv env) {
		this.plan = plan;
		this.env = env;
	}

	/**
	 * Create a planning with a plan with one node
	 * 
	 * @param planWithOnlyOneOperation
	 *            the operation of the node
	 * @param world
	 *            the world of the planning
	 */
	public Planning(
			Operation planWithOnlyOneOperation,
			World world) throws Exception {
		plan = new Plan();
		plan.initialize(world, planWithOnlyOneOperation);
		setEnv(new PlannerEnv());
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

	public PlannerEnv getEnv() {
		return env;
	}

	public void setEnv(PlannerEnv env) {
		this.env = env;
	}

	/**
	 * Log some information at info level
	 * 
	 * @param objectsToLog
	 *            the information
	 */
	public void info(Object... objectsToLog) {
		getEnv().getPlanningLog().info(objectsToLog);
	}

	/**
	 * Log some information at debug level
	 * 
	 * @param objectsToLog
	 *            the information
	 */
	public void debug(Object... objectsToLog) {
		getEnv().getPlanningLog().info(objectsToLog);
	}

	/**
	 * Log some information at warn level
	 * 
	 * @param objectsToLog
	 *            the information
	 */
	public void warn(Object... objectsToLog) {
		getEnv().getPlanningLog().info(objectsToLog);
	}

	/**
	 * Log some information at error level
	 * 
	 * @param objectsToLog
	 *            the information
	 */
	public void error(Object... objectsToLog) {
		getEnv().getPlanningLog().info(objectsToLog);
	}

	/**
	 * Transforms an unsolved node of the plan into a subgraph and replaces the node by the subgraph.
	 * 
	 * @param unsolvedNodeOfPlan
	 *            the node to be transformed
	 * @param transformer
	 *            the transformer of the node
	 * @return true if something has been done
	 */
	public boolean transformNode(BoostedNode unsolvedNodeOfPlan,
			Callable transformer)
			throws Exception {
		
		plan.selfcheck();
		BoostedNodeGraph subgraph;
		subgraph = transformer.call(unsolvedNodeOfPlan, createHashtableWithPlan(plan));
		// plan.selfcheck();


		if (subgraph == null) {
			return false; // nothing has been done
		} else {
			// Check operations
			for (BoostedNode node : subgraph.getAllVertices()) {
				plan.getOperation(node).checkOperator();
			}

			// add preconditions of node to sources
			Operation unsolvedPlanNode = Operation
					.getOperation(unsolvedNodeOfPlan);
			Collection newSourceVertices = subgraph
					.getAllSourceVertices();
			if (newSourceVertices.isEmpty()) {
				// use the sucessors instead
				newSourceVertices = BoostedNodeUtils
						.getSuccessors(unsolvedNodeOfPlan);
			}

			plan.addPreconditionToNodes(newSourceVertices,
					unsolvedPlanNode.getPrecondition());
			plan.addGlobalPreconditionToNodes(newSourceVertices,
					unsolvedPlanNode.getGlobalPrecondition());

			plan.addDefaultConditionsOfOperators(subgraph);

			plan.replaceNode(unsolvedNodeOfPlan, subgraph);

			plan.selfcheck();
			return true;
		}

	}

	/**
	 * Instantiates a virtual object with a real object.
	 * 
	 * @param nodeWithVirtualObject
	 *            the node which virtual object is to be replaced
	 * @param virtualObject
	 *            the object to replace
	 * @param concreteObject
	 *            the replacement
	 */
	public void instantiateVirtualObject(BoostedNode nodeWithVirtualObject,
			HasName virtualObject, HasName concreteObject)
			throws Exception {

		getPlan().instantiateVirtualObject(plan.getGraph(),
				nodeWithVirtualObject,
				virtualObject,
				concreteObject);

	}

	/**
	 * Create a hashtable that contains a plan. This method must be coordinated with getPlanFromHashtable.
	 * 
	 * @param plan
	 *            the given plan
	 * @return the key of the plan is Plan.class
	 */
	public static Hashtable createHashtableWithPlan(Plan plan) {
		Hashtable environmentWithPlan = new Hashtable();
		environmentWithPlan.put(Plan.class, plan);
		return environmentWithPlan;
	}

	/**
	 * Extracts a plan from the hashtable. This method must be coordinated with createHashtableWithPlan.
	 * 
	 * @param env
	 *            must contain the plan with key Plan.class
	 * @return the found plan.
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static  Plan getPlanFromHashtable(
			Hashtable env) {
		return (Plan) env.get(Plan.class);
	}

	/**
	 * Remove superfluous operator nodes from the plan.
	 * 
	 * @return true if something has changed
	 */
	public boolean removeSuperfluousOperators() throws Exception {
		return getPlan().removeSuperfluousOperators();
	}

	/**
	 * Creates a simple display of this planning
	 * 
	 * @return a frame containing the display
	 */
	public Frame simpleDisplay() throws Exception {
		return plan.simpleDisplay();
	}

	/**
	 * 
	 * Creates a simple display of this planning
	 * 
	 * @param displayConfiguration
	 *            configuration of the display.
	 * @return a frame containing the display
	 */
	public Frame simpleDisplay(GraphDisplayConfiguration displayConfiguration) throws Exception {
		return plan.simpleDisplay(displayConfiguration);
	}
}