001/** 002 * 003 */ 004/* 005 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved. 006 * 007 * This program and the accompanying materials are made available under the terms of the 008 * Eclipse Public License v2.0 which accompanies this distribution, and is available at 009 * http://www.eclipse.org/legal/epl-v20.html 010 */ 011package net.mdatools.modelant.core.operation.model.transform; 012 013import java.util.Map; 014 015import javax.jmi.model.Association; 016import javax.jmi.reflect.RefAssociation; 017import javax.jmi.reflect.RefAssociationLink; 018import javax.jmi.reflect.RefClass; 019import javax.jmi.reflect.RefObject; 020import javax.jmi.reflect.RefPackage; 021import javax.jmi.reflect.RefStruct; 022 023import net.mdatools.modelant.core.api.Operation; 024import net.mdatools.modelant.core.api.Procedure; 025import net.mdatools.modelant.core.api.model.NameMapping; 026import net.mdatools.modelant.core.api.name.AssociationName; 027import net.mdatools.modelant.core.api.name.ClassName; 028import net.mdatools.modelant.core.api.name.EnumValueName; 029import net.mdatools.modelant.core.api.name.FieldName; 030import net.mdatools.modelant.core.api.name.StructName; 031 032/** 033 * Identity names mapping 034 */ 035public final class IdentityNameMapping implements NameMapping { 036 037 /** 038 * @see NameMapping#mapMetaClass(ClassName, RefPackage, RefPackage, Map) 039 */ 040 public Procedure<RefObject> mapMetaClass(ClassName name, RefPackage sourceExtent, RefPackage targetExtent, Map<RefObject, RefObject> objectsMap) { 041 RefClass targetRefClass; 042 043 targetRefClass = name.getMetaClass(targetExtent); 044 045 return new Procedure<RefObject>() { 046 public void execute(RefObject sourceObject) throws RuntimeException, IllegalArgumentException { 047 RefObject result; 048 049 result = targetRefClass.refCreateInstance(null); 050 051 objectsMap.put( sourceObject, result ); 052 } 053 054 /** 055 * @see java.lang.Object#toString() 056 */ 057 public String toString() { 058 return "identity mapMetaClass: "+name; 059 } 060 }; 061 } 062 063 /** 064 * @see NameMapping#mapMetaAssociation(AssociationName, RefPackage, RefPackage, Map) 065 */ 066 public Procedure<RefAssociationLink> mapMetaAssociation(AssociationName association, RefPackage sourceExtent, RefPackage targetExtent, Map<RefObject, RefObject> objectsMap) { 067 Procedure<RefAssociationLink> result; 068 RefAssociation assoc; 069 070 assoc = association.getMetaAssociation(targetExtent); 071 072 if (((Association) assoc.refMetaObject()).isDerived()) { // the derived associations are not copied! 073 result = Procedure.EMPTY; 074 075 } else { 076 result = new Procedure<RefAssociationLink>() { 077 public void execute(RefAssociationLink source) throws RuntimeException, IllegalArgumentException { 078 RefObject targetFirst; 079 RefObject targetSecond; 080 081 targetFirst = objectsMap.get(source.refFirstEnd()); 082 targetSecond = objectsMap.get(source.refSecondEnd()); 083 084 assoc.refAddLink(targetFirst, targetSecond); 085 } 086 087 /** 088 * @see java.lang.Object#toString() 089 */ 090 public String toString() { 091 return "identity mapMetaAssociation: "+association; 092 } 093 }; 094 } 095 return result; 096 } 097 098 /** 099 * @see NameMapping#mapMetaFieldName(FieldName, RefPackage, RefPackage, Map) 100 */ 101 public Procedure<RefObject> mapMetaFieldName(FieldName name, 102 RefPackage sourceExtent, 103 RefPackage targetExtent, Map<RefObject, RefObject> objectsMap) { 104 return (Procedure<RefObject>) name.constructTransfromation().construct( sourceExtent, targetExtent, objectsMap, this ); 105 } 106 107 /** 108 * @see NameMapping#mapEnum(net.mdatools.modelant.core.api.name.EnumValueName) 109 */ 110 public EnumValueName mapEnum(EnumValueName value) { 111 return value; 112 } 113 114 /** 115 * @see NameMapping#mapStruct(StructName, RefPackage, Map) 116 */ 117 public Operation<RefStruct> mapStruct(StructName structName, RefPackage targetExtent, Map<RefObject, RefObject> objectsMap) { 118 return structName.constructCopyOperation().construct( targetExtent, objectsMap, this ); 119 } 120}