<commit_before>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.graql.internal.reasoner;

import ai.grakn.GraknGraph;
import ai.grakn.concept.Rule;
import ai.grakn.concept.TypeName;
import ai.grakn.exception.GraknValidationException;
import ai.grakn.graql.internal.reasoner.cache.LazyQueryCache;
import ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery;
import ai.grakn.graql.internal.reasoner.rule.InferenceRule;
import ai.grakn.util.Schema;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

import static ai.grakn.graql.Graql.name;
import static ai.grakn.graql.Graql.var;

/**
 *
 * <p>
 * Class providing reasoning utility functions.
 * </p>
 *
 * @author <NAME>
 *
 */
public class Reasoner {

    private static int commitFrequency = 50;
    private static final Logger LOG = LoggerFactory.getLogger(Reasoner.class);

    public static void commitGraph(GraknGraph graph) {
        try {
            graph.commit();
        } catch (GraknValidationException e) {
            LOG.error(e.getMessage());
        }
    }

    public static void setCommitFrequency(int freq){ commitFrequency = freq;}
    public static int getCommitFrequency(){ return commitFrequency;}

    /**
     *
     * @param graph to be checked against
     * @return set of inference rule contained in the graph
     */
    public static Set<Rule> getRules(GraknGraph graph) {
        return new HashSet<>(graph.admin().getMetaRuleInference().instances());
    }

    /**
     *
     * @param graph to be checked against
     * @return true if at least one inference rule is present in the graph
     */
    public static boolean hasRules(GraknGraph graph) {
        TypeName inferenceRule = Schema.MetaSchema.INFERENCE_RULE.getName();
        return graph.graql().infer(false).match(var("x").isa(name(inferenceRule))).ask().execute();
    }

    /**
     * materialise all possible inferences
     */

    public static void precomputeInferences(GraknGraph graph){
        LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
        Set<ReasonerAtomicQuery> subGoals = new HashSet<>();
        getRules(graph).forEach(rl -> {
            InferenceRule rule = new InferenceRule(rl, graph);
            ReasonerAtomicQuery atomicQuery = new ReasonerAtomicQuery(rule.getHead());
            long dAns;
            Set<ReasonerAtomicQuery> SG;
            do {
                SG = new HashSet<>(subGoals);
                dAns = cache.getAnswers(atomicQuery).count();
                atomicQuery.answerStream(SG, cache, true);
                LOG.debug("Atom: " + atomicQuery.getAtom() + " answers: " + dAns);
                dAns = cache.getAnswers(atomicQuery).count() - dAns;
            } while (dAns != 0);
            subGoals.addAll(SG);
        });
    }

}<commit_msg>Make sure to consume streams when precompute all (#1142)<commit_after>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Grakn 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.graql.internal.reasoner;

import ai.grakn.GraknGraph;
import ai.grakn.concept.Concept;
import ai.grakn.concept.Rule;
import ai.grakn.concept.TypeName;
import ai.grakn.exception.GraknValidationException;
import ai.grakn.graql.VarName;
import ai.grakn.graql.internal.reasoner.cache.LazyQueryCache;
import ai.grakn.graql.internal.reasoner.query.ReasonerAtomicQuery;
import ai.grakn.graql.internal.reasoner.rule.InferenceRule;
import ai.grakn.util.Schema;

import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

import static ai.grakn.graql.Graql.name;
import static ai.grakn.graql.Graql.var;

/**
 *
 * <p>
 * Class providing reasoning utility functions.
 * </p>
 *
 * @author <NAME>
 *
 */
public class Reasoner {

    private static int commitFrequency = 50;
    private static final Logger LOG = LoggerFactory.getLogger(Reasoner.class);

    public static void commitGraph(GraknGraph graph) {
        try {
            graph.commit();
        } catch (GraknValidationException e) {
            LOG.error(e.getMessage());
        }
    }

    public static void setCommitFrequency(int freq){ commitFrequency = freq;}
    public static int getCommitFrequency(){ return commitFrequency;}

    /**
     *
     * @param graph to be checked against
     * @return set of inference rule contained in the graph
     */
    public static Set<Rule> getRules(GraknGraph graph) {
        return new HashSet<>(graph.admin().getMetaRuleInference().instances());
    }

    /**
     *
     * @param graph to be checked against
     * @return true if at least one inference rule is present in the graph
     */
    public static boolean hasRules(GraknGraph graph) {
        TypeName inferenceRule = Schema.MetaSchema.INFERENCE_RULE.getName();
        return graph.graql().infer(false).match(var("x").isa(name(inferenceRule))).ask().execute();
    }

    /**
     * materialise all possible inferences
     */

    public static void precomputeInferences(GraknGraph graph){
        LazyQueryCache<ReasonerAtomicQuery> cache = new LazyQueryCache<>();
        Set<ReasonerAtomicQuery> subGoals = new HashSet<>();
        getRules(graph).forEach(rl -> {
            InferenceRule rule = new InferenceRule(rl, graph);
            ReasonerAtomicQuery atomicQuery = new ReasonerAtomicQuery(rule.getHead());
            long dAns = 0;
            Set<ReasonerAtomicQuery> SG;
            do {
                SG = new HashSet<>(subGoals);
                Set<Map<VarName, Concept>> answers = atomicQuery.answerStream(SG, cache, true).collect(Collectors.toSet());
                LOG.debug("Atom: " + atomicQuery.getAtom() + " answers: " + answers.size() + " dAns: " + dAns);
                dAns = cache.answerSize(SG) - dAns;
                Reasoner.commitGraph(graph);
            } while (dAns != 0);
            subGoals.addAll(SG);
        });
    }
}
{'fraction_non_alphanumeric': 0.10403513587702443, 'fraction_numerical': 0.0034312379906670325, 'mean_word_length': 3.0082508250825084, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '680654', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2087, 'n_words': 601}