
io.mindmaps.graql.internal.reasoner.query.Query 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.query;
import com.google.common.collect.Sets;
import io.mindmaps.MindmapsGraph;
import io.mindmaps.concept.Concept;
import io.mindmaps.concept.Type;
import io.mindmaps.graql.Graql;
import io.mindmaps.graql.MatchQuery;
import io.mindmaps.graql.admin.Conjunction;
import io.mindmaps.graql.internal.query.match.MatchOrder;
import io.mindmaps.graql.internal.query.match.MatchQueryInternal;
import io.mindmaps.util.ErrorMessage;
import io.mindmaps.graql.admin.PatternAdmin;
import io.mindmaps.graql.internal.pattern.Patterns;
import io.mindmaps.graql.internal.reasoner.predicate.Atomic;
import io.mindmaps.graql.internal.reasoner.predicate.AtomicFactory;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.mindmaps.graql.internal.reasoner.Utility.createFreshVariable;
public class Query implements MatchQueryInternal {
protected final MindmapsGraph graph;
protected final Set atomSet;
private final Conjunction pattern;
private final Set selectVars;
public Query(String query, MindmapsGraph graph) {
this.graph = graph;
MatchQuery matchQuery = Graql.withGraph(graph).parse(query);
this.selectVars = Sets.newHashSet(matchQuery.admin().getSelectedNames());
this.atomSet = AtomicFactory.createAtomSet(matchQuery.admin().getPattern(), this);
this.pattern = createPattern(atomSet);
}
public Query(MatchQuery query, MindmapsGraph graph) {
this.graph = graph;
this.selectVars = Sets.newHashSet(query.admin().getSelectedNames());
this.atomSet = AtomicFactory.createAtomSet(query.admin().getPattern(), this);
this.pattern = createPattern(atomSet);
}
public Query(Query q) {
this(q.toString(), q.graph);
}
protected Query(Atomic atom) {
if (atom.getParentQuery() == null)
throw new IllegalArgumentException(ErrorMessage.PARENT_MISSING.getMessage(atom.toString()));
this.graph = atom.getParentQuery().getGraph().orElse(null);
this.selectVars = Sets.newHashSet(atom.getMatchQuery(graph).admin().getSelectedNames());
this.pattern = Patterns.conjunction(Sets.newHashSet());
atomSet = new HashSet<>();
addAtom(AtomicFactory.create(atom, this));
addAtomConstraints(atom.getSubstitutions());
addAtomConstraints(atom.getValuePredicates());
if(atom.isRelation() || atom.isResource())
addAtomConstraints(atom.getTypeConstraints()
.stream().filter(at -> !at.isRuleResolvable())
.collect(Collectors.toSet()));
}
//alpha-equivalence equality
@Override
public boolean equals(Object obj){
if (!(obj instanceof Query)) return false;
Query a2 = (Query) obj;
return this.isEquivalent(a2);
}
@Override
public int hashCode(){
int hashCode = 1;
SortedSet hashes = new TreeSet<>();
atomSet.forEach(atom -> hashes.add(atom.equivalenceHashCode()));
Iterator it = hashes.iterator();
while(it.hasNext()){
Integer hash = it.next();
hashCode = hashCode * 37 + hash;
}
return hashCode;
}
public void print(){ atomSet.forEach(System.out::println);}
@Override
public String toString() { return getMatchQuery().toString();}
@Override
public Set getTypes(MindmapsGraph graph){ return getMatchQuery().admin().getTypes(graph);}
@Override
public Set getTypes() { return getMatchQuery().admin().getTypes(); }
@Override
public Set getSelectedNames() { return selectVars;}
@Override
public Stream
© 2015 - 2025 Weber Informatics LLC | Privacy Policy