Skip to main content

Create a Model-to-Model Mapping

Create a Mapping between source and target class

Let's start by defining the target class, for which we would like to create the mapping.

  1. Create a new class called NewPerson.
  2. Add a property to NewPerson called name. Make the property a string with a cardinality of 1.
  3. Add a property to the existing Person class called lastName. Make the property a string with a cardinality of 1.
  4. Click the+ icon and selectNew Mapping to create a new mapping called NewPersonMapping.
  5. Drag and drop the NewPerson target class into the Add a mapping element section on the left.
  6. Hit the Create button using the default setting.

Define the source class for our mapping and specifiy the transformation.

  1. Drag and drop the Person source class into the Choose a source section on the right.
  2. To specify the transformation, add the following expression for the name property: $src.firstName + ' ' + $src.lastName. You can also drag and drop the firstName and lastName properties from the Person source class into the function panel.

Create model-to-model

Add a filter to model-to-model mappings

This allows you to further restrict the values they would like to retrieve as part of the defined transformation.

filter in mapping

Create a Mapping Test

Legend provides an execution feature to run queries, constraints, and model-to-model/model-to-database transformations. You can use live data (pulled from physical data stores) or generated sample data as inputs to the executions, and review the results to test your model as you build it.

You can save these tests and continue to enhance your data models with the comfort that your changes can be tested against expected results.

It is good practice to add test to any mapping that you specify:

  1. Right click on the NewPerson class and selectTest. Alternatively, you can click on the + icon in the bottom left panel to add a test.

Create a sample query to test if the model-to-model mapping executes correctly:

  1. Click on the pencil icon to edit your query.
  2. Create a very simple query. Make sure to select the Graph fetch option for model-to-model queries. Studio does not currently support Projection queries on top of model-to-model mappings. Drag and drop the name property from the explorer panel into the fetch structure panel.
  3. Hit the Save query button.

Refer to the create a query section of the docs for more detailed information about the Query Editor

Studio generates sample input data for your defined query. You can also modify it if you want:

  1. Hit the generate Result icon.
  2. Congrats! You now have a mapping test and can run it any time to validate the execution of your model-to-model mapping.

Execute a Model-to-Model Mapping

  1. Drag the Execute panel up from the bottom of Studio screen.

  2. Select the edit button to select the NewPerson class for testing. Then select the attribute(s) to test. The source model is auto-populated.

  3. Execute the mapping and review the results.

  4. Save this execution as a test to run as part of the pipeline.

    Save execution as a test

You can promote this ad hoc execution to a test by clicking on the beaker icon.

Execute a Model-to-Model Mapping with Constraints

Remember that we can add a constraint on classes? We can also execute model-to-model mappings with constraints to check if the constraint criteria are violated.

Let's create a constraint on the NewPerson class that checks if the name value is empty $this.name-> == ''

  1. Right click on the NewPerson class and select Execute
  2. Create a sample query by clicking on the pencil icon
  3. Drag and drop the name property from the explorer panel into the fetch structure panel
  4. Select Check graph fetch
  5. Hit the Save query button
  6. Let's remove the firstName test data to intentionally violate the constraint criteria that we specified on our NewPerson class
  7. Hit the Execute button to execute the defined query

Studio will show a defect with the details about the violated constraint. Constraints can be both on source and target classes.

More