<commit_before>/*******************************************************************************
 * Copyright 2011
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.dkpro.lab.task.impl;

import static org.dkpro.lab.storage.StorageService.CONTEXT_ID_SCHEME;
import static org.dkpro.lab.storage.StorageService.LATEST_CONTEXT_SCHEME;
import static org.dkpro.lab.storage.filesystem.FileSystemStorageService.isStaticImport;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.core.UriBuilder;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dkpro.lab.Util;
import org.dkpro.lab.conversion.ConversionService;
import org.dkpro.lab.engine.TaskContext;
import org.dkpro.lab.reporting.Report;
import org.dkpro.lab.storage.StorageService;
import org.dkpro.lab.storage.impl.PropertiesAdapter;
import org.dkpro.lab.task.Discriminator;
import org.dkpro.lab.task.Property;
import org.dkpro.lab.task.Task;
import org.dkpro.lab.task.TaskContextMetadata;

public class TaskBase
	implements Task
{
	private final Log log = LogFactory.getLog(getClass());

	private String type;
	private Map<String, String> imports;
	private Map<String, String> attributes;
	private Map<String, String> discriminators;
	private List<Class<? extends Report>> reports;
	
	private boolean initialized = false;

    private TaskContext aContext;

	{
		attributes = new HashMap<String, String>();
		discriminators = new HashMap<String, String>();
		reports = new ArrayList<Class<? extends Report>>();
		imports = new HashMap<String, String>();
	}

	/**
	 * Create a new task with a default type name. The type is the simple name of the class. If 
	 * it is an inner class, only the name of the inner class is used. Be careful that type names
	 * must uniquely identify a task class.
	 */
	public TaskBase()
	{
		String className = getClass().getName();
		int innerClassSep = className.lastIndexOf('$');
		if (innerClassSep != 1) {
			String innerName = className.substring(innerClassSep+1);
			if (!StringUtils.isNumeric(innerName)) {
				className = innerName;
			}
		}
		setType(className);
	}

	public TaskBase(String aType)
	{
		setType(aType);
	}

	@Override
	public void initialize(TaskContext aContext)
	{
        this.aContext = aContext;
        initialized = true;
	}
	
	@Override
    public boolean isInitialized()
	{
	    return initialized;
	}
	
	@Override
	public final void analyze()
	{
        attributes = new HashMap<String, String>();
        discriminators = new HashMap<String, String>();
        analyze(getClass(), Property.class, attributes);
        analyze(getClass(), Discriminator.class, discriminators);
	}
	
	@Override
	public void destroy(TaskContext aContext)
	{
	    initialized = false;
	}
	
	public void setType(String aType)
	{
		if (aType == null) {
			throw new IllegalArgumentException("Must specify a type");
		}
		type = aType;
	}

	@Override
	public String getType()
	{
		return type;
	}

	@Override
	public void setAttribute(String aKey, String aValue)
	{
		if (aKey == null) {
			throw new IllegalArgumentException("Must specify a key");
		}
		if (aValue == null) {
			attributes.remove(aKey);
		}
		else {
			attributes.put(aKey, aValue);
		}
	}

	@Override
	public String getAttribute(String aKey)
	{
		return attributes.get(aKey);
	}

	@Override
	public Map<String, String> getAttributes()
	{
		return attributes;
	}

	@Override
	public void setDescriminator(String aKey, String aValue)
	{
		if (aKey == null) {
			throw new IllegalArgumentException("Must specify a key");
		}
		if (aValue == null) {
			discriminators.remove(aKey);
		}
		else {
			discriminators.put(aKey, aValue);
		}
	}

	@Override
	public String getDescriminator(String aKey)
	{
		return discriminators.get(aKey);
	}

	@Override
	public Map<String, String> getDescriminators()
	{
		return discriminators;
	}

	@Override
	public Map<String, String> getResolvedDescriminators(TaskContext aContext)
	{
		StorageService storageService = aContext.getStorageService();
		Map<String, String> descs = new HashMap<String, String>();
		descs.putAll(getDescriminators());

		// Load previous discriminators and check that the do not conflict with discriminators
		// defined in this task
		for (String rawUri : aContext.getMetadata().getImports().values()) {
			URI uri = URI.create(rawUri);

			if (isStaticImport(uri)) {
				continue;
			}

			final TaskContextMetadata meta = aContext.resolve(uri);

			Map<String, String> prerequisiteDiscriminators = storageService.retrieveBinary(
					meta.getId(), DISCRIMINATORS_KEY, new PropertiesAdapter()).getMap();<commit_msg>#97 - distinction between analyzed and user-set attributes/disc.<commit_after>/*******************************************************************************
 * Copyright 2016
 * Ubiquitous Knowledge Processing (UKP) Lab
 * Technische Universität Darmstadt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.dkpro.lab.task.impl;

import static org.dkpro.lab.storage.StorageService.CONTEXT_ID_SCHEME;
import static org.dkpro.lab.storage.StorageService.LATEST_CONTEXT_SCHEME;
import static org.dkpro.lab.storage.filesystem.FileSystemStorageService.isStaticImport;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.ws.rs.core.UriBuilder;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dkpro.lab.Util;
import org.dkpro.lab.conversion.ConversionService;
import org.dkpro.lab.engine.TaskContext;
import org.dkpro.lab.reporting.Report;
import org.dkpro.lab.storage.StorageService;
import org.dkpro.lab.storage.impl.PropertiesAdapter;
import org.dkpro.lab.task.Discriminator;
import org.dkpro.lab.task.Property;
import org.dkpro.lab.task.Task;
import org.dkpro.lab.task.TaskContextMetadata;

public class TaskBase
	implements Task
{
	private final Log log = LogFactory.getLog(getClass());

	private String type;
	private Map<String, String> imports;
	private Map<String, String> attributes;
	private Map<String, String> discriminators;
	private Map<String, String> analyzedAttributes;
    private Map<String, String> analyzedDiscriminators;
	private List<Class<? extends Report>> reports;
	
	private boolean initialized = false;

    private TaskContext aContext;

	{
		attributes = new HashMap<String, String>();
		analyzedAttributes = new HashMap<String, String>();
		discriminators = new HashMap<String, String>();
		analyzedDiscriminators = new HashMap<String, String>();
		reports = new ArrayList<Class<? extends Report>>();
		imports = new HashMap<String, String>();
	}

	/**
	 * Create a new task with a default type name. The type is the simple name of the class. If 
	 * it is an inner class, only the name of the inner class is used. Be careful that type names
	 * must uniquely identify a task class.
	 */
	public TaskBase()
	{
		String className = getClass().getName();
		int innerClassSep = className.lastIndexOf('$');
		if (innerClassSep != 1) {
			String innerName = className.substring(innerClassSep+1);
			if (!StringUtils.isNumeric(innerName)) {
				className = innerName;
			}
		}
		setType(className);
	}

	public TaskBase(String aType)
	{
		setType(aType);
	}

	@Override
	public void initialize(TaskContext aContext)
	{
        this.aContext = aContext;
        initialized = true;
	}
	
	@Override
    public boolean isInitialized()
	{
	    return initialized;
	}
	
	@Override
	public final void analyze()
	{
	    analyzedAttributes = new HashMap<String, String>();
        analyzedDiscriminators = new HashMap<String, String>();
        analyze(getClass(), Property.class, analyzedAttributes);
        analyze(getClass(), Discriminator.class, analyzedDiscriminators);
	}
	
	@Override
	public void destroy(TaskContext aContext)
	{
	    initialized = false;
	}
	
	public void setType(String aType)
	{
		if (aType == null) {
			throw new IllegalArgumentException("Must specify a type");
		}
		type = aType;
	}

	@Override
	public String getType()
	{
		return type;
	}

	@Override
	public void setAttribute(String aKey, String aValue)
	{
	    
		if (aKey == null) {
			throw new IllegalArgumentException("Must specify a key");
		}
		if (aValue == null) {
			attributes.remove(aKey);
		}
		else {
			attributes.put(aKey, aValue);
		}
	}

	@Override
	public String getAttribute(String aKey)
	{
	    String value = analyzedAttributes.get(aKey);
	    if(value != null){
	        return value;
	    }
	    
		return attributes.get(aKey);
	}

	@Override
	public Map<String, String> getAttributes()
	{
	    Map<String, String> allAttributes = new HashMap<>();
	    allAttributes.putAll(attributes);
	    allAttributes.putAll(analyzedAttributes);
	    
		return allAttributes;
	}

	@Override
	public void setDescriminator(String aKey, String aValue)
	{
		if (aKey == null) {
			throw new IllegalArgumentException("Must specify a key");
		}
		if (aValue == null) {
			discriminators.remove(aKey);
		}
		else {
			discriminators.put(aKey, aValue);
		}
	}

	@Override
	public String getDescriminator(String aKey)
	{
	    String value = analyzedDiscriminators.get(aKey);
	    if(value != null){
	        return value;
	    }
	    
		return discriminators.get(aKey);
	}

	@Override
	public Map<String, String> getDescriminators()
	{
	    Map<String,String> allDiscriminators = new HashMap<>();
        allDiscriminators.putAll(discriminators);
        allDiscriminators.putAll(analyzedDiscriminators);
		return allDiscriminators;
	}

	@Override
	public Map<String, String> getResolvedDescriminators(TaskContext aContext)
	{
		StorageService storageService = aContext.getStorageService();
		Map<String, String> descs = new HashMap<String, String>();
		descs.putAll(getDescriminators());

		// Load previous discriminators and check that the do not conflict with discriminators
		// defined in this task
		for (String rawUri : aContext.getMetadata().getImports().values()) {
			URI uri = URI.create(rawUri);

			if (isStaticImport(uri)) {
				continue;
			}

			final TaskContextMetadata meta = aContext.resolve(uri);

			Map<String, String> prerequisiteDiscriminators = storageService.retrieveBinary(
					meta.getId(), DISCRIMINATORS_KEY, new PropertiesAdapter()).getMap();
{'fraction_non_alphanumeric': 0.12784661754855994, 'fraction_numerical': 0.001841929002009377, 'mean_word_length': 4.315976858032933, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4044498', 'n_tokens_mistral': 3769, 'n_tokens_neox': 3470, 'n_words': 966}