001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.operation.element;
009
010import javax.jmi.reflect.RefClass;
011import javax.jmi.reflect.RefObject;
012import javax.jmi.reflect.RefPackage;
013
014import net.mdatools.modelant.core.api.Function;
015import net.mdatools.modelant.core.util.Navigator;
016
017/**
018 * Instantiate a model element of a metamodel.
019 * @author Rusi Popov (popovr@mdatools.net)
020 */
021public class Create implements Function<String, RefObject> {
022
023  /**
024   * Extent where the model is loaded
025   */
026  private final RefPackage extent;
027
028  public Create(RefPackage extent) {
029    if ( extent == null ) { // no extent provided
030      throw new IllegalArgumentException( "Expected a non-null extent provided to process");
031    }
032    this.extent = extent;
033  }
034
035
036  /**
037   * The task's execution method. This method filters the model classes associated with metaclass and
038   * executes the nested &lt;template&gt; tasks for the filtered classes collection. This
039   * methods invokes the internal formatting tasks with the metaclass itself.
040   * @param metaclass Path in the metamodel to the metaclass whose instances (in the model/extent) are to be
041   * processed. Format of metaclass attribute: {&lt;package&gt;::} &lt;meta class&gt;
042   */
043  public RefObject execute(String metaclass) throws IllegalArgumentException {
044    RefClass refClass;
045
046    RefObject result;
047
048    if ( metaclass == null ) {
049      throw new IllegalArgumentException( "Expected a non-null metaclass provided");
050    }
051
052    refClass = Navigator.getMetaClass( extent, metaclass ); // non-null
053    result = refClass.refCreateInstance( null );
054
055    return result;
056  }
057}