<commit_before>/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

The updating of this file to JADE 2.0 has been partially supported by the IST-1999-10211 LEAP Project

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package jade.domain.KBManagement;

//#MIDP_EXCLUDE_FILE

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import jade.util.leap.Map;
import jade.util.leap.HashMap;
import jade.util.leap.List;
import jade.util.leap.ArrayList;
import jade.util.leap.LinkedList;
import jade.util.leap.Iterator;

import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.ServiceDescription;

import jade.lang.acl.ACLMessage;
import jade.proto.SubscriptionResponder;
import jade.core.AID;

/**
 * @author <NAME> - TILab
 *
 */
/** Common base class for AMS and DF Knowledge Base*/
public abstract class MemKB extends KB {

	protected Map facts = new HashMap();
	protected Hashtable subscriptions = new Hashtable();
	protected LeaseManager lm;
	protected int currentReg = 0;
	protected SubscriptionResponder sr;
	
	protected static int maxResults = -1;
	protected final static int MAX_REGISTER_WITHOUT_CLEAN = 100;
	
	public MemKB(){
		
	}
	
	protected Object insert(Object name, Object fact) {
		currentReg ++;
		if(currentReg > MAX_REGISTER_WITHOUT_CLEAN){
			clean();
			currentReg = 0;
		}
	    return facts.put(name, fact);
	}
	
	protected Object remove(Object name) {
	    return facts.remove(name);
	}
	
	  // This abstract method has to perform pattern matching
	protected abstract boolean match(Object template, Object fact);


	protected abstract void clean();
	
	public List search(Object template) {
		List result = new ArrayList();
	    Iterator it = facts.values().iterator();
		int found = 0;
	    while(it.hasNext() && (found < maxResults || maxResults < 0)) {
	        Object fact = it.next();
		    if(match(template, fact)){
				result.add(fact);
				found ++;
		    }
      	}
        return result;
	}
	
	
	// 
	public void subscribe(Object dfd, SubscriptionResponder.Subscription s) throws NotUnderstoodException{
		try{
			DFAgentDescription dfdTemplate = (DFAgentDescription) dfd;
			ACLMessage aclSub = (ACLMessage) s.getMessage();
			subscriptions.put(dfdTemplate, s);
		}catch(Exception e){
			System.out.println("Subscribe error: "+e.getMessage());
			throw new NotUnderstoodException(e.getMessage());
		}
	}
		

	public Enumeration getSubscriptionDfAgentDescriptions(){<commit_msg>Migrated file towards the new logging system.<commit_after>/*****************************************************************
JADE - Java Agent DEvelopment Framework is a framework to develop 
multi-agent systems in compliance with the FIPA specifications.
Copyright (C) 2000 CSELT S.p.A. 

The updating of this file to JADE 2.0 has been partially supported by the IST-1999-10211 LEAP Project

GNU Lesser General Public License

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, 
version 2.1 of the License. 

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA  02111-1307, USA.
*****************************************************************/

package jade.domain.KBManagement;

//#MIDP_EXCLUDE_FILE

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import jade.util.leap.Map;
import jade.util.leap.HashMap;
import jade.util.leap.List;
import jade.util.leap.ArrayList;
import jade.util.leap.LinkedList;
import jade.util.leap.Iterator;
import jade.util.Logger;

import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.Property;
import jade.domain.FIPAAgentManagement.ServiceDescription;

import jade.lang.acl.ACLMessage;
import jade.proto.SubscriptionResponder;
import jade.core.AID;

/**
 * @author <NAME> - TILab
 *
 */
/** Common base class for AMS and DF Knowledge Base*/
public abstract class MemKB extends KB {

	protected Map facts = new HashMap();
	protected Hashtable subscriptions = new Hashtable();
	protected LeaseManager lm;
	protected int currentReg = 0;
	protected SubscriptionResponder sr;
	
	protected static int maxResults = -1;
	protected final static int MAX_REGISTER_WITHOUT_CLEAN = 100;
	
	private Logger logger = Logger.getMyLogger(this.getClass().getName());
	
	public MemKB(){
		
	}
	
	protected Object insert(Object name, Object fact) {
		currentReg ++;
		if(currentReg > MAX_REGISTER_WITHOUT_CLEAN){
			clean();
			currentReg = 0;
		}
	    return facts.put(name, fact);
	}
	
	protected Object remove(Object name) {
	    return facts.remove(name);
	}
	
	  // This abstract method has to perform pattern matching
	protected abstract boolean match(Object template, Object fact);


	protected abstract void clean();
	
	public List search(Object template) {
		List result = new ArrayList();
	    Iterator it = facts.values().iterator();
		int found = 0;
	    while(it.hasNext() && (found < maxResults || maxResults < 0)) {
	        Object fact = it.next();
		    if(match(template, fact)){
				result.add(fact);
				found ++;
		    }
      	}
        return result;
	}
	
	
	// 
	public void subscribe(Object dfd, SubscriptionResponder.Subscription s) throws NotUnderstoodException{
		try{
			DFAgentDescription dfdTemplate = (DFAgentDescription) dfd;
			ACLMessage aclSub = (ACLMessage) s.getMessage();
			subscriptions.put(dfdTemplate, s);
		}catch(Exception e){
			logger.log(Logger.SEVERE,"Subscribe error: "+e.getMessage());
			throw new NotUnderstoodException(e.getMessage());
		}
	}
		

	public Enumeration getSubscriptionDfAgentDescriptions(){
{'fraction_non_alphanumeric': 0.11850809289232934, 'fraction_numerical': 0.010978184377199156, 'mean_word_length': 4.412033511043412, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '397996', 'n_tokens_mistral': 2220, 'n_tokens_neox': 1988, 'n_words': 687}