All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bigml.binding.resources.Execution Maven / Gradle / Ivy

Go to download

An open source Java client that gives you a simple binding to interact with BigML. You can use it to easily create, retrieve, list, update, and delete BigML resources.

The newest version!
package org.bigml.binding.resources;

import org.bigml.binding.BigMLClient;
import org.bigml.binding.utils.CacheManager;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;


/**
 * This class is used by the BigML class as a mixin that provides the Whizzml
 * Execution' REST calls.
 *
 * It should not be instantiated independently.
 *
 * Full API documentation on the API can be found from BigML at:
 * https://bigml.com/api/executions
 *
 */
public class Execution extends AbstractResource {

    // Logging
    Logger logger = LoggerFactory.getLogger(Execution.class);


    /**
     * Constructor
     *
     * @param bigmlClient	the client with connection to BigML
     * @param apiUser		API user
     * @param apiKey		API key
     * @param project		project id
     * @param organization	organization id
     * @param cacheManager	cache manager
     */
    public Execution(final BigMLClient bigmlClient,
    				 final String apiUser, final String apiKey,
    				 final String project, final String organization,
    				 final CacheManager cacheManager) {
    		super.init(bigmlClient, apiUser, apiKey, project, organization,
    				   cacheManager, EXECUTION_RE, EXECUTION_PATH);
    }

    /**
     * Creates a whizzml execution for a script.
     *
     * POST /andromeda/execution?username=$BIGML_USERNAME&api_key=$BIGML_API_KEY&
     * HTTP/1.1 Host: bigml.io Content-Type: application/json
     *
     * @param script
     *            a unique identifier in the form script/id where id is a string
     *            of 24 alpha-numeric chars for the script to attach the execution.
     * @param args
     *            set of parameters for the new execution. Optional
     * @param waitTime
     *            time (milliseconds) to wait for next check of FINISHED status
     *            for source before to start to create the execution. Optional
     * @param retries
     *            number of times to try the operation. Optional
     *
     * @return a JSONObject for the new execution
     */
    public JSONObject create(String script, JSONObject args,
                             Integer waitTime, Integer retries) {

        try {
            JSONObject requestObject = new JSONObject();
            if (args != null) {
                requestObject = args;
            }
            if (script != null && script.matches(SCRIPT_RE)) {
            		waitForResource(script, "scriptIsReady", waitTime, retries);

                requestObject.put("script", script);
                return createResource(resourceUrl,
                		requestObject.toJSONString());
            } else {
                logger.info("A script id or a list of them is needed to create a whizzml execution");
                return null;
            }
        } catch (Throwable e) {
            logger.error("Error creating execution");
            return null;
        }
    }


    /**
     * Creates a whizzml execution for a list of scripts.
     *
     * POST /andromeda/execution?username=$BIGML_USERNAME&api_key=$BIGML_API_KEY&
     * HTTP/1.1 Host: bigml.io Content-Type: application/json
     *
     * @param scripts
     *            a list of identifiers in the form script/id where id is a string
     *            of 24 alpha-numeric chars for the script to attach the execution.
     * @param args
     *            set of parameters for the new execution. Optional
     * @param waitTime
     *            time (milliseconds) to wait for next check of FINISHED status
     *            for source before to start to create the execution. Optional
     * @param retries
     *            number of times to try the operation. Optional
     *
     * @return a JSONObject for the new execution
     */
    public JSONObject create(List scripts, JSONObject args,
                             Integer waitTime, Integer retries) {

        if (scripts == null || scripts.size()==0) {
            logger.info("A script id or a list of them is needed to create a whizzml execution");
            return null;
        }

        try {
            JSONObject requestObject = new JSONObject();
            if (args != null) {
                requestObject = args;
            }
            List scriptsIds = new ArrayList();

            for (String scriptId : scripts) {
                // Checking valid scriptId
                if (scriptId == null || scriptId.length() == 0
                        || !(scriptId.matches(SCRIPT_RE))) {
                    logger.info("Wrong scriptId id");
                    return null;
                }

                // Checking status
                try {
                		waitForResource(scriptId, "scriptIsReady", waitTime, retries);
                    scriptsIds.add(scriptId);
                } catch (Throwable e) {
                    logger.error("Error creating object");
                    return null;
                }
            }

            requestObject.put("scripts", scriptsIds);
            return createResource(resourceUrl,
            		requestObject.toJSONString());
        } catch (Throwable e) {
            logger.error("Error creating execution");
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy