<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.
 *******************************************************************************/
/*
 * LpoptFacade.cpp
 *
 *  Created on: Mar 28, 2017
 *      Author: davide
 */

#include "LpoptFacade.h"

#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <memory>

#include "../../input/LpoptInputBuilder.h"
#include "../../input/InputDirector.h"
#include "../exception/LpoptException.h"

namespace DLV2{
namespace grounder{


constexpr char LpoptFacade::TEMPFILE[];

void LpoptFacade::decomposeProgram() {
	lpopt_s_input ss = generateStringStreamFromRules();
	callLpopt(ss);

	DLV2::InputDirector director;
	LpoptInputBuilder* builder=new LpoptInputBuilder;
	director.configureBuilder(builder);
	director.parse({TEMPFILE});
	bool errors=builder->errorsFound();
	delete builder;
	//	remove(TEMPFILE);

	if(errors)
		throw LpoptException("--> Errors occurred while decomposing the input program.");

}

LpoptFacade::lpopt_s_input LpoptFacade::generateStringStreamFromRules() {
	lpopt_s_input ss;
	auto sd = StatementDependency::getInstance();
	vector<DLV2::grounder::Rule*> orderedRule;
	unsigned sizeRule = sd->getRulesSize(), sizeConstraints =
			sd->getConstraintSize(), sizeWeak = sd->getWeakContraintsSize();
	orderedRule.resize(sizeRule + sizeConstraints + sizeWeak);
	for (unsigned i = 0; i < sizeRule + sizeConstraints + sizeWeak; i++) {
		auto r =
				(i < sizeRule) ? sd->getRule(i) :
				(i < sizeConstraints+sizeRule) ?
						sd->getConstraint(i - sizeRule) :
						sd->getWeakContraint(i-sizeRule-sizeConstraints);
		unsigned index=r->getIndex();
		if(index>=orderedRule.size())
			orderedRule.resize(index+1);
		orderedRule[index] = r;
	}
	for (auto rule : orderedRule) {
		Rule* cloned=rule;
		bool applyPush=true;
		for(auto& atom:rule->getBody())
			if(atom->isAggregateAtom())
				applyPush=false;
		if(rule->isChoiceRule())
			applyPush=false;
		if(rule->isWeakConstraint())
			applyPush=false;
		if(applyPush){
			cloned=rule->clone();
			rewriter.pushBuiltinArithOnlyInBody(cloned);
		}
//		cout << "INDEX " << cloned->getIndex() << " ";cloned->print();
		cloned->print(ss);
		if(applyPush){
			cloned->free();
			delete cloned;
		}
	}
	return ss;
}

void LpoptFacade::callLpopt(LpoptFacade::lpopt_s_input& ss) {

	using namespace lpopt;

	fstream myfile;
	myfile.open(TEMPFILE, fstream::out);

	LpoptOptions options;
	unsigned int seed = (options.seed==-1)?(unsigned int)time(NULL):(unsigned int)options.seed;<commit_msg>Fixed default seed to 0<commit_after>/*******************************************************************************
 *   Copyright 2016 <NAME>, <NAME>', <NAME> and <NAME>ari
 *  
 *   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.
 *******************************************************************************/
/*
 * LpoptFacade.cpp
 *
 *  Created on: Mar 28, 2017
 *      Author: davide
 */

#include "LpoptFacade.h"

#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <memory>

#include "../../input/LpoptInputBuilder.h"
#include "../../input/InputDirector.h"
#include "../exception/LpoptException.h"

namespace DLV2{
namespace grounder{


constexpr char LpoptFacade::TEMPFILE[];

void LpoptFacade::decomposeProgram() {
	lpopt_s_input ss = generateStringStreamFromRules();
	callLpopt(ss);

	DLV2::InputDirector director;
	LpoptInputBuilder* builder=new LpoptInputBuilder;
	director.configureBuilder(builder);
	director.parse({TEMPFILE});
	bool errors=builder->errorsFound();
	delete builder;
	//	remove(TEMPFILE);

	if(errors)
		throw LpoptException("--> Errors occurred while decomposing the input program.");

}

LpoptFacade::lpopt_s_input LpoptFacade::generateStringStreamFromRules() {
	lpopt_s_input ss;
	auto sd = StatementDependency::getInstance();
	vector<DLV2::grounder::Rule*> orderedRule;
	unsigned sizeRule = sd->getRulesSize(), sizeConstraints =
			sd->getConstraintSize(), sizeWeak = sd->getWeakContraintsSize();
	orderedRule.resize(sizeRule + sizeConstraints + sizeWeak);
	for (unsigned i = 0; i < sizeRule + sizeConstraints + sizeWeak; i++) {
		auto r =
				(i < sizeRule) ? sd->getRule(i) :
				(i < sizeConstraints+sizeRule) ?
						sd->getConstraint(i - sizeRule) :
						sd->getWeakContraint(i-sizeRule-sizeConstraints);
		unsigned index=r->getIndex();
		if(index>=orderedRule.size())
			orderedRule.resize(index+1);
		orderedRule[index] = r;
	}
	for (auto rule : orderedRule) {
		Rule* cloned=rule;
		bool applyPush=true;
		for(auto& atom:rule->getBody())
			if(atom->isAggregateAtom())
				applyPush=false;
		if(rule->isChoiceRule())
			applyPush=false;
		if(rule->isWeakConstraint())
			applyPush=false;
		if(applyPush){
			cloned=rule->clone();
			rewriter.pushBuiltinArithOnlyInBody(cloned);
		}
//		cout << "INDEX " << cloned->getIndex() << " ";cloned->print();
		cloned->print(ss);
		if(applyPush){
			cloned->free();
			delete cloned;
		}
	}
	return ss;
}

void LpoptFacade::callLpopt(LpoptFacade::lpopt_s_input& ss) {

	using namespace lpopt;

	fstream myfile;
	myfile.open(TEMPFILE, fstream::out);

	LpoptOptions options;
	unsigned int seed = (options.seed==-1)?(unsigned int)0:(unsigned int)options.seed;
{'fraction_non_alphanumeric': 0.17755548608940294, 'fraction_numerical': 0.006564551422319475, 'mean_word_length': 4.770063119927863, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 61, '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': '1443429', 'n_tokens_mistral': 2175, 'n_tokens_neox': 2009, 'n_words': 481}