package org.grouplens.lenskit.eval.config;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.PropertyHelper;

import javax.annotation.Nullable;
import java.util.Map;
import java.util.Properties;
import java.util.Random;

/**
 * An eval "project", the eval script equivalent of an Ant project.  This is used and configured
 * by an {@link EvalScript} and provides access to options, targets, etc.
 *
 * @since 1.3
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 */
public class EvalProject {
    private Project antProject;
    private Random random = new Random();

    /**
     * Construct a new eval project.
     * @param props A set of additional properties.  These properties will be available in the
     *              project, in addition to the system properties.  This is not where properties
     *              from the command line should be supplied; those should be set with
     *              {@link #setUserProperty(String, String)} so that they have Ant-like behavior.
     */
    public EvalProject(@Nullable Properties props) {
        antProject = new Project();
        antProject.init();
        if (props != null) {
            PropertyHelper ph = PropertyHelper.getPropertyHelper(antProject);
            for (Map.Entry<Object,Object> prop: props.entrySet()) {
                ph.setProperty(prop.getKey().toString(), prop.getValue().toString(), false);
            }
        }
    }

    /**
     * Get the Ant project from this eval project.
     *
     * @return The Ant project.
     */
    public Project getAntProject() {
        return antProject;
    }

    /**
     * Get the project's random number generator.
     * @return The project's random number generator.
     */
    public Random getRandom() {
        return random;
    }

    /**
     * Set the project's random number generator.
     * @param random The project's random number generator.
     */
    public void setRandom(Random random) {
        this.random = random;
    }

    /**
     * Get the (legacy) configuration view of this project.
     * @return An accessor for the project's configuration.
     */
    public EvalConfig getConfig() {
        return new EvalConfig(antProject.getProperties());
    }

    /**
     * Get a property value.
     * @param propertyName The property name.
     * @return The property value, or {@code null} if no such property exists.
     */
    public String getProperty(String propertyName) {
        return antProject.getProperty(propertyName);
    }

    /**
     * Set a "user" property.  These properties cannot be overridden by property sets in the script.
     * @param name The property to set.
     * @param value The property value.
     */
    public void setUserProperty(String name, String value) {
        antProject.setUserProperty(name, value);
    }

    /**
     * Execute a target in the project.
     * @param name The target to execute.
     */
    public void executeTarget(String name) throws BuildException {
        antProject.executeTarget(name);
    }
}

@@ -1,8 +1,1013 @@
+/*%0A * LensKit, an open source recommender systems toolkit.%0A * Copyright 2010-2013 Regents of the University of Minnesota and contributors%0A * Work on LensKit has been funded by the National Science Foundation under%0A * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.%0A *%0A * This program is free software; you can redistribute it and/or modify%0A * it under the terms of the GNU Lesser General Public License as%0A * published by the Free Software Foundation; either version 2.1 of the%0A * License, or (at your option) any later version.%0A *%0A * This program is distributed in the hope that it will be useful, but WITHOUT%0A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS%0A * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more%0A * details.%0A *%0A * You should have received a copy of the GNU General Public License along with%0A * this program; if not, write to the Free Software Foundation, Inc., 51%0A * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.%0A */%0A
 package 

Add missing license header