<commit_before>/*
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Accada (www.accada.org).
 *
 * Accada is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Accada 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 Accada; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package org.accada.ale.server.readers;

import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.accada.ale.server.Tag;
import org.accada.ale.wsdl.ale.epcglobal.ECSpecValidationException;
import org.accada.ale.wsdl.ale.epcglobal.ImplementationException;
import org.apache.log4j.Logger;

/**
 * represents a compositeReader that is a composition of different logicalreaders.
 * @author sawielan
 *
 */
public class CompositeReader extends org.accada.ale.server.readers.LogicalReader implements Observer  {

	/** logger. */
	private static final Logger LOG = Logger.getLogger(CompositeReader.class);
	
	/** logical readers within the composite reader. */
	private final java.util.Map<String, LogicalReader> logicalReaders = new HashMap<String, LogicalReader>();;

	/**
	 * constructor for the composite reader.
	 */
	public CompositeReader() {
		super();
	}
	
	/**
	 * initializes a Composite Reader. this method must be called befor the Reader can
	 * be used.
 	 * @param name the name for the reader encapsulated by this reader.
	 * @param aspec the specification that describes the current reader.
	 * @throws ImplementationException whenever an internal error occurs.
	 */
	public void initialize(String name, LRSpec aspec) throws ImplementationException {
		super.initialize(name, aspec);

		// create the sub parts by calling the factory method
		List<String> readers = aspec.getReaders();
		for (String reader : readers) {
			
			LOG.debug(String.format("retrieving reader part %s", reader));
			
			// just retrieve the reader from the LogicalReaderManager
			LogicalReader logicalReader = LogicalReaderManager.getLogicalReader(reader);
			
//			 add the reader to the observable
			logicalReader.addObserver(this);
			logicalReaders.put(logicalReader.getName(), logicalReader);
		}
	}
	
	/**
	 * unregister this compositeReader from its observables.
	 */
	public void unregisterAsObserver() {
		for (LogicalReader reader : logicalReaders.values()) {
			LOG.debug("undefining observer " + readerName + " on reader " + reader.getName());
			reader.deleteObserver(this);
		}
	}
		
	/**
	 * implements the update-method for the observer-pattern for events.
	 * @param o the observed object
	 * @param arg the arguments passed by the observable
	 */
	public void update(Observable o, Object arg) {
		// deliver tags only if the reader is not suspended
		if (isStarted()) {
			if (arg instanceof Tag) {
				setChanged();
				Tag tag = (Tag) arg;
				tag.setReader(getName());
				tag.addTrace(getName());
				
				notifyObservers((Tag) arg);
			} else if (arg instanceof List) {
				// process multiple tags at once				
				List<Tag> tagList = (List<Tag>) arg;				
				for (Tag tag : tagList) {
					tag.setReader(getName());
					tag.addTrace(getName());
				}
				setChanged();
			}
		}
	}

	/**
	 * add a logicalReader to the composite.
	 * @param reader a logicalReader (baseReader or CompositeReader)
	 */
	public void addReader(LogicalReader reader) {<commit_msg>fixed small bug when notifying observers<commit_after>/*
 * Copyright (C) 2007 ETH Zurich
 *
 * This file is part of Accada (www.accada.org).
 *
 * Accada is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * Accada 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 Accada; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */

package org.accada.ale.server.readers;

import java.util.HashMap;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import org.accada.ale.server.Tag;
import org.accada.ale.wsdl.ale.epcglobal.ECSpecValidationException;
import org.accada.ale.wsdl.ale.epcglobal.ImplementationException;
import org.apache.log4j.Logger;

/**
 * represents a compositeReader that is a composition of different logicalreaders.
 * @author sawielan
 *
 */
public class CompositeReader extends org.accada.ale.server.readers.LogicalReader implements Observer  {

	/** logger. */
	private static final Logger LOG = Logger.getLogger(CompositeReader.class);
	
	/** logical readers within the composite reader. */
	private final java.util.Map<String, LogicalReader> logicalReaders = new HashMap<String, LogicalReader>();;

	/**
	 * constructor for the composite reader.
	 */
	public CompositeReader() {
		super();
	}
	
	/**
	 * initializes a Composite Reader. this method must be called befor the Reader can
	 * be used.
 	 * @param name the name for the reader encapsulated by this reader.
	 * @param aspec the specification that describes the current reader.
	 * @throws ImplementationException whenever an internal error occurs.
	 */
	public void initialize(String name, LRSpec aspec) throws ImplementationException {
		super.initialize(name, aspec);

		// create the sub parts by calling the factory method
		List<String> readers = aspec.getReaders();
		for (String reader : readers) {
			
			LOG.debug(String.format("retrieving reader part %s", reader));
			
			// just retrieve the reader from the LogicalReaderManager
			LogicalReader logicalReader = LogicalReaderManager.getLogicalReader(reader);
			
//			 add the reader to the observable
			logicalReader.addObserver(this);
			logicalReaders.put(logicalReader.getName(), logicalReader);
		}
	}
	
	/**
	 * unregister this compositeReader from its observables.
	 */
	public void unregisterAsObserver() {
		for (LogicalReader reader : logicalReaders.values()) {
			LOG.debug("undefining observer " + readerName + " on reader " + reader.getName());
			reader.deleteObserver(this);
		}
	}
		
	/**
	 * implements the update-method for the observer-pattern for events.
	 * @param o the observed object
	 * @param arg the arguments passed by the observable
	 */
	public void update(Observable o, Object arg) {
		// deliver tags only if the reader is not suspended
		if (isStarted()) {
			if (arg instanceof Tag) {
				setChanged();
				Tag tag = (Tag) arg;
				tag.setReader(getName());
				tag.addTrace(getName());
				
				notifyObservers((Tag) arg);
			} else if (arg instanceof List) {
				LOG.debug("processing multiple tags");
				
				// process multiple tags at once				
				List<Tag> tagList = (List<Tag>) arg;				
				for (Tag tag : tagList) {
					tag.setReader(getName());
					tag.addTrace(getName());
				}
				setChanged();
				notifyObservers(tagList);
			}
		}
	}

	/**
	 * add a logicalReader to the composite.
	 * @param reader a logicalReader (baseReader or CompositeReader)
	 */
	public void addReader(LogicalReader reader) {
{'fraction_non_alphanumeric': 0.09531837634548049, 'fraction_numerical': 0.0046686551679419016, 'mean_word_length': 3.7197062423500613, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4760415', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2109, 'n_words': 773}