package be.iminds.iot.dianne.rl.command;

import java.util.HashMap;
import java.util.Map;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

import be.iminds.iot.dianne.api.nn.learn.Learner;
import be.iminds.iot.dianne.api.rl.Agent;

/**
 * Separate component for rl commands ... should be moved to the command bundle later on
 */
@Component(
		service=Object.class,
		property={"osgi.command.scope=dianne",
				  "osgi.command.function=act",
				  "osgi.command.function=stopAct",
				  "osgi.command.function=learn",
				  "osgi.command.function=stopLearn"},
		immediate=true)
public class DianneRLCommands {

	private Agent agent;
	private Learner learner;
	
	public void act(String nnName, String environment){
		act(nnName, environment, null);
	}
	
	public void act(String nnName, String environment, String experiencePool){
		try {
			agent.act(nnName, environment, experiencePool, new HashMap<String, String>());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stopAct(){
		this.agent.stop();
	}
	
	public void learn(String nnName, String dataset, String tag){
		try {
			Map<String, String> config = new HashMap<String, String>();
			if(tag!=null){
				config.put("tag", tag);
			}
			learner.learn(nnName, dataset, config);
		} catch(Exception e){
			e.printStackTrace();
		}
	}

	public void learn(String nnName, String dataset){
		learn(nnName, dataset, null);
	}
	
	@Reference
	public void setAgent(Agent agent){
		this.agent = agent;
	}
	
	@Reference
	public void setLearner(Learner l){
		this.learner = l;
	}
}

@@ -35,16 +35,41 @@
 mmand;%0A%0A
+import java.util.Arrays;%0A
 import j
@@ -910,16 +910,38 @@
 encePool
+, String... properties
 )%7B%0A%09%09try
@@ -997,36 +997,31 @@
 ol, 
-new HashMap%3CString, String%3E(
+createConfig(properties
 ));%0A
@@ -1190,135 +1190,32 @@
 ing 
-tag)%7B%0A%09%09try %7B%0A%09%09%09Map%3CString, String%3E config = new HashMap%3CString, String%3E();%0A%09%09%09if(tag!=null)%7B%0A%09%09%09%09config.put(%22tag%22, tag);%0A%09%09%09%7D
+... properties)%7B%0A%09%09try %7B
 %0A%09%09%09
@@ -1246,21 +1246,39 @@
 taset, c
-onfig
+reateConfig(properties)
 );%0A%09%09%7D c
@@ -1412,24 +1412,320 @@
 null);%0A%09%7D%0A%09%0A
+%09private Map%3CString, String%3E createConfig(String%5B%5D properties)%7B%0A%09%09Map%3CString, String%3E config = new HashMap%3CString, String%3E();%0A%09%09for(String property : properties)%7B%0A%09%09%09String%5B%5D p = property.split(%22=%22);%0A%09%09%09if(p.length==2)%7B%0A%09%09%09%09config.put(p%5B0%5D.trim(), p%5B1%5D.trim());%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A%09%09return config;%0A%09%7D%0A%09%0A
 %09@Reference%0A

allow to pass any configuration parameters in commands