Search in sources :

Example 1 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class ModelGenerationMojo method serializePureModelContextData.

private void serializePureModelContextData(PureModelContextData pureModelContextData) throws Exception {
    PureToEntityConverter converter = new PureToEntityConverter();
    List<Entity> entities = ListIterate.collect(pureModelContextData.getAllElements(), converter::toEntity);
    this.serializeEntities(entities);
}
Also used : PureToEntityConverter(org.finos.legend.sdlc.protocol.pure.v1.PureToEntityConverter) Entity(org.finos.legend.sdlc.domain.model.entity.Entity)

Example 2 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class ModelGenerationMojo method serializeEntities.

private void serializeEntities(List<Entity> entities) throws IOException {
    long serializeStart = System.nanoTime();
    getLog().info(String.format("Serializing %,d entities to %s", entities.size(), this.outputDirectory));
    Path outputDirPath = this.outputDirectory.toPath();
    Path entitiesDir = outputDirPath.resolve("entities");
    Pattern pkgSepPattern = Pattern.compile("::", Pattern.LITERAL);
    String replacement = Matcher.quoteReplacement(outputDirPath.getFileSystem().getSeparator());
    EntitySerializer entitySerializer = EntitySerializers.getDefaultJsonSerializer();
    for (Entity entity : entities) {
        Path entityFilePath = entitiesDir.resolve(pkgSepPattern.matcher(entity.getPath()).replaceAll(replacement) + "." + entitySerializer.getDefaultFileExtension());
        Files.createDirectories(entityFilePath.getParent());
        try (OutputStream stream = Files.newOutputStream(entityFilePath)) {
            entitySerializer.serialize(entity, stream);
        }
    }
    getLog().info(String.format("Done serializing %,d entities to %s (%.9fs)", entities.size(), this.outputDirectory, (System.nanoTime() - serializeStart) / 1_000_000_000.0));
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) OutputStream(java.io.OutputStream) EntitySerializer(org.finos.legend.sdlc.serialization.EntitySerializer)

Example 3 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class ServicesGenerationMojo method resolveServicesSpecification.

private static ResolvedServicesSpecification resolveServicesSpecification(ServicesSpecification servicesSpec) throws Exception {
    Set<String> servicePaths = null;
    if (servicesSpec.directories != null) {
        try (EntityLoader directoriesLoader = EntityLoader.newEntityLoader(servicesSpec.directories)) {
            EntityToPureConverter converter = new EntityToPureConverter();
            servicePaths = directoriesLoader.getAllEntities().filter(e -> converter.fromEntityIfPossible(e).filter(s -> s instanceof Service).isPresent()).map(Entity::getPath).collect(Collectors.toSet());
        }
    }
    if (servicesSpec.servicePaths != null) {
        if (servicePaths == null) {
            servicePaths = servicesSpec.servicePaths;
        } else {
            servicePaths.addAll(servicesSpec.servicePaths);
        }
    }
    return new ResolvedServicesSpecification(servicePaths, servicesSpec.packages);
}
Also used : Arrays(java.util.Arrays) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Lists(org.eclipse.collections.api.factory.Lists) Parameter(org.apache.maven.plugins.annotations.Parameter) MutableList(org.eclipse.collections.api.list.MutableList) EntityToPureConverter(org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter) ArrayList(java.util.ArrayList) MapperFeature(com.fasterxml.jackson.databind.MapperFeature) Mojo(org.apache.maven.plugins.annotations.Mojo) RichIterable(org.eclipse.collections.api.RichIterable) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) PureModelContextData(org.finos.legend.engine.protocol.pure.v1.model.context.PureModelContextData) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Map(java.util.Map) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) PlanTransformer(org.finos.legend.engine.plan.generation.transformers.PlanTransformer) PureModelBuilder(org.finos.legend.sdlc.language.pure.compiler.toPureGraph.PureModelBuilder) PureModel(org.finos.legend.engine.language.pure.compiler.toPureGraph.PureModel) Set(java.util.Set) ServiceLoader(java.util.ServiceLoader) StreamWriteFeature(com.fasterxml.jackson.core.StreamWriteFeature) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) StreamReadFeature(com.fasterxml.jackson.core.StreamReadFeature) JsonMapper(com.fasterxml.jackson.databind.json.JsonMapper) Collectors(java.util.stream.Collectors) Root_meta_pure_router_extension_RouterExtension(org.finos.legend.pure.generated.Root_meta_pure_router_extension_RouterExtension) File(java.io.File) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) PureProtocolObjectMapperFactory(org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory) SerializationFeature(com.fasterxml.jackson.databind.SerializationFeature) Comparator(java.util.Comparator) AbstractMojo(org.apache.maven.plugin.AbstractMojo) PlanGeneratorExtension(org.finos.legend.engine.plan.generation.extension.PlanGeneratorExtension) Entity(org.finos.legend.sdlc.domain.model.entity.Entity) EntityLoader(org.finos.legend.sdlc.serialization.EntityLoader) EntityToPureConverter(org.finos.legend.sdlc.protocol.pure.v1.EntityToPureConverter) Service(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.service.Service)

Example 4 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class TestEntityToPureConverter method testFromEntity.

@Test
public void testFromEntity() {
    Entity entity = TestTools.newClassEntity("EmptyClass", "model::test");
    PackageableElement result = this.converter.fromEntity(entity);
    Assert.assertTrue(result instanceof Class);
    Class resultClass = (Class) result;
    Assert.assertEquals("EmptyClass", resultClass.name);
    Assert.assertEquals("model::test", resultClass._package);
    Assert.assertEquals(Collections.singletonList("meta::pure::metamodel::type::Any"), resultClass.superTypes);
    Assert.assertEquals(Collections.emptyList(), resultClass.properties);
    IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> this.converter.fromEntity(Entity.newEntity("not::a::real::PureEntity", "meta::unknown::NotAClassifier", Collections.emptyMap())));
    String message = e.getMessage();
    Assert.assertNotNull(message);
    String expectedPrefix = "Could not convert entity not::a::real::PureEntity with classifier meta::unknown::NotAClassifier to class PackageableElement";
    if (!message.startsWith(expectedPrefix)) {
        Assert.assertEquals(expectedPrefix, message);
    }
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) PackageableElement(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement) Class(org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class) Test(org.junit.Test)

Example 5 with Entity

use of org.finos.legend.sdlc.domain.model.entity.Entity in project legend-sdlc by finos.

the class TestPureEntitySerializer method testNonPureEntity.

@Test
public void testNonPureEntity() {
    Entity entity = readEntityFromJsonResource(buildResourceName("invalid", "TestNonPureEntity.json"));
    Assert.assertFalse(this.pureSerializer.canSerialize(entity));
    IllegalArgumentException e = Assert.assertThrows(IllegalArgumentException.class, () -> this.pureSerializer.serializeToString(entity));
    String message = e.getMessage();
    String expectedStart = "Could not convert entity model::otherthings::AnotherThing with classifier meta::notpure::something::SomeType to class PackageableElement";
    if (!message.startsWith(expectedStart)) {
        // We don't really want these to be equal, this is just to produce a decent failure message
        Assert.assertEquals(expectedStart, message);
    }
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) Test(org.junit.Test)

Aggregations

Entity (org.finos.legend.sdlc.domain.model.entity.Entity)81 Test (org.junit.Test)41 File (java.io.File)17 List (java.util.List)17 Path (java.nio.file.Path)15 Collections (java.util.Collections)15 MavenProject (org.apache.maven.project.MavenProject)15 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)15 IOException (java.io.IOException)14 Map (java.util.Map)13 Assert (org.junit.Assert)12 Iterate (org.eclipse.collections.impl.utility.Iterate)11 Sets (org.eclipse.collections.api.factory.Sets)10 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)10 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)10 EntityLoader (org.finos.legend.sdlc.serialization.EntityLoader)10 Files (java.nio.file.Files)9 Class (org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Class)9 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)9 Paths (java.nio.file.Paths)8