<commit_before>/**
 * Copyright (C) 2013 – 2015 SLUB Dresden & Avantgarde Labs GmbH (<<EMAIL>>)
 *
 * 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.dswarm.converter.flow;

import java.io.Reader;

import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableList;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.name.Named;
import org.culturegraph.mf.framework.ObjectPipe;
import org.culturegraph.mf.framework.ObjectReceiver;
import org.culturegraph.mf.stream.converter.StreamToTriples;
import org.culturegraph.mf.types.Triple;

import org.dswarm.converter.DMPConverterException;
import org.dswarm.converter.mf.stream.reader.CsvReader;
import org.dswarm.converter.pipe.timing.ObjectTimer;
import org.dswarm.converter.pipe.timing.StreamTimer;
import org.dswarm.converter.pipe.timing.TimerBasedFactory;
import org.dswarm.persistence.model.resource.Configuration;
import org.dswarm.persistence.model.resource.DataModel;

/**
 * @author phorn
 */
public class CSVSourceResourceTriplesFlow extends AbstractCSVResourceFlow<ImmutableList<Triple>> {

	private final MetricRegistry registry;
	private final TimerBasedFactory timerBasedFactory;

	@AssistedInject
	private CSVSourceResourceTriplesFlow(
			@Named("Monitoring") final MetricRegistry registry,
			final TimerBasedFactory timerBasedFactory,
			@Assisted("encoding") final String encoding,
			@Assisted("escapeCharacter") final Character escapeCharacter,
			@Assisted("quoteCharacter") final Character quoteCharacter,
			@Assisted("columnDelimiter") final Character columnDelimiter,
			@Assisted("rowDelimiter") final String rowDelimiter) {
		super(encoding, escapeCharacter, quoteCharacter, columnDelimiter, rowDelimiter);
		this.registry = registry;
		this.timerBasedFactory = timerBasedFactory;
	}

	@AssistedInject
	private CSVSourceResourceTriplesFlow(
			@Named("Monitoring") final MetricRegistry registry,
			final TimerBasedFactory timerBasedFactory,
			@Assisted final Configuration configuration) throws DMPConverterException {
		super(configuration);
		this.registry = registry;
		this.timerBasedFactory = timerBasedFactory;
	}

	@AssistedInject
	private CSVSourceResourceTriplesFlow(
			@Named("Monitoring") final MetricRegistry registry,
			final TimerBasedFactory timerBasedFactory,
			@Assisted final DataModel dataModel) throws DMPConverterException {
		super(dataModel);
		this.registry = registry;
		this.timerBasedFactory = timerBasedFactory;
	}

	@Override
	protected ImmutableList<Triple> process(final ObjectPipe<String, ObjectReceiver<Reader>> opener, final String obj, final CsvReader pipe) {

		final ListTripleReceiver tripleReceiver = new ListTripleReceiver();
		final StreamTimer csvInputTimer = timerBasedFactory.forStream("csv-input");
		final ObjectTimer csvTriplesTimer = timerBasedFactory.forObject("csv-triples");
		final ObjectTimer csvReaderTimer = timerBasedFactory.forObject("csv-reader");

		pipe
				.setReceiver(csvInputTimer)
				.setReceiver(new StreamToTriples())
				.setReceiver(csvTriplesTimer)
				.setReceiver(tripleReceiver);

		//noinspection unchecked
		opener.setReceiver(csvReaderTimer).process(obj);

		opener.process(obj);
		return tripleReceiver.getCollection();
	}

	private static class ListTripleReceiver implements ObjectReceiver<Triple> {<commit_msg>[DD-958][BUGFIX] Reappend pipe to csv flow opener<commit_after>/**
 * Copyright (C) 2013 – 2015 SLUB Dresden & Avantgarde Labs GmbH (<<EMAIL>>)
 *
 * 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.dswarm.converter.flow;

import java.io.Reader;

import com.codahale.metrics.MetricRegistry;
import com.google.common.collect.ImmutableList;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.name.Named;
import org.culturegraph.mf.framework.ObjectPipe;
import org.culturegraph.mf.framework.ObjectReceiver;
import org.culturegraph.mf.stream.converter.StreamToTriples;
import org.culturegraph.mf.types.Triple;

import org.dswarm.converter.DMPConverterException;
import org.dswarm.converter.mf.stream.reader.CsvReader;
import org.dswarm.converter.pipe.timing.ObjectTimer;
import org.dswarm.converter.pipe.timing.StreamTimer;
import org.dswarm.converter.pipe.timing.TimerBasedFactory;
import org.dswarm.persistence.model.resource.Configuration;
import org.dswarm.persistence.model.resource.DataModel;

/**
 * @author phorn
 */
public class CSVSourceResourceTriplesFlow extends AbstractCSVResourceFlow<ImmutableList<Triple>> {

	private final MetricRegistry registry;
	private final TimerBasedFactory timerBasedFactory;

	@AssistedInject
	private CSVSourceResourceTriplesFlow(
			@Named("Monitoring") final MetricRegistry registry,
			final TimerBasedFactory timerBasedFactory,
			@Assisted("encoding") final String encoding,
			@Assisted("escapeCharacter") final Character escapeCharacter,
			@Assisted("quoteCharacter") final Character quoteCharacter,
			@Assisted("columnDelimiter") final Character columnDelimiter,
			@Assisted("rowDelimiter") final String rowDelimiter) {
		super(encoding, escapeCharacter, quoteCharacter, columnDelimiter, rowDelimiter);
		this.registry = registry;
		this.timerBasedFactory = timerBasedFactory;
	}

	@AssistedInject
	private CSVSourceResourceTriplesFlow(
			@Named("Monitoring") final MetricRegistry registry,
			final TimerBasedFactory timerBasedFactory,
			@Assisted final Configuration configuration) throws DMPConverterException {
		super(configuration);
		this.registry = registry;
		this.timerBasedFactory = timerBasedFactory;
	}

	@AssistedInject
	private CSVSourceResourceTriplesFlow(
			@Named("Monitoring") final MetricRegistry registry,
			final TimerBasedFactory timerBasedFactory,
			@Assisted final DataModel dataModel) throws DMPConverterException {
		super(dataModel);
		this.registry = registry;
		this.timerBasedFactory = timerBasedFactory;
	}

	@Override
	protected ImmutableList<Triple> process(final ObjectPipe<String, ObjectReceiver<Reader>> opener, final String obj, final CsvReader pipe) {

		final ListTripleReceiver tripleReceiver = new ListTripleReceiver();
		final StreamTimer csvInputTimer = timerBasedFactory.forStream("csv-input");
		final ObjectTimer csvTriplesTimer = timerBasedFactory.forObject("csv-triples");
		final ObjectTimer csvReaderTimer = timerBasedFactory.forObject("csv-reader");

		pipe
				.setReceiver(csvInputTimer)
				.setReceiver(new StreamToTriples())
				.setReceiver(csvTriplesTimer)
				.setReceiver(tripleReceiver);

		//noinspection unchecked
		opener.setReceiver(csvReaderTimer).setReceiver(pipe);

		opener.process(obj);
		return tripleReceiver.getCollection();
	}

	private static class ListTripleReceiver implements ObjectReceiver<Triple> {
{'fraction_non_alphanumeric': 0.08668453976764968, 'fraction_numerical': 0.0034469551895825352, 'mean_word_length': 5.89612676056338, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '5399677', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2143, 'n_words': 546}