<commit_before>/*******************************************************************************
 * Copyright (c) 2015 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * <NAME> - initial API and implementation
 ******************************************************************************/


package com.net2plan.cli.plugins;

import com.net2plan.cli.plugins.utils.ClassUtils;
import com.net2plan.interfaces.networkDesign.*;
import com.net2plan.internal.CommandLineParser;
import com.net2plan.internal.plugins.ICLIModule;
import com.net2plan.utils.ClassLoaderUtils;
import com.net2plan.utils.StringUtils;
import com.net2plan.utils.Triple;
import org.apache.commons.cli.*;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Offline network design tool (CLI mode).
 *
 * @author <NAME>, <NAME>
 */
public class CLINetworkDesign extends ICLIModule
{
    private final static String TITLE = "Offline network design";
    private final static Options OPTIONS;

    static
    {
        OPTIONS = new Options();

        Option inputFile = new Option(null, "input-file", true, "(Optional) .n2p file containing an initial network design");
        inputFile.setType(PatternOptionBuilder.FILE_VALUE);
        inputFile.setArgName("file");
        OPTIONS.addOption(inputFile);

        Option trafficFile = new Option(null, "traffic-file", true, "(Optional) .n2p file containing traffic demands");
        trafficFile.setType(PatternOptionBuilder.FILE_VALUE);
        trafficFile.setArgName("file");
        OPTIONS.addOption(trafficFile);

        Option trafficLayer = new Option(null, "traffic-layer", true, "(Optional) Identifier of the layer where demands from 'traffic-file' will be loaded");
        trafficLayer.setType(PatternOptionBuilder.NUMBER_VALUE);
        trafficLayer.setArgName("layer");
        OPTIONS.addOption(trafficLayer);

        Option internalSearch = new Option(null, "package-search", true, "(Optional) Search for algorithm in the application's class-path. Looks for the algorithm under the given package name. Class-file is unused under this context.");
        internalSearch.setRequired(false);
        OPTIONS.addOption(internalSearch);

        Option classFile = new Option(null, "class-file", true, ".class/.jar file containing the algorithm");
        classFile.setType(PatternOptionBuilder.FILE_VALUE);
        classFile.setArgName("file");
        classFile.setRequired(true);
        OPTIONS.addOption(classFile);

        Option className = new Option(null, "class-name", true, "Class name of the algorithm (package name could be omitted)");
        className.setType(PatternOptionBuilder.STRING_VALUE);
        className.setArgName("classname");
        className.setRequired(true);
        OPTIONS.addOption(className);

        Option outputFile = new Option(null, "output-file", true, ".n2p file where saving the resulting design");
        outputFile.setType(PatternOptionBuilder.FILE_VALUE);
        outputFile.setArgName("file");
        outputFile.setRequired(true);
        OPTIONS.addOption(outputFile);

        Option algorithmParameters = new Option(null, "alg-param", true, "(Optional) algorithm parameters (use one of this for each parameter)");
        algorithmParameters.setArgName("property=value");
        algorithmParameters.setArgs(2);
        algorithmParameters.setValueSeparator('=');
        OPTIONS.addOption(algorithmParameters);
    }

    @Override
    public void executeFromCommandLine(String[] args) throws ParseException
    {
        final CommandLineParser parser = new CommandLineParser();
        final CommandLine cli = parser.parse(OPTIONS, args);

        final File classFile = (File) cli.getParsedOptionValue("class-file");
        final String className = (String) cli.getParsedOptionValue("class-name");

        NetPlan netPlan;
        if (cli.hasOption("input-file"))
        {
            final File inputFile = (File) cli.getParsedOptionValue("input-file");
            netPlan = new NetPlan(inputFile);
        } else
        {
            netPlan = new NetPlan();
        }

        if (cli.hasOption("traffic-file"))
        {
            NetPlan demands = new NetPlan((File) cli.getParsedOptionValue("traffic-file"));

            NetworkLayer layer = (cli.hasOption("traffic-layer")) ? netPlan.getNetworkLayerFromId(((Number) cli.getParsedOptionValue("traffic-layer")).longValue()) : netPlan.getNetworkLayerDefault();

            netPlan.removeAllDemands(layer);
            for (Demand demand : demands.getDemands())
            {
                netPlan.addDemand(demand.getIngressNode(), demand.getEgressNode(), demand.getOfferedTraffic(), demand.getAttributes(), layer);
            }
            netPlan.removeAllMulticastDemands(layer);
            for (MulticastDemand demand : demands.getMulticastDemands())
            {
                netPlan.addMulticastDemand(demand.getIngressNode(), demand.getEgressNodes(), demand.getOfferedTraffic(), demand.getAttributes(), layer);
            }
        }

        File outputFile = (File) cli.getParsedOptionValue("output-file");

        IAlgorithm algorithm;
        if (!cli.hasOption("package-search"))
        {
            algorithm = ClassLoaderUtils.getInstance(classFile, className, IAlgorithm.class);
        } else
        {
            algorithm = ClassUtils.findAlgorithm(className, cli.getOptionValue("package-search"));
        }
<commit_msg>Putting the two options under a option group, only one option at a time<commit_after>/*******************************************************************************
 * Copyright (c) 2015 <NAME>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Contributors:
 * <NAME> - initial API and implementation
 ******************************************************************************/


package com.net2plan.cli.plugins;

import com.net2plan.cli.plugins.utils.ClassUtils;
import com.net2plan.interfaces.networkDesign.*;
import com.net2plan.internal.CommandLineParser;
import com.net2plan.internal.plugins.ICLIModule;
import com.net2plan.utils.ClassLoaderUtils;
import com.net2plan.utils.StringUtils;
import com.net2plan.utils.Triple;
import org.apache.commons.cli.*;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Offline network design tool (CLI mode).
 *
 * @author <NAME>, <NAME>
 */
public class CLINetworkDesign extends ICLIModule
{
    private final static String TITLE = "Offline network design";
    private final static Options OPTIONS;

    static
    {
        OPTIONS = new Options();

        Option inputFile = new Option(null, "input-file", true, "(Optional) .n2p file containing an initial network design");
        inputFile.setType(PatternOptionBuilder.FILE_VALUE);
        inputFile.setArgName("file");
        OPTIONS.addOption(inputFile);

        Option trafficFile = new Option(null, "traffic-file", true, "(Optional) .n2p file containing traffic demands");
        trafficFile.setType(PatternOptionBuilder.FILE_VALUE);
        trafficFile.setArgName("file");
        OPTIONS.addOption(trafficFile);

        Option trafficLayer = new Option(null, "traffic-layer", true, "(Optional) Identifier of the layer where demands from 'traffic-file' will be loaded");
        trafficLayer.setType(PatternOptionBuilder.NUMBER_VALUE);
        trafficLayer.setArgName("layer");
        OPTIONS.addOption(trafficLayer);

        OptionGroup group = new OptionGroup();
        group.setRequired(true);

        Option packageSearch = new Option(null, "package-name", true, "Search for algorithm under the given package in the application's class-path");
        packageSearch.setType(PatternOptionBuilder.STRING_VALUE);
        packageSearch.setArgName("package");

        Option classFile = new Option(null, "class-file", true, ".class/.jar file containing the algorithm");
        classFile.setType(PatternOptionBuilder.FILE_VALUE);
        classFile.setArgName("file");

        group.addOption(packageSearch);
        group.addOption(classFile);
        OPTIONS.addOptionGroup(group);

        Option className = new Option(null, "class-name", true, "Class name of the algorithm (package name could be omitted)");
        className.setType(PatternOptionBuilder.STRING_VALUE);
        className.setArgName("classname");
        OPTIONS.addOption(className);

        Option outputFile = new Option(null, "output-file", true, ".n2p file where saving the resulting design");
        outputFile.setType(PatternOptionBuilder.FILE_VALUE);
        outputFile.setArgName("file");
        outputFile.setRequired(true);
        OPTIONS.addOption(outputFile);

        Option algorithmParameters = new Option(null, "alg-param", true, "(Optional) algorithm parameters (use one of this for each parameter)");
        algorithmParameters.setArgName("property=value");
        algorithmParameters.setArgs(2);
        algorithmParameters.setValueSeparator('=');
        OPTIONS.addOption(algorithmParameters);
    }

    @Override
    public void executeFromCommandLine(String[] args) throws ParseException
    {
        final CommandLineParser parser = new CommandLineParser();
        final CommandLine cli = parser.parse(OPTIONS, args);

        final File classFile = (File) cli.getParsedOptionValue("class-file");
        final String className = (String) cli.getParsedOptionValue("class-name");

        NetPlan netPlan;
        if (cli.hasOption("input-file"))
        {
            final File inputFile = (File) cli.getParsedOptionValue("input-file");
            netPlan = new NetPlan(inputFile);
        } else
        {
            netPlan = new NetPlan();
        }

        if (cli.hasOption("traffic-file"))
        {
            NetPlan demands = new NetPlan((File) cli.getParsedOptionValue("traffic-file"));

            NetworkLayer layer = (cli.hasOption("traffic-layer")) ? netPlan.getNetworkLayerFromId(((Number) cli.getParsedOptionValue("traffic-layer")).longValue()) : netPlan.getNetworkLayerDefault();

            netPlan.removeAllDemands(layer);
            for (Demand demand : demands.getDemands())
            {
                netPlan.addDemand(demand.getIngressNode(), demand.getEgressNode(), demand.getOfferedTraffic(), demand.getAttributes(), layer);
            }
            netPlan.removeAllMulticastDemands(layer);
            for (MulticastDemand demand : demands.getMulticastDemands())
            {
                netPlan.addMulticastDemand(demand.getIngressNode(), demand.getEgressNodes(), demand.getOfferedTraffic(), demand.getAttributes(), layer);
            }
        }

        File outputFile = (File) cli.getParsedOptionValue("output-file");

        IAlgorithm algorithm;
        if (!cli.hasOption("package-name"))
        {
            algorithm = ClassLoaderUtils.getInstance(classFile, className, IAlgorithm.class);
        } else
        {
            algorithm = ClassUtils.findAlgorithm(className, cli.getOptionValue("package-name"));
        }

{'fraction_non_alphanumeric': 0.1336964160224877, 'fraction_numerical': 0.003162333099086437, 'mean_word_length': 3.3991499227202473, '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': '6652570', 'n_tokens_mistral': 3036, 'n_tokens_neox': 2960, 'n_words': 698}