/** {@inheritDoc} */
    @Override
    protected CommandResult doExecuteWithResult(IProgressMonitor progressMonitor, IAdaptable info)
        throws ExecutionException {
      // Remove elements whose container is getting copied.
      // ClipboardSupportUtil.getCopyElements(getObjectsToBeDuplicated());

      // Perform the copy and update the references.
      EcoreUtil.Copier copier = new TableCopier(getAllDuplicatedObjectsMap());
      copier.copy(TableToDuplicate);
      copier.copyReferences();

      EObject duplicateTable = copier.get(TableToDuplicate);
      Resource targetResource =
          getNotationResourceForTable(((Table) duplicateTable).getContext(), getEditingDomain());
      Resource diTargetResource =
          getDiResourceForTable(((Table) duplicateTable).getContext(), getEditingDomain());

      if (targetResource != null) {
        targetResource.getContents().add(duplicateTable);

        if (diTargetResource != null) {
          try {
            IPageManager pageManager =
                ServiceUtilsForResource.getInstance().getIPageManager(diTargetResource);
            pageManager.addPage(duplicateTable);
          } catch (ServiceException e) {
            Activator.log.error(e);
          }
        } else {
          Activator.log.error(
              "It was not possible to find the di resource where to add the Table page",
              null); //$NON-NLS-1$
        }
      } else {
        Activator.log.warn(
            "It was not possible to find the Resource with the target EObject"); //$NON-NLS-1$
        targetResource = TableToDuplicate.eResource();
        if (targetResource != null) {
          Activator.log.error(
              "It was not possible to find the Resource with the source Table",
              null); //$NON-NLS-1$
          targetResource.getContents().add(duplicateTable);
          if (diTargetResource != null) {
            try {
              IPageManager pageManager =
                  ServiceUtilsForResource.getInstance().getIPageManager(diTargetResource);
              pageManager.addPage(duplicateTable);
            } catch (ServiceException e) {
              Activator.log.error(e);
            }
          } else {
            Activator.log.error(
                "It was not possible to find the di resource where to add the Table page",
                null); //$NON-NLS-1$
          }
        }
      }
      return CommandResult.newOKCommandResult(getAllDuplicatedObjectsMap());
    }
Пример #2
0
  public static AbstractElement getPredicatedElement(AbstractElement element) {
    if (element.isPredicated()) {
      return element;
    }
    if (element instanceof Assignment) {
      return getPredicatedElement(((Assignment) element).getTerminal());
    }
    if (element instanceof RuleCall) {
      final RuleCall ruleCall = (RuleCall) element;
      final AbstractRule calledRule = ruleCall.getRule();
      Group group = (Group) calledRule.getAlternatives();
      AbstractElement first = group.getElements().get(0);
      AbstractElement result = getPredicatedElement(first);
      if (ruleCall.getArguments().isEmpty()) {
        return result;
      }
      EcoreUtil.Copier copier =
          new EcoreUtil.Copier() {
            private static final long serialVersionUID = 1L;

            @Override
            public EObject copy(EObject eObject) {
              if (eObject instanceof ParameterReference) {
                ParameterReference paramRef = (ParameterReference) eObject;
                Parameter parameter = paramRef.getParameter();
                if (calledRule == GrammarUtil.containingRule(parameter)) {
                  for (NamedArgument givenArgument : ruleCall.getArguments()) {
                    if (givenArgument.getParameter() == parameter) {
                      EObject result = copy(givenArgument.getValue());
                      return result;
                    }
                  }
                  throw new IllegalStateException();
                }
              }
              return super.copy(eObject);
            }
          };
      AbstractElement clone = (AbstractElement) copier.copy(result);
      copier.copyReferences();
      return clone;
    }
    return element;
  }
Пример #3
0
  private Multimap<TerminalRule, AbstractRule> copyRuleBodies(
      final List<AbstractRule> copies,
      final Map<RuleWithParameterValues, AbstractRule> origToCopy) {
    abstract class __FlattenedGrammarAccess_1 extends EcoreUtil.Copier {
      final __FlattenedGrammarAccess_1 _this__FlattenedGrammarAccess_1 = this;

      abstract Set<Parameter> getParameterConfig(
          final RuleCall origRuleCall, final RuleCall copyRuleCall);

      abstract void mergePredicates(final AbstractElement into, final AbstractElement from);

      abstract void mergeCardinalities(final AbstractElement into, final AbstractElement from);

      abstract boolean evaluate(final Condition condition);
    }

    final HashMultimap<TerminalRule, AbstractRule> calledFrom =
        HashMultimap.<TerminalRule, AbstractRule>create();
    for (final AbstractRule copy : copies) {
      {
        AbstractRule orig = RuleWithParameterValues.getOriginalRule(copy);
        final Set<Parameter> paramValues = RuleWithParameterValues.getParamValues(copy);
        EcoreUtil.Copier copier =
            new __FlattenedGrammarAccess_1() {
              @Override
              protected void copyReference(
                  final EReference eReference, final EObject eObject, final EObject copyEObject) {
                if ((eReference == XtextPackage.Literals.RULE_CALL__RULE)) {
                  RuleCall origRuleCall = ((RuleCall) eObject);
                  RuleCall copyRuleCall = ((RuleCall) copyEObject);
                  AbstractRule _rule = origRuleCall.getRule();
                  Set<Parameter> _parameterConfig =
                      this.getParameterConfig(origRuleCall, copyRuleCall);
                  RuleWithParameterValues _ruleWithParameterValues =
                      new RuleWithParameterValues(_rule, _parameterConfig);
                  AbstractRule calledCopy = origToCopy.get(_ruleWithParameterValues);
                  copyRuleCall.setRule(calledCopy);
                  if ((calledCopy instanceof TerminalRule)) {
                    calledFrom.put(((TerminalRule) calledCopy), copy);
                  }
                } else {
                  super.copyReference(eReference, eObject, copyEObject);
                }
              }

              Set<Parameter> getParameterConfig(
                  final RuleCall origRuleCall, final RuleCall copyRuleCall) {
                EList<NamedArgument> _arguments = origRuleCall.getArguments();
                boolean _isEmpty = _arguments.isEmpty();
                if (_isEmpty) {
                  return Collections.<Parameter>emptySet();
                }
                EList<NamedArgument> _arguments_1 = origRuleCall.getArguments();
                final Function1<NamedArgument, Boolean> _function =
                    (NamedArgument it) -> {
                      Condition _value = it.getValue();
                      return Boolean.valueOf(this.evaluate(_value));
                    };
                Iterable<NamedArgument> _filter =
                    IterableExtensions.<NamedArgument>filter(_arguments_1, _function);
                final Function1<NamedArgument, Parameter> _function_1 =
                    (NamedArgument it) -> {
                      return it.getParameter();
                    };
                Iterable<Parameter> _map =
                    IterableExtensions.<NamedArgument, Parameter>map(_filter, _function_1);
                Set<Parameter> result = IterableExtensions.<Parameter>toSet(_map);
                return result;
              }

              @Override
              protected void copyContainment(
                  final EReference eReference, final EObject eObject, final EObject copyEObject) {
                boolean _matched = false;
                if (Objects.equal(eReference, XtextPackage.Literals.RULE_CALL__ARGUMENTS)) {
                  _matched = true;
                }
                if (!_matched) {
                  if (Objects.equal(eReference, XtextPackage.Literals.GROUP__GUARD_CONDITION)) {
                    _matched = true;
                  }
                }
                if (_matched) {
                  return;
                }
                super.copyContainment(eReference, eObject, copyEObject);
              }

              @Override
              public EObject copy(final EObject eObject) {
                if ((eObject instanceof Group)) {
                  Group group = ((Group) eObject);
                  Condition _guardCondition = group.getGuardCondition();
                  boolean _tripleNotEquals = (_guardCondition != null);
                  if (_tripleNotEquals) {
                    Condition _guardCondition_1 = group.getGuardCondition();
                    boolean _evaluate = this.evaluate(_guardCondition_1);
                    boolean _not = (!_evaluate);
                    if (_not) {
                      return null;
                    }
                  }
                }
                EObject result = super.copy(eObject);
                if ((result instanceof CompoundElement)) {
                  List<AbstractElement> elements = ((CompoundElement) result).getElements();
                  int _size = elements.size();
                  boolean _tripleEquals = (_size == 1);
                  if (_tripleEquals) {
                    if (((!((CompoundElement) result).isFirstSetPredicated())
                        && (!((CompoundElement) result).isPredicated()))) {
                      AbstractElement element = elements.get(0);
                      this.mergeCardinalities(element, ((AbstractElement) result));
                      this.mergePredicates(element, ((AbstractElement) result));
                      return element;
                    } else {
                      AbstractElement element_1 = elements.get(0);
                      this.mergePredicates(((AbstractElement) result), element_1);
                      element_1.setFirstSetPredicated(false);
                      element_1.setPredicated(false);
                    }
                  }
                }
                if ((eObject instanceof AbstractElement)) {
                  OriginalElement original = new OriginalElement(((AbstractElement) eObject));
                  EClass _eClass = ((AbstractElement) eObject).eClass();
                  EClass _eClass_1 = result.eClass();
                  boolean _notEquals = (!Objects.equal(_eClass, _eClass_1));
                  if (_notEquals) {
                    EClass _eClass_2 = result.eClass();
                    String _name = _eClass_2.getName();
                    String _plus = ("copy is: \'" + _name);
                    String _plus_1 = (_plus + "\' but original was: \'");
                    EClass _eClass_3 = ((AbstractElement) eObject).eClass();
                    String _name_1 = _eClass_3.getName();
                    String _plus_2 = (_plus_1 + _name_1);
                    String _plus_3 = (_plus_2 + "\'");
                    throw new IllegalStateException(_plus_3);
                  }
                  original.attachToEmfObject(result);
                }
                return result;
              }

              void mergePredicates(final AbstractElement into, final AbstractElement from) {
                boolean _isPredicated = from.isPredicated();
                if (_isPredicated) {
                  into.setPredicated(true);
                  into.setFirstSetPredicated(false);
                } else {
                  if (((!into.isPredicated()) && from.isFirstSetPredicated())) {
                    into.setFirstSetPredicated(true);
                  }
                }
              }

              void mergeCardinalities(final AbstractElement into, final AbstractElement from) {
                String c1 = into.getCardinality();
                String c2 = from.getCardinality();
                String _switchResult = null;
                boolean _matched = false;
                if ((Objects.equal(c1, "*") || Objects.equal(c2, "*"))) {
                  _matched = true;
                }
                if (!_matched) {
                  if ((Objects.equal(c1, "+") && Objects.equal(c2, "?"))) {
                    _matched = true;
                  }
                }
                if (!_matched) {
                  if ((Objects.equal(c1, "?") && Objects.equal(c2, "+"))) {
                    _matched = true;
                  }
                }
                if (_matched) {
                  _switchResult = "*";
                }
                if (!_matched) {
                  if (Objects.equal(c1, null)) {
                    _matched = true;
                    _switchResult = c2;
                  }
                }
                if (!_matched) {
                  _switchResult = c1;
                }
                into.setCardinality(_switchResult);
              }

              boolean evaluate(final Condition condition) {
                ConditionEvaluator _conditionEvaluator = new ConditionEvaluator(paramValues);
                boolean result = _conditionEvaluator.evaluate(condition);
                return result;
              }
            };
        AbstractElement _alternatives = orig.getAlternatives();
        EObject _copy = copier.copy(_alternatives);
        AbstractElement copiedBody = ((AbstractElement) _copy);
        copier.copyReferences();
        copy.setAlternatives(copiedBody);
        if ((orig instanceof ParserRule)) {
          ParserRule castedCopy = ((ParserRule) copy);
          boolean _isDefinesHiddenTokens = ((ParserRule) orig).isDefinesHiddenTokens();
          if (_isDefinesHiddenTokens) {
            castedCopy.setDefinesHiddenTokens(true);
            EList<AbstractRule> _hiddenTokens = ((ParserRule) orig).getHiddenTokens();
            for (final AbstractRule rule : _hiddenTokens) {
              {
                RuleWithParameterValues _ruleWithParameterValues =
                    new RuleWithParameterValues(rule);
                final AbstractRule copiedTerminalRule = origToCopy.get(_ruleWithParameterValues);
                EList<AbstractRule> _hiddenTokens_1 = castedCopy.getHiddenTokens();
                _hiddenTokens_1.add(copiedTerminalRule);
                calledFrom.put(((TerminalRule) copiedTerminalRule), castedCopy);
              }
            }
          }
        }
      }
    }
    return calledFrom;
  }
  /**
   * It copies the component, for the referenced resources we create a copy of the resource and
   * store it in it's own CDOResource. For this we open a transaction.
   *
   * @param resourceSet
   * @param component
   * @return
   */
  @SuppressWarnings("unused")
  private Component copyComponentWithResources(Component component) {

    @SuppressWarnings("serial")
    EcoreUtil.Copier nodeTypeCopier =
        new EcoreUtil.Copier() {

          /** Our version of copy reference has a special treatment for NetXResource object. */
          @Override
          protected void copyReference(
              EReference eReference, EObject eObject, EObject copyEObject) {

            if (eReference == LibraryPackage.Literals.COMPONENT__RESOURCE_REFS) {
              copyResourceReference(eReference, eObject, copyEObject);
            } else {
              super.copyReference(eReference, eObject, copyEObject);
            }
          }

          protected void copyResourceReference(
              EReference eReference, EObject eObject, EObject copyEObject) {
            if (eObject.eIsSet(eReference) && eReference.isMany()) {
              @SuppressWarnings("unchecked")
              InternalEList<EObject> source = (InternalEList<EObject>) eObject.eGet(eReference);
              @SuppressWarnings("unchecked")
              InternalEList<EObject> target =
                  (InternalEList<EObject>) copyEObject.eGet(getTarget(eReference));
              if (source.isEmpty()) {
                target.clear();
              } else {
                boolean isBidirectional = eReference.getEOpposite() != null;
                int index = 0;
                for (Iterator<EObject> k =
                        resolveProxies ? source.iterator() : source.basicIterator();
                    k.hasNext(); ) {
                  EObject referencedEObject = k.next();
                  EObject copyReferencedEObject = get(referencedEObject);
                  if (copyReferencedEObject == null) {
                    if (useOriginalReferences) {
                      // NetXResource is a bidi link, so
                      // make an actual copy (A copier
                      // within a copier... auch).
                      if (isBidirectional) {
                        EcoreUtil.Copier defaultCopier = new EcoreUtil.Copier();
                        EObject newEObject = defaultCopier.copy(referencedEObject);

                        if (copyEObject instanceof Component) {
                          // bug 235, component name must
                          // exist.
                          if (copyEObject instanceof Equipment) {
                            ((Component) copyEObject).setName("<name>");
                          }

                          // FIXME.
                          // we don't know where to store this
                          // non-contained reference????

                        }

                        target.addUnique(index, newEObject);
                        index++;
                      } else {
                        target.addUnique(index, referencedEObject);
                        ++index;
                      }
                    }
                  } else {

                    // This would actually do what?
                    if (isBidirectional) {
                      int position = target.indexOf(copyReferencedEObject);
                      if (position == -1) {
                        target.addUnique(index, copyReferencedEObject);
                      } else if (index != position) {
                        target.move(index, copyReferencedEObject);
                      }
                    } else {
                      target.addUnique(index, copyReferencedEObject);
                    }
                    ++index;
                  }
                }
              }
            }
          }
        };

    Component copyOfComponent = (Component) nodeTypeCopier.copy(component);
    nodeTypeCopier.copyReferences();
    return copyOfComponent;
  }