<commit_before>package brooklyn.entity.basic

import java.lang.reflect.Field
import java.util.Collection
import java.util.Map
import java.util.concurrent.CopyOnWriteArrayList

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import brooklyn.entity.Application
import brooklyn.entity.Effector
import brooklyn.entity.Entity
import brooklyn.entity.EntityClass
import brooklyn.entity.Group
import brooklyn.entity.ParameterType
import brooklyn.event.AttributeSensor
import brooklyn.event.EventListener
import brooklyn.event.Sensor
import brooklyn.event.adapter.PropertiesSensorAdapter
import brooklyn.event.basic.AttributeMap
import brooklyn.event.basic.ConfigKey
import brooklyn.event.basic.SensorEvent
import brooklyn.location.Location
import brooklyn.management.ManagementContext
import brooklyn.management.SubscriptionContext
import brooklyn.management.Task
import brooklyn.management.internal.LocalManagementContext
import brooklyn.management.internal.LocalSubscriptionContext
import brooklyn.util.internal.LanguageUtils
import brooklyn.util.task.ExecutionContext

/**
 * Default {@link Entity} implementation
 * 
 * Provides several common fields ({@link #name}, {@link #id});
 * a map {@link #config} which contains arbitrary config data;
 * sensors and effectors; policies; managementContext.
 * <p>
 * Fields in config can be accessed (get and set) without referring to config,
 * (through use of propertyMissing). Note that config is typically inherited
 * by children, whereas the fields are not. (Attributes cannot be so accessed,
 * nor are they inherited.)
 *
 * @author alex, aled
 */
public abstract class AbstractEntity implements EntityLocal, GroovyInterceptable {
    private static final Logger log = LoggerFactory.getLogger(AbstractEntity.class);
 
    String id = LanguageUtils.newUid();
    Map<String,Object> presentationAttributes = [:]
    String displayName;
    final Collection<Group> groups = new CopyOnWriteArrayList<Group>()
    volatile Application application
    Collection<Location> locations = []
    Group owner
 
    protected transient volatile ExecutionContext execution
    protected transient volatile SubscriptionContext subscription
    protected transient volatile LocalManagementContext management = LocalManagementContext.getContext()
 
    /**
     * The sensor-attribute values of this entity. Updating this map should be done
     * via getAttribute/updateAttribute; it will automatically emit an attribute-change event.
     */
    protected final AttributeMap attributesInternal = new AttributeMap(this)
	
	//ENGR-1458  interesting to use property change. if it works great. 
	//if there are any issues with it consider instead just making attributesInternal private,
	//and forcing all changes to attributesInternal to go through update(AttributeSensor,...)
	//and do the publishing there...  (please leave this comment here for several months until we know... it's Jun 2011 right now)
    protected final PropertiesSensorAdapter propertiesAdapter = new PropertiesSensorAdapter(this, attributes)

    /*
     * TODO An alternative implementation approach would be to have:
     *   setOwner(Entity o, Map<ConfigKey,Object> inheritedConfig=[:])
     * The idea is that the owner could in theory decide explicitly what in its config
     * would be shared.
     * I (Aled) am undecided as to whether that would be better...
     */
    /**
     * Map of configuration information that is defined at start-up time for the entity. These
     * configuration parameters are shared and made accessible to the "owned children" of this
     * entity.
     */
    protected final Map<ConfigKey,Object> inheritableConfig = [:]
    
    public AbstractEntity(Map flags=[:]) {
		this.@skipCustomInvokeMethod.set(true)
        Entity suppliedOwner = flags.remove('owner')
        Map<ConfigKey,Object> suppliedInheritableConfig = flags.remove('inheritableConfig')

        if (suppliedInheritableConfig) inheritableConfig.putAll(suppliedInheritableConfig)
        
        //place named-arguments into corresponding fields if they exist, otherwise put into attributes map
        this.attributes << LanguageUtils.setFieldsFromMap(this, flags)

        // initialize the effectors defined on the class
		// (dynamic effectors could still be added; see #getEffectors
		Map<String,Effector> effectorsT = [:]
		getClass().getFields().each { Field f ->
			if (Effector.class.isAssignableFrom(f.getType())) {<commit_msg>changed closure to for loop because groovy was giving a 'semantic analysis' error about generic type parameter V; now it works...<commit_after>package brooklyn.entity.basic

import java.lang.reflect.Field
import java.util.Collection
import java.util.Map
import java.util.concurrent.CopyOnWriteArrayList

import org.slf4j.Logger
import org.slf4j.LoggerFactory

import brooklyn.entity.Application
import brooklyn.entity.Effector
import brooklyn.entity.Entity
import brooklyn.entity.EntityClass
import brooklyn.entity.Group
import brooklyn.entity.ParameterType
import brooklyn.event.AttributeSensor
import brooklyn.event.EventListener
import brooklyn.event.Sensor
import brooklyn.event.adapter.PropertiesSensorAdapter
import brooklyn.event.basic.AttributeMap
import brooklyn.event.basic.ConfigKey
import brooklyn.event.basic.SensorEvent
import brooklyn.location.Location
import brooklyn.management.ManagementContext
import brooklyn.management.SubscriptionContext
import brooklyn.management.Task
import brooklyn.management.internal.LocalManagementContext
import brooklyn.management.internal.LocalSubscriptionContext
import brooklyn.util.internal.LanguageUtils
import brooklyn.util.task.ExecutionContext

/**
 * Default {@link Entity} implementation
 * 
 * Provides several common fields ({@link #name}, {@link #id});
 * a map {@link #config} which contains arbitrary config data;
 * sensors and effectors; policies; managementContext.
 * <p>
 * Fields in config can be accessed (get and set) without referring to config,
 * (through use of propertyMissing). Note that config is typically inherited
 * by children, whereas the fields are not. (Attributes cannot be so accessed,
 * nor are they inherited.)
 *
 * @author alex, aled
 */
public abstract class AbstractEntity implements EntityLocal, GroovyInterceptable {
    private static final Logger log = LoggerFactory.getLogger(AbstractEntity.class);
 
    String id = LanguageUtils.newUid();
    Map<String,Object> presentationAttributes = [:]
    String displayName;
    final Collection<Group> groups = new CopyOnWriteArrayList<Group>()
    volatile Application application
    Collection<Location> locations = []
    Group owner
 
    protected transient volatile ExecutionContext execution
    protected transient volatile SubscriptionContext subscription
    protected transient volatile LocalManagementContext management = LocalManagementContext.getContext()
 
    /**
     * The sensor-attribute values of this entity. Updating this map should be done
     * via getAttribute/updateAttribute; it will automatically emit an attribute-change event.
     */
    protected final AttributeMap attributesInternal = new AttributeMap(this)
	
	//ENGR-1458  interesting to use property change. if it works great. 
	//if there are any issues with it consider instead just making attributesInternal private,
	//and forcing all changes to attributesInternal to go through update(AttributeSensor,...)
	//and do the publishing there...  (please leave this comment here for several months until we know... it's Jun 2011 right now)
    protected final PropertiesSensorAdapter propertiesAdapter = new PropertiesSensorAdapter(this, attributes)

    /*
     * TODO An alternative implementation approach would be to have:
     *   setOwner(Entity o, Map<ConfigKey,Object> inheritedConfig=[:])
     * The idea is that the owner could in theory decide explicitly what in its config
     * would be shared.
     * I (Aled) am undecided as to whether that would be better...
     */
    /**
     * Map of configuration information that is defined at start-up time for the entity. These
     * configuration parameters are shared and made accessible to the "owned children" of this
     * entity.
     */
    protected final Map<ConfigKey,Object> inheritableConfig = [:]
    
    public AbstractEntity(Map flags=[:]) {
		this.@skipCustomInvokeMethod.set(true)
        Entity suppliedOwner = flags.remove('owner')
        Map<ConfigKey,Object> suppliedInheritableConfig = flags.remove('inheritableConfig')

        if (suppliedInheritableConfig) inheritableConfig.putAll(suppliedInheritableConfig)
        
        //place named-arguments into corresponding fields if they exist, otherwise put into attributes map
        this.attributes << LanguageUtils.setFieldsFromMap(this, flags)

        // initialize the effectors defined on the class
		// (dynamic effectors could still be added; see #getEffectors
		Map<String,Effector> effectorsT = [:]
		for (Field f in getClass().getFields()) {
			if (Effector.class.isAssignableFrom(f.getType())) {
{'fraction_non_alphanumeric': 0.07385671575683755, 'fraction_numerical': 0.0022145941756173183, 'mean_word_length': 4.424624624624625, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2559681', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2219, 'n_words': 796}