<commit_before>package nl.wietmazairac.bimql;

import java.util.List;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.bimserver.emf.IdEObject;
import org.bimserver.emf.IfcModelInterface;
import org.bimserver.emf.IfcModelInterfaceException;
import org.bimserver.models.ifc2x3tc1.IfcProject;
import org.bimserver.plugins.ModelHelper;
import org.bimserver.plugins.Reporter;
import org.bimserver.plugins.objectidms.ObjectIDM;
import org.bimserver.plugins.queryengine.QueryEngine;
import org.bimserver.plugins.queryengine.QueryEngineException;
import org.bimserver.plugins.schema.Attribute;
import org.bimserver.plugins.schema.EntityDefinition;
import org.bimserver.plugins.schema.InverseAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BimQLQueryEngine implements QueryEngine {
	private static final Logger LOGGER = LoggerFactory.getLogger(BimQLQueryEngine.class);

	@Override
	public IfcModelInterface query(final IfcModelInterface model, String code, final Reporter reporter, ModelHelper modelHelper) throws QueryEngineException {
		model.fixInverseMismatches();
		
		CharStream charStream = new ANTLRStringStream(code);
		BimQLLexer lexer = new BimQLLexer(charStream) {
			@Override
			public void reportError(RecognitionException e) {
				String hdr = getErrorHeader(e);
				String msg = getErrorMessage(e, getTokenNames());
				emitErrorMessage(hdr + " " + msg);
				reporter.error(new Exception(hdr + " fault " + msg));
			}
		};
		TokenStream tokenStream = new CommonTokenStream(lexer);
		BimQLParser parser = new BimQLParser(tokenStream) {
			@Override
			public void reportError(RecognitionException e) {
				String hdr = getErrorHeader(e);
				String msg = getErrorMessage(e, tokenNames);
				emitErrorMessage(hdr + " threw this " + msg);
				reporter.error(new Exception(hdr + " " + msg));
			}
		};
		
		try {
			IfcModelInterface resultModel = modelHelper.getTargetModel();
			ObjectIDM idm = new ObjectIDM(){

				@Override
				public boolean shouldFollowReference(EClass originalClass,
						EClass eClass, EStructuralFeature eStructuralFeature) {
					EntityDefinition entityBN = model.getPackageMetaData().getSchemaDefinition().getEntityBN(eClass.getName());
					if (entityBN == null) {
						LOGGER.error(eClass.getName() + " not found");
						return false;
					} else {
						Attribute attributeBN = entityBN.getAttributeBNWithSuper(eStructuralFeature.getName());
						if (attributeBN instanceof InverseAttribute) {
							return false;
						} else {
							return true;
						}
					}
//					if (eStructuralFeature instanceof EAttribute)
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("HasAssignments"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ContainedInStructure"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("RelatingStructure"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("RelatingObject"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("UnitsInContext"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("Units"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ConversionFactor"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("PlacesObject"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ReferencedByPlacements"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ObjectPlacement"))
//						return true;<commit_msg>Changed so it works with collada export<commit_after>package nl.wietmazairac.bimql;

import java.util.List;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.bimserver.emf.IdEObject;
import org.bimserver.emf.IfcModelInterface;
import org.bimserver.emf.IfcModelInterfaceException;
import org.bimserver.models.ifc2x3tc1.IfcProject;
import org.bimserver.plugins.ModelHelper;
import org.bimserver.plugins.Reporter;
import org.bimserver.plugins.objectidms.ObjectIDM;
import org.bimserver.plugins.queryengine.QueryEngine;
import org.bimserver.plugins.queryengine.QueryEngineException;
import org.bimserver.plugins.schema.Attribute;
import org.bimserver.plugins.schema.EntityDefinition;
import org.bimserver.plugins.schema.InverseAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BimQLQueryEngine implements QueryEngine {
	private static final Logger LOGGER = LoggerFactory.getLogger(BimQLQueryEngine.class);

	@Override
	public IfcModelInterface query(final IfcModelInterface model, String code, final Reporter reporter, ModelHelper modelHelper) throws QueryEngineException {
		model.fixInverseMismatches();
		
		CharStream charStream = new ANTLRStringStream(code);
		BimQLLexer lexer = new BimQLLexer(charStream) {
			@Override
			public void reportError(RecognitionException e) {
				String hdr = getErrorHeader(e);
				String msg = getErrorMessage(e, getTokenNames());
				emitErrorMessage(hdr + " " + msg);
				reporter.error(new Exception(hdr + " fault " + msg));
			}
		};
		TokenStream tokenStream = new CommonTokenStream(lexer);
		BimQLParser parser = new BimQLParser(tokenStream) {
			@Override
			public void reportError(RecognitionException e) {
				String hdr = getErrorHeader(e);
				String msg = getErrorMessage(e, tokenNames);
				emitErrorMessage(hdr + " threw this " + msg);
				reporter.error(new Exception(hdr + " " + msg));
			}
		};
		
		try {
			IfcModelInterface resultModel = modelHelper.getTargetModel();
			ObjectIDM idm = new ObjectIDM(){

				@Override
				public boolean shouldFollowReference(EClass originalClass,
						EClass eClass, EStructuralFeature eStructuralFeature) {
					EntityDefinition entityBN = model.getPackageMetaData().getSchemaDefinition().getEntityBN(eClass.getName());
					if (entityBN == null) {
						if (eClass.getName().equals("GeometryInfo")) {
							// ignore
							return true;
						} else if (eClass.getName().equals("GeometryData") || eClass.getName().equals("Vector3f")) {
							return true;
						} else {
							LOGGER.error(eClass.getName() + " not found");
						}
						return false;
					} else {
						Attribute attributeBN = entityBN.getAttributeBNWithSuper(eStructuralFeature.getName());
						if (attributeBN instanceof InverseAttribute) {
							return false;
						} else {
							return true;
						}
					}
//					if (eStructuralFeature instanceof EAttribute)
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("HasAssignments"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ContainedInStructure"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("RelatingStructure"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("RelatingObject"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("UnitsInContext"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("Units"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ConversionFactor"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("PlacesObject"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ReferencedByPlacements"))
//						return true;
//					if (eStructuralFeature.getName().equalsIgnoreCase("ObjectPlacement"))
//						return true;
{'fraction_non_alphanumeric': 0.11120851596743894, 'fraction_numerical': 0.001377582968065122, 'mean_word_length': 4.3418060200668895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1884163', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2380, 'n_words': 443}