<commit_before>package org.graphwalker.cli.service;

/*
 * #%L
 * GraphWalker Command Line Interface
 * %%
 * Copyright (C) 2005 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import org.graphwalker.cli.CLI;
import org.graphwalker.cli.Util;
import org.graphwalker.core.machine.FailFastStrategy;
import org.graphwalker.core.machine.MachineException;
import org.graphwalker.core.machine.SimpleMachine;
import org.graphwalker.core.model.Action;
import org.json.JSONObject;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.util.Iterator;

/**
 * Created by krikar on 5/30/14.
 */
@Path("graphwalker")
public class Restful {
    CLI cli;
    private SimpleMachine machine;

    public Restful(CLI cli, Iterator itr) throws Exception {
        this.cli = cli;
        this.machine = new SimpleMachine(cli.getContextsWithPathGenerators(itr));
    }

    @GET
    @Produces("text/plain")
    @Path("hasNext")
    public String hasNext() {
        if (machine.hasNextStep()) {
            if (cli.getOnline().json) {
                JSONObject obj = new JSONObject();
                return obj.put("HasNext", "true").toString();
            } else {
                return "true";
            }
        } else {
            if (cli.getOnline().json) {
                JSONObject obj = new JSONObject();
                return obj.put("HasNext", "false").toString();
            } else {
                return "false";
            }
        }
    }

    @GET
    @Produces("text/plain")
    @Path("getNext")
    public String getNext() {
        try {
            machine.getNextStep();
            if (cli.getOnline().json) {
                return Util.getStepAsJSON(machine, cli.getOnline().verbose, cli.getOnline().unvisited).toString();
            } else {
                return Util.getStepAsString(machine, cli.getOnline().verbose, cli.getOnline().unvisited);
            }
        } catch (MachineException e) {
            throw e;
        }
    }

    @GET
    @Produces("text/plain")
    @Path("getData")
    public String getData(@QueryParam("key") String key) {
        String value = machine.getCurrentContext().getKeys().get(key);
        if (cli.getOnline().json) {
            JSONObject obj = new JSONObject();
            return obj.put("Value", value).toString();
        } else {<commit_msg>Added some documentation<commit_after>package org.graphwalker.cli.service;

/*
 * #%L
 * GraphWalker Command Line Interface
 * %%
 * Copyright (C) 2005 - 2014 GraphWalker
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
 */

import org.graphwalker.cli.CLI;
import org.graphwalker.cli.Util;
import org.graphwalker.core.machine.FailFastStrategy;
import org.graphwalker.core.machine.MachineException;
import org.graphwalker.core.machine.SimpleMachine;
import org.graphwalker.core.model.Action;
import org.json.JSONObject;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import java.util.Iterator;

/**
 * JAX-RS (Java API for RESTful Services (JAX-RS)) service implementation.
 * Created by krikar on 5/30/14.
 */
@Path("graphwalker")
public class Restful {
    CLI cli;
    private SimpleMachine machine;

    public Restful(CLI cli, Iterator itr) throws Exception {
        this.cli = cli;
        this.machine = new SimpleMachine(cli.getContextsWithPathGenerators(itr));
    }

    /**
     * Returns the next step (element) to be executed on the model. An element can
     * be either a vertex or an edge,
     * @return The label of the next step as a plain or a JSON formatted string. If the label is empty or
     * non-existent, the label of the step is an empty string.
     */
    @GET
    @Produces("text/plain")
    @Path("hasNext")
    public String hasNext() {
        if (machine.hasNextStep()) {
            if (cli.getOnline().json) {
                JSONObject obj = new JSONObject();
                return obj.put("HasNext", "true").toString();
            } else {
                return "true";
            }
        } else {
            if (cli.getOnline().json) {
                JSONObject obj = new JSONObject();
                return obj.put("HasNext", "false").toString();
            } else {
                return "false";
            }
        }
    }

    /**
     * Gets the next step (element) to be executed on the model. An element can
     * be either a vertex or an edge,
     * @return The label of the next step as a plain or a JSON formatted string. If the label is empty or
     * non-existent, the label of the step is an empty string.
     */
    @GET
    @Produces("text/plain")
    @Path("getNext")
    public String getNext() {
        try {
            machine.getNextStep();
            if (cli.getOnline().json) {
                return Util.getStepAsJSON(machine, cli.getOnline().verbose, cli.getOnline().unvisited).toString();
            } else {
                return Util.getStepAsString(machine, cli.getOnline().verbose, cli.getOnline().unvisited);
            }
        } catch (MachineException e) {
            throw e;
        }
    }

    @GET
    @Produces("text/plain")
    @Path("getData")
    public String getData(@QueryParam("key") String key) {
        String value = machine.getCurrentContext().getKeys().get(key);
        if (cli.getOnline().json) {
            JSONObject obj = new JSONObject();
            return obj.put("Value", value).toString();
        } else {
{'fraction_non_alphanumeric': 0.10236116607307744, 'fraction_numerical': 0.0034296266983247594, 'mean_word_length': 2.6434406535319557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6817482', 'n_tokens_mistral': 2158, 'n_tokens_neox': 2046, 'n_words': 721}