<commit_before>package org.score.samples;

import com.hp.score.events.EventConstants;
import org.score.samples.openstack.actions.ExecutionPlanBuilder;
import com.hp.score.api.ExecutionPlan;
import com.hp.score.api.Score;
import com.hp.score.events.EventBus;
import com.hp.score.events.ScoreEvent;
import com.hp.score.events.ScoreEventListener;
import org.apache.log4j.Logger;
import org.score.samples.openstack.actions.MatchType;
import org.score.samples.openstack.actions.NavigationMatcher;
import org.score.samples.openstack.actions.OOActionRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Date: 7/28/2014
 *
 * @author <NAME>
 */
public class OpenstackApplications {
	private final static Logger logger = Logger.getLogger(OpenstackApplications.class);
	private ApplicationContext context;

	@Autowired
	private Score score;

	@Autowired
	private EventBus eventBus;

	public static void main(String[] args) {
		OpenstackApplications app = loadApp();
		app.registerEventListeners();
		app.start();
	}

	private void start() {
		    String command = "";
			BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

			while(!command.equals("4")) {
				System.out.println("Select command:");
				System.out.println("1 - Create server on OpenStack");
				System.out.println("2 - List servers from OpenStack");
				System.out.println("3 - Input missing scenario");
				System.out.println("4 - Quit\n");

				System.out.print("Command:\n");
				command = readLineDenyNulls(reader);

				switch (command) {

					case "1":
						createServerInputs(true);
						break;
					case "2":
						listServerInputs(true);
						break;
					case "3":
						listServerInputs(false);
						break;
					case "4":
						System.exit(0);
						break;
					default:
						System.out.println("Unknown command..");
						break;
				}
			}
	}

	private void listServerInputs(Boolean nullAllowed) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String username;
		String password;
		String host;
		String port;
		try {
			System.out.println("Host: ");
			host = readLineAdmitNulls(br);
			System.out.println("Port: ");
			port = br.readLine();
			System.out.println("Username: ");
			username = br.readLine();
			System.out.println("Password: ");
			password = br.readLine();
			listServers(host, port, username, password, nullAllowed);
		} catch (IOException ioe) {
			System.out.println("IO error");
			System.exit(1);
		}
	}

	private void createServerInputs(Boolean nullAllowed) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String username;
		String password;
		String host;
		String port;
		String serverName;
		try {
			System.out.println("Host: ");
			host = br.readLine();
			System.out.println("Port: ");
			port = br.readLine();
			System.out.println("Username: ");
			username = br.readLine();
			System.out.println("Password: ");
			password = br.readLine();
			System.out.println("Server name: ");
			serverName = br.readLine();
			createServer(host, port, serverName, username, password, nullAllowed);
		} catch (IOException ioe) {
			System.out.println("IO error");
			System.exit(1);
		}
	}

	private void createServer(String host, String port, String serverName, String username, String password, Boolean nullAllowed){
		ExecutionPlanBuilder builder = new ExecutionPlanBuilder();
		Map<String, Serializable> executionContext = new HashMap<>();

		createGetTokenStep(host, port, username, password, builder, executionContext, nullAllowed);

		createContextMergerStep(builder, nullAllowed);

		startServerStep(serverName, builder, executionContext, nullAllowed);

		ExecutionPlan executionPlan = builder.getExecutionPlan();
		score.trigger(executionPlan, executionContext);
	}

	private void createGetTokenStep(String host, String port, String username, String password, ExecutionPlanBuilder builder, Map<String, Serializable> executionContext, Boolean nullAllowed){
		String url = "http://" + host + ":" + port + "/v2.0/tokens";
		String body = "{\"auth\": {\"tenantName\": \"demo\",\"passwordCredentials\": {\"username\": \"" + username +"\",\"password\": \"" + password + "\"}}}";

		executionContext.put("url", url);
		executionContext.put("method", "post");
		executionContext.put("body", body);
		executionContext.put("contentType", "application/json");

		List<NavigationMatcher<Serializable>> navigationMatchers = new ArrayList<>();

		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.EQUAL, "returnCode", "0", 1L)); // how will we know the key
		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.DEFAULT, 3L));

		builder.addOOActionStep(0L, "org.score.content.httpclient.HttpClientAction", "execute", nullAllowed, navigationMatchers);
	}

	private void createContextMergerStep(ExecutionPlanBuilder builder, Boolean nullAllowed) {
		List<NavigationMatcher<Serializable>> navigationMatchers = new ArrayList<>();

		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.EQUAL, "result", "0", 2L)); // how will we know the key
		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.DEFAULT, 3L));

		builder.addOOActionStep(1L, "org.score.samples.openstack.actions.ContextMerger", "prepareCreateServer", nullAllowed, navigationMatchers);
	}

	private void startServerStep(String serverName, ExecutionPlanBuilder builder, Map<String, Serializable> executionContext, Boolean nullAllowed){
		executionContext.put("serverName", serverName);
		executionContext.put("method", "post");

		List<NavigationMatcher<Serializable>> navigationMatchers = new ArrayList<>();

		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.EQUAL, "returnCode", "0", 3L)); // how will we know the key
		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.DEFAULT, 3L));

		builder.addOOActionStep(2L, "org.score.content.httpclient.HttpClientAction", "execute", nullAllowed, navigationMatchers);
		builder.addOOActionFinalStep(3L, "org.score.samples.openstack.actions.FinalStepActions", "successStepAction");
	}

	private void listServers(String host, String port, String username, String password, Boolean nullAllowed){
		ExecutionPlanBuilder builder = new ExecutionPlanBuilder();
		Map<String, Serializable> executionContext = new HashMap<>();

		createGetTokenStep(host, port, username, password, builder, executionContext, nullAllowed);

		createPrepareGetServersStep(builder, nullAllowed);

		createGetServersStep(builder, nullAllowed);

		ExecutionPlan executionPlan = builder.getExecutionPlan();
		score.trigger(executionPlan, executionContext);<commit_msg>adapted OpenstackApplications after TriggeringProperties feature<commit_after>package org.score.samples;

import com.hp.score.api.TriggeringProperties;
import com.hp.score.events.EventConstants;
import org.score.samples.openstack.actions.ExecutionPlanBuilder;
import com.hp.score.api.ExecutionPlan;
import com.hp.score.api.Score;
import com.hp.score.events.EventBus;
import com.hp.score.events.ScoreEvent;
import com.hp.score.events.ScoreEventListener;
import org.apache.log4j.Logger;
import org.score.samples.openstack.actions.MatchType;
import org.score.samples.openstack.actions.NavigationMatcher;
import org.score.samples.openstack.actions.OOActionRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Date: 7/28/2014
 *
 * @author <NAME>
 */
public class OpenstackApplications {
	private final static Logger logger = Logger.getLogger(OpenstackApplications.class);
	private ApplicationContext context;

	@Autowired
	private Score score;

	@Autowired
	private EventBus eventBus;

	public static void main(String[] args) {
		OpenstackApplications app = loadApp();
		app.registerEventListeners();
		app.start();
	}

	private void start() {
		String command = "";
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

		while(!command.equals("4")) {
			System.out.println("Select command:");
			System.out.println("1 - Create server on OpenStack");
			System.out.println("2 - List servers from OpenStack");
			System.out.println("3 - Input missing scenario");
			System.out.println("4 - Quit\n");

			System.out.print("Command:\n");
			command = readLineDenyNulls(reader);

			switch (command) {

				case "1":
					createServerInputs(true);
					break;
				case "2":
					listServerInputs(true);
					break;
				case "3":
					listServerInputs(false);
					break;
				case "4":
					System.exit(0);
					break;
				default:
					System.out.println("Unknown command..");
					break;
			}
		}
	}

	private void listServerInputs(Boolean nullAllowed) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String username;
		String password;
		String host;
		String port;
		try {
			System.out.println("Host: ");
			host = readLineAdmitNulls(br);
			System.out.println("Port: ");
			port = br.readLine();
			System.out.println("Username: ");
			username = br.readLine();
			System.out.println("Password: ");
			password = br.readLine();
			listServers(host, port, username, password, nullAllowed);
		} catch (IOException ioe) {
			System.out.println("IO error");
			System.exit(1);
		}
	}

	private void createServerInputs(Boolean nullAllowed) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String username;
		String password;
		String host;
		String port;
		String serverName;
		try {
			System.out.println("Host: ");
			host = br.readLine();
			System.out.println("Port: ");
			port = br.readLine();
			System.out.println("Username: ");
			username = br.readLine();
			System.out.println("Password: ");
			password = br.readLine();
			System.out.println("Server name: ");
			serverName = br.readLine();
			createServer(host, port, serverName, username, password, nullAllowed);
		} catch (IOException ioe) {
			System.out.println("IO error");
			System.exit(1);
		}
	}

	private void createServer(String host, String port, String serverName, String username, String password, Boolean nullAllowed){
		ExecutionPlanBuilder builder = new ExecutionPlanBuilder();
		Map<String, Serializable> executionContext = new HashMap<>();

		createGetTokenStep(host, port, username, password, builder, executionContext, nullAllowed);

		createContextMergerStep(builder, nullAllowed);

		startServerStep(serverName, builder, executionContext, nullAllowed);

		triggerWithContext(builder, executionContext);
	}

	private void createGetTokenStep(String host, String port, String username, String password, ExecutionPlanBuilder builder, Map<String, Serializable> executionContext, Boolean nullAllowed){
		String url = "http://" + host + ":" + port + "/v2.0/tokens";
		String body = "{\"auth\": {\"tenantName\": \"demo\",\"passwordCredentials\": {\"username\": \"" + username +"\",\"password\": \"" + password + "\"}}}";

		executionContext.put("url", url);
		executionContext.put("method", "post");
		executionContext.put("body", body);
		executionContext.put("contentType", "application/json");

		List<NavigationMatcher<Serializable>> navigationMatchers = new ArrayList<>();

		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.EQUAL, "returnCode", "0", 1L)); // how will we know the key
		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.DEFAULT, 3L));

		builder.addOOActionStep(0L, "org.score.content.httpclient.HttpClientAction", "execute", nullAllowed, navigationMatchers);
	}

	private void createContextMergerStep(ExecutionPlanBuilder builder, Boolean nullAllowed) {
		List<NavigationMatcher<Serializable>> navigationMatchers = new ArrayList<>();

		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.EQUAL, "result", "0", 2L)); // how will we know the key
		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.DEFAULT, 3L));

		builder.addOOActionStep(1L, "org.score.samples.openstack.actions.ContextMerger", "prepareCreateServer", nullAllowed, navigationMatchers);
	}

	private void startServerStep(String serverName, ExecutionPlanBuilder builder, Map<String, Serializable> executionContext, Boolean nullAllowed){
		executionContext.put("serverName", serverName);
		executionContext.put("method", "post");

		List<NavigationMatcher<Serializable>> navigationMatchers = new ArrayList<>();

		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.EQUAL, "returnCode", "0", 3L)); // how will we know the key
		navigationMatchers.add(new NavigationMatcher<Serializable>(MatchType.DEFAULT, 3L));

		builder.addOOActionStep(2L, "org.score.content.httpclient.HttpClientAction", "execute", nullAllowed, navigationMatchers);
		builder.addOOActionFinalStep(3L, "org.score.samples.openstack.actions.FinalStepActions", "successStepAction");
	}

	private void listServers(String host, String port, String username, String password, Boolean nullAllowed){
		ExecutionPlanBuilder builder = new ExecutionPlanBuilder();
		Map<String, Serializable> executionContext = new HashMap<>();

		createGetTokenStep(host, port, username, password, builder, executionContext, nullAllowed);

		createPrepareGetServersStep(builder, nullAllowed);

		createGetServersStep(builder, nullAllowed);

		triggerWithContext(builder, executionContext);
	}

	private void triggerWithContext(ExecutionPlanBuilder builder, Map<String, Serializable> executionContext) {
		ExecutionPlan executionPlan = builder.getExecutionPlan();
		TriggeringProperties triggeringProperties = TriggeringProperties.create(executionPlan);
		triggeringProperties.setContext(executionContext);
		triggeringProperties.setStartStep(0L);
		score.trigger(triggeringProperties);
{'fraction_non_alphanumeric': 0.12570292140995748, 'fraction_numerical': 0.004869016595803045, 'mean_word_length': 5.45837023914969, 'pattern_counts': {'":': 20, '<': 48, '<?xml version=': 0, '>': 48, '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': '7395612', 'n_tokens_mistral': 4563, 'n_tokens_neox': 4113, 'n_words': 895}