use of org.eclipse.vorto.core.api.model.mapping.MappingModel in project vorto by eclipse.
the class ModelDtoFactory method createResource.
public static Infomodel createResource(InformationModel model, Optional<MappingModel> mappingModel) {
Infomodel infoResource = new Infomodel(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), ModelType.InformationModel);
for (FunctionblockProperty property : model.getProperties()) {
infoResource.getFunctionblocks().add(createProperty(property, mappingModel));
}
infoResource.setDescription(model.getDescription());
infoResource.setDisplayName(model.getDisplayname());
infoResource.setReferences(model.getReferences().stream().map(reference -> createModelId(reference)).collect(Collectors.toList()));
if (mappingModel.isPresent()) {
MappingModel _mappingModel = mappingModel.get();
infoResource.setTargetPlatformKey(_mappingModel.getTargetPlatform());
for (MappingRule rule : getInfoModelRule(_mappingModel.getRules())) {
if (rule.getTarget() instanceof StereoTypeTarget) {
StereoTypeTarget target = (StereoTypeTarget) rule.getTarget();
infoResource.addStereotype(Stereotype.create(target.getName(), convertAttributesToMap(target.getAttributes())));
} else if (rule.getTarget() instanceof ReferenceTarget) {
ReferenceTarget target = (ReferenceTarget) rule.getTarget();
infoResource.setMappingReference(createModelId(target.getMappingModel()));
}
}
}
return infoResource;
}
use of org.eclipse.vorto.core.api.model.mapping.MappingModel in project vorto by eclipse.
the class ModelDtoFactory method createProperty.
private static ModelProperty createProperty(Property property, Optional<MappingModel> mappingModel) {
ModelProperty p = new ModelProperty();
p.setDescription(property.getDescription());
p.setMandatory(property.getPresence() != null ? property.getPresence().isMandatory() : true);
p.setMultiple(property.isMultiplicity());
p.setName(property.getName());
if (property.getType() instanceof PrimitivePropertyType) {
PrimitiveType pt = ((PrimitivePropertyType) property.getType()).getType();
p.setType(org.eclipse.vorto.repository.api.content.PrimitiveType.valueOf(pt.name()));
} else {
p.setType(createModelId(((ObjectPropertyType) property.getType()).getType()));
}
if (property.getConstraintRule() != null && property.getConstraintRule().getConstraints() != null) {
List<Constraint> constraints = property.getConstraintRule().getConstraints().stream().map(c -> createConstraint(c)).collect(Collectors.toList());
p.setConstraints(constraints);
}
if (property.getPropertyAttributes() != null) {
List<IPropertyAttribute> attributes = property.getPropertyAttributes().stream().map(a -> createAttribute(a)).collect(Collectors.toList());
p.setAttributes(attributes);
}
if (mappingModel.isPresent()) {
p.setTargetPlatformKey(mappingModel.get().getTargetPlatform());
for (MappingRule rule : getPropertyRule(p.getName(), mappingModel.get().getRules())) {
StereoTypeTarget target = (StereoTypeTarget) rule.getTarget();
p.addStereotype(Stereotype.create(target.getName(), convertAttributesToMap(target.getAttributes())));
}
}
return p;
}
use of org.eclipse.vorto.core.api.model.mapping.MappingModel in project vorto by eclipse.
the class ModelRepositoryController method getModelContentByModelAndMappingId.
@ApiOperation(value = "Returns the model content including target platform specific attributes for the given model- and mapping modelID")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{modelId:.+}/mapping/{mappingId:.+}", method = RequestMethod.GET)
public AbstractModel getModelContentByModelAndMappingId(@ApiParam(value = "The model ID (prettyFormat)", required = true) @PathVariable final String modelId, @ApiParam(value = "The mapping Model ID (prettyFormat)", required = true) @PathVariable final String mappingId) {
ModelInfo vortoModelInfo = modelRepository.getById(ModelId.fromPrettyFormat(modelId));
ModelInfo mappingModelInfo = modelRepository.getById(ModelId.fromPrettyFormat(mappingId));
if (vortoModelInfo == null) {
throw new ModelNotFoundException("Could not find vorto model with ID: " + modelId);
} else if (mappingModelInfo == null) {
throw new ModelNotFoundException("Could not find mapping with ID: " + mappingId);
}
byte[] mappingContentZip = createZipWithAllDependencies(mappingModelInfo.getId(), ContentType.DSL);
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(mappingContentZip))).read();
MappingModel mappingModel = (MappingModel) workspace.get().stream().filter(p -> p instanceof MappingModel).findFirst().get();
byte[] modelContent = createZipWithAllDependencies(vortoModelInfo.getId(), ContentType.DSL);
workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(vortoModelInfo.getId().getName())).findFirst().get(), Optional.of(mappingModel));
}
use of org.eclipse.vorto.core.api.model.mapping.MappingModel in project vorto by eclipse.
the class VortoService method generate.
public IGenerationResult generate(String key, String namespace, String name, String version, Map<String, String> parameters) {
LOGGER.info(String.format("Generating for Platform [%s] and Model [%s.%s:%s]", key, namespace, name, version));
Generator generator = repo.get(key).orElseThrow(GatewayUtils.notFound(String.format("[Generator %s]", key)));
InformationModel model = getModel(namespace, name, version).orElseThrow(GatewayUtils.notFound(String.format("[Model %s.%s:%s]", namespace, name, version)));
List<MappingModel> mappings = getMappings(key, namespace, name, version);
InvocationContext invocationContext = new InvocationContext(mappings, repo.newGeneratorLookup(), parameters);
return generate(generator.getInstance(), model, invocationContext);
}
use of org.eclipse.vorto.core.api.model.mapping.MappingModel in project vorto by eclipse.
the class GeneratorMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Executing Generator of class '" + getGeneratorClass());
try {
final IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(loadInformationModels()))).read();
List<MappingModel> mappingModels = workspace.get().stream().filter(p -> p instanceof MappingModel).map(MappingModel.class::cast).collect(Collectors.toList());
for (Model model : workspace.get().stream().filter(p -> p instanceof InformationModel).collect(Collectors.toList())) {
InformationModel infomodel = (InformationModel) model;
IVortoCodeGenerator codeGenerator = (IVortoCodeGenerator) Class.forName(generatorClass).newInstance();
IGenerationResult result = codeGenerator.generate(infomodel, new InvocationContext(mappingModels, null, new HashMap<String, String>()), null);
if (result.getMediatype().equalsIgnoreCase("application/zip")) {
final ZipContentExtractCodeGeneratorTask task = new ZipContentExtractCodeGeneratorTask(result.getContent());
task.generate(null, InvocationContext.simpleInvocationContext(), new IGeneratedWriter() {
public void write(Generated generated) {
if (generated.getFileName() == null) {
File generatedDirectory = new File(outputPath, stripPath(generated.getFolderPath()));
generatedDirectory.mkdirs();
} else {
if (generated.getFileName().equals("pom.xml")) {
return;
}
File generatedDirectory = new File(outputPath, stripPath(generated.getFolderPath()));
File generatedFile = new File(generatedDirectory, generated.getFileName());
try {
FileUtils.writeByteArrayToFile(generatedFile, generated.getContent(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String stripPath(String folderPath) {
final String mavenSourcePath = "src/main/java/";
if (folderPath.indexOf(mavenSourcePath) > -1) {
return folderPath.substring(folderPath.indexOf(mavenSourcePath) + mavenSourcePath.length());
}
return folderPath;
}
});
} else {
File generatedFile = new File(outputPath, result.getFileName());
FileUtils.writeByteArrayToFile(generatedFile, result.getContent(), false);
}
}
} catch (InstantiationException e) {
throw new MojoExecutionException("Could not instantiate vorto code generator from given generatorClass", e);
} catch (IllegalAccessException e) {
throw new MojoExecutionException("Error during resolving code generator", e);
} catch (ClassNotFoundException e) {
throw new MojoExecutionException("Could not instantiate vorto code generator from given generatorClass", e);
} catch (Exception e) {
throw new MojoExecutionException("Problem during code generator invocation", e);
}
}
Aggregations