<commit_before>/*
 * Copyright (C) 2020 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package grakn.core.graql.reasoner.query;

import com.google.common.collect.Sets;
import grakn.core.concept.answer.ConceptMap;
import grakn.core.kb.concept.api.EntityType;
import grakn.core.kb.concept.api.Label;
import grakn.core.kb.concept.api.RelationType;
import grakn.core.kb.server.Session;
import grakn.core.kb.server.Transaction;
import grakn.core.rule.GraknTestServer;
import graql.lang.Graql;
import graql.lang.query.GraqlGet;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.List;
import java.util.stream.Collectors;

import static grakn.core.util.GraqlTestUtil.assertCollectionsNonTriviallyEqual;
import static grakn.core.util.GraqlTestUtil.loadFromFileAndCommit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

@SuppressWarnings("CheckReturnValue")
public class OntologicalQueryIT {

    @Rule
    public final ExpectedException expectedException = ExpectedException.none();

    private static String resourcePath = "test-integration/graql/reasoner/resources/";

    @ClassRule
    public static final GraknTestServer server = new GraknTestServer();

    private static Session session;
    
    @BeforeClass
    public static void loadContext(){
        session = server.sessionWithNewKeyspace();
        loadFromFileAndCommit(resourcePath, "ruleApplicabilityTest.gql", session);
    }

    @Test
    public void instancePairsRelatedToSameTypeOfEntity(){
        Session session = server.sessionWithNewKeyspace();
        loadFromFileAndCommit(resourcePath, "matchingTypesTest.gql", session);
        for(int i = 0; i < 500 ; i++) {
            try (Transaction tx = session.writeTransaction()) {
                String basePattern = "$x isa service;" +
                        "$y isa service;" +
                        "(owner: $x, capability: $xx) isa has-capability; $xx isa $type;" +
                        "(owner: $y, capability: $yy) isa has-capability; $yy isa $type;" +
                        "$y != $x;";

                String simpleQuery = "match " +
                        basePattern +
                        "get $x, $y;";
                String queryWithExclusions = "match " +
                        basePattern +
                        "$meta type entity; $type != $meta;" +
                        "$meta2 type thing; $type != $meta2;" +
                        "$meta3 type capability-type; $type != $meta3;" +
                        "get $x, $y, $type;";

                List<ConceptMap> simpleAnswers = tx.execute(Graql.parse(simpleQuery).asGet(), false);
                List<ConceptMap> simpleAnswersInferred = tx.execute(Graql.parse(simpleQuery).asGet());
                List<ConceptMap> answersWithExclusions = tx.execute(Graql.parse(queryWithExclusions).asGet(), false);
                List<ConceptMap> answersWithExclusionsInferred = tx.execute(Graql.parse(queryWithExclusions).asGet());
                assertFalse(simpleAnswers.isEmpty());
                assertFalse(answersWithExclusions.isEmpty());
                assertCollectionsNonTriviallyEqual(simpleAnswers, simpleAnswersInferred);
                assertCollectionsNonTriviallyEqual(answersWithExclusions, answersWithExclusionsInferred);
            }
        }
    }

    @Test
    public void instancesOfSubsetOfTypesExcludingGivenType() {
        try(Transaction tx = session.readTransaction()){
            String queryString = "match $x isa $type; $type sub entity; $type2 type noRoleEntity; $type2 != $type; get $x, $type;";

            List<ConceptMap> answers = tx.execute(Graql.parse(queryString).asGet(), false);
            List<ConceptMap> answersInferred = tx.execute(Graql.parse(queryString).asGet());

            assertFalse(answers.isEmpty());
            assertCollectionsNonTriviallyEqual(answers, answersInferred);
        }
    }

    //TODO need to correctly return THING and RELATION mapping for %type
    @Ignore
    @Test
    public void allInstancesAndTheirType() {
        try(Transaction tx = session.readTransaction()) {
            String queryString = "match $x isa $type; get;";

            List<ConceptMap> answers = tx.execute(Graql.parse(queryString).asGet());
            assertCollectionsNonTriviallyEqual(answers, tx.execute(Graql.parse(queryString).asGet(), false));
        }
    }<commit_msg>remove accidental loop in test (#5656)<commit_after>/*
 * Copyright (C) 2020 Grakn Labs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

package grakn.core.graql.reasoner.query;

import com.google.common.collect.Sets;
import grakn.core.concept.answer.ConceptMap;
import grakn.core.kb.concept.api.EntityType;
import grakn.core.kb.concept.api.Label;
import grakn.core.kb.concept.api.RelationType;
import grakn.core.kb.server.Session;
import grakn.core.kb.server.Transaction;
import grakn.core.rule.GraknTestServer;
import graql.lang.Graql;
import graql.lang.query.GraqlGet;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.List;
import java.util.stream.Collectors;

import static grakn.core.util.GraqlTestUtil.assertCollectionsNonTriviallyEqual;
import static grakn.core.util.GraqlTestUtil.loadFromFileAndCommit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

@SuppressWarnings("CheckReturnValue")
public class OntologicalQueryIT {

    @Rule
    public final ExpectedException expectedException = ExpectedException.none();

    private static String resourcePath = "test-integration/graql/reasoner/resources/";

    @ClassRule
    public static final GraknTestServer server = new GraknTestServer();

    private static Session session;
    
    @BeforeClass
    public static void loadContext(){
        session = server.sessionWithNewKeyspace();
        loadFromFileAndCommit(resourcePath, "ruleApplicabilityTest.gql", session);
    }

    @Test
    public void instancePairsRelatedToSameTypeOfEntity(){
        Session session = server.sessionWithNewKeyspace();
        loadFromFileAndCommit(resourcePath, "matchingTypesTest.gql", session);
        try (Transaction tx = session.writeTransaction()) {
            String basePattern = "$x isa service;" +
                    "$y isa service;" +
                    "(owner: $x, capability: $xx) isa has-capability; $xx isa $type;" +
                    "(owner: $y, capability: $yy) isa has-capability; $yy isa $type;" +
                    "$y != $x;";

            String simpleQuery = "match " +
                    basePattern +
                    "get $x, $y;";
            String queryWithExclusions = "match " +
                    basePattern +
                    "$meta type entity; $type != $meta;" +
                    "$meta2 type thing; $type != $meta2;" +
                    "$meta3 type capability-type; $type != $meta3;" +
                    "get $x, $y, $type;";

            List<ConceptMap> simpleAnswers = tx.execute(Graql.parse(simpleQuery).asGet(), false);
            List<ConceptMap> simpleAnswersInferred = tx.execute(Graql.parse(simpleQuery).asGet());
            List<ConceptMap> answersWithExclusions = tx.execute(Graql.parse(queryWithExclusions).asGet(), false);
            List<ConceptMap> answersWithExclusionsInferred = tx.execute(Graql.parse(queryWithExclusions).asGet());
            assertFalse(simpleAnswers.isEmpty());
            assertFalse(answersWithExclusions.isEmpty());
            assertCollectionsNonTriviallyEqual(simpleAnswers, simpleAnswersInferred);
            assertCollectionsNonTriviallyEqual(answersWithExclusions, answersWithExclusionsInferred);
        }
    }

    @Test
    public void instancesOfSubsetOfTypesExcludingGivenType() {
        try(Transaction tx = session.readTransaction()){
            String queryString = "match $x isa $type; $type sub entity; $type2 type noRoleEntity; $type2 != $type; get $x, $type;";

            List<ConceptMap> answers = tx.execute(Graql.parse(queryString).asGet(), false);
            List<ConceptMap> answersInferred = tx.execute(Graql.parse(queryString).asGet());

            assertFalse(answers.isEmpty());
            assertCollectionsNonTriviallyEqual(answers, answersInferred);
        }
    }

    //TODO need to correctly return THING and RELATION mapping for %type
    @Ignore
    @Test
    public void allInstancesAndTheirType() {
        try(Transaction tx = session.readTransaction()) {
            String queryString = "match $x isa $type; get;";

            List<ConceptMap> answers = tx.execute(Graql.parse(queryString).asGet());
            assertCollectionsNonTriviallyEqual(answers, tx.execute(Graql.parse(queryString).asGet(), false));
        }
    }
{'fraction_non_alphanumeric': 0.10302911479266738, 'fraction_numerical': 0.0029408881482207625, 'mean_word_length': 3.035601265822785, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 20, 'https://': 2, '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': '5216010', 'n_tokens_mistral': 2941, 'n_tokens_neox': 2783, 'n_words': 716}