Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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;
import com.google.common.collect.Lists;
import io.mindmaps.MindmapsGraph;
import io.mindmaps.concept.Concept;
import io.mindmaps.concept.RoleType;
import io.mindmaps.concept.Rule;
import io.mindmaps.concept.Type;
import io.mindmaps.graql.internal.reasoner.query.AtomicMatchQuery;
import io.mindmaps.graql.internal.reasoner.query.AtomicQuery;
import io.mindmaps.graql.internal.reasoner.query.QueryAnswers;
import io.mindmaps.graql.internal.reasoner.query.ReasonerMatchQuery;
import io.mindmaps.graql.internal.reasoner.rule.InferenceRule;
import io.mindmaps.graql.internal.reasoner.predicate.Atomic;
import io.mindmaps.graql.internal.reasoner.query.Query;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
import java.util.stream.Collectors;
import static io.mindmaps.graql.internal.reasoner.query.QueryAnswers.getUnifiedAnswers;
public class Reasoner {
private final MindmapsGraph graph;
private final QueryBuilder qb;
private final Logger LOG = LoggerFactory.getLogger(Reasoner.class);
private final Map workingMemory = new HashMap<>();
public Reasoner(MindmapsGraph graph) {
this.graph = graph;
qb = Graql.withGraph(graph);
linkConceptTypes();
}
private boolean checkRuleApplicableToAtom(Atomic parentAtom, InferenceRule child) {
boolean relRelevant = true;
Query parent = parentAtom.getParentQuery();
Atomic childAtom = child.getRuleConclusionAtom();
if (parentAtom.isRelation()) {
Map> childRoleVarTypeMap = childAtom.getRoleVarTypeMap();
//Check for role compatibility
Map> parentRoleVarTypeMap = parentAtom.getRoleVarTypeMap();
for (Map.Entry> entry : parentRoleVarTypeMap.entrySet()) {
RoleType role = entry.getKey();
Type pType = entry.getValue().getValue();
if (pType != null) {
//vars can be matched by role types
if (childRoleVarTypeMap.containsKey(role)) {
Type chType = childRoleVarTypeMap.get(role).getValue();
//check type compatibility
if (chType != null) {
relRelevant &= pType.equals(chType) || chType.subTypes().contains(pType);
//Check for any constraints on the variables
String chVar = childRoleVarTypeMap.get(role).getKey();
String pVar = entry.getValue().getKey();
String chId = child.getBody().getSubstitution(chVar);
String pId = parent.getSubstitution(pVar);
if (!chId.isEmpty() && !pId.isEmpty())
relRelevant &= chId.equals(pId);
}
}
}
}
}
else if (parentAtom.isResource()) {
String childVal = child.getHead().getValuePredicate(childAtom.getVal());
String parentVal = parent.getValuePredicate(parentAtom.getVal());
relRelevant = parentVal.isEmpty() || parentVal.equals(childVal);
}
return relRelevant;
}
private Set getApplicableRules(Atomic atom) {
Set children = new HashSet<>();
Type type = atom.getType();
if (type == null) return children;
Collection rulesFromType = type.getRulesOfConclusion();
rulesFromType.forEach( rule -> {
InferenceRule child = workingMemory.get(rule.getId());
boolean ruleRelevant = checkRuleApplicableToAtom(atom, child);
if (ruleRelevant) children.add(rule);
});
return children;
}
private void linkConceptTypes(Rule rule) {
LOG.debug("Linking rule " + rule.getId() + "...");
MatchQuery qLHS = qb.match(qb.parsePatterns(rule.getLHS()));
MatchQuery qRHS = qb.match(qb.parsePatterns(rule.getRHS()));
Set hypothesisConceptTypes = qLHS.admin().getTypes();
Set conclusionConceptTypes = qRHS.admin().getTypes();
hypothesisConceptTypes.forEach(rule::addHypothesis);
conclusionConceptTypes.forEach(rule::addConclusion);
LOG.debug("Rule " + rule.getId() + " linked");
}
public Set getRules() {
Set rules = new HashSet<>();
MatchQuery sq = qb.parse("match $x isa inference-rule;");
List