ContextMapper / context-mapper-standalone-example

Standalone Example Project using the Context Mapper DSL as Library
https://contextmapper.org/
Apache License 2.0
22 stars 10 forks source link

Importing a cml file doesn't merge its content into the current one #3

Open secretworry opened 4 years ago

secretworry commented 4 years ago

After divided a CML file into several ones by type and imported them in the primary context map file, the generic generator doesn't find any object declared in an imported file.

To reproduce the issue:

  1. Check out the branch, in which I split the example Insurance-Example-Model.cml into several ones

  2. Execute the GenericGeneratorExample.java in the example directory

  3. Inspect the src-gen/sample-output.md, in which all the bounded contexts are missing

stefan-ka commented 4 years ago

Thanks @secretworry for reporting this!

There is indeed something missing here for this case. We need a helper function or something that can be used in the Freemarker template (that collects all BCs; also the imported ones).

Please give me a few days to find a solution here.

stefan-ka commented 4 years ago

@secretworry As a temporary workaround you could just merge the Bounded Context lists in the example code, as I do it in the following Java example. But I know, its just an ugly workaround :)

/*
 * Copyright 2019 The Context Mapper Project Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.contextmapper.standalone.example;

import org.contextmapper.dsl.ContextMappingDSLStandaloneSetup;
import org.contextmapper.dsl.cml.CMLResourceContainer;
import org.contextmapper.dsl.generator.GenericContentGenerator;
import org.contextmapper.dsl.generator.PlantUMLGenerator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.xtext.generator.GeneratorContext;
import org.eclipse.xtext.generator.IGenerator2;
import org.eclipse.xtext.generator.JavaIoFileSystemAccess;

import java.io.File;

import static org.contextmapper.standalone.example.ReadingModelExample.INSURANCE_EXAMPLE_URI;

/**
 * This example shows how you can read your CML model and generate arbitrary textual
 * files by using Freemarker templates.
 *
 * @author Stefan Kapferer
 */
public class GenericGeneratorExample {

  public final static String FREEMARKER_TEMPLATE = "./src/main/resources/freemarker-sample-template.ftl";

  public static void main(String[] args) {
    // Setup and loading CML file:
    ContextMappingDSLStandaloneSetup.doSetup();
    ResourceSet resourceSet = new ResourceSetImpl();

    CMLResourceContainer mainResrouce = new CMLResourceContainer(resourceSet.getResource(URI.createURI("./src/main/cml/Insurance-Example-Model.cml"), true));
    CMLResourceContainer importedResource1 = new CMLResourceContainer(resourceSet.getResource(URI.createURI("./src/main/cml/domains.cml"), true));
    CMLResourceContainer importedResource2 = new CMLResourceContainer(resourceSet.getResource(URI.createURI("./src/main/cml/bounded_contexts.cml"), true));

    mainResrouce.getContextMappingModel().getBoundedContexts().addAll(importedResource1.getContextMappingModel().getBoundedContexts());
    mainResrouce.getContextMappingModel().getBoundedContexts().addAll(importedResource2.getContextMappingModel().getBoundedContexts());

    // Create the generic generator (using Freemarker template)
    GenericContentGenerator generator = new GenericContentGenerator();
    generator.setFreemarkerTemplateFile(new File(FREEMARKER_TEMPLATE));
    generator.setTargetFileName("sample-output.md");

    // Use custom data if you want:
    generator.registerCustomModelProperty("customText", "hello freemarker world");

    // Generate the diagrams into 'src-gen'
    JavaIoFileSystemAccess javaIoFileSystemAccess = FileSystemHelper.getFileSystemAccess();
    javaIoFileSystemAccess.setOutputPath("./src-gen");
    generator.doGenerate(mainResrouce.getResource(), javaIoFileSystemAccess, new GeneratorContext());
  }

}
secretworry commented 4 years ago

@stefan-ka thanks for the fast response! I will just go with the single file solution for now. Though I'm not a professional about eclipse xtext, I dig a little about the code. I don't know if it will help but I found that the import statement just creates a scope to help with reference finding without introducing any new value into the current object.

btw, ContextMapper is great, that for bringing it out:)