<commit_before>package eu.scasefp7.eclipse.reqeditor.handlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import eu.scasefp7.eclipse.core.ontology.StaticOntologyAPI;
import eu.scasefp7.eclipse.reqeditor.helpers.RQSHelpers;

/**
 * A command handler for exporting all the annotated instances to the static ontology.
 * 
 * @author themis
 */
public class ExportToOntologyHandler extends ProjectAwareHandler {

	/**
	 * This function is called when the user selects the menu item. It reads the selected resource(s) and populates the
	 * static ontology.
	 * 
	 * @param event the event containing the information about which file was selected.
	 * @return the result of the execution which must be {@code null}.
	 */
	@SuppressWarnings("unchecked")
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			List<Object> selectionList = structuredSelection.toList();
			// Iterate over the selected files
			for (Object object : selectionList) {
				IFile file = (IFile) Platform.getAdapterManager().getAdapter(object, IFile.class);
				if (file == null) {
					if (object instanceof IAdaptable) {
						file = (IFile) ((IAdaptable) object).getAdapter(IFile.class);
					}
				}
				if (file != null) {
					// Instantiate the static ontology
					StaticOntologyAPI ontology = new StaticOntologyAPI(file.getProject(), true);
					instantiateOntology(file, ontology);
					ontology.close();
				}
			}
		}
		return null;
	}

	protected void instantiateOntology(IFile file, StaticOntologyAPI ontology) {
		// Read the annotations and add them to the ontology
		ArrayList<String> requirements = RQSHelpers.getRequirements(file);
		ArrayList<String> annotations = RQSHelpers.getAnnotations(file);

		for (int i = 1; i < requirements.size() + 1; i++) {
			// Add a new requirement
			String reqId = "FR" + i;
			ontology.addRequirement(reqId);

			HashMap<String, String> idmap = new HashMap<String, String>();
			for (String annotation : annotations) {
				if (annotation.startsWith(i + ":")) {
					String Id = annotation.split("\t")[0];
					String type = Id.split(":")[1].substring(0, 1);
					if (type.equals("T")) {
						// Add a class instance in the ontology
						String anntype = annotation.split("\t")[1].split(" ")[0];
						String annword = annotation.split("\t")[2];
						if (anntype.equals("Actor")) {
							ontology.addActor(annword);
							ontology.connectRequirementToConcept(reqId, annword);
						} else if (anntype.equals("Action")) {<commit_msg>Added the text of requirements as comment when exporting to the ontology<commit_after>package eu.scasefp7.eclipse.reqeditor.handlers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.handlers.HandlerUtil;

import eu.scasefp7.eclipse.core.ontology.StaticOntologyAPI;
import eu.scasefp7.eclipse.reqeditor.helpers.RQSHelpers;

/**
 * A command handler for exporting all the annotated instances to the static ontology.
 * 
 * @author themis
 */
public class ExportToOntologyHandler extends ProjectAwareHandler {

	/**
	 * This function is called when the user selects the menu item. It reads the selected resource(s) and populates the
	 * static ontology.
	 * 
	 * @param event the event containing the information about which file was selected.
	 * @return the result of the execution which must be {@code null}.
	 */
	@SuppressWarnings("unchecked")
	public Object execute(ExecutionEvent event) throws ExecutionException {
		ISelection selection = HandlerUtil.getCurrentSelection(event);
		if (selection instanceof IStructuredSelection) {
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
			List<Object> selectionList = structuredSelection.toList();
			// Iterate over the selected files
			for (Object object : selectionList) {
				IFile file = (IFile) Platform.getAdapterManager().getAdapter(object, IFile.class);
				if (file == null) {
					if (object instanceof IAdaptable) {
						file = (IFile) ((IAdaptable) object).getAdapter(IFile.class);
					}
				}
				if (file != null) {
					// Instantiate the static ontology
					StaticOntologyAPI ontology = new StaticOntologyAPI(file.getProject(), true);
					instantiateOntology(file, ontology);
					ontology.close();
				}
			}
		}
		return null;
	}

	protected void instantiateOntology(IFile file, StaticOntologyAPI ontology) {
		// Read the annotations and add them to the ontology
		ArrayList<String> requirements = RQSHelpers.getRequirements(file);
		ArrayList<String> annotations = RQSHelpers.getAnnotations(file);

		for (int i = 1; i < requirements.size() + 1; i++) {
			// Add a new requirement
			String reqId = "FR" + i;
			ontology.addRequirement(reqId, requirements.get(i - 1));

			HashMap<String, String> idmap = new HashMap<String, String>();
			for (String annotation : annotations) {
				if (annotation.startsWith(i + ":")) {
					String Id = annotation.split("\t")[0];
					String type = Id.split(":")[1].substring(0, 1);
					if (type.equals("T")) {
						// Add a class instance in the ontology
						String anntype = annotation.split("\t")[1].split(" ")[0];
						String annword = annotation.split("\t")[2];
						if (anntype.equals("Actor")) {
							ontology.addActor(annword);
							ontology.connectRequirementToConcept(reqId, annword);
						} else if (anntype.equals("Action")) {
{'fraction_non_alphanumeric': 0.11297539149888143, 'fraction_numerical': 0.003994886545222115, 'mean_word_length': 4.211490424646128, 'pattern_counts': {'":': 4, '<': 15, '<?xml version=': 0, '>': 13, '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': '1896594', 'n_tokens_mistral': 2139, 'n_tokens_neox': 1816, 'n_words': 485}