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.selector;
009
010import java.util.Collection;
011
012import javax.jmi.reflect.JmiException;
013import javax.jmi.reflect.RefObject;
014import javax.jmi.reflect.RefPackage;
015
016import net.mdatools.modelant.core.api.Selector;
017import net.mdatools.modelant.core.util.Navigator;
018
019/**
020 * Selector of all model elements, that are instances of a metaclass
021 * @author popovr
022 */
023public class SelectMetaclassInstances implements Selector<RefPackage, RefObject> {
024
025  /**
026   * The qualified name of a metaclass
027   * not null
028   */
029  private final String metaclass;
030
031  /**
032   * @param metaclass Path in the metamodel to the metaclass whose instances (in the model/extent) are to be
033   * processed. Format of metaclass attribute: {&lt;package&gt;::}&lt;meta class&gt;
034   */
035  public SelectMetaclassInstances(String metaclass) {
036    if ( metaclass == null || metaclass.trim().isEmpty() ) { // iteration on model class instances
037      throw new IllegalArgumentException( "Empty metaclass provided ");
038    }
039    this.metaclass = metaclass;
040  }
041
042  /**
043   * @param sourceExtent not null extent
044   */
045  public Collection<RefObject> execute(RefPackage sourceExtent) throws JmiException {
046    Collection<RefObject> result;
047
048    // locate the meta class (in metaClass) within the metamodel
049    result = Navigator.getMetaClass( sourceExtent, metaclass ).refAllOfClass();
050
051    return result;
052  }
053}