<commit_before>/*******************************************************************************
 * Copyright 2012 <NAME>
 * 
 * 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 de.hub.emffrag.reflective;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import com.google.common.base.Throwables;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.name.Named;
import com.google.inject.name.Names;

import de.hub.emffrag.kvstore.IKeyValueStore;
import de.hub.emffrag.util.ILogger;

public class FragmentedModel extends ResourceSetImpl {

	/**
	 * Creates a {@link FragmentedModel}. This is the only difference to
	 * {@link ResourceSetImpl}. Afterwards the model can be handles as usual
	 * with EMF.
	 * 
	 * @param keyValueStore
	 *            The {@link IKeyValueStore} that is used to persist the model.
	 * @param name
	 *            The name of the model. Refers to a table in the
	 *            {@link IKeyValueStore}.
	 * @param packages
	 *            All meta-model packages that are/will be used by the model.
	 */
	public FragmentedModel(final IKeyValueStore keyValueStore, String name, EPackage... packages) {
		Module module = new Module() {
			@Override
			public void configure(Binder binder) {
				binder.bind(ILogger.class).toInstance(new ILogger() {
					@Override
					public void log(int level, String message, Throwable exception) {
						if (level <= ILogger.WARNING) {
							System.out.println("[" + level + "] " + message);
						}
					}
				});
				binder.bind(Integer.class).annotatedWith(Names.named(FragmentedModel.OPTION_WEAK_UNLOAD_CACHE_SIZE)).toInstance(0);
				binder.bind(IKeyValueStore.class).toInstance(keyValueStore);
				binder.bind(FragmentedModel.class).toInstance(FragmentedModel.this);
			}
		};

		// TODO FStoreImpl is build as a singleton, which is wrong with the
		// current design.
		FStoreImpl.INSTANCE = Guice.createInjector(module).getInstance(FStoreImpl.class);
		FStoreImpl.INSTANCE.initialize(Arrays.asList(packages), name, false);
	}

	/**
	 * Creates a {@link FragmentedModel}. This is the only difference to
	 * {@link ResourceSetImpl}. Afterwards the model can be handles as usual
	 * with EMF.
	 * 
	 * @param module
	 *            The GUICE module that configures how emf-fragments is working.
	 *            Most importantly the module has to bind a concrete
	 *            {@link IKeyValueStore}.
	 * @param name
	 *            The name of the model. Refers to a table in the
	 *            {@link IKeyValueStore}.
	 * @param readOnly
	 *            True if the model does exist and is not changed. False
	 *            otherwise.
	 * @param packages
	 *            All meta-model packages that are/will be used by the model.
	 */
	public FragmentedModel(Module module, String name, boolean readOnly, EPackage... packages) {
		assert (module != null);

		// TODO FStoreImpl is build as a singleton, which is wrong with the
		// current design.
		FStoreImpl.INSTANCE = Guice.createInjector(module).getInstance(FStoreImpl.class);
		FStoreImpl.INSTANCE.initialize(Arrays.asList(packages), name, readOnly);
	}

	/**
	 * Saves all loaded and cached fragments to the underlying
	 * {@link IKeyValueStore}. Unloaded fragments are stored independently when
	 * they are changed and unloaded.
	 */
	public void save() {<commit_msg>Corrected spelling.<commit_after>/*******************************************************************************
 * Copyright 2012 <NAME>
 * 
 * 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 de.hub.emffrag.reflective;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;

import com.google.common.base.Throwables;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.name.Named;
import com.google.inject.name.Names;

import de.hub.emffrag.kvstore.IKeyValueStore;
import de.hub.emffrag.util.ILogger;

public class FragmentedModel extends ResourceSetImpl {

	/**
	 * Creates a {@link FragmentedModel}. This is the only difference to
	 * {@link ResourceSetImpl}. Afterwards the model can be handled as usual
	 * with EMF.
	 * 
	 * @param keyValueStore
	 *            The {@link IKeyValueStore} that is used to persist the model.
	 * @param name
	 *            The name of the model. Refers to a table in the
	 *            {@link IKeyValueStore}.
	 * @param packages
	 *            All meta-model packages that are/will be used by the model.
	 */
	public FragmentedModel(final IKeyValueStore keyValueStore, String name, EPackage... packages) {
		Module module = new Module() {
			@Override
			public void configure(Binder binder) {
				binder.bind(ILogger.class).toInstance(new ILogger() {
					@Override
					public void log(int level, String message, Throwable exception) {
						if (level <= ILogger.WARNING) {
							System.out.println("[" + level + "] " + message);
						}
					}
				});
				binder.bind(Integer.class).annotatedWith(Names.named(FragmentedModel.OPTION_WEAK_UNLOAD_CACHE_SIZE)).toInstance(0);
				binder.bind(IKeyValueStore.class).toInstance(keyValueStore);
				binder.bind(FragmentedModel.class).toInstance(FragmentedModel.this);
			}
		};

		// TODO FStoreImpl is build as a singleton, which is wrong with the
		// current design.
		FStoreImpl.INSTANCE = Guice.createInjector(module).getInstance(FStoreImpl.class);
		FStoreImpl.INSTANCE.initialize(Arrays.asList(packages), name, false);
	}

	/**
	 * Creates a {@link FragmentedModel}. This is the only difference to
	 * {@link ResourceSetImpl}. Afterwards the model can be handled as usual
	 * with EMF.
	 * 
	 * @param module
	 *            The GUICE module that configures how emf-fragments is working.
	 *            Most importantly the module has to bind a concrete
	 *            {@link IKeyValueStore}.
	 * @param name
	 *            The name of the model. Refers to a table in the
	 *            {@link IKeyValueStore}.
	 * @param readOnly
	 *            True if the model does exist and is not changed. False
	 *            otherwise.
	 * @param packages
	 *            All meta-model packages that are/will be used by the model.
	 */
	public FragmentedModel(Module module, String name, boolean readOnly, EPackage... packages) {
		assert (module != null);

		// TODO FStoreImpl is build as a singleton, which is wrong with the
		// current design.
		FStoreImpl.INSTANCE = Guice.createInjector(module).getInstance(FStoreImpl.class);
		FStoreImpl.INSTANCE.initialize(Arrays.asList(packages), name, readOnly);
	}

	/**
	 * Saves all loaded and cached fragments to the underlying
	 * {@link IKeyValueStore}. Unloaded fragments are stored independently when
	 * they are changed and unloaded.
	 */
	public void save() {
{'fraction_non_alphanumeric': 0.1296381654759161, 'fraction_numerical': 0.0020742106476146576, 'mean_word_length': 3.5368531102979612, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '5451388', 'n_tokens_mistral': 2607, 'n_tokens_neox': 2382, 'n_words': 775}