All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.mindmaps.graql.internal.reasoner.container.QueryAnswers Maven / Gradle / Ivy

/*
 * MindmapsDB - A Distributed Semantic Database
 * Copyright (C) 2016  Mindmaps Research Ltd
 *
 * MindmapsDB 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.
 *
 * MindmapsDB 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 MindmapsDB. If not, see .
 */

package io.mindmaps.graql.internal.reasoner.container;

import io.mindmaps.concept.Concept;
import io.mindmaps.graql.internal.reasoner.predicate.Substitution;

import java.util.*;
import java.util.stream.Collectors;

public class QueryAnswers extends HashSet> {

    public QueryAnswers(){super();}
    public QueryAnswers(Collection> ans){ super(ans);}

    public QueryAnswers filter(Set vars) {
        QueryAnswers results = new QueryAnswers();
        if (this.isEmpty()) return results;

        this.forEach(answer -> {
            Map map = new HashMap<>();
            answer.forEach((var, concept) -> {
                if (vars.contains(var))
                    map.put(var, concept);
            });
            if (!map.isEmpty()) results.add(map);
        });

        return new QueryAnswers(results.stream().distinct().collect(Collectors.toSet()));
    }

    public QueryAnswers join(QueryAnswers localTuples) {
        if (this.isEmpty() || localTuples.isEmpty())
            return new QueryAnswers();

        QueryAnswers join = new QueryAnswers();
        for( Map lanswer : localTuples){
            for (Map answer : this){
                boolean isCompatible = true;
                Iterator> it = lanswer.entrySet().iterator();
                while(it.hasNext() && isCompatible) {
                    Map.Entry entry = it.next();
                    String var = entry.getKey();
                    Concept concept = entry.getValue();
                    if(answer.containsKey(var) && !concept.equals(answer.get(var)))
                        isCompatible = false;
                }

                if (isCompatible) {
                    Map merged = new HashMap<>();
                    merged.putAll(lanswer);
                    merged.putAll(answer);
                    join.add(merged);
                }
            }
        }
        return join;
    }

    public void materialize(AtomicQuery atomicQuery){
        this.forEach(answer -> {
            Set subs = new HashSet<>();
            answer.forEach((var, con) -> {
                Substitution sub = new Substitution(var, con);
                if (!atomicQuery.containsAtom(sub))
                    subs.add(sub);
            });
            atomicQuery.materialize(subs);
        });
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy