<commit_before>package it.unibz.krdb.obda.reformulation.tests;

/*
 * #%L
 * ontop-reformulation-core
 * %%
 * Copyright (C) 2009 - 2014 Free University of Bozen-Bolzano
 * %%
 * 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.
 * #L%
 */

import it.unibz.krdb.obda.model.*;
import it.unibz.krdb.obda.model.Predicate.COL_TYPE;
import it.unibz.krdb.obda.model.impl.FunctionalTermImpl;
import it.unibz.krdb.obda.model.impl.OBDADataFactoryImpl;
import it.unibz.krdb.obda.ontology.ClassDescription;
import it.unibz.krdb.obda.ontology.Ontology;
import it.unibz.krdb.obda.ontology.impl.OntologyFactoryImpl;
import it.unibz.krdb.obda.ontology.impl.SubClassAxiomImpl;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.CQCUtilities;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.CQContainmentCheckSyntactic;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.CQContainmentCheckUnderLIDs;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.LinearInclusionDependencies;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.PositiveInclusionApplicator;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.SyntacticCQC;
import it.unibz.krdb.obda.owlrefplatform.core.dagjgrapht.TBoxReasonerImpl;

import org.junit.Before;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class CQCUtilitiesTest {

	CQIE initialquery1 = null;

	PositiveInclusionApplicator piapplicator = new PositiveInclusionApplicator();

	OBDADataFactory pfac = OBDADataFactoryImpl.getInstance();
	OBDADataFactory tfac = OBDADataFactoryImpl.getInstance();

	Predicate r = pfac.getPredicate("R", 2, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT });
	Predicate s = pfac.getPredicate("S", 3, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT, COL_TYPE.OBJECT });
	Predicate q = pfac.getPredicate("q", 5, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT, COL_TYPE.OBJECT,
			COL_TYPE.OBJECT, COL_TYPE.OBJECT });

	Term x = tfac.getVariable("x");
	Term y = tfac.getVariable("y");
	Term c1 = tfac.getConstantURI("URI1");
	Term c2 = tfac.getConstantLiteral("m");

	Term u1 = tfac.getVariableNondistinguished();
	Term u2 = tfac.getVariableNondistinguished();

    @Before
	public void setUp() throws Exception {
		/*
		 * Creating the query:
		 * 
		 * q(x, <URI1>, 'm', y, f(x,y)) :- R(x,y), S('m', f(x), y)
		 * 
		 * Should generate
		 * 
		 * q('CANx1', <URI1>, 'm', 'CANy2', f('CANx1','CANy2')) :-
		 * R('CANx1','CANy2'), S('m', f('CANx1'), 'CANy')
		 */
		List<Term> headTerms = new LinkedList<Term>();
		headTerms.add(x);
		headTerms.add(c1);
		headTerms.add(c2);
		headTerms.add(y);
		List<Term> fterms1 = new LinkedList<Term>();
		fterms1.add(x);
		fterms1.add(y);
		headTerms.add(tfac.getFunction(pfac.getPredicate("f", 2, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT }),
				fterms1));

		Function head = tfac.getFunction(q, headTerms);

		List<Function> body = new LinkedList<Function>();

		List<Term> atomTerms1 = new LinkedList<Term>();
		atomTerms1.add(x);
		atomTerms1.add(y);
		body.add(tfac.getFunction(r, atomTerms1));

		List<Term> atomTerms2 = new LinkedList<Term>();
		atomTerms2.add(c2);
		List<Term> fterms2 = new LinkedList<Term>();
		fterms2.add(x);
		atomTerms2.add(tfac.getFunction(pfac.getPredicate("f", 1, new COL_TYPE[] { COL_TYPE.OBJECT }), fterms2));
		atomTerms2.add(y);
		body.add(tfac.getFunction(s, atomTerms2));

		initialquery1 = tfac.getCQIE(head, body);
	}

    @Test
	public void testGrounding() {
    	CQContainmentCheckUnderLIDs.FreezeCQ c2cq = new CQContainmentCheckUnderLIDs.FreezeCQ(initialquery1.getHead(), initialquery1.getBody());

    	final String CANx1 = "f0";
    	final String CANy2 = "f1";
    	
		List<Term> head = c2cq.getHead().getTerms();
		assertTrue(head.get(0).equals(tfac.getConstantLiteral(CANx1)));
		assertTrue(head.get(1).equals(tfac.getConstantURI("URI1")));
		assertTrue(head.get(2).equals(tfac.getConstantLiteral("m")));
		assertTrue(head.get(3).equals(tfac.getConstantLiteral(CANy2)));
		FunctionalTermImpl f1 = (FunctionalTermImpl) head.get(4);
		assertTrue(f1.getTerms().get(0).equals(tfac.getConstantLiteral(CANx1)));
		assertTrue(f1.getTerms().get(1).equals(tfac.getConstantLiteral(CANy2)));

		head = c2cq.getBodyAtoms().get(r).get(0).getTerms();
		assertTrue(head.get(0).equals(tfac.getConstantLiteral(CANx1)));
		assertTrue(head.get(1).equals(tfac.getConstantLiteral(CANy2)));

		head = c2cq.getBodyAtoms().get(s).get(0).getTerms();
		assertTrue(head.get(0).equals(tfac.getConstantLiteral("m")));
		f1 = (FunctionalTermImpl) head.get(1);<commit_msg>CQCUtilitiesTest fix<commit_after>package it.unibz.krdb.obda.reformulation.tests;

/*
 * #%L
 * ontop-reformulation-core
 * %%
 * Copyright (C) 2009 - 2014 Free University of Bozen-Bolzano
 * %%
 * 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.
 * #L%
 */

import it.unibz.krdb.obda.model.*;
import it.unibz.krdb.obda.model.Predicate.COL_TYPE;
import it.unibz.krdb.obda.model.impl.FunctionalTermImpl;
import it.unibz.krdb.obda.model.impl.OBDADataFactoryImpl;
import it.unibz.krdb.obda.ontology.ClassDescription;
import it.unibz.krdb.obda.ontology.Ontology;
import it.unibz.krdb.obda.ontology.impl.OntologyFactoryImpl;
import it.unibz.krdb.obda.ontology.impl.SubClassAxiomImpl;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.CQCUtilities;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.CQContainmentCheckUnderLIDs;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.LinearInclusionDependencies;
import it.unibz.krdb.obda.owlrefplatform.core.basicoperations.PositiveInclusionApplicator;
import it.unibz.krdb.obda.owlrefplatform.core.dagjgrapht.TBoxReasonerImpl;

import org.junit.Before;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class CQCUtilitiesTest {

	CQIE initialquery1 = null;

	PositiveInclusionApplicator piapplicator = new PositiveInclusionApplicator();

	OBDADataFactory pfac = OBDADataFactoryImpl.getInstance();
	OBDADataFactory tfac = OBDADataFactoryImpl.getInstance();

	Predicate r = pfac.getPredicate("R", 2, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT });
	Predicate s = pfac.getPredicate("S", 3, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT, COL_TYPE.OBJECT });
	Predicate q = pfac.getPredicate("q", 5, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT, COL_TYPE.OBJECT,
			COL_TYPE.OBJECT, COL_TYPE.OBJECT });

	Term x = tfac.getVariable("x");
	Term y = tfac.getVariable("y");
	Term c1 = tfac.getConstantURI("URI1");
	Term c2 = tfac.getConstantLiteral("m");

	Term u1 = tfac.getVariableNondistinguished();
	Term u2 = tfac.getVariableNondistinguished();

    @Before
	public void setUp() throws Exception {
		/*
		 * Creating the query:
		 * 
		 * q(x, <URI1>, 'm', y, f(x,y)) :- R(x,y), S('m', f(x), y)
		 * 
		 * Should generate
		 * 
		 * q('CANx1', <URI1>, 'm', 'CANy2', f('CANx1','CANy2')) :-
		 * R('CANx1','CANy2'), S('m', f('CANx1'), 'CANy')
		 */
		List<Term> headTerms = new LinkedList<Term>();
		headTerms.add(x);
		headTerms.add(c1);
		headTerms.add(c2);
		headTerms.add(y);
		List<Term> fterms1 = new LinkedList<Term>();
		fterms1.add(x);
		fterms1.add(y);
		headTerms.add(tfac.getFunction(pfac.getPredicate("f", 2, new COL_TYPE[] { COL_TYPE.OBJECT, COL_TYPE.OBJECT }),
				fterms1));

		Function head = tfac.getFunction(q, headTerms);

		List<Function> body = new LinkedList<Function>();

		List<Term> atomTerms1 = new LinkedList<Term>();
		atomTerms1.add(x);
		atomTerms1.add(y);
		body.add(tfac.getFunction(r, atomTerms1));

		List<Term> atomTerms2 = new LinkedList<Term>();
		atomTerms2.add(c2);
		List<Term> fterms2 = new LinkedList<Term>();
		fterms2.add(x);
		atomTerms2.add(tfac.getFunction(pfac.getPredicate("f", 1, new COL_TYPE[] { COL_TYPE.OBJECT }), fterms2));
		atomTerms2.add(y);
		body.add(tfac.getFunction(s, atomTerms2));

		initialquery1 = tfac.getCQIE(head, body);
	}

    @Test
	public void testGrounding() {
    	CQContainmentCheckUnderLIDs.FreezeCQ c2cq = new CQContainmentCheckUnderLIDs.FreezeCQ(initialquery1.getHead(), initialquery1.getBody());

    	final String CANx1 = "f0";
    	final String CANy2 = "f1";

    	
		List<Term> head = c2cq.getHead().getTerms();
		
    	System.err.println(head.get(0));
    	System.err.println(head.get(3));
		
		assertTrue(head.get(0).equals(tfac.getConstantLiteral(CANx1)));
		assertTrue(head.get(1).equals(tfac.getConstantURI("URI1")));
		assertTrue(head.get(2).equals(tfac.getConstantLiteral("m")));
		assertTrue(head.get(3).equals(tfac.getConstantLiteral(CANy2)));
		FunctionalTermImpl f1 = (FunctionalTermImpl) head.get(4);
		assertTrue(f1.getTerms().get(0).equals(tfac.getConstantLiteral(CANx1)));
		assertTrue(f1.getTerms().get(1).equals(tfac.getConstantLiteral(CANy2)));

		head = c2cq.getBodyAtoms().get(r).get(0).getTerms();
		assertTrue(head.get(0).equals(tfac.getConstantLiteral(CANx1)));
		assertTrue(head.get(1).equals(tfac.getConstantLiteral(CANy2)));

		head = c2cq.getBodyAtoms().get(s).get(0).getTerms();
		assertTrue(head.get(0).equals(tfac.getConstantLiteral("m")));
		f1 = (FunctionalTermImpl) head.get(1);
{'fraction_non_alphanumeric': 0.14692449713341754, 'fraction_numerical': 0.016907977844718685, 'mean_word_length': 5.144477611940299, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '7665215', 'n_tokens_mistral': 3964, 'n_tokens_neox': 3780, 'n_words': 648}