package planner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import fluentSolver.Fluent;
import fluentSolver.FluentNetworkSolver;
import htn.TaskApplicationMetaConstraint.markings;
import hybridDomainParsing.HybridDomain;

public class DynamicProblem implements CHIMPProblem {
	
	private class FluentPrototype {
		
		public FluentPrototype(String p, String[] a) {
			this.predicate = p;
			this.args = a;
		}
		
		String predicate;
		String[] args;
	}
	
	private final Set<String> argumentSymbols = new HashSet<String>();
	private final Map<String, String[]> typesInstancesMap = new HashMap<String, String[]>();
	
	private final List<FluentPrototype> stateVars = new ArrayList<FluentPrototype>();
	private final List<FluentPrototype> taskVars = new ArrayList<FluentPrototype>();

	private boolean createdState = false;
	
	public void addArgumentSymbol(String symbol) {
		argumentSymbols.add(symbol);
	}
	
	public void setTypesInstances(String type, String[] instances) {
		typesInstancesMap.put(type, instances);
	}
	
	public void addState(String predicate, String[] args) {
		stateVars.add(new FluentPrototype(predicate, args));
		for (String s : args) {
			argumentSymbols.add(s);
		}
	}
	
	public void addTask(String predicate, String[] args) {
		taskVars.add(new FluentPrototype(predicate, args));
		for (String s : args) {
			argumentSymbols.add(s);
		}
	}

	@Override
	public String[] getArgumentSymbols() {
		return argumentSymbols.toArray(new String[argumentSymbols.size()]);
	}

	@Override
	public Map<String, String[]> getTypesInstancesMap() {
		return typesInstancesMap;
	}

	@Override
	public void createState(FluentNetworkSolver fluentSolver, HybridDomain domain) {
		if (createdState) {
			throw new IllegalStateException("State has already been created.");
		}
		createdState = true;
		for (FluentPrototype p : stateVars) {

			Fluent fl = (Fluent) fluentSolver.createVariable();
			fl.setName(p.predicate, p.args);
			fl.setMarking(markings.OPEN);
		}
		
		for (FluentPrototype p : taskVars) {

			Fluent fl = (Fluent) fluentSolver.createVariable();
			fl.setName(p.predicate, p.args);
			fl.setMarking(markings.UNPLANNED);
		}
		
		// TODO Create AllenIntervals
		// TODO Create Ordering Constraints
		// TODO Create FluentResourceUsage constraints
		
		
	}

}

@@ -321,16 +321,107 @@
 omain;%0A%0A
+/**%0A * CHIMPProblem that is not based on a PDL file.%0A * %0A * @author Sebastian Stock%0A *%0A */%0A
 public c
@@ -1988,16 +1988,56 @@
 = true;%0A
+%09%09System.out.println(%22Creating state%22);%0A
 %09%09for (F
@@ -2199,52 +2199,243 @@
 ;%0A%09%09
-%7D%0A%09%09%0A%09%09for (FluentPrototype p : taskVars) %7B%0A
+%09System.out.println(%22Created state fluent %22 + fl.toString());%0A%09%09%7D%0A%09%09%0A%09%09for (FluentPrototype p : taskVars) %7B%0A%09%09%09String component;%0A%09%09%09if (p.predicate.startsWith(%22!%22)) %7B%0A%09%09%09%09component = %22Activity%22;%0A%09%09%09%7D else %7B%0A%09%09%09%09component = %22Task%22;%0A%09%09%09%7D
 %0A%09%09%09
@@ -2475,32 +2475,41 @@
 .createVariable(
+component
 );%0A%09%09%09fl.setName
@@ -2569,16 +2569,75 @@
 ANNED);%0A
+%09%09%09System.out.println(%22Created fluent: %22 + fl.toString());%0A
 %09%09%7D%0A%09%09%0A%09

Use component in DynamicProblem