Skip to content

Commit

Permalink
Allow multiple sources in the code.
Browse files Browse the repository at this point in the history
1. Multiple text sources but only consider the first hit
2. Multiple KGs, use the union.
3. Choose from multiple KGs.
  • Loading branch information
gadelrab committed Apr 22, 2019
1 parent 6a4053e commit 9b15ec6
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 49 deletions.
20 changes: 17 additions & 3 deletions QueryRewriting/src/main/java/checker/ExplanationsExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.deri.iris.evaluation.IEvaluationStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import text.FactSpottingConnector;
import text.ITextConnector;
import utils.DataUtils;

import javax.inject.Singleton;
Expand Down Expand Up @@ -48,7 +50,7 @@ public class ExplanationsExtractor implements IDeepChecker/*<InputQuery>*/ {
* Facts (iris facts)
*/
// private Map<IPredicate,IRelation> factsMap;
private IExtendedFacts facts;
private List<IExtendedFacts> facts;


/**
Expand Down Expand Up @@ -97,7 +99,8 @@ private ExplanationsExtractor() {


// Load facts
facts= DataUtils.loadFacts(config);
//TODO create loader for several KGS
facts= Arrays.asList(DataUtils.loadFacts(config));
//logger.debug(facts.toString());
// Checks if there external data sources
// if (config.externalDataSources.size() > 0)
Expand Down Expand Up @@ -151,14 +154,25 @@ public IQueryExplanations check(InputQuery query,Collection<IRule> specificRules

RecSLDEvaluator evaluator ;
EvaluatorFactory evaluatorFactory=new EvaluatorFactory(config);
evaluator = evaluatorFactory.getEvaluator(facts, rules);
List<IExtendedFacts> usedFactSources = getUsedFactResources(Arrays.asList("yago", "dbpedia"));
List<ITextConnector> usedTextualResources = getUsedTextualResources(Arrays.asList("wiki", "bing"));
// evaluator = evaluatorFactory.getEvaluator(facts, rules);
evaluator = evaluatorFactory.getEvaluator(usedFactSources, usedTextualResources, rules);
IQueryExplanations relation = evaluator.getExplanation(query.getIQuery());
return relation;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

private List<ITextConnector> getUsedTextualResources(List<String> strings) {
return Arrays.asList(new FactSpottingConnector(config));
}

private List<IExtendedFacts> getUsedFactResources(List<String> strings) {
return facts;
}
//
// @Override
// public IQueryExplanations check(Fact fact, Collection<IRule> ruleSet) {
Expand Down
14 changes: 8 additions & 6 deletions QueryRewriting/src/main/java/extendedsldnf/EvaluatorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import extendedsldnf.datastructure.IExtendedFacts;
import org.deri.iris.api.basics.IRule;
import text.FactSpottingConnector;
import text.ITextConnector;
import utils.Enums;

import java.util.List;
Expand All @@ -27,21 +28,22 @@ public EvaluatorFactory(Configuration config) {
// }


public RecSLDEvaluator getEvaluator(IExtendedFacts facts, List<IRule> rules) {
return getEvaluator(config.getEvaluationMethod(),facts,rules,new FactSpottingConnector(config),config.getPartialBindingType(),config.isSuspectsFromKG() );
public RecSLDEvaluator getEvaluator(List<IExtendedFacts> facts, List<ITextConnector> uncertainResources,List<IRule> rules) {
// return getEvaluator(config.getEvaluationMethod(),facts,rules,new FactSpottingConnector(config),config.getPartialBindingType(),config.isSuspectsFromKG() );
return getEvaluator(config.getEvaluationMethod(),facts,rules,uncertainResources,config.getPartialBindingType(),config.isSuspectsFromKG() );

}

public RecSLDEvaluator getEvaluator(Enums.EvalMethod evalMethod, IExtendedFacts facts, List<IRule> rules, FactSpottingConnector factSpottingConnector, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG) {
public RecSLDEvaluator getEvaluator(Enums.EvalMethod evalMethod, List<IExtendedFacts> facts, List<IRule> rules, List<ITextConnector> factSpottingConnectors, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG) {
switch (evalMethod){

case SLD_ITR:
return new ItrSLDEvaluator(facts, rules,factSpottingConnector,partialBindingType,suspectsFromKG, AbstractQueriesPool.ComparisionMethod.DFS,config.getMaxExplanations(),config.getMaxRuleNesting() );
return new ItrSLDEvaluator(facts, rules,factSpottingConnectors,partialBindingType,suspectsFromKG, AbstractQueriesPool.ComparisionMethod.DFS,config.getMaxExplanations(),config.getMaxRuleNesting() );
case HEURISTIC:
return new ItrSLDEvaluator(facts, rules,factSpottingConnector,partialBindingType,suspectsFromKG, AbstractQueriesPool.ComparisionMethod.HEURISTIC,config.getMaxExplanations(),config.getMaxRuleNesting() );
return new ItrSLDEvaluator(facts, rules,factSpottingConnectors,partialBindingType,suspectsFromKG, AbstractQueriesPool.ComparisionMethod.HEURISTIC,config.getMaxExplanations(),config.getMaxRuleNesting() );
case SLD:
default:
return new RecSLDEvaluator(facts, rules,factSpottingConnector,partialBindingType,suspectsFromKG,config.getMaxExplanations(),config.getMaxRuleNesting() );
return new RecSLDEvaluator(facts, rules,factSpottingConnectors,partialBindingType,suspectsFromKG,config.getMaxExplanations(),config.getMaxRuleNesting() );

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
* Integrated Rule Inference System (IRIS):
* An extensible rule inference system for datalog with extensions.
*
* Copyright (C) 2008 Semantic Technology Institute (STI) Innsbruck,
*
* Copyright (C) 2008 Semantic Technology Institute (STI) Innsbruck,
* University of Innsbruck, Technikerstrasse 21a, 6020 Innsbruck, Austria.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library 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
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package extendedsldnf;
Expand All @@ -40,9 +40,9 @@

/**
* Implementation of the SLDNF evaluation strategy.
* For details see 'Deduktive Datenbanken' by Cremers, Griefahn
* For details see 'Deduktive Datenbanken' by Cremers, Griefahn
* and Hinze (ISBN 978-3528047009).
*
*
* @author gigi
*
*/
Expand All @@ -60,23 +60,23 @@ public ExtendedSLDNFEvaluationStrategy(IFacts facts, List<IRule> rules, Configur
mRules = rules;
mConfiguration = configuration;
}

/**
* Evaluate the query
*/
public IRelation evaluateQuery(IQuery query, List<IVariable> outputVariables) throws ProgramNotStratifiedException, RuleUnsafeException, EvaluationException {
if( query == null )
throw new IllegalArgumentException( "SLDEvaluationStrategy.evaluateQuery() - query must not be null." );
RecSLDEvaluator evaluator = new RecSLDEvaluator( (IExtendedFacts) mFacts, mRules );
throw new IllegalArgumentException( "SLDEvaluationStrategy.evaluateQuery() - query must not be null." );

RecSLDEvaluator evaluator = new RecSLDEvaluator( (List<IExtendedFacts>) mFacts, mRules );
IRelation relation = evaluator.evaluate(query);
if(outputVariables==null)
outputVariables=new LinkedList<>();
outputVariables.addAll( evaluator.getOutputVariables() );

return relation;
}

protected final IFacts mFacts;
protected final List<IRule> mRules;
protected final Configuration mConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/*
* Integrated Rule Inference System (IRIS):
* An extensible rule inference system for datalog with extensions.
*
* Copyright (C) 2008 Semantic Technology Institute (STI) Innsbruck,
*
* Copyright (C) 2008 Semantic Technology Institute (STI) Innsbruck,
* University of Innsbruck, Technikerstrasse 21a, 6020 Innsbruck, Austria.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
*
* This library 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
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package extendedsldnf;
Expand All @@ -37,7 +37,7 @@
/**
* Factory for Extended SLDNF evaluation strategy
*
*
*
*/
public class ExtendedSLDNFEvaluationStrategyFactory implements IEvaluationStrategyFactory
{
Expand All @@ -54,3 +54,4 @@ public IEvaluationStrategy createEvaluator( IFacts facts, List<IRule> rules, org
}

}
//
24 changes: 24 additions & 0 deletions QueryRewriting/src/main/java/extendedsldnf/FactsUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package extendedsldnf;

import extendedsldnf.datastructure.IExtendedFacts;
import org.deri.iris.api.basics.ILiteral;
import org.deri.iris.api.basics.IPredicate;
import org.deri.iris.api.basics.ITuple;
Expand All @@ -17,6 +18,29 @@

public class FactsUtils {


/**
* Tries to find a fact that matches the given query.
* The variableMap will be populated if a matching fact was found.
* @param queryLiteral the given query
*
* @return true if a matching fact is found, false otherwise
*/
public static boolean getMatchingFacts(ILiteral queryLiteral, List<Map<IVariable, ITerm>> variableMapList, List<IExtendedFacts> factsSources){

for(IFacts factsSource:factsSources){
getMatchingFacts( queryLiteral, variableMapList, factsSource);
}

if (variableMapList.isEmpty())
return false; // No fact found

return true;

}



/**
* Tries to find a fact that matches the given query.
* The variableMap will be populated if a matching fact was found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import config.Configuration;
import extendedsldnf.datastructure.*;

import extendedsldnf.viewing.ExecutionTree;
import org.deri.iris.EvaluationException;
import org.deri.iris.api.basics.ILiteral;
import org.deri.iris.api.basics.IQuery;
Expand Down Expand Up @@ -38,8 +37,8 @@ public class ItrSLDEvaluator extends RecSLDEvaluator {
// this.compareMethod=compareMethod;
// }

public ItrSLDEvaluator(IExtendedFacts facts, List<IRule> rules, ITextConnector textConnector, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG, AbstractQueriesPool.ComparisionMethod compareMethod, int maxExplanations, int maxRuleDepth) {
super(facts, rules, textConnector, partialBindingType, suspectsFromKG, maxExplanations, maxRuleDepth);
public ItrSLDEvaluator(List<IExtendedFacts> facts, List<IRule> rules, List<ITextConnector> textConnectors, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG, AbstractQueriesPool.ComparisionMethod compareMethod, int maxExplanations, int maxRuleDepth) {
super(facts, rules, textConnectors, partialBindingType, suspectsFromKG, maxExplanations, maxRuleDepth);
this.compareMethod = compareMethod;

}
Expand Down
44 changes: 29 additions & 15 deletions QueryRewriting/src/main/java/extendedsldnf/RecSLDEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,12 @@
import org.deri.iris.builtins.ExactEqualBuiltin;
import org.deri.iris.evaluation.topdown.*;
import org.deri.iris.factory.Factory;
import org.deri.iris.facts.IFacts;
import org.deri.iris.rules.RuleManipulator;
import org.deri.iris.rules.optimisation.ReOrderLiteralsOptimiser;
import org.deri.iris.rules.ordering.SimpleReOrdering;
import org.deri.iris.storage.IRelation;
import org.deri.iris.storage.simple.SimpleRelationFactory;
import org.deri.iris.utils.TermMatchingAndSubstitution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import text.FactSpottingConnector;
import text.FactSpottingResult;
import text.ITextConnector;
import utils.Enums;
Expand All @@ -66,18 +62,18 @@
* @author gigi
*
*/
public class RecSLDEvaluator implements ITopDownEvaluator, IExplanationGenerator {
public class RecSLDEvaluator {// implements ITopDownEvaluator, IExplanationGenerator {

private int maxExplanations;
private boolean suspectsFromKG;
private ITextConnector textConnector;
private List<ITextConnector> textConnectors;
private Configuration.PartialBindingType partialBindingType;
private Logger logger = LoggerFactory.getLogger(getClass());

protected static final int _MAX_NESTING_LEVEL = 5;


private IExtendedFacts mFacts;
private List<IExtendedFacts> mFacts;
private List<IRule> mRules;


Expand All @@ -94,7 +90,7 @@ public class RecSLDEvaluator implements ITopDownEvaluator, IExplanationGenerator
* @param facts one or many facts
* @param rules list of rules
*/
public RecSLDEvaluator(IExtendedFacts facts, List<IRule> rules) {
public RecSLDEvaluator(List<IExtendedFacts> facts, List<IRule> rules) {
this(facts,rules,null, Configuration.PartialBindingType.NONE,false,Integer.MAX_VALUE,10);
}

Expand All @@ -105,13 +101,13 @@ public RecSLDEvaluator(IExtendedFacts facts, List<IRule> rules) {
* @param facts one or many facts
* @param rules list of rules
*/
public RecSLDEvaluator(IExtendedFacts facts, List<IRule> rules, ITextConnector textConnector, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG,int maxExplanations,int maxRuleDepth) {
public RecSLDEvaluator(List<IExtendedFacts> facts, List<IRule> rules, List<ITextConnector> textConnectors, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG, int maxExplanations, int maxRuleDepth) {
mFacts = facts;
mRules = getOrderedRules(rules);


// Text connector
this.textConnector=textConnector;
this.textConnectors = textConnectors;
this.partialBindingType=partialBindingType;
this.suspectsFromKG=suspectsFromKG;
this.maxExplanations=maxExplanations;
Expand Down Expand Up @@ -390,7 +386,15 @@ public List<ExtQuerySubs> processQueryAgainstText(ExtQuerySubs query, ILiteral q
// Check text
FactSpottingResult result = null;
try {
result = textConnector.queryText(queryLiteral);
// Check textual sources in order and skip if it was found
//TODO combine results from different sources.
for (ITextConnector textConnector:textConnectors){
result = textConnector.queryText(queryLiteral);

if(result.found()){
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -430,7 +434,17 @@ public List<ExtQuerySubs> processQueryAgainstText(ExtQuerySubs query, ILiteral q
*/
public List<ExtQuerySubs> bindFromKGAggressively(ExtQuerySubs query, ILiteral queryLiteral, CostAccumulator costAccumulator) {

IRelation factRelation=mFacts.getHypotheticalBindings(queryLiteral);
// with only one source
IRelation factRelation = null;
if(!mFacts.isEmpty()) {
factRelation = mFacts.get(0).getHypotheticalBindings(queryLiteral);
for (int i=1;i<mFacts.size();i++){
factRelation.addAll(mFacts.get(i).getHypotheticalBindings(queryLiteral));
}


}

logger.debug(queryLiteral+" Hypothetical Bindings "+factRelation.size());

List<Map<IVariable, ITerm>> variableMapList=new LinkedList<>();
Expand Down Expand Up @@ -526,7 +540,7 @@ public List<ExtQuerySubs> processQueryAgainstFacts(ExtQuerySubs query, ILiteral


List<Map<IVariable, ITerm>> variableMapList = new LinkedList<Map<IVariable,ITerm>>();
if ( FactsUtils.getMatchingFacts( queryLiteral, variableMapList,mFacts ) ) {
if ( FactsUtils.getMatchingFacts( queryLiteral, variableMapList, mFacts ) ) {
boolean removePredicate=true;
// EvidenceNode evidenceNode=(queryLiteral.getAtom().isGround())? new EvidenceNode(queryLiteral, EvidenceNode.Type.KG_VALID):new EvidenceNode(queryLiteral, EvidenceNode.Type.VAR_BIND);
EvidenceNode evidenceNode=(queryLiteral.getAtom().isGround())? new EvidenceNode(queryLiteral, Enums.ActionType.KG_VALID):new EvidenceNode(queryLiteral, Enums.ActionType.KG_BIND);
Expand Down Expand Up @@ -759,7 +773,7 @@ public String getDebugPrefix(int recursionDepth, boolean inNegationAsFailureFlip
return debugPrefix;
}

@Override
// @Override
public IQueryExplanations getExplanation(IQuery query) throws EvaluationException {
return (IQueryExplanations) evaluate(query);
}
Expand Down Expand Up @@ -789,7 +803,7 @@ public void setMaxRuleDepth(int maxRuleDepth) {
this.maxRuleDepth = maxRuleDepth;
}

public IFacts getFacts() {
public List<IExtendedFacts> getFacts() {
return mFacts;
}
}
2 changes: 1 addition & 1 deletion Utils
Submodule Utils updated from 87f4ee to 10cf0a

0 comments on commit 9b15ec6

Please sign in to comment.