Search in sources :

Example 1 with Entity

use of org.eclipse.sirius.components.domain.Entity in project sirius-components by eclipse-sirius.

the class DiagramDescriptionValidator method hasProperDomainType.

private boolean hasProperDomainType(DiagramElementDescription diagramElementDescription, DiagnosticChain diagnostics) {
    boolean isValid = false;
    ResourceSet resourceSet = diagramElementDescription.eResource().getResourceSet();
    List<Entity> entities = this.getDomainEntitiesFromResourceSet(resourceSet);
    List<EPackage> ePackages = this.getEPackagesFromRegistry(resourceSet.getPackageRegistry());
    // $NON-NLS-1$
    String domainType = Optional.ofNullable(diagramElementDescription.getDomainType()).orElse("");
    isValid = entities.stream().anyMatch(entity -> this.describesEntity(domainType, entity));
    if (!isValid && !domainType.isBlank()) {
        // @formatter:off
        isValid = ePackages.stream().map(EPackage::getEClassifiers).flatMap(Collection::stream).filter(EClass.class::isInstance).map(EClass.class::cast).anyMatch(classifier -> new DomainClassPredicate(domainType).test(classifier));
    // @formatter:off
    }
    if (!isValid && diagnostics != null) {
        // @formatter:off
        BasicDiagnostic basicDiagnostic = new BasicDiagnostic(Diagnostic.ERROR, SIRIUS_COMPONENTS_EMF_PACKAGE, 0, String.format(DIAGRAM_ELEMENT_DESCRIPTION_INVALID_DOMAIN_TYPE_ERROR_MESSAGE, domainType), new Object[] { diagramElementDescription, ViewPackage.Literals.DIAGRAM_ELEMENT_DESCRIPTION__DOMAIN_TYPE });
        // @formatter:on
        diagnostics.add(basicDiagnostic);
    }
    return isValid;
}
Also used : BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) EDataType(org.eclipse.emf.ecore.EDataType) DiagnosticChain(org.eclipse.emf.common.util.DiagnosticChain) CreateInstance(org.eclipse.sirius.components.view.CreateInstance) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Entity(org.eclipse.sirius.components.domain.Entity) ArrayList(java.util.ArrayList) EValidator(org.eclipse.emf.ecore.EValidator) DomainPackage(org.eclipse.sirius.components.domain.DomainPackage) EClass(org.eclipse.emf.ecore.EClass) Map(java.util.Map) DomainClassPredicate(org.eclipse.sirius.components.emf.compatibility.DomainClassPredicate) Conditional(org.eclipse.sirius.components.view.Conditional) NodeStyle(org.eclipse.sirius.components.view.NodeStyle) DiagramDescription(org.eclipse.sirius.components.view.DiagramDescription) Collection(java.util.Collection) Diagnostic(org.eclipse.emf.common.util.Diagnostic) DiagramElementDescription(org.eclipse.sirius.components.view.DiagramElementDescription) EObject(org.eclipse.emf.ecore.EObject) Collectors(java.util.stream.Collectors) EList(org.eclipse.emf.common.util.EList) EPackage(org.eclipse.emf.ecore.EPackage) ViewPackage(org.eclipse.sirius.components.view.ViewPackage) Objects(java.util.Objects) List(java.util.List) Domain(org.eclipse.sirius.components.domain.Domain) Entry(java.util.Map.Entry) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) Entity(org.eclipse.sirius.components.domain.Entity) EClass(org.eclipse.emf.ecore.EClass) BasicDiagnostic(org.eclipse.emf.common.util.BasicDiagnostic) Collection(java.util.Collection) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) DomainClassPredicate(org.eclipse.sirius.components.emf.compatibility.DomainClassPredicate) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with Entity

use of org.eclipse.sirius.components.domain.Entity in project sirius-components by eclipse-sirius.

the class DomainConverterTests method testConvertEntityWithValidName.

@ParameterizedTest
@ValueSource(strings = { "lowercase", "snake_case", "CamelCase", "Sample_Entity", "A1" })
public void testConvertEntityWithValidName(String name) {
    Domain fixture = DomainFactory.eINSTANCE.createDomain();
    fixture.setName(FIXTURE);
    Entity entity = DomainFactory.eINSTANCE.createEntity();
    entity.setName(name);
    fixture.getTypes().add(entity);
    Optional<EPackage> converted = this.convert(fixture);
    assertThat(converted).isPresent();
    EList<EClassifier> convertedTypes = converted.get().getEClassifiers();
    assertThat(convertedTypes).hasSize(1);
    assertThat(convertedTypes.get(0)).extracting(EClassifier::getName).isEqualTo(name);
}
Also used : Entity(org.eclipse.sirius.components.domain.Entity) EClassifier(org.eclipse.emf.ecore.EClassifier) Domain(org.eclipse.sirius.components.domain.Domain) EPackage(org.eclipse.emf.ecore.EPackage) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with Entity

use of org.eclipse.sirius.components.domain.Entity in project sirius-components by eclipse-sirius.

the class DomainConverterTests method testConvertEntityWithInvalidName.

@ParameterizedTest
@ValueSource(strings = { "", "42", "almost!valid", "foo-bar", "foo.bar", "foo/bar" })
public void testConvertEntityWithInvalidName(String name) {
    Domain fixture = DomainFactory.eINSTANCE.createDomain();
    fixture.setName(FIXTURE);
    Entity invalidEntity = DomainFactory.eINSTANCE.createEntity();
    invalidEntity.setName(name);
    fixture.getTypes().add(invalidEntity);
    Optional<EPackage> converted = this.convert(fixture);
    assertThat(converted).isEmpty();
}
Also used : Entity(org.eclipse.sirius.components.domain.Entity) Domain(org.eclipse.sirius.components.domain.Domain) EPackage(org.eclipse.emf.ecore.EPackage) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Entity

use of org.eclipse.sirius.components.domain.Entity in project sirius-components by eclipse-sirius.

the class DomainConverterTests method testConvertEntityWithNullName.

@Test
public void testConvertEntityWithNullName() {
    Domain fixture = DomainFactory.eINSTANCE.createDomain();
    fixture.setName(FIXTURE);
    Entity invalidEntity = DomainFactory.eINSTANCE.createEntity();
    invalidEntity.setName(null);
    fixture.getTypes().add(invalidEntity);
    Optional<EPackage> converted = this.convert(fixture);
    assertThat(converted).isEmpty();
}
Also used : Entity(org.eclipse.sirius.components.domain.Entity) Domain(org.eclipse.sirius.components.domain.Domain) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with Entity

use of org.eclipse.sirius.components.domain.Entity in project sirius-components by eclipse-sirius.

the class DomainConverterTests method testConvertSystemDomainDefinition.

@Test
void testConvertSystemDomainDefinition() {
    Domain componentDomain = DomainFactory.eINSTANCE.createDomain();
    // $NON-NLS-1$
    componentDomain.setName("component");
    Entity namedEntity = DomainFactory.eINSTANCE.createEntity();
    // $NON-NLS-1$
    namedEntity.setName("Named");
    namedEntity.setAbstract(true);
    Attribute nameAttribute = DomainFactory.eINSTANCE.createAttribute();
    // $NON-NLS-1$
    nameAttribute.setName("name");
    nameAttribute.setMany(false);
    nameAttribute.setOptional(false);
    nameAttribute.setType(DataType.STRING);
    namedEntity.getAttributes().add(nameAttribute);
    componentDomain.getTypes().add(namedEntity);
    Entity systemEntity = DomainFactory.eINSTANCE.createEntity();
    // $NON-NLS-1$
    systemEntity.setName("System");
    systemEntity.getSuperTypes().add(namedEntity);
    componentDomain.getTypes().add(systemEntity);
    Entity componentEntity = DomainFactory.eINSTANCE.createEntity();
    // $NON-NLS-1$
    componentEntity.setName("Component");
    componentEntity.getSuperTypes().add(namedEntity);
    componentDomain.getTypes().add(componentEntity);
    Relation systemComponentsRelation = DomainFactory.eINSTANCE.createRelation();
    // $NON-NLS-1$
    systemComponentsRelation.setName("parts");
    systemComponentsRelation.setContainment(true);
    systemComponentsRelation.setMany(true);
    systemComponentsRelation.setOptional(true);
    systemComponentsRelation.setTargetType(componentEntity);
    systemEntity.getRelations().add(systemComponentsRelation);
    Optional<EPackage> converted = this.convert(componentDomain);
    assertThat(converted).isPresent();
    EPackage convertedPackage = converted.get();
    assertThat(convertedPackage.getEClassifiers()).hasSize(3);
    // @formatter:off
    assertThat(convertedPackage.getEClassifier(namedEntity.getName())).asInstanceOf(InstanceOfAssertFactories.type(EClass.class)).matches(EClass::isAbstract).matches(eClass -> eClass.getEAllAttributes().size() == 1).matches(eClass -> eClass.getEAllReferences().isEmpty());
    assertThat(convertedPackage.getEClassifier(systemEntity.getName())).asInstanceOf(InstanceOfAssertFactories.type(EClass.class)).matches(eClass -> !eClass.isAbstract()).matches(eClass -> eClass.getESuperTypes().size() == 1).matches(eClass -> eClass.getEAttributes().isEmpty()).matches(eClass -> eClass.getEAllAttributes().size() == 1);
    assertThat(convertedPackage.getEClassifier(componentEntity.getName())).asInstanceOf(InstanceOfAssertFactories.type(EClass.class)).matches(eClass -> !eClass.isAbstract()).matches(eClass -> eClass.getESuperTypes().size() == 1).matches(eClass -> eClass.getEAttributes().isEmpty()).matches(eClass -> eClass.getEAllAttributes().size() == 1);
// @formatter:on
}
Also used : ValueSource(org.junit.jupiter.params.provider.ValueSource) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EClassifier(org.eclipse.emf.ecore.EClassifier) EList(org.eclipse.emf.common.util.EList) Entity(org.eclipse.sirius.components.domain.Entity) EPackage(org.eclipse.emf.ecore.EPackage) Relation(org.eclipse.sirius.components.domain.Relation) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EClass(org.eclipse.emf.ecore.EClass) DataType(org.eclipse.sirius.components.domain.DataType) Domain(org.eclipse.sirius.components.domain.Domain) DomainFactory(org.eclipse.sirius.components.domain.DomainFactory) Optional(java.util.Optional) Attribute(org.eclipse.sirius.components.domain.Attribute) Entity(org.eclipse.sirius.components.domain.Entity) Relation(org.eclipse.sirius.components.domain.Relation) EClass(org.eclipse.emf.ecore.EClass) Attribute(org.eclipse.sirius.components.domain.Attribute) Domain(org.eclipse.sirius.components.domain.Domain) EPackage(org.eclipse.emf.ecore.EPackage) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Entity (org.eclipse.sirius.components.domain.Entity)24 Domain (org.eclipse.sirius.components.domain.Domain)12 Test (org.junit.jupiter.api.Test)12 BasicDiagnostic (org.eclipse.emf.common.util.BasicDiagnostic)11 EPackage (org.eclipse.emf.ecore.EPackage)8 EClass (org.eclipse.emf.ecore.EClass)7 Diagnostic (org.eclipse.emf.common.util.Diagnostic)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 Objects (java.util.Objects)5 DiagnosticChain (org.eclipse.emf.common.util.DiagnosticChain)5 EDataType (org.eclipse.emf.ecore.EDataType)5 EObject (org.eclipse.emf.ecore.EObject)5 EValidator (org.eclipse.emf.ecore.EValidator)5 DomainPackage (org.eclipse.sirius.components.domain.DomainPackage)5 Relation (org.eclipse.sirius.components.domain.Relation)5 Optional (java.util.Optional)4 EList (org.eclipse.emf.common.util.EList)4 Attribute (org.eclipse.sirius.components.domain.Attribute)4