<commit_before>/*******************************************************************************
 * Copyright 2016 <NAME>, <NAME>', <NAME> and <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
/*
 * AggregateElement.cpp
 *
 *  Created on: 08/mar/2014
 *      Author: Jessica
 */

#include "AggregateElement.h"
#include "../../util/Utils.h"


namespace DLV2{

namespace grounder{

	AggregateElement* AggregateElement::clone(){
		AggregateElement *element=new AggregateElement;
		element->setTerms(terms);
		for(auto atom:nafLiterals)
			element->addNafLiterals(atom->clone());
		return element;
	}

	void AggregateElement::clearNafLiterals() {
		for(auto atom:nafLiterals)
			delete atom;
		nafLiterals.clear();
	}

	void AggregateElement::deleteAtoms() {
		for(auto atom:nafLiterals)
			delete atom;
	}

	set_term AggregateElement::getSafeVariable() {
		set_term safeVars;
		for(auto atom:nafLiterals)
			if(!atom->isNegative() && ! atom->isBuiltIn())
				for(auto variable:atom->getVariable())
					safeVars.insert(variable);
		return safeVars;
	}

	set_term AggregateElement::getUnsafeVariable() {
		set_term safeVars=getSafeVariable();
		set_term unsafeVars;
		for(auto atom:nafLiterals){
			set_term variableInArith;
			for(auto t:atom->getTerms())t->getVariablesInArith(variableInArith);
			if(variableInArith.size()>0)unsafeVars.insert(variableInArith.begin(),variableInArith.end());
			if(atom->isNegative() || atom->isBuiltIn()){
				for(auto variable:atom->getVariable())
					if(!safeVars.count(variable))
						unsafeVars.insert(variable);
			}
		}

		return unsafeVars;
	}

	void AggregateElement::getPredicates(set_predicate& predicates)const{
		for(auto atom:nafLiterals){
			if(atom->isClassicalLiteral())
				predicates.insert(atom->getPredicate());
		}
	}

	bool AggregateElement::areAggregationTermsSafe() const {
		set_term variables;
		for(auto term: terms)
			term->getVariable(variables);
		for(auto term: variables){
			if(term->isGround())continue;
			bool ok=false;
			for(auto atom:nafLiterals){
				if(((atom->isClassicalLiteral() && !atom->isNegative()) || atom->getBinop()==Binop::EQUAL)  && atom->getVariable().count(term)){
					ok=true;
				}
				if(atom->isExternalAtom() && atom->getOutputVariables().count(term))
					ok = true;
			}
			if(!ok)
				return false;
		}
		return true;
	}

	bool AggregateElement::operator ==(const AggregateElement& element) const {
		if(terms.size()!=element.terms.size())return false;
		if(nafLiterals.size()!=element.nafLiterals.size())return false;
		for(unsigned i=0;i<terms.size();i++)
			if(terms[i]->getIndex()!=element.terms[i]->getIndex())
				return false;
		for(unsigned i=0;i<nafLiterals.size();i++)
			if(!(*nafLiterals[i]==*element.nafLiterals[i]))
				return false;


		return true;
}



bool AggregateElement::isAllAggregateTermShared(set_term& shared_variable)const{
	for(auto term:terms){
		set_term variable_term;
		term->getVariable(variable_term);
		if(!Utils::isContained(variable_term,shared_variable))
			return false;
	}
	return true;
}

void AggregateElement::getUnsolvedPredicateVariable(set_term& vars) {
	for(auto atom:nafLiterals){
		if(atom->isClassicalLiteral() && !atom->getPredicate()->isSolved()){
			const set_term& variables=atom->getVariable();
			vars.insert(variables.begin(),variables.end());
		}
	}
}

}
}


<commit_msg>Fix external function in aggregate. Add input variable of ext atom in unsafe variable<commit_after>/*******************************************************************************
 * Copyright 2016 <NAME>, <NAME>', <NAME> and <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
/*
 * AggregateElement.cpp
 *
 *  Created on: 08/mar/2014
 *      Author: Jessica
 */

#include "AggregateElement.h"
#include "../../util/Utils.h"


namespace DLV2{

namespace grounder{

	AggregateElement* AggregateElement::clone(){
		AggregateElement *element=new AggregateElement;
		element->setTerms(terms);
		for(auto atom:nafLiterals)
			element->addNafLiterals(atom->clone());
		return element;
	}

	void AggregateElement::clearNafLiterals() {
		for(auto atom:nafLiterals)
			delete atom;
		nafLiterals.clear();
	}

	void AggregateElement::deleteAtoms() {
		for(auto atom:nafLiterals)
			delete atom;
	}

	set_term AggregateElement::getSafeVariable() {
		set_term safeVars;
		for(auto atom:nafLiterals)
			if(!atom->isNegative() && ! atom->isBuiltIn())
				for(auto variable:atom->getVariable())
					safeVars.insert(variable);
		return safeVars;
	}

	set_term AggregateElement::getUnsafeVariable() {
		set_term safeVars=getSafeVariable();
		set_term unsafeVars;
		for(auto atom:nafLiterals){
			set_term variableInArith;
			for(auto t:atom->getTerms())t->getVariablesInArith(variableInArith);
			if(variableInArith.size()>0)unsafeVars.insert(variableInArith.begin(),variableInArith.end());
			if(atom->isNegative() || atom->isBuiltIn()){
				for(auto variable:atom->getVariable())
					if(!safeVars.count(variable))
						unsafeVars.insert(variable);
			}else if(atom->isExternalAtom()){
				for(auto t:atom->getInputVariables())
					unsafeVars.insert(t);
			}
		}

		return unsafeVars;
	}

	void AggregateElement::getPredicates(set_predicate& predicates)const{
		for(auto atom:nafLiterals){
			if(atom->isClassicalLiteral())
				predicates.insert(atom->getPredicate());
		}
	}

	bool AggregateElement::areAggregationTermsSafe() const {
		set_term variables;
		for(auto term: terms)
			term->getVariable(variables);
		for(auto term: variables){
			if(term->isGround())continue;
			bool ok=false;
			for(auto atom:nafLiterals){
				if(((atom->isClassicalLiteral() && !atom->isNegative()) || atom->getBinop()==Binop::EQUAL)  && atom->getVariable().count(term)){
					ok=true;
				}
				if(atom->isExternalAtom() && atom->getOutputVariables().count(term))
					ok = true;
			}
			if(!ok)
				return false;
		}
		return true;
	}

	bool AggregateElement::operator ==(const AggregateElement& element) const {
		if(terms.size()!=element.terms.size())return false;
		if(nafLiterals.size()!=element.nafLiterals.size())return false;
		for(unsigned i=0;i<terms.size();i++)
			if(terms[i]->getIndex()!=element.terms[i]->getIndex())
				return false;
		for(unsigned i=0;i<nafLiterals.size();i++)
			if(!(*nafLiterals[i]==*element.nafLiterals[i]))
				return false;


		return true;
}



bool AggregateElement::isAllAggregateTermShared(set_term& shared_variable)const{
	for(auto term:terms){
		set_term variable_term;
		term->getVariable(variable_term);
		if(!Utils::isContained(variable_term,shared_variable))
			return false;
	}
	return true;
}

void AggregateElement::getUnsolvedPredicateVariable(set_term& vars) {
	for(auto atom:nafLiterals){
		if(atom->isClassicalLiteral() && !atom->getPredicate()->isSolved()){
			const set_term& variables=atom->getVariable();
			vars.insert(variables.begin(),variables.end());
		}
	}
}

}
}



{'fraction_non_alphanumeric': 0.17613353263577478, 'fraction_numerical': 0.004484304932735426, 'mean_word_length': 5.096431283219438, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6187039', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2558, 'n_words': 502}