<commit_before>package org.xtuml.bp.xtext.masl.scoping

import com.google.common.base.Charsets
import com.google.common.hash.Hashing
import com.google.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.eclipse.xtext.resource.EObjectDescription
import org.eclipse.xtext.resource.IEObjectDescription
import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy
import org.eclipse.xtext.util.IAcceptor
import org.xtuml.bp.xtext.masl.MASLExtensions
import org.xtuml.bp.xtext.masl.masl.structure.DomainDefinition
import org.xtuml.bp.xtext.masl.masl.structure.MaslModel
import org.xtuml.bp.xtext.masl.masl.structure.ObjectDefinition
import org.xtuml.bp.xtext.masl.masl.structure.TerminatorDefinition
import org.xtuml.bp.xtext.masl.masl.types.BuiltinTypeDeclaration
import org.xtuml.bp.xtext.masl.masl.types.EnumerationTypeDefinition
import org.xtuml.bp.xtext.masl.masl.types.TypeDeclaration
import org.apache.log4j.Logger
import org.xtuml.bp.xtext.masl.masl.structure.AbstractActionDefinition
import org.xtuml.bp.xtext.masl.masl.structure.AbstractActionDeclaration
import org.xtuml.bp.xtext.masl.masl.structure.RelationshipDefinition
import org.xtuml.bp.xtext.masl.masl.structure.EventDefinition

class MASLResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy {

	static val String HASH_USER_DATA_KEY = 'HASH'

	static val LOG = Logger.getLogger(MASLResourceDescriptionStrategy)

	@Inject extension MASLExtensions

	override createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		switch eObject {
			BuiltinTypeDeclaration:
				if (eObject.anonymous)
					return false
			TypeDeclaration: {
				val definition = eObject.definition
				if (definition instanceof EnumerationTypeDefinition) {
					definition.enumerators.forEach [
						acceptor.accept(EObjectDescription.create(QualifiedName.create(domainName, name), it))
					]
				}
			}
		}
		doCreateEObjectDescriptions(eObject, acceptor)
		return eObject instanceof ObjectDefinition 
			|| eObject instanceof DomainDefinition 
			|| eObject instanceof MaslModel 
			|| eObject instanceof TerminatorDefinition
			|| eObject instanceof AbstractActionDeclaration
			|| eObject instanceof AbstractActionDefinition
			|| eObject instanceof RelationshipDefinition
			|| eObject instanceof EventDefinition
	}

	private def doCreateEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		try {
			val qualifiedName = qualifiedNameProvider.getFullyQualifiedName(eObject)
			if (qualifiedName !== null) {
				val hash = eObject.hash
				if(hash != null) 
					acceptor.accept(EObjectDescription.create(qualifiedName, eObject, #{HASH_USER_DATA_KEY->hash}))
				else 
					acceptor.accept(EObjectDescription.create(qualifiedName, eObject))
			}
		} catch (Exception exc) {
			LOG.error(exc.getMessage(), exc)
		}
		return true
	}

	/**
	 * We have to make sure the downstream dependencies get rebuilt when a signature changes. 
	 * This is why we add a hash of the signature to the eobject description. <commit_msg>job #9025 added ProjectDefinition to the MASL description strategy so projects are indexed<commit_after>package org.xtuml.bp.xtext.masl.scoping

import com.google.common.base.Charsets
import com.google.common.hash.Hashing
import com.google.inject.Inject
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.naming.QualifiedName
import org.eclipse.xtext.nodemodel.util.NodeModelUtils
import org.eclipse.xtext.resource.EObjectDescription
import org.eclipse.xtext.resource.IEObjectDescription
import org.eclipse.xtext.resource.impl.DefaultResourceDescriptionStrategy
import org.eclipse.xtext.util.IAcceptor
import org.xtuml.bp.xtext.masl.MASLExtensions
import org.xtuml.bp.xtext.masl.masl.structure.DomainDefinition
import org.xtuml.bp.xtext.masl.masl.structure.MaslModel
import org.xtuml.bp.xtext.masl.masl.structure.ObjectDefinition
import org.xtuml.bp.xtext.masl.masl.structure.TerminatorDefinition
import org.xtuml.bp.xtext.masl.masl.types.BuiltinTypeDeclaration
import org.xtuml.bp.xtext.masl.masl.types.EnumerationTypeDefinition
import org.xtuml.bp.xtext.masl.masl.types.TypeDeclaration
import org.apache.log4j.Logger
import org.xtuml.bp.xtext.masl.masl.structure.AbstractActionDefinition
import org.xtuml.bp.xtext.masl.masl.structure.AbstractActionDeclaration
import org.xtuml.bp.xtext.masl.masl.structure.RelationshipDefinition
import org.xtuml.bp.xtext.masl.masl.structure.EventDefinition
import org.xtuml.bp.xtext.masl.masl.structure.ProjectDefinition

class MASLResourceDescriptionStrategy extends DefaultResourceDescriptionStrategy {

	static val String HASH_USER_DATA_KEY = 'HASH'

	static val LOG = Logger.getLogger(MASLResourceDescriptionStrategy)

	@Inject extension MASLExtensions

	override createEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		switch eObject {
			BuiltinTypeDeclaration:
				if (eObject.anonymous)
					return false
			TypeDeclaration: {
				val definition = eObject.definition
				if (definition instanceof EnumerationTypeDefinition) {
					definition.enumerators.forEach [
						acceptor.accept(EObjectDescription.create(QualifiedName.create(domainName, name), it))
					]
				}
			}
		}
		doCreateEObjectDescriptions(eObject, acceptor)
		return eObject instanceof ObjectDefinition 
			|| eObject instanceof DomainDefinition 
			|| eObject instanceof MaslModel 
			|| eObject instanceof TerminatorDefinition
			|| eObject instanceof AbstractActionDeclaration
			|| eObject instanceof AbstractActionDefinition
			|| eObject instanceof RelationshipDefinition
			|| eObject instanceof EventDefinition
			|| eObject instanceof ProjectDefinition
	}

	private def doCreateEObjectDescriptions(EObject eObject, IAcceptor<IEObjectDescription> acceptor) {
		try {
			val qualifiedName = qualifiedNameProvider.getFullyQualifiedName(eObject)
			if (qualifiedName !== null) {
				val hash = eObject.hash
				if(hash != null) 
					acceptor.accept(EObjectDescription.create(qualifiedName, eObject, #{HASH_USER_DATA_KEY->hash}))
				else 
					acceptor.accept(EObjectDescription.create(qualifiedName, eObject))
			}
		} catch (Exception exc) {
			LOG.error(exc.getMessage(), exc)
		}
		return true
	}

	/**
	 * We have to make sure the downstream dependencies get rebuilt when a signature changes. 
	 * This is why we add a hash of the signature to the eobject description. 
{'fraction_non_alphanumeric': 0.08197226502311249, 'fraction_numerical': 0.0009244992295839754, 'mean_word_length': 5.25940212150434, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7666703', 'n_tokens_mistral': 2141, 'n_tokens_neox': 1930, 'n_words': 365}