AlisaToBeBuiltComputer.java

/**
 * Copyright (c) 2004-2025 Carnegie Mellon University and others. (see Contributors file).
 * All Rights Reserved.
 * 
 * NO WARRANTY. ALL MATERIAL IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE
 * OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT
 * MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.
 * 
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Created, in part, with funding and support from the United States Government. (see Acknowledgments file).
 * 
 * This program includes and/or can make use of certain third party source code, object code, documentation and other
 * files ("Third Party Software"). The Third Party Software that is used by this program is dependent upon your system
 * configuration. By using this program, You agree to comply with any and all relevant Third Party Software terms and
 * conditions contained in any such Third Party Software or separate license file distributed with such Third Party
 * Software. The parties who own the Third Party Software ("Third Party Licensors") are intended third party benefici-
 * aries to this license with respect to the terms applicable to their Third Party Software. Third Party Software li-
 * censes only apply to the Third Party Software and not any other portion of this program or this program as a whole.
 */
package org.osate.alisa.common.ui.builder;

import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.builder.builderState.IBuilderState;
import org.eclipse.xtext.builder.clustering.CopiedResourceDescription;
import org.eclipse.xtext.builder.impl.IToBeBuiltComputerContribution;
import org.eclipse.xtext.builder.impl.ToBeBuilt;
import org.eclipse.xtext.resource.IReferenceDescription;
import org.eclipse.xtext.resource.IResourceDescription;
import org.eclipse.xtext.ui.resource.IStorage2UriMapper;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;

@SuppressWarnings("restriction")
public class AlisaToBeBuiltComputer implements IToBeBuiltComputerContribution {
  @Inject
  protected IBuilderState builderState;

  @Inject
  protected IStorage2UriMapper mapper;

  @Override
  public void removeProject(final ToBeBuilt toBeBuilt, final IProject project, final IProgressMonitor monitor) {
  }

  @Override
  public void updateProject(final ToBeBuilt toBeBuilt, final IProject project, final IProgressMonitor monitor) throws CoreException {
  }

  @Override
  public boolean removeStorage(final ToBeBuilt toBeBuilt, final IStorage storage, final IProgressMonitor monitor) {
    boolean _xifexpression = false;
    boolean _isAlisaResource = this.isAlisaResource(storage);
    if (_isAlisaResource) {
      boolean _xblockexpression = false;
      {
        toBeBuilt.getToBeUpdated().addAll(this.dependencies(storage));
        toBeBuilt.getToBeDeleted().add(this.mapper.getUri(storage));
        _xblockexpression = true;
      }
      _xifexpression = _xblockexpression;
    } else {
      _xifexpression = false;
    }
    return _xifexpression;
  }

  @Override
  public boolean updateStorage(final ToBeBuilt toBeBuilt, final IStorage storage, final IProgressMonitor monitor) {
    boolean _xifexpression = false;
    boolean _isAlisaResource = this.isAlisaResource(storage);
    if (_isAlisaResource) {
      boolean _xblockexpression = false;
      {
        toBeBuilt.getToBeUpdated().addAll(this.dependencies(storage));
        toBeBuilt.getToBeUpdated().add(this.mapper.getUri(storage));
        _xblockexpression = true;
      }
      _xifexpression = _xblockexpression;
    } else {
      _xifexpression = false;
    }
    return _xifexpression;
  }

  @Override
  public boolean isPossiblyHandled(final IStorage storage) {
    return this.isAlisaResource(storage);
  }

  @Override
  public boolean isRejected(final IFolder folder) {
    return false;
  }

  private HashSet<URI> dependencies(final IStorage storage) {
    HashSet<URI> _xblockexpression = null;
    {
      final HashMap<URI, HashSet<URI>> depCache = new HashMap<URI, HashSet<URI>>();
      final Function1<IResourceDescription, Boolean> _function = (IResourceDescription d) -> {
        return Boolean.valueOf(this.isAlisaResource(d.getURI()));
      };
      Iterable<IResourceDescription> _filter = IterableExtensions.<IResourceDescription>filter(this.builderState.getAllResourceDescriptions(), _function);
      for (final IResourceDescription rd : _filter) {
        {
          final URI sourceURI = rd.getURI();
          if ((!(rd instanceof CopiedResourceDescription))) {
            Iterable<IReferenceDescription> _referenceDescriptions = rd.getReferenceDescriptions();
            for (final IReferenceDescription reference : _referenceDescriptions) {
              {
                final URI targetURI = reference.getTargetEObjectUri().trimFragment();
                boolean _isAlisaResource = this.isAlisaResource(targetURI);
                if (_isAlisaResource) {
                  HashSet<URI> _elvis = null;
                  HashSet<URI> _get = depCache.get(targetURI);
                  if (_get != null) {
                    _elvis = _get;
                  } else {
                    HashSet<URI> _hashSet = new HashSet<URI>();
                    _elvis = _hashSet;
                  }
                  final HashSet<URI> deps = _elvis;
                  deps.add(sourceURI);
                  HashSet<URI> _get_1 = depCache.get(targetURI);
                  boolean _tripleEquals = (_get_1 == null);
                  if (_tripleEquals) {
                    depCache.put(targetURI, deps);
                  }
                }
              }
            }
          }
        }
      }
      final HashSet<URI> deps = new HashSet<URI>();
      final URI uri = this.mapper.getUri(storage);
      if ((uri != null)) {
        Set<URI> _elvis = null;
        HashSet<URI> _get = depCache.get(uri);
        if (_get != null) {
          _elvis = _get;
        } else {
          _elvis = Collections.<URI>unmodifiableSet(CollectionLiterals.<URI>newHashSet());
        }
        Iterables.<URI>addAll(deps, _elvis);
        boolean _isEmpty = deps.isEmpty();
        boolean changed = (!_isEmpty);
        while (changed) {
          {
            final Function1<URI, Set<URI>> _function_1 = (URI it) -> {
              Set<URI> _elvis_1 = null;
              HashSet<URI> _get_1 = depCache.get(it);
              if (_get_1 != null) {
                _elvis_1 = _get_1;
              } else {
                _elvis_1 = Collections.<URI>unmodifiableSet(CollectionLiterals.<URI>newHashSet());
              }
              return _elvis_1;
            };
            final List<URI> newDependencies = IterableExtensions.<URI>toList(Iterables.<URI>concat(IterableExtensions.<URI, Set<URI>>map(deps, _function_1)));
            boolean _add = Iterables.<URI>addAll(deps, newDependencies);
            changed = _add;
          }
        }
      }
      _xblockexpression = deps;
    }
    return _xblockexpression;
  }

  private boolean isAlisaResource(final IStorage storage) {
    boolean _xblockexpression = false;
    {
      final URI uri = this.mapper.getUri(storage);
      boolean _xifexpression = false;
      if ((uri != null)) {
        _xifexpression = this.isAlisaResource(uri);
      } else {
        _xifexpression = false;
      }
      _xblockexpression = _xifexpression;
    }
    return _xblockexpression;
  }

  private boolean isAlisaResource(final URI uri) {
    return this.isAlisaResource(uri.fileExtension());
  }

  public boolean isAlisaResource(final String ext) {
    return Collections.<String>unmodifiableSet(CollectionLiterals.<String>newHashSet("cat", "filter", "org", "verify", "methodregistry", "constants", "goals", "goaldoc", "reqspec", "reqdoc", "alisa", "aadl")).contains(ext);
  }
}