11/*******************************************************************************
2- * Copyright 2011-2015 Data Access Technologies, Inc. (Model Driven Solutions)
2+ * Copyright 2011-2016 Data Access Technologies, Inc. (Model Driven Solutions)
33 * All rights reserved worldwide. This program and the accompanying materials
44 * are made available for use under the terms of the GNU General Public License
55 * (GPL) version 3 that accompanies this distribution and is available at
2424import org .modeldriven .alf .syntax .units .NamespaceDefinition ;
2525import org .modeldriven .alf .syntax .units .RootNamespace ;
2626import org .modeldriven .alf .syntax .units .UnitDefinition ;
27-
27+ import org . modeldriven . alf . uml . BehavioralFeature ;
2828import org .modeldriven .alf .uml .Class_ ;
2929import org .modeldriven .alf .uml .Classifier ;
3030import org .modeldriven .alf .uml .Element ;
3333import org .modeldriven .alf .uml .Namespace ;
3434import org .modeldriven .alf .uml .Package ;
3535import org .modeldriven .alf .uml .PackageableElement ;
36+ import org .modeldriven .alf .uml .Parameter ;
3637import org .modeldriven .alf .uml .ParameterableElement ;
3738import org .modeldriven .alf .uml .Signal ;
3839import org .modeldriven .alf .uml .SignalEvent ;
@@ -377,7 +378,7 @@ private static void fixExternalReferences(
377378 (InstanceSpecification )reference .getClass ().newInstance ();
378379 for (Classifier classifier :
379380 ((InstanceSpecification )reference ).getClassifier ()) {
380- Classifier newClassifier = (Classifier )getNewReference (
381+ Classifier newClassifier = (Classifier )getNewElement (
381382 instantiation , classifier , templateParameters , templateArguments );
382383 if (newClassifier == null ) {
383384 newInstanceSpecification .addClassifier (classifier );
@@ -393,7 +394,7 @@ private static void fixExternalReferences(
393394 }
394395 } else if (reference instanceof SignalEvent ) {
395396 Signal signal = (Signal )((SignalEvent ) reference ).getSignal ();
396- Signal newSignal = (Signal )getNewReference (
397+ Signal newSignal = (Signal )getNewElement (
397398 instantiation , signal , templateParameters , templateArguments );
398399 if (newSignal != null ) {
399400 try {
@@ -406,7 +407,7 @@ private static void fixExternalReferences(
406407 }
407408 }
408409 } else if (reference instanceof NamedElement ) {
409- newReference = getNewReference (
410+ newReference = getNewElement (
410411 instantiation ,
411412 (NamedElement )reference ,
412413 templateParameters , templateArguments );
@@ -418,65 +419,24 @@ private static void fixExternalReferences(
418419 }
419420 }
420421
421- /*
422- TemplateSignature instantiationSignature =
423- instantiation.getOwnedTemplateSignature();
424- TemplateSignature originalSignature =
425- template.getOwnedTemplateSignature();
426- List<TemplateParameter> instantiationParameters =
427- instantiationSignature.getOwnedParameter();
428- List<TemplateParameter> originalParameters =
429- originalSignature.getOwnedParameter();
430- references.add(instantiationSignature);
431- newReferences.add(originalSignature);
432- for (int i = 0; i < instantiationParameters.size(); i++) {
433- references.add(instantiationParameters.get(i));
434- newReferences.add(originalParameters.get(i));
435- }
436- */
437-
438422 instantiation .replaceAll (references , newReferences );
439423 }
440424
441- private static Element getNewReference (
425+ private static Element getNewElement (
442426 TemplateableElement instantiation ,
443427 NamedElement reference ,
444428 List <ElementReference > templateParameters ,
445429 List <ElementReference > templateArguments ) {
446- /*
447- QualifiedName qualifiedName =
448- makeQualifiedName((NamedElement)reference);
449- qualifiedName = qualifiedName.getImpl().updateBindings(
450- templateParameters, templateArguments);
451- for (ElementReference referent: qualifiedName.getReferent()) {
452- Element element = referent.getImpl().getUml();
453- if (element != null && !element.equals(reference) &&
454- isSameKind(element, reference)) {
455- return element;
456- }
457- }
458- */
459- // System.out.println("[getNewReference] reference=" + reference);
460- for (ElementReference referent : getNewReferences (
461- instantiation , reference , templateParameters , templateArguments )) {
462- Element element = referent .getImpl ().getUml ();
463- if (element != null && !element .equals (reference ) &&
464- isSameKind (element , reference )) {
465- // System.out.println("[getNewReference] element=" + element);
466- return element ;
467- }
468- }
469- return null ;
430+ ElementReference newReference = getNewReference (instantiation , reference , templateParameters , templateArguments );
431+ return newReference == null ? null : newReference .getImpl ().getUml ();
470432 }
471433
472- private static Collection < ElementReference > getNewReferences (
434+ private static ElementReference getNewReference (
473435 TemplateableElement instantiation ,
474436 NamedElement reference ,
475437 List <ElementReference > templateParameters ,
476438 List <ElementReference > templateArguments ) {
477- Collection <ElementReference > newReferences =
478- new ArrayList <ElementReference >();
479- String name = reference .getName ();
439+ ElementReference newReference = null ;
480440 org .modeldriven .alf .uml .TemplateBinding templateBinding =
481441 ExternalElementReferenceImpl .getTemplateBinding (reference );
482442 if (templateBinding == null ) {
@@ -485,18 +445,26 @@ private static Collection<ElementReference> getNewReferences(
485445 (Namespace )((TemplateParameter )owner ).getSignature ().getTemplate ():
486446 reference .getNamespace ();
487447 if (namespace != null ) {
488- for (ElementReference namespaceReference : getNewReferences (
489- instantiation , namespace , templateParameters , templateArguments )) {
448+ ElementReference namespaceReference = getNewReference (
449+ instantiation , namespace , templateParameters , templateArguments );
450+ if (namespaceReference != null ) {
490451 NamespaceDefinition namespaceDefinition =
491452 namespaceReference .getImpl ().asNamespace ();
492453 if (namespaceDefinition != null ) {
493- for ( Member member : namespaceDefinition . getImpl ().
494- resolveVisible ( name , null , false )) {
454+ String name = reference . getName ();
455+ for ( Member member : namespaceDefinition . getOwnedMember ( )) {
495456 ElementReference referent = member .getImpl ().getReferent ();
496- if (referent .getImpl ().isClassifierTemplateParameter ()) {
497- referent = referent .getImpl ().getParameteredElement ();
457+ if (name == null && referent .getImpl ().getName () == null ||
458+ name != null && name .equals (referent .getImpl ().getName ())) {
459+ if (referent .getImpl ().isClassifierTemplateParameter ()) {
460+ referent = referent .getImpl ().getParameteredElement ();
461+ }
462+ Element element = referent .getImpl ().getUml ();
463+ if (element != null && !element .equals (reference ) &&
464+ isSameKind (element , reference , instantiation , templateParameters , templateArguments )) {
465+ newReference = referent ;
466+ }
498467 }
499- newReferences .add (referent );
500468 }
501469 }
502470 }
@@ -512,91 +480,96 @@ private static Collection<ElementReference> getNewReferences(
512480 List <ElementReference > actuals = new ArrayList <ElementReference >();
513481 for (org .modeldriven .alf .uml .TemplateParameterSubstitution parameterSubstitution :
514482 templateBinding .getParameterSubstitution ()) {
515- formals .add (ElementReferenceImpl .
516- makeElementReference (parameterSubstitution .getFormal ()));
517- ElementReference actual = ElementReferenceImpl .
518- makeElementReference (parameterSubstitution .getActual ());
519- for (int i = 0 ; i < templateParameters .size (); i ++) {
520- if (actual .getImpl ().equals (
521- templateParameters .get (i ).getImpl ().getParameteredElement ())) {
522- actual = i >= templateArguments .size ()? null :
523- templateArguments .get (i );
524- break ;
525- }
526- }
527- actuals .add (actual );
483+ formals .add (
484+ ElementReferenceImpl .makeElementReference (parameterSubstitution .getFormal ()));
485+ actuals .add (makeSubstitution (
486+ ElementReferenceImpl .makeElementReference (parameterSubstitution .getActual ()),
487+ templateParameters , templateArguments ));
528488 }
529- Collection < ElementReference > templateReferences =
530- getNewReferences (instantiation , template , templateParameters , templateArguments );
531- if (templateReferences . isEmpty () ) {
532- newReferences . add ( QualifiedNameImpl .getBoundElement (
533- ElementReferenceImpl .makeElementReference (template ), formals , actuals )) ;
489+ ElementReference templateReference =
490+ getNewReference (instantiation , template , templateParameters , templateArguments );
491+ if (templateReference == null ) {
492+ newReference = QualifiedNameImpl .getBoundElement (
493+ ElementReferenceImpl .makeElementReference (template ), formals , actuals );
534494 } else {
535- for (ElementReference templateReferent : getNewReferences (
536- instantiation , template , templateParameters , templateArguments )) {
537- newReferences .add (QualifiedNameImpl .getBoundElement (
538- templateReferent , formals , actuals ));
539- }
495+ newReference = QualifiedNameImpl .getBoundElement (
496+ templateReference , formals , actuals );
540497 }
541498 }
542- return newReferences ;
499+ return newReference ;
543500 }
544501
545- /**
546- * Return a qualified name for the given element, including template
547- * bindings. All templates involved must be named elements.
548- */
549- /*
550- private static QualifiedName makeQualifiedName(
551- NamedElement element) {
552- QualifiedName qualifiedName;
553- org.modeldriven.alf.uml.TemplateBinding templateBinding =
554- ExternalElementReferenceImpl.getTemplateBinding(element);
555-
556- if (templateBinding == null) {
557- Element owner = element.getOwner();
558- Namespace namespace = owner instanceof TemplateParameter?
559- (Namespace)((TemplateParameter)owner).getSignature().getTemplate():
560- element.getNamespace();
561-
562- qualifiedName = namespace == null?
563- new QualifiedName(): makeQualifiedName(namespace).getImpl().copy().getSelf();
564- NameBinding nameBinding = new NameBinding();
565- nameBinding.getImpl().setExactName(element.getName());
566- qualifiedName.addNameBinding(nameBinding);
567-
502+ private static boolean isSameKind (
503+ Element element1 , Element element2 ,
504+ TemplateableElement instantiation ,
505+ List <ElementReference > templateParameters ,
506+ List <ElementReference > templateArguments ) {
507+ if (element1 .getClass () != element2 .getClass ()) {
508+ return false ;
509+ } else if (!(element1 instanceof BehavioralFeature )){
510+ return true ;
568511 } else {
569- qualifiedName = makeQualifiedName(
570- (NamedElement)templateBinding.getSignature().getTemplate());
571- List<NameBinding> nameBindings = qualifiedName.getNameBinding();
572- NameBinding nameBinding = nameBindings.get(nameBindings.size() - 1);
573- NamedTemplateBinding binding = new NamedTemplateBinding();
574- nameBinding.setBinding(binding);
575- for (org.modeldriven.alf.uml.TemplateParameterSubstitution parameterSubstitution:
576- templateBinding.getParameterSubstitution()) {
577- TemplateParameterSubstitution substitution = new
578- TemplateParameterSubstitution();
579- substitution.setParameterName(
580- ((NamedElement)parameterSubstitution.getFormal().
581- getParameteredElement()).getName());
582- NamedElement actual = (NamedElement)parameterSubstitution.getActual();
583- substitution.setArgumentName(actual == null? new QualifiedName():
584- makeQualifiedName(actual));
585- binding.addSubstitution(substitution);
512+ List <Parameter > parameters1 = ((BehavioralFeature )element1 ).getOwnedParameter ();
513+ List <Parameter > parameters2 = ((BehavioralFeature )element2 ).getOwnedParameter ();
514+ if (parameters1 .size () != parameters2 .size ()) {
515+ return false ;
516+ } else {
517+ for (int i = 0 ; i < parameters1 .size (); i ++) {
518+ Parameter parameter1 = parameters1 .get (i );
519+ Parameter parameter2 = parameters2 .get (i );
520+ if (
521+ parameter1 .getName () == null && parameter2 .getName () != null ||
522+ parameter1 .getName () != null && !parameter1 .getName ().equals (parameter2 .getName ()) ||
523+ !parameter1 .getDirection ().equals (parameter2 .getDirection ()) ||
524+ parameter1 .getLower () != parameter2 .getLower () ||
525+ parameter1 .getUpper () != parameter2 .getUpper () ||
526+ parameter1 .getIsOrdered () != parameter2 .getIsOrdered () ||
527+ parameter1 .getIsUnique () != parameter2 .getIsUnique () ||
528+ !matches (parameter1 .getType (), parameter2 .getType (),
529+ instantiation , templateParameters , templateArguments )
530+ ) {
531+ return false ;
532+ }
533+ }
534+ return true ;
586535 }
587536 }
588-
589- System.out.println("[makeQualifiedName] element=" + element);
590- System.out.println("[makeQualifiedName] qualifiedName=" + qualifiedName.getPathName());
591- qualifiedName.getImpl().setCurrentScope(RootNamespace.getRootScope());
592- qualifiedName.getImpl().setIsVisibleOnly(false);
593- return qualifiedName;
594537 }
595- */
596538
597- private static boolean isSameKind (Element element1 , Element element2 ) {
598- // TODO: Allow for overloading of operations.
599- return element1 .getClass () == element2 .getClass ();
539+ private static boolean matches (
540+ NamedElement element1 , NamedElement element2 ,
541+ TemplateableElement instantiation ,
542+ List <ElementReference > templateParameters ,
543+ List <ElementReference > templateArguments ) {
544+ if (element1 != null ) {
545+ // NOTE: This accounts for possible deferred template bindings.
546+ ElementReference reference = ElementReferenceImpl .makeElementReference (element1 );
547+ element1 = reference == null ? null : (NamedElement )reference .getImpl ().getUml ();
548+ }
549+ if (element2 != null ) {
550+ ElementReference reference = getNewReference (instantiation , element2 , templateParameters , templateArguments );
551+ if (reference == null ) {
552+ reference = ElementReferenceImpl .makeBoundReference (element2 );
553+ }
554+ reference = makeSubstitution (reference , templateParameters , templateArguments );
555+ element2 = reference == null ? null : (NamedElement )reference .getImpl ().getUml ();
556+ }
557+ return element1 == null && element2 == null ||
558+ element1 != null && element1 .equals (element2 );
559+ }
560+
561+ private static ElementReference makeSubstitution (
562+ ElementReference reference ,
563+ List <ElementReference > templateParameters ,
564+ List <ElementReference > templateArguments ) {
565+ for (int i = 0 ; i < templateParameters .size (); i ++) {
566+ if (reference .getImpl ().equals (
567+ templateParameters .get (i ).getImpl ().getParameteredElement ())) {
568+ return i >= templateArguments .size ()? null :
569+ templateArguments .get (i );
570+ }
571+ }
572+ return reference ;
600573 }
601574
602575}
0 commit comments