<commit_before>/*
 * generated by Xtext 2.11.0
 */
package de.cooperateproject.modeling.textual.usecase.validation;

import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.validation.Check;

import com.google.inject.Inject;

import de.cooperateproject.modeling.textual.common.metamodel.textualCommons.NamedElement;
import de.cooperateproject.modeling.textual.usecase.usecase.Actor;
import de.cooperateproject.modeling.textual.usecase.usecase.RootPackage;
import de.cooperateproject.modeling.textual.usecase.usecase.System;
import de.cooperateproject.modeling.textual.usecase.usecase.UseCase;
import de.cooperateproject.modeling.textual.usecase.usecase.UsecasePackage;
import de.cooperateproject.modeling.textual.xtext.runtime.issues.IIssueCodeRegistry;
import de.cooperateproject.modeling.textual.xtext.runtime.validator.ICooperateAutomatedValidator;

/**
 * This class contains custom validation rules.
 *
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
 */
public class UsecaseValidator extends AbstractUsecaseValidator {

    private static final String NAME_TAKEN = "name_taken";

    @Inject
    @SuppressWarnings("unused")
    private ICooperateAutomatedValidator automatedValidator;

    @Inject
    @SuppressWarnings("unused")
    private IIssueCodeRegistry issueCodeRegistry;

    @Check
    private void checkUniqueActor(RootPackage rootPackage) {
        EList<Actor> actors = rootPackage.getActors();
        for (Actor actor : actors) {
            compareNamedElements(actors, actor, UsecasePackage.Literals.ROOT_PACKAGE__ACTORS, NAME_TAKEN);
        }
    }

    @Check
    private void checkUniqueUseCase(UseCase useCase) {
        EList<UseCase> useCases = useCase.getSystem().getUsecases();
        compareNamedElements(useCases, useCase, UsecasePackage.Literals.USE_CASE__SYSTEM, NAME_TAKEN);
    }

    @Check
    private void checkUniqueSystems(System system) {
        EList<System> systems = system.getPackage().getSystems();
        compareNamedElements(systems, system, UsecasePackage.Literals.SYSTEM__PACKAGE, NAME_TAKEN);
    }

    private void compareNamedElements(List<? extends NamedElement> elements, NamedElement comparableElement,
            EStructuralFeature feature, String code) {
        for (NamedElement element : elements) {
            if (element.equals(comparableElement)) {
                continue;
            }
            if (element.getName().equals(comparableElement.getName())) {
                error("\"" + element.getName() + "\"" + " no duplicates!", feature, code);
            }

        }
    }
}<commit_msg>#85 Added Alias check for usecase<commit_after>/*
 * generated by Xtext 2.11.0
 */
package de.cooperateproject.modeling.textual.usecase.validation;

import java.util.List;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.validation.Check;

import com.google.inject.Inject;

import de.cooperateproject.modeling.textual.common.metamodel.textualCommons.AliasedElement;
import de.cooperateproject.modeling.textual.common.metamodel.textualCommons.NamedElement;
import de.cooperateproject.modeling.textual.usecase.usecase.Actor;
import de.cooperateproject.modeling.textual.usecase.usecase.RootPackage;
import de.cooperateproject.modeling.textual.usecase.usecase.System;
import de.cooperateproject.modeling.textual.usecase.usecase.UseCase;
import de.cooperateproject.modeling.textual.usecase.usecase.UsecasePackage;
import de.cooperateproject.modeling.textual.xtext.runtime.issues.IIssueCodeRegistry;
import de.cooperateproject.modeling.textual.xtext.runtime.validator.ICooperateAutomatedValidator;

/**
 * This class contains custom validation rules.
 *
 * See https://www.eclipse.org/Xtext/documentation/303_runtime_concepts.html#validation
 */
public class UsecaseValidator extends AbstractUsecaseValidator {

    private static final String NAME_TAKEN = "name_taken";
    private static final String ALIAS_TAKEN = "alias_taken";

    @Inject
    @SuppressWarnings("unused")
    private ICooperateAutomatedValidator automatedValidator;

    @Inject
    @SuppressWarnings("unused")
    private IIssueCodeRegistry issueCodeRegistry;

    @Check
    private void checkUniqueActor(RootPackage rootPackage) {
        EList<Actor> actors = rootPackage.getActors();
        for (Actor actor : actors) {
            compareNamedElements(actors, actor, UsecasePackage.Literals.ROOT_PACKAGE__ACTORS, NAME_TAKEN);
            compareAliasedElements(actors, actor, UsecasePackage.Literals.ROOT_PACKAGE__ACTORS, ALIAS_TAKEN);
        }
    }

    @Check
    private void checkUniqueUseCase(UseCase useCase) {
        EList<UseCase> useCases = useCase.getSystem().getUsecases();
        compareNamedElements(useCases, useCase, UsecasePackage.Literals.USE_CASE__SYSTEM, NAME_TAKEN);
        compareAliasedElements(useCases, useCase, UsecasePackage.Literals.USE_CASE__SYSTEM, ALIAS_TAKEN);
    }

    @Check
    private void checkUniqueSystems(System system) {
        EList<System> systems = system.getPackage().getSystems();
        compareNamedElements(systems, system, UsecasePackage.Literals.SYSTEM__PACKAGE, NAME_TAKEN);
    }

    private void compareNamedElements(List<? extends NamedElement> elements, NamedElement comparableElement,
            EStructuralFeature feature, String code) {
        for (NamedElement element : elements) {
            if (element.equals(comparableElement)) {
                continue;
            }
            if (element.getName().equals(comparableElement.getName())) {
                error("\"" + element.getName() + "\"" + " no duplicates!", feature, code);
            }

        }
    }

    private void compareAliasedElements(List<? extends AliasedElement> elements, AliasedElement comparableElement,
            EStructuralFeature feature, String code) {
        for (AliasedElement element : elements) {
            if (element.equals(comparableElement)) {
                continue;
            }
            if (element.getAlias().equals(comparableElement.getAlias())) {
                error("\"" + element.getAlias() + "\"" + " alias taken!", feature, code);
            }

        }
    }
}
{'fraction_non_alphanumeric': 0.09845713376809288, 'fraction_numerical': 0.0025449339907746144, 'mean_word_length': 3.836923076923077, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 2, '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': '5634727', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1845, 'n_words': 306}