use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement in project legend-sdlc by finos.
the class TestFileGenerationFactory method testFileGenerationFactory.
@Test
public void testFileGenerationFactory() {
PureModelContextData pureModelContextData = getPureModelContextDataFromPath("FileGenerationFactoryTestData.json");
GenerationSpecification generationSpecification = pureModelContextData.getElementsOfType(GenerationSpecification.class).get(0);
FileGenerationFactory factory = FileGenerationFactory.newFactory(generationSpecification, pureModelContextData);
MutableMap<FileGenerationSpecification, List<GenerationOutput>> result = factory.generateFiles();
MapIterable<String, FileGenerationSpecification> specifications = LazyIterate.selectInstancesOf(pureModelContextData.getElements(), FileGenerationSpecification.class).groupByUniqueKey(PackageableElement::getPath);
// avro
List<GenerationOutput> avroResult = result.get(specifications.get("generation::MyAvro"));
testAvroOutput(avroResult);
// protobuf
List<GenerationOutput> protoBufResult = result.get(specifications.get("generation::MyProtobuf"));
Assert.assertEquals(1, protoBufResult.size());
MapIterable<String, GenerationOutput> protobufOutputs = LazyIterate.adapt(protoBufResult).groupByUniqueKey(GenerationOutput::getFileName);
Assert.assertEquals("syntax = \"proto3\";\n" + "package model;\n" + "\n" + "message Firm {\n" + " Person employees = 1;\n" + "}\n" + "\n" + "message Person {\n" + " string first_name = 1;\n" + " string last_name = 2;\n" + "}", protobufOutputs.get("model.proto").getContent());
// rosetta
List<GenerationOutput> rosettaResult = result.get(specifications.get("generation::MyRosetta"));
Assert.assertEquals(1, rosettaResult.size());
Assert.assertEquals("rosettaTypes.txt", rosettaResult.get(0).getFileName());
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement in project legend-sdlc by finos.
the class ModelGenerationFactory method validateGeneratedElements.
private void validateGeneratedElements(PureModelContextData generatedPureModelContextData) {
generatedPureModelContextData.getElements().forEach(e -> {
String path = e.getPath();
PackageableElement coreElement = this.coreModelElementIndex.get(path);
if (coreElement != null) {
throw new RuntimeException("Generated element '" + path + "' of type " + e.getClass().getSimpleName() + " can't override existing element of type " + coreElement.getClass().getSimpleName());
}
});
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement in project legend-sdlc by finos.
the class PureProtocolHelper method serializeForComparison.
public static String serializeForComparison(PackageableElement element) {
PureModelContextData pureModelContextData = PureModelContextData.newBuilder().withElement(element).build();
PureGrammarComposerContext composerContext = PureGrammarComposerContext.Builder.newInstance().withRenderStyle(RenderStyle.PRETTY).build();
String result = PureGrammarComposer.newInstance(composerContext).renderPureModelContextData(pureModelContextData);
if (result.startsWith("###")) {
result = result.substring(result.indexOf('\n') + 1);
}
return result;
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement 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);
}
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.PackageableElement in project legend-sdlc by finos.
the class FileGenerationMojo method execute.
@Override
public void execute() throws MojoExecutionException {
long generateStart = System.nanoTime();
if (this.inclusions != null) {
getLog().info("include generation specification paths: " + this.inclusions.paths);
getLog().info("include generation specification packages: " + this.inclusions.packages);
getLog().info("include generation specification directories: " + Arrays.toString(this.inclusions.directories));
}
if (this.exclusions != null) {
getLog().info("exclude generation specification paths: " + this.exclusions.paths);
getLog().info("exclude generation specification packages: " + this.exclusions.packages);
getLog().info("exclude generation specification directories: " + Arrays.toString(this.exclusions.directories));
}
getLog().info("Output directory: " + this.outputDirectory);
// Load Model
long modelStart = System.nanoTime();
getLog().info("Start loading model");
PureModelBuilder pureModelBuilder = PureModelBuilder.newBuilder();
try (EntityLoader allEntities = EntityLoader.newEntityLoader(Thread.currentThread().getContextClassLoader())) {
pureModelBuilder.addEntitiesIfPossible(allEntities.getAllEntities());
int entityCount = pureModelBuilder.getElementCount();
getLog().info("Found " + entityCount + " entities");
if (entityCount == 0) {
long modelEnd = System.nanoTime();
getLog().info(String.format("Finished loading model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
getLog().info("No elements found to generate");
return;
}
} catch (Exception e) {
throw new MojoExecutionException("Error loading entities from model", e);
}
getLog().info("Compiling model");
PureModelBuilder.PureModelWithContextData pureModelWithContextData = pureModelBuilder.build();
PureModelContextData pureModelContextData = pureModelWithContextData.getPureModelContextData();
PureModel pureModel = pureModelWithContextData.getPureModel();
long modelEnd = System.nanoTime();
getLog().info(String.format("Finished loading and compiling model (%.9fs)", (modelEnd - modelStart) / 1_000_000_000.0));
Map<String, GenerationSpecification> generationSpecificationMap = LazyIterate.selectInstancesOf(pureModelContextData.getElements(), GenerationSpecification.class).groupByUniqueKey(PackageableElement::getPath, Maps.mutable.empty());
filterGenerationSpecsByIncludes(generationSpecificationMap);
filterGenerationSpecsByExcludes(generationSpecificationMap);
if (generationSpecificationMap.isEmpty()) {
getLog().info("No generation specification found, nothing to generate");
return;
}
if (generationSpecificationMap.size() > 1) {
throw new MojoExecutionException(Iterate.toSortedList(generationSpecificationMap.keySet()).makeString("Only 1 generation specification allowed, found " + generationSpecificationMap.size() + ": ", ", ", ""));
}
try {
// Start generating
GenerationSpecification generationSpecification = generationSpecificationMap.values().iterator().next();
getLog().info(String.format("Start generating file generations for generation specification '%s', %,d file generations found", generationSpecification.getPath(), generationSpecification.fileGenerations.size()));
FileGenerationFactory fileGenerationFactory = FileGenerationFactory.newFactory(generationSpecification, pureModelContextData, pureModel);
MutableMap<FileGenerationSpecification, List<GenerationOutput>> outputs = fileGenerationFactory.generateFiles();
serializeOutput(outputs);
getLog().info(String.format("Done (%.9fs)", (System.nanoTime() - generateStart) / 1_000_000_000.0));
} catch (Exception e) {
throw new MojoExecutionException("Error generating files: " + e.getMessage(), e);
}
}
Aggregations