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.condition;
009
010import javax.jmi.reflect.RefClass;
011import javax.jmi.reflect.RefObject;
012import javax.jmi.reflect.RefPackage;
013
014import net.mdatools.modelant.core.api.Condition;
015import net.mdatools.modelant.core.util.Navigator;
016
017
018/**
019 * Check if a model element is of a meta-model class.
020 * @author Rusi Popov (popovr@mdatools.net)
021 */
022public class IsInstanceOf implements Condition<RefObject> {
023
024  private final String metaClassName;
025
026  /**
027   * The name of the metaclass that holds the model elements for processing.
028   * <p>
029   * Format: <b>{&lt;package&gt;::}&lt;meta class&gt; </b>
030   * <p>
031   * In UML 1.3 the metaclass name might be:
032   * <ul>
033   * <li>Foundation::Core::Class - contains all model classes
034   * <li>Foundation::Core::Attribute - holds all the attributes of model classes
035   * <li>Behavioral_Elements::State_Machines::State holds all states in state machine models
036   * </ul>
037   * @param metaclass is the metaclass to set.
038   */
039  public IsInstanceOf(String metaclass) {
040    if ( metaclass == null || metaclass.isEmpty() ) {
041      throw new IllegalArgumentException("Expected a non-empty metaclass provided");
042    }
043    this.metaClassName = metaclass;
044  }
045
046  /**
047   * @return the result of eval() method of the class provided
048   */
049  public final boolean eval(RefObject value) {
050    boolean result;
051    RefPackage extent;
052    RefClass metaclass;
053
054    extent = value.refOutermostPackage();
055
056    metaclass = Navigator.getMetaClass( extent, metaClassName );
057    result = value.refIsInstanceOf( metaclass.refMetaObject(), true );
058
059    return result;
060  }
061}