<commit_before>package org.pikater.core.agents.experiment.recommend;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAService;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREResponder;
import jade.util.leap.ArrayList;
import jade.util.leap.List;

import org.pikater.core.agents.PikaterAgent;
import org.pikater.core.agents.experiment.Agent_AbstractExperiment;
import org.pikater.core.agents.experiment.computing.Agent_ComputingAgent;
import org.pikater.core.agents.experiment.computing.Agent_WekaRBFNetworkCA;
import org.pikater.core.agents.system.data.DataManagerService;
import org.pikater.core.agents.system.managerAgent.ManagerAgentCommunicator;
import org.pikater.core.configuration.Arguments;
import org.pikater.core.ontology.AgentInfoOntology;
import org.pikater.core.ontology.AgentManagementOntology;
import org.pikater.core.ontology.MetadataOntology;
import org.pikater.core.ontology.RecommendOntology;
import org.pikater.core.ontology.subtrees.data.Datas;
import org.pikater.core.ontology.subtrees.metadata.GetMetadata;
import org.pikater.core.ontology.subtrees.metadata.Metadata;
import org.pikater.core.ontology.subtrees.newOption.NewOptions;
import org.pikater.core.ontology.subtrees.newOption.base.NewOption;
import org.pikater.core.ontology.subtrees.option.GetOptions;
import org.pikater.core.ontology.subtrees.recommend.Recommend;



public abstract class Agent_Recommender extends Agent_AbstractExperiment {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1314060594137998065L;

	protected abstract org.pikater.core.ontology.subtrees.management.Agent chooseBestAgent(Datas data);
	protected abstract String getAgentType();
	protected abstract boolean finished();
	protected abstract void updateFinished();
    
	public static Class<? extends Agent_ComputingAgent> DEFAULT_AGENT = Agent_WekaRBFNetworkCA.class;
	
	private org.pikater.core.ontology.subtrees.management.Agent myAgentOntology =
			new org.pikater.core.ontology.subtrees.management.Agent();
	


    @Override
	public java.util.List<Ontology> getOntologies() {
		
		java.util.List<Ontology> ontologies =
				new java.util.ArrayList<Ontology>();

		ontologies.add(RecommendOntology.getInstance());
		ontologies.add(MetadataOntology.getInstance());
		ontologies.add(AgentManagementOntology.getInstance());
		ontologies.add(AgentInfoOntology.getInstance());
		
		return ontologies;
	}
	
    @Override
    protected void setup() {
    	
        initDefault();
        
        registerWithDF("Recommender");
        
        Ontology ontology = RecommendOntology.getInstance();
        
        // receive request
        MessageTemplate mt = MessageTemplate.and(
        		MessageTemplate.MatchOntology(ontology.getName()),
        		MessageTemplate.MatchPerformative(ACLMessage.REQUEST));        
		addBehaviour(new RecommendBehaviour(this, mt));


		addAgentInfoBehaviour(getAgentInfo());

    }
         
	
	protected class RecommendBehaviour extends AchieveREResponder {

		/**
		 * 
		 */
		private static final long serialVersionUID = -8353926385111974474L;
		private PikaterAgent agent;
		
		public RecommendBehaviour(PikaterAgent agent, MessageTemplate mt) {
			super(agent, mt);
			this.agent = agent;
		}

        @Override
        protected ACLMessage handleRequest(ACLMessage request)
        	throws NotUnderstoodException, RefuseException {
        	
        	ACLMessage reply = request.createReply();
            
        	try {        		
        		Action a = (Action) getContentManager().extractContent(request);

                if (a.getAction() instanceof Recommend) {
 
					if (finished()){
						log("Recommendation finished.");
						reply.setPerformative(ACLMessage.INFORM);					
						reply.setContent("finished");
						return reply;
					}
                	
                	Recommend rec = (Recommend) a.getAction();
                    myAgentOntology = rec.getRecommender();
                    
                    // merge options with .opt file options
                    myAgentOntology.setOptions(getParameters());

                    log("options: " + NewOptions.exportToWeka(myAgentOntology.getOptions()), 2);

                    Datas datas = rec.getDatas();
                    
                    // Get metadata:
					Metadata metadata = null;    
					
					// if metatada are not yet in ontology		
					if (rec.getDatas().getMetadata() == null) {
						// or fetch them from database:
						GetMetadata gm = new GetMetadata();
						gm.setInternal_filename(rec.getDatas().exportInternalTestFileName());
						metadata = DataManagerService.getMetadata(agent, gm);
						datas.setMetadata(metadata);
					}                            			

					// else TODO - overit, jestli jsou metadata OK, pripadne vygenerovat
					
					org.pikater.core.ontology.subtrees.management.Agent recommended_agent = chooseBestAgent(rec.getDatas());
                    
					if(recommended_agent==null){
						reply.setPerformative(ACLMessage.FAILURE);
						return reply;
					}
					

					//TODO:
                    // fill options
                	//recommended_agent.setOptions(mergeOptions(recommended_agent.getOptions(), getAgentOptions(recommended_agent.getType()) ));

        			//log("********** Agent "
        				//	+ recommended_agent.getType()
        				//	+ " recommended. Options: "
        				//	+ NewOptions.exportToWeka(recommended_agent.getOptions())
        				//	+ "**********", Verbosity.MINIMAL);

            		// Prepare the content of inform message                       
    				Result result = new Result(a, recommended_agent);
					reply.setPerformative(ACLMessage.INFORM);					
    				
    				updateFinished();
    				<commit_msg>Agent_Recommender - options aggregation<commit_after>package org.pikater.core.agents.experiment.recommend;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.Ontology;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.content.onto.basic.Result;
import jade.core.AID;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.domain.FIPANames;
import jade.domain.FIPAService;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREResponder;
import jade.util.leap.ArrayList;
import jade.util.leap.List;

import org.pikater.core.agents.PikaterAgent;
import org.pikater.core.agents.experiment.Agent_AbstractExperiment;
import org.pikater.core.agents.experiment.computing.Agent_ComputingAgent;
import org.pikater.core.agents.experiment.computing.Agent_WekaRBFNetworkCA;
import org.pikater.core.agents.system.data.DataManagerService;
import org.pikater.core.agents.system.managerAgent.ManagerAgentCommunicator;
import org.pikater.core.configuration.Arguments;
import org.pikater.core.ontology.AgentInfoOntology;
import org.pikater.core.ontology.AgentManagementOntology;
import org.pikater.core.ontology.MetadataOntology;
import org.pikater.core.ontology.RecommendOntology;
import org.pikater.core.ontology.subtrees.agentInfo.AgentInfo;
import org.pikater.core.ontology.subtrees.data.Datas;
import org.pikater.core.ontology.subtrees.metadata.GetMetadata;
import org.pikater.core.ontology.subtrees.metadata.Metadata;
import org.pikater.core.ontology.subtrees.newOption.NewOptions;
import org.pikater.core.ontology.subtrees.newOption.base.NewOption;
import org.pikater.core.ontology.subtrees.option.GetOptions;
import org.pikater.core.ontology.subtrees.recommend.Recommend;



public abstract class Agent_Recommender extends Agent_AbstractExperiment {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1314060594137998065L;

	protected abstract org.pikater.core.ontology.subtrees.management.Agent chooseBestAgent(Datas data);
	protected abstract String getAgentType();
	protected abstract boolean finished();
	protected abstract void updateFinished();
    
	public static Class<? extends Agent_ComputingAgent> DEFAULT_AGENT = Agent_WekaRBFNetworkCA.class;
	
	private org.pikater.core.ontology.subtrees.management.Agent myAgentOntology =
			new org.pikater.core.ontology.subtrees.management.Agent();
	


    @Override
	public java.util.List<Ontology> getOntologies() {
		
		java.util.List<Ontology> ontologies =
				new java.util.ArrayList<Ontology>();

		ontologies.add(RecommendOntology.getInstance());
		ontologies.add(MetadataOntology.getInstance());
		ontologies.add(AgentManagementOntology.getInstance());
		ontologies.add(AgentInfoOntology.getInstance());
		
		return ontologies;
	}
	
    @Override
    protected void setup() {
    	
        initDefault();
        
        registerWithDF("Recommender");
        
        Ontology ontology = RecommendOntology.getInstance();
        
        // receive request
        MessageTemplate mt = MessageTemplate.and(
        		MessageTemplate.MatchOntology(ontology.getName()),
        		MessageTemplate.MatchPerformative(ACLMessage.REQUEST));        
		addBehaviour(new RecommendBehaviour(this, mt));


		addAgentInfoBehaviour(getAgentInfo());

    }
         
	
	protected class RecommendBehaviour extends AchieveREResponder {

		/**
		 * 
		 */
		private static final long serialVersionUID = -8353926385111974474L;
		private PikaterAgent agent;
		
		public RecommendBehaviour(PikaterAgent agent, MessageTemplate mt) {
			super(agent, mt);
			this.agent = agent;
		}

        @Override
        protected ACLMessage handleRequest(ACLMessage request)
        	throws NotUnderstoodException, RefuseException {
        	
        	ACLMessage reply = request.createReply();
            
        	try {        		
        		Action a = (Action) getContentManager().extractContent(request);

                if (a.getAction() instanceof Recommend) {
 
					if (finished()){
						log("Recommendation finished.");
						reply.setPerformative(ACLMessage.INFORM);					
						reply.setContent("finished");
						return reply;
					}
                	
                	Recommend rec = (Recommend) a.getAction();
                    myAgentOntology = rec.getRecommender();
                    
                    // merge options with .opt file options
                    myAgentOntology.setOptions(getParameters());

                    log("options: " + NewOptions.exportToWeka(myAgentOntology.getOptions()), 2);

                    Datas datas = rec.getDatas();
                    
                    // Get metadata:
					Metadata metadata = null;    
					
					// if metatada are not yet in ontology		
					if (rec.getDatas().getMetadata() == null) {
						// or fetch them from database:
						GetMetadata gm = new GetMetadata();
						gm.setInternal_filename(rec.getDatas().exportInternalTestFileName());
						metadata = DataManagerService.getMetadata(agent, gm);
						datas.setMetadata(metadata);
					}                            			

					// else TODO - overit, jestli jsou metadata OK, pripadne vygenerovat
					
					org.pikater.core.ontology.subtrees.management.Agent recommended_agent = chooseBestAgent(rec.getDatas());
                    
					if(recommended_agent==null){
						reply.setPerformative(ACLMessage.FAILURE);
						return reply;
					}
					

					//TODO:
                    // fill options
/*
					DataManagerService service = new DataManagerService();
					agent.logError(recommended_agent.getType());
					AgentInfo agentInfo = service.getAgentInfo(agent, recommended_agent.getType());
					NewOptions optionsOnt = agentInfo.getOptions();
					java.util.List<NewOption> options = optionsOnt.getOptions();
					
					java.util.List<NewOption> recommendedAgentOptions = recommended_agent.getOptions();
					
					java.util.List<NewOption> mergedOptions =
							mergeOptions(options, recommendedAgentOptions);
					recommended_agent.setOptions(mergedOptions);
*/					
            		// Prepare the content of inform message                       
    				Result result = new Result(a, recommended_agent);
					reply.setPerformative(ACLMessage.INFORM);					
    				
    				updateFinished();
    				
{'fraction_non_alphanumeric': 0.09700327766505384, 'fraction_numerical': 0.006087092242859373, 'mean_word_length': 3.1580142764438675, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1892068', 'n_tokens_mistral': 4338, 'n_tokens_neox': 3695, 'n_words': 600}