From 9b15ec630d0db19804c314ee3aed26b7efd3576b Mon Sep 17 00:00:00 2001 From: Mohamed Gad-Elrab Date: Mon, 22 Apr 2019 21:43:07 +0200 Subject: [PATCH] Allow multiple sources in the code. 1. Multiple text sources but only consider the first hit 2. Multiple KGs, use the union. 3. Choose from multiple KGs. --- .../java/checker/ExplanationsExtractor.java | 20 +++++++-- .../java/extendedsldnf/EvaluatorFactory.java | 14 +++--- .../ExtendedSLDNFEvaluationStrategy.java | 28 ++++++------ ...xtendedSLDNFEvaluationStrategyFactory.java | 15 ++++--- .../main/java/extendedsldnf/FactsUtils.java | 24 ++++++++++ .../java/extendedsldnf/ItrSLDEvaluator.java | 5 +-- .../java/extendedsldnf/RecSLDEvaluator.java | 44 ++++++++++++------- Utils | 2 +- 8 files changed, 103 insertions(+), 49 deletions(-) diff --git a/QueryRewriting/src/main/java/checker/ExplanationsExtractor.java b/QueryRewriting/src/main/java/checker/ExplanationsExtractor.java index b0d3d3a..e14af81 100644 --- a/QueryRewriting/src/main/java/checker/ExplanationsExtractor.java +++ b/QueryRewriting/src/main/java/checker/ExplanationsExtractor.java @@ -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; @@ -48,7 +50,7 @@ public class ExplanationsExtractor implements IDeepChecker/**/ { * Facts (iris facts) */ // private Map factsMap; - private IExtendedFacts facts; + private List facts; /** @@ -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) @@ -151,7 +154,10 @@ public IQueryExplanations check(InputQuery query,Collection specificRules RecSLDEvaluator evaluator ; EvaluatorFactory evaluatorFactory=new EvaluatorFactory(config); - evaluator = evaluatorFactory.getEvaluator(facts, rules); + List usedFactSources = getUsedFactResources(Arrays.asList("yago", "dbpedia")); + List 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) { @@ -159,6 +165,14 @@ public IQueryExplanations check(InputQuery query,Collection specificRules } return null; } + + private List getUsedTextualResources(List strings) { + return Arrays.asList(new FactSpottingConnector(config)); + } + + private List getUsedFactResources(List strings) { + return facts; + } // // @Override // public IQueryExplanations check(Fact fact, Collection ruleSet) { diff --git a/QueryRewriting/src/main/java/extendedsldnf/EvaluatorFactory.java b/QueryRewriting/src/main/java/extendedsldnf/EvaluatorFactory.java index c8ad6b5..ce7ebd1 100644 --- a/QueryRewriting/src/main/java/extendedsldnf/EvaluatorFactory.java +++ b/QueryRewriting/src/main/java/extendedsldnf/EvaluatorFactory.java @@ -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; @@ -27,21 +28,22 @@ public EvaluatorFactory(Configuration config) { // } - public RecSLDEvaluator getEvaluator(IExtendedFacts facts, List rules) { - return getEvaluator(config.getEvaluationMethod(),facts,rules,new FactSpottingConnector(config),config.getPartialBindingType(),config.isSuspectsFromKG() ); + public RecSLDEvaluator getEvaluator(List facts, List uncertainResources,List 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 rules, FactSpottingConnector factSpottingConnector, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG) { + public RecSLDEvaluator getEvaluator(Enums.EvalMethod evalMethod, List facts, List rules, List 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() ); } diff --git a/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategy.java b/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategy.java index 55008a1..c1279bf 100644 --- a/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategy.java +++ b/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategy.java @@ -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; @@ -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 * */ @@ -60,23 +60,23 @@ public ExtendedSLDNFEvaluationStrategy(IFacts facts, List rules, Configur mRules = rules; mConfiguration = configuration; } - + /** * Evaluate the query */ public IRelation evaluateQuery(IQuery query, List 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) 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 mRules; protected final Configuration mConfiguration; diff --git a/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategyFactory.java b/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategyFactory.java index 88b40a4..8717eed 100644 --- a/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategyFactory.java +++ b/QueryRewriting/src/main/java/extendedsldnf/ExtendedSLDNFEvaluationStrategyFactory.java @@ -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; @@ -37,7 +37,7 @@ /** * Factory for Extended SLDNF evaluation strategy * - * + * */ public class ExtendedSLDNFEvaluationStrategyFactory implements IEvaluationStrategyFactory { @@ -54,3 +54,4 @@ public IEvaluationStrategy createEvaluator( IFacts facts, List rules, org } } +// \ No newline at end of file diff --git a/QueryRewriting/src/main/java/extendedsldnf/FactsUtils.java b/QueryRewriting/src/main/java/extendedsldnf/FactsUtils.java index f7b6dbb..8d0b1de 100644 --- a/QueryRewriting/src/main/java/extendedsldnf/FactsUtils.java +++ b/QueryRewriting/src/main/java/extendedsldnf/FactsUtils.java @@ -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; @@ -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> variableMapList, List 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. diff --git a/QueryRewriting/src/main/java/extendedsldnf/ItrSLDEvaluator.java b/QueryRewriting/src/main/java/extendedsldnf/ItrSLDEvaluator.java index 97df48e..4a9333a 100644 --- a/QueryRewriting/src/main/java/extendedsldnf/ItrSLDEvaluator.java +++ b/QueryRewriting/src/main/java/extendedsldnf/ItrSLDEvaluator.java @@ -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; @@ -38,8 +37,8 @@ public class ItrSLDEvaluator extends RecSLDEvaluator { // this.compareMethod=compareMethod; // } - public ItrSLDEvaluator(IExtendedFacts facts, List 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 facts, List rules, List textConnectors, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG, AbstractQueriesPool.ComparisionMethod compareMethod, int maxExplanations, int maxRuleDepth) { + super(facts, rules, textConnectors, partialBindingType, suspectsFromKG, maxExplanations, maxRuleDepth); this.compareMethod = compareMethod; } diff --git a/QueryRewriting/src/main/java/extendedsldnf/RecSLDEvaluator.java b/QueryRewriting/src/main/java/extendedsldnf/RecSLDEvaluator.java index df0897d..afd0648 100644 --- a/QueryRewriting/src/main/java/extendedsldnf/RecSLDEvaluator.java +++ b/QueryRewriting/src/main/java/extendedsldnf/RecSLDEvaluator.java @@ -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; @@ -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 textConnectors; private Configuration.PartialBindingType partialBindingType; private Logger logger = LoggerFactory.getLogger(getClass()); protected static final int _MAX_NESTING_LEVEL = 5; - private IExtendedFacts mFacts; + private List mFacts; private List mRules; @@ -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 rules) { + public RecSLDEvaluator(List facts, List rules) { this(facts,rules,null, Configuration.PartialBindingType.NONE,false,Integer.MAX_VALUE,10); } @@ -105,13 +101,13 @@ public RecSLDEvaluator(IExtendedFacts facts, List rules) { * @param facts one or many facts * @param rules list of rules */ - public RecSLDEvaluator(IExtendedFacts facts, List rules, ITextConnector textConnector, Configuration.PartialBindingType partialBindingType, boolean suspectsFromKG,int maxExplanations,int maxRuleDepth) { + public RecSLDEvaluator(List facts, List rules, List 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; @@ -390,7 +386,15 @@ public List 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(); } @@ -430,7 +434,17 @@ public List processQueryAgainstText(ExtQuerySubs query, ILiteral q */ public List 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> variableMapList=new LinkedList<>(); @@ -526,7 +540,7 @@ public List processQueryAgainstFacts(ExtQuerySubs query, ILiteral List> variableMapList = new LinkedList>(); - 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); @@ -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); } @@ -789,7 +803,7 @@ public void setMaxRuleDepth(int maxRuleDepth) { this.maxRuleDepth = maxRuleDepth; } - public IFacts getFacts() { + public List getFacts() { return mFacts; } } diff --git a/Utils b/Utils index 87f4eeb..10cf0a3 160000 --- a/Utils +++ b/Utils @@ -1 +1 @@ -Subproject commit 87f4eeb888447f3c3b9a52c15dab03113e719e23 +Subproject commit 10cf0a3f1e117bb26b9242b953ddc81d4a8cfe8e