<commit_before>package org.intermine.task;

/*
 * Copyright (C) 2002-2010 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.intermine.metadata.ClassDescriptor;
import org.intermine.metadata.Model;
import org.intermine.modelproduction.ModelMerger;
import org.intermine.modelproduction.xml.InterMineModelParser;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * Task to merge a single additions file into an intermine XML model.
 *
 * @see org.intermine.modelproduction.ModelMerger
 * @author <NAME>
 */

public class ModelMergerTask extends Task
{
    protected List<File> additionsFiles = new ArrayList<File>();
    protected File inputModelFile;
    protected File outputModelFile;

    /**
     * Set the model to add additions to.
     * @param file path to model file
     */
    public void setInputModelFile(File file) {
        inputModelFile = file;
    }

    /**
     * The file containing model additions.
     * @param file the additions file
     */
    public void setAdditionsFile(File file) {
        additionsFiles.add(file);
    }

    /**
     * The files containing model additions.
     * @param files the additions files
     */
    public void setAdditionsFiles(List<File> files) {
        additionsFiles = files;
    }

    /**
     * Path of file to write resulting model to. May be the same as <code>inputModelFile</code>.
     * @param file path to write resulting model to
     */
    public void setOutputFile(File file) {
        outputModelFile = file;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute() {
        Model mergedModel = null;
        try {
            InterMineModelParser parser = new InterMineModelParser();
            FileReader reader = new FileReader(inputModelFile);
            mergedModel = parser.process(reader);
            reader.close();
        } catch (Exception e) {
            throw new BuildException("failed to read model file: " + inputModelFile, e);
        }
        if (additionsFiles.size() == 0) {
            throw new BuildException("no addition files set");
        } else {
            for (File additionsFile: additionsFiles) {
                mergedModel = processFile(mergedModel, additionsFile);
            }
        }
        try {
            FileWriter writer = new FileWriter(outputModelFile);
            writer.write(mergedModel.toString());
            writer.close();
        } catch (IOException e) {
            throw new BuildException("failed to write model file: " + outputModelFile, e);
        }
    }

    private Model processFile(Model mergedModel, File newAdditionsFile) {
        try {
            InterMineModelParser parser = new InterMineModelParser();
            Set<ClassDescriptor> additionClds =
                parser.generateClassDescriptors(new FileReader(newAdditionsFile),
                        mergedModel.getPackageName());
            System.err .println("merging model additions from: " + newAdditionsFile);
            Model newMergedModel = ModelMerger.mergeModel(mergedModel, additionClds);
            return newMergedModel;
        } catch (Exception e) {
            throw new BuildException("Exception while merging " + newAdditionsFile + " into "
                                     + inputModelFile, e);
        }
    }
}<commit_msg>Refactored out the model merging process to be more atomic<commit_after>package org.intermine.task;

/*
 * Copyright (C) 2002-2010 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;
import java.util.List;

import org.intermine.metadata.MetaDataException;
import org.intermine.metadata.Model;
import org.intermine.modelproduction.ModelFileMerger;
import org.intermine.modelproduction.xml.InterMineModelParser;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

/**
 * Task to merge a single additions file into an intermine XML model.
 *
 * @see org.intermine.modelproduction.ModelMerger
 * @author <NAME>
 */

public class ModelMergerTask extends Task
{
    protected List<File> additionsFiles = new ArrayList<File>();
    protected File inputModelFile;
    protected File outputModelFile;

    /**
     * Set the model to add additions to.
     * @param file path to model file
     */
    public void setInputModelFile(File file) {
        inputModelFile = file;
    }

    /**
     * The file containing model additions.
     * @param file the additions file
     */
    public void setAdditionsFile(File file) {
        additionsFiles.add(file);
    }

    /**
     * The files containing model additions.
     * @param files the additions files
     */
    public void setAdditionsFiles(List<File> files) {
        additionsFiles = files;
    }

    /**
     * Path of file to write resulting model to. May be the same as <code>inputModelFile</code>.
     * @param file path to write resulting model to
     */
    public void setOutputFile(File file) {
        outputModelFile = file;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute() {
    	InterMineModelParser parser = new InterMineModelParser();
    	Model mergedModel;
    	try {
    		mergedModel = ModelFileMerger.mergeModelFromFiles(inputModelFile, additionsFiles, parser);
    	} catch (MetaDataException e) {
    		throw new BuildException("Failed to parse model from input files", e);
    	}
        
        try {
            FileWriter writer = new FileWriter(outputModelFile);
            writer.write(mergedModel.toString());
            writer.close();
        } catch (IOException e) {
            throw new BuildException("failed to write model file: " + outputModelFile, e);
        }
    }
}
{'fraction_non_alphanumeric': 0.08979974968710888, 'fraction_numerical': 0.0026595744680851063, 'mean_word_length': 2.603720405862458, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741601', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1709, 'n_words': 513}