From 799d9461cacf4b5db986b675d695f875c9aa93c7 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Mon, 1 Apr 2019 17:45:39 +0200 Subject: [PATCH 01/22] remove unneeded constructors --- src/sneps/network/PropositionNode.java | 23 ++---------- src/sneps/network/RuleNode.java | 35 +++---------------- src/sneps/snip/rules/AndEntailment.java | 11 ++---- src/sneps/snip/rules/AndOrEntailment.java | 13 ++----- src/sneps/snip/rules/DoIfNode.java | 7 ++-- src/sneps/snip/rules/NumericalEntailment.java | 6 ++-- src/sneps/snip/rules/OrEntailment.java | 13 ++----- src/sneps/snip/rules/ThreshEntailment.java | 13 ++----- src/sneps/snip/rules/WhenDoNode.java | 7 ++-- 9 files changed, 20 insertions(+), 108 deletions(-) diff --git a/src/sneps/network/PropositionNode.java b/src/sneps/network/PropositionNode.java index 79c4b448..3b291759 100644 --- a/src/sneps/network/PropositionNode.java +++ b/src/sneps/network/PropositionNode.java @@ -8,6 +8,7 @@ import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.ReportSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Term; import sneps.network.Node; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -42,33 +43,13 @@ public PropositionNode() { knownInstances = new ReportSet(); newInstances = new ReportSet(); } - + public PropositionNode(Term trm) { super(Semantic.proposition, trm); outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); knownInstances = new ReportSet(); newInstances = new ReportSet(); - setTerm(trm); - } - public PropositionNode(Term trm, Semantic sem) { - this(); - setTerm(trm); - setSemanticType(sem); - } - public PropositionNode(Semantic sym, Term trm){ - super(sym, trm); - outgoingChannels = new ChannelSet(); - incomingChannels = new ChannelSet(); - knownInstances = new ReportSet(); - newInstances = new ReportSet(); - } - public PropositionNode(Semantic sym){ - super(sym); - outgoingChannels = new ChannelSet(); - incomingChannels = new ChannelSet(); - knownInstances = new ReportSet(); - newInstances = new ReportSet(); } public void processSingleChannelReports(Channel currentChannel) { diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 49a58941..4472715f 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -1,4 +1,3 @@ - package sneps.network; import java.io.Serializable; @@ -32,6 +31,7 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; + private NodeSet consequents; /** * a NodeSet containing all the pattern antecedents attached to this Node @@ -45,34 +45,7 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ protected ContextRuisSet contextRuisSet; private Hashtable contextConstantRUI; - public RuleNode(){ - super(); - consequents = new NodeSet(); - antNodesWithoutVars = new NodeSet(); - antNodesWithoutVarsIDs = new HashSet(); - antNodesWithVars = new NodeSet(); - antNodesWithVarsIDs = new HashSet(); - shareVars = false; - sharedVars = new VarNodeSet(); - contextRuisSet = new ContextRuisSet(); - contextConstantRUI = new Hashtable(); - } - - public RuleNode(Semantic sym){ - super(sym); - consequents = new NodeSet(); - antNodesWithoutVars = new NodeSet(); - antNodesWithoutVarsIDs = new HashSet(); - antNodesWithVars = new NodeSet(); - antNodesWithVarsIDs = new HashSet(); - shareVars = false; - sharedVars = new VarNodeSet(); - contextRuisSet = new ContextRuisSet(); - contextConstantRUI = new Hashtable(); - } - - public RuleNode(Term syn) { - super(syn); + public RuleNode() { consequents = new NodeSet(); antNodesWithoutVars = new NodeSet(); antNodesWithoutVarsIDs = new HashSet(); @@ -84,8 +57,8 @@ public RuleNode(Term syn) { contextConstantRUI = new Hashtable(); } - public RuleNode(Semantic sym, Term syn) { - super(sym, syn); + public RuleNode(Molecular syn) { + super(syn); consequents = new NodeSet(); antNodesWithoutVars = new NodeSet(); antNodesWithoutVarsIDs = new HashSet(); diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index eaeb5d3c..7d4f9965 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -11,6 +11,7 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; import sneps.snip.Report; @@ -40,17 +41,9 @@ public class AndEntailment extends RuleNode { * Constructor for the AndEntailment rule node * @param syn */ - public AndEntailment(Term syn) { + public AndEntailment(Molecular syn) { super(syn); } - /** - * Constructor for the AndEntailment rule node - * @param sym - * @param syn - */ - public AndEntailment(Semantic sym, Term syn) { - super(sym, syn); - } /** * Creates the first RuleUseInfo from a given Report and stores it(if positive) diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index c9ff1fd7..46ad70c2 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -14,6 +14,7 @@ import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; import sneps.snebr.Context; @@ -65,19 +66,9 @@ public void setAndOrArgs(int args) { * @param syn */ - public AndOrEntailment(Term syn) { + public AndOrEntailment(Molecular syn) { super(syn); } - - /** - * Constructor for the AndOr Entailment - * @param sym - * @param syn - */ - - public AndOrEntailment(Semantic sym, Term syn) { - super(sym, syn); - } /** * When a report is received, it checks whether it is true or false diff --git a/src/sneps/snip/rules/DoIfNode.java b/src/sneps/snip/rules/DoIfNode.java index f416b3dd..89d1b8b8 100644 --- a/src/sneps/snip/rules/DoIfNode.java +++ b/src/sneps/snip/rules/DoIfNode.java @@ -3,6 +3,7 @@ import sneps.network.RuleNode; import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Term; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleUseInfo; @@ -10,14 +11,10 @@ public class DoIfNode extends RuleNode { private static final long serialVersionUID = -262476672166406490L; - public DoIfNode(Term syn) { + public DoIfNode(Molecular syn) { super(syn); } - public DoIfNode(Semantic sem, Term syn) { - super(sem, syn); - } - @Override protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 71fda669..c607beeb 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -11,6 +11,7 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; import sneps.snip.Report; @@ -37,12 +38,9 @@ public class NumericalEntailment extends RuleNode { private static final long serialVersionUID = 3546852401118194013L; private int i; - public NumericalEntailment(Term syn) { + public NumericalEntailment(Molecular syn) { super(syn); } - public NumericalEntailment(Semantic sym, Term syn) { - super(sym, syn); - } /** * Creates the first RuleUseInfo from a given Report and stores it(if positive) diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index aa2a93c8..6d147580 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -12,6 +12,7 @@ import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Term; import sneps.snebr.Support; import sneps.snip.Report; @@ -30,19 +31,9 @@ public class OrEntailment extends RuleNode { *Constructor for the Or Entailment * @param syn */ - public OrEntailment(Term syn) { + public OrEntailment(Molecular syn) { super(syn); } - - - /** - * Constructor for the Or Entailment - * @param sym - * @param syn - */ - public OrEntailment(Semantic sym, Term syn) { - super(sym, syn); - } /** * Checks if the report received is true diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index e9f6304c..5cb13b90 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -15,6 +15,7 @@ import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; import sneps.snebr.Context; @@ -66,19 +67,9 @@ public void setThreshArgs(int args) { * Constructor for the Thresh Entailment * @param syn */ - public ThreshEntailment(Term syn) { + public ThreshEntailment(Molecular syn) { super(syn); } - - /** - * Constructor for the Thresh Entailment - * @param sym - * @param syn - */ - public ThreshEntailment(Semantic sym, Term syn) { - super(sym, syn); - } - /** * When a report is received, it checks whether it is true or false diff --git a/src/sneps/snip/rules/WhenDoNode.java b/src/sneps/snip/rules/WhenDoNode.java index b7d94dd9..d3646aad 100644 --- a/src/sneps/snip/rules/WhenDoNode.java +++ b/src/sneps/snip/rules/WhenDoNode.java @@ -3,18 +3,15 @@ import sneps.network.RuleNode; import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Term; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleUseInfo; public class WhenDoNode extends RuleNode { private static final long serialVersionUID = 2515697705889848498L; - - public WhenDoNode(Semantic sem, Term syn) { - super(sem, syn); - } - public WhenDoNode(Term syn) { + public WhenDoNode(Molecular syn) { super(syn); } From 7608a34f6074d66a834dd15fedb5b2347f79264f Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Fri, 5 Apr 2019 20:52:23 +0200 Subject: [PATCH 02/22] initial revision of previously implemented SNIP classes --- .../classes/setClasses/ContextRuisSet.java | 9 +++++---- .../classes/setClasses/FlagNodeSet.java | 2 ++ .../classes/setClasses/RuleUseInfoSet.java | 4 ++-- src/sneps/snip/classes/FlagNode.java | 2 +- src/sneps/snip/classes/RuisHandler.java | 2 ++ src/sneps/snip/classes/RuleUseInfo.java | 18 +++++------------- 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/sneps/network/classes/setClasses/ContextRuisSet.java b/src/sneps/network/classes/setClasses/ContextRuisSet.java index 251fb7ef..2c06a7cf 100644 --- a/src/sneps/network/classes/setClasses/ContextRuisSet.java +++ b/src/sneps/network/classes/setClasses/ContextRuisSet.java @@ -51,20 +51,21 @@ public RuisHandler getByContext(String contextName) { public boolean contains(RuisHandler handler){ return ruisHandlers.contains(handler); } + public boolean containsKey(String handlerKey){ return ruisHandlers.containsKey(handlerKey); } - public RuisHandler getHandler(RuisHandler handler){ - return ruisHandlers.get(handler); - } + public Enumeration getKeys(){ return ruisHandlers.keys(); } + public int size(){ return ruisHandlers.size(); } + public void remove(String contextName, RuisHandler handler){ - ruisHandlers.remove(handler, handler); + ruisHandlers.remove(contextName, handler); } public void clear() { diff --git a/src/sneps/network/classes/setClasses/FlagNodeSet.java b/src/sneps/network/classes/setClasses/FlagNodeSet.java index 25dfe2b0..2fd65e14 100644 --- a/src/sneps/network/classes/setClasses/FlagNodeSet.java +++ b/src/sneps/network/classes/setClasses/FlagNodeSet.java @@ -66,9 +66,11 @@ public FlagNodeSet union(FlagNodeSet f) { for (FlagNode fn : flagNodes) { res.insert(fn); } + for (FlagNode fn : f) { res.insert(fn); } + return res; } diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 0b953455..8b8695fd 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -37,8 +37,8 @@ public boolean add(RuleUseInfo rui) { public RuleUseInfoSet combine(RuleUseInfoSet second) { RuleUseInfoSet res = new RuleUseInfoSet(this.context, false); for(RuleUseInfo rui1 : this){ - for(RuleUseInfo rui2: second){ - if(rui1.isDisjoint(rui2)) + for(RuleUseInfo rui2 : second){ + //if(rui1.isDisjoint(rui2)) res.add(rui1.combine(rui2)); } } diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index ebdaefd7..c0833cff 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -15,7 +15,7 @@ public class FlagNode { * node * @param set * support - * @param f + * @param f, * true or false */ public FlagNode(Node n, PropositionSet set, int f) { diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 8ff3cffa..5b9251f9 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -20,6 +20,7 @@ public abstract class RuisHandler { public RuisHandler(){ positiveNodes = new NodeSet(); } + public RuisHandler(String contextID) { this(); this.context = contextID; @@ -30,6 +31,7 @@ public String getContext() { } abstract public RuleUseInfoSet insertRUI(RuleUseInfo rui); + public NodeSet getPositiveNodes() { return positiveNodes; } diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index 28107b29..fdc93ac0 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -53,10 +53,6 @@ public int getNegCount() { */ public FlagNodeSet getNegSubs() { FlagNodeSet res = new FlagNodeSet(); - // for (int i = 0; i < fns.cardinality(); i++) { - // if (fns.getFlagNode(i).getFlag() == 2) - // res.putIn(fns.getFlagNode(i)); - // } for (FlagNode fn : fns) { if (fn.getFlag() == 2) res.insert(fn); @@ -71,10 +67,6 @@ public FlagNodeSet getNegSubs() { */ public FlagNodeSet getPosSubs() { FlagNodeSet res = new FlagNodeSet(); - // for (int i = 0; i < fns.cardinality(); i++) { - // if (fns.getFlagNode(i).getFlag() == 1) - // res.putIn(fns.getFlagNode(i)); - // } for (FlagNode fn : fns) { if (fn.getFlag() == 1) res.insert(fn); @@ -99,11 +91,11 @@ public FlagNodeSet getFlagNodeSet() { * @return true or false */ public boolean isVarsCompatible(RuleUseInfo r) { - return sub.isCompatible(r.sub); + return sub.isCompatible(r.getSubstitutions()); } /** - * Check if this and r are joint + * Check if this flagged node set and r's flagged node set are joint * * @param r * rule use info @@ -131,7 +123,7 @@ public boolean isJoint(RuleUseInfo r) { } /** - * Check if this and r are disjoint + * Check if this flagged node set and r's flagged node set are disjoint * * @param r * rule use info @@ -151,8 +143,8 @@ public boolean isDisjoint(RuleUseInfo r) { public RuleUseInfo combine(RuleUseInfo rui) { //System.out.println(this.isDisjoint(rui) + " " + this.isVarsCompatible(rui)); if (this.isDisjoint(rui) && this.isVarsCompatible(rui)) { - return new RuleUseInfo(this.getSubstitutions().union(rui.getSubstitutions()), this.pos - + rui.pos, this.neg + rui.neg, this.fns.union(rui.fns)); + return new RuleUseInfo(this.getSubstitutions().union(rui.getSubstitutions()), + this.pos + rui.getPosCount(), this.neg + rui.getNegCount(), this.fns.union(rui.getFlagNodeSet())); } return null; } From f174d6a7fbf1afd2e6bf6f4ad2480df299349524 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Thu, 11 Apr 2019 00:59:43 +0200 Subject: [PATCH 03/22] change structure of supports in Report and FlagNode to be a set of Supports --- src/sneps/network/RuleNode.java | 10 ++-- .../classes/setClasses/FlagNodeSet.java | 4 +- src/sneps/snip/Report.java | 10 ++-- src/sneps/snip/classes/FlagNode.java | 10 ++-- src/sneps/snip/classes/RuleUseInfo.java | 46 +++++++++++-------- src/sneps/snip/rules/AndEntailment.java | 31 ++++++++----- src/sneps/snip/rules/AndOrEntailment.java | 23 +++++++--- src/sneps/snip/rules/NumericalEntailment.java | 22 +++++++-- src/sneps/snip/rules/OrEntailment.java | 2 +- src/sneps/snip/rules/ThreshEntailment.java | 21 +++++++-- 10 files changed, 118 insertions(+), 61 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 4472715f..d73a67a1 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -5,20 +5,18 @@ import java.util.Hashtable; import java.util.Set; -import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.ContextRuisSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; -import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.ReportSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; import sneps.network.classes.term.Variable; import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.AntecedentToRuleChannel; import sneps.snip.channels.Channel; @@ -94,13 +92,13 @@ public void applyRuleHandler(Report report, Node signature) { String contextID = report.getContextName(); RuleUseInfo rui; if (report.isPositive()) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns); } else { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 2)); rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns); @@ -206,7 +204,7 @@ public RuisHandler addContextRUIS(String contextName) { si = new SIndex(contextName, sharedVars, getSIndexContextType()); return this.addContextRUIS(si); } else { - return this.addContextRUIS(contextName,createRuisHandler(contextName)); + return this.addContextRUIS(contextName, createRuisHandler(contextName)); } } diff --git a/src/sneps/network/classes/setClasses/FlagNodeSet.java b/src/sneps/network/classes/setClasses/FlagNodeSet.java index 2fd65e14..61116f03 100644 --- a/src/sneps/network/classes/setClasses/FlagNodeSet.java +++ b/src/sneps/network/classes/setClasses/FlagNodeSet.java @@ -40,8 +40,7 @@ public boolean isNew() { * FlagNode */ public void insert(FlagNode fn) { - if (!this.contains(fn)) - flagNodes.add(fn); + flagNodes.add(fn); } /** @@ -77,6 +76,7 @@ public FlagNodeSet union(FlagNodeSet f) { public int size() { return flagNodes.size(); } + /** * Check if fn is in this * diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index c0164d10..ede6af62 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -1,6 +1,8 @@ package sneps.snip; -import sneps.network.classes.setClasses.PropositionSet; +import java.util.Set; + +import sneps.snebr.Support; import sneps.snip.matching.Substitutions; /** @@ -12,11 +14,11 @@ */ public class Report { private Substitutions substitution; - private PropositionSet supports; + private Set supports; private boolean sign; private String contextName; - public Report(Substitutions substitution, PropositionSet set, boolean sign, String contextID) { + public Report(Substitutions substitution, Set set, boolean sign, String contextID) { this.substitution = substitution; this.supports = set; this.sign = sign; @@ -27,7 +29,7 @@ public Substitutions getSubstitutions() { return substitution; } - public PropositionSet getSupports() { + public Set getSupports() { return supports; } diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index c0833cff..51ec9de4 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -1,11 +1,13 @@ package sneps.snip.classes; +import java.util.Set; + import sneps.network.Node; -import sneps.network.classes.setClasses.PropositionSet; +import sneps.snebr.Support; public class FlagNode { private Node node; - private PropositionSet supports; + private Set supports; private int flag; /** @@ -18,7 +20,7 @@ public class FlagNode { * @param f, * true or false */ - public FlagNode(Node n, PropositionSet set, int f) { + public FlagNode(Node n, Set set, int f) { node = n; supports = set; flag = f; @@ -38,7 +40,7 @@ public Node getNode() { * * @return support */ - public PropositionSet getSupports() { + public Set getSupports() { return supports; } diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index fdc93ac0..b6ff53a2 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -1,9 +1,11 @@ package sneps.snip.classes; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.network.classes.setClasses.PropositionSet; +import sneps.snebr.Support; import sneps.snip.matching.Substitutions; /** @@ -82,6 +84,15 @@ public FlagNodeSet getPosSubs() { public FlagNodeSet getFlagNodeSet() { return fns; } + + /** + * Return the substitutions list + * + * @return Substitutions + */ + public Substitutions getSubstitutions() { + return sub; + } /** * Check if this and r have no binding conflicts @@ -149,30 +160,29 @@ public RuleUseInfo combine(RuleUseInfo rui) { return null; } - public PropositionSet getSupports() { + public Set getCombinedSupports() { if (fns.isNew()) - return new PropositionSet(); + return new HashSet(); + if (fns.cardinality() == 1) return fns.iterator().next().getSupports(); - PropositionSet res = new PropositionSet(); - for(FlagNode fn : fns){ + Iterator fnIter = fns.iterator(); + Set res = fnIter.next().getSupports(); + while (fnIter.hasNext()) { + Set toBeCombined = fnIter.next().getSupports(); + res = Support.combine(res, toBeCombined); + } + + /*for(FlagNode fn : fns){ try { res = res.union(fn.getSupports()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { } - } + }*/ + return res; } - - /** - * Return the substitutions list - * - * @return Substitutions - */ - public Substitutions getSubstitutions() { - return sub; - } -} +} \ No newline at end of file diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index 7d4f9965..2b8c5a4c 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -1,19 +1,18 @@ package sneps.snip.rules; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; +import java.util.Iterator; +import java.util.Set; + import sneps.network.Node; import sneps.network.RuleNode; import sneps.network.VariableNode; -import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; -import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; @@ -55,7 +54,7 @@ public AndEntailment(Molecular syn) { public void applyRuleHandler(Report report, Node signature) { String contxt = report.getContextName(); if (report.isPositive()) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), @@ -78,20 +77,30 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { Substitutions sub = Rui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(Rui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); + //PropositionSet supports = new PropositionSet(); - for(FlagNode fn : justification){ + /*for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) {} + }*/ + + Iterator fnIter = justification.iterator(); + Set supports = fnIter.next().getSupports(); + while (fnIter.hasNext()) { + Set toBeCombined = fnIter.next().getSupports(); + supports = Support.combine(supports, toBeCombined); } - try { + /*try { supports = supports.union(Rui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - + | NodeNotFoundInNetworkException e) {}*/ + + Set RuiSupport = Rui.getCombinedSupports(); + supports = Support.combine(supports, RuiSupport); + if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound VarNodeSet freeVars = ((Open)this.getTerm()).getFreeVariables(); diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index 46ad70c2..d199db34 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -1,6 +1,7 @@ package sneps.snip.rules; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -87,7 +88,7 @@ public void applyRuleHandler(Report report, Node signature) { int rem = args-(pos+neg); if(remrem) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -98,7 +99,7 @@ public void applyRuleHandler(Report report, Node signature) { if(pos+neg==args) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -140,19 +141,29 @@ protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { Substitutions sub = tRui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(tRui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); + //PropositionSet supports = new PropositionSet(); - for(FlagNode fn : justification){ + /*for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) {} + }*/ + + Iterator fnIter = justification.iterator(); + Set supports = fnIter.next().getSupports(); + while (fnIter.hasNext()) { + Set toBeCombined = fnIter.next().getSupports(); + supports = Support.combine(supports, toBeCombined); } - try { + /*try { supports = supports.union(tRui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} + | NodeNotFoundInNetworkException e) {}*/ + + Set tRuiSupport = tRui.getCombinedSupports(); + supports = Support.combine(supports, tRuiSupport); if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index c607beeb..e731c323 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -1,5 +1,8 @@ package sneps.snip.rules; +import java.util.Iterator; +import java.util.Set; + import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; @@ -14,6 +17,7 @@ import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; @@ -52,7 +56,7 @@ public NumericalEntailment(Molecular syn) { public void applyRuleHandler(Report report, Node signature) { String contxt = report.getContextName(); if (report.isPositive()) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), @@ -76,9 +80,9 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { Substitutions sub = Rui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(Rui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); + //PropositionSet supports = new PropositionSet(); - for(FlagNode fn : justification){ + /*for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException @@ -88,7 +92,17 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { try { supports = supports.union(Rui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} + | NodeNotFoundInNetworkException e) {}*/ + + Iterator fnIter = justification.iterator(); + Set supports = fnIter.next().getSupports(); + while (fnIter.hasNext()) { + Set toBeCombined = fnIter.next().getSupports(); + supports = Support.combine(supports, toBeCombined); + } + + Set RuiSupport = Rui.getCombinedSupports(); + supports = Support.combine(supports, RuiSupport); if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 6d147580..053d68f1 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -45,7 +45,7 @@ public void applyRuleHandler(Report report, Node node) { sign = true; - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(node, propSet, 1)); diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index 5cb13b90..d15c23bc 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -1,6 +1,7 @@ package sneps.snip.rules; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -93,7 +94,7 @@ public void applyRuleHandler(Report report, Node signature) { int rem = args-(pos+neg); if(pos>min && posrem) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -104,7 +105,7 @@ public void applyRuleHandler(Report report, Node signature) { if(neg+pos==args) { - PropositionSet propSet = report.getSupports(); + Set propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -145,9 +146,9 @@ else if (tRui.getPosCount()>= min && tRui.getPosCount() <= max) Substitutions sub = tRui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(tRui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); + //PropositionSet supports = new PropositionSet(); - for(FlagNode fn : justification){ + /*for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException @@ -157,7 +158,17 @@ else if (tRui.getPosCount()>= min && tRui.getPosCount() <= max) try { supports = supports.union(tRui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} + | NodeNotFoundInNetworkException e) {}*/ + + Iterator fnIter = justification.iterator(); + Set supports = fnIter.next().getSupports(); + while (fnIter.hasNext()) { + Set toBeCombined = fnIter.next().getSupports(); + supports = Support.combine(supports, toBeCombined); + } + + Set tRuiSupport = tRui.getCombinedSupports(); + supports = Support.combine(supports, tRuiSupport); if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound From b2db718b406e4b2e16ae85a4fadea8de7c36fd44 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Thu, 18 Apr 2019 03:12:46 +0200 Subject: [PATCH 04/22] change supports in Report and FlagNode back to a PropSet --- src/sneps/network/RuleNode.java | 6 ++-- src/sneps/snip/Report.java | 10 +++--- src/sneps/snip/classes/FlagNode.java | 10 +++--- src/sneps/snip/classes/RuisHandler.java | 1 + src/sneps/snip/classes/RuleUseInfo.java | 32 +++++++++---------- src/sneps/snip/rules/AndEntailment.java | 26 +++++---------- src/sneps/snip/rules/AndOrEntailment.java | 29 ++++------------- src/sneps/snip/rules/NumericalEntailment.java | 24 +++----------- src/sneps/snip/rules/OrEntailment.java | 11 +------ src/sneps/snip/rules/ThreshEntailment.java | 28 +++------------- 10 files changed, 51 insertions(+), 126 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index d73a67a1..430a8a7f 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -8,6 +8,7 @@ import sneps.network.classes.setClasses.ContextRuisSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.ReportSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; @@ -16,7 +17,6 @@ import sneps.network.classes.term.Variable; import sneps.snebr.Context; import sneps.snebr.Controller; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.AntecedentToRuleChannel; import sneps.snip.channels.Channel; @@ -92,13 +92,13 @@ public void applyRuleHandler(Report report, Node signature) { String contextID = report.getContextName(); RuleUseInfo rui; if (report.isPositive()) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns); } else { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 2)); rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns); diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index ede6af62..c0164d10 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -1,8 +1,6 @@ package sneps.snip; -import java.util.Set; - -import sneps.snebr.Support; +import sneps.network.classes.setClasses.PropositionSet; import sneps.snip.matching.Substitutions; /** @@ -14,11 +12,11 @@ */ public class Report { private Substitutions substitution; - private Set supports; + private PropositionSet supports; private boolean sign; private String contextName; - public Report(Substitutions substitution, Set set, boolean sign, String contextID) { + public Report(Substitutions substitution, PropositionSet set, boolean sign, String contextID) { this.substitution = substitution; this.supports = set; this.sign = sign; @@ -29,7 +27,7 @@ public Substitutions getSubstitutions() { return substitution; } - public Set getSupports() { + public PropositionSet getSupports() { return supports; } diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index 51ec9de4..c0833cff 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -1,13 +1,11 @@ package sneps.snip.classes; -import java.util.Set; - import sneps.network.Node; -import sneps.snebr.Support; +import sneps.network.classes.setClasses.PropositionSet; public class FlagNode { private Node node; - private Set supports; + private PropositionSet supports; private int flag; /** @@ -20,7 +18,7 @@ public class FlagNode { * @param f, * true or false */ - public FlagNode(Node n, Set set, int f) { + public FlagNode(Node n, PropositionSet set, int f) { node = n; supports = set; flag = f; @@ -40,7 +38,7 @@ public Node getNode() { * * @return support */ - public Set getSupports() { + public PropositionSet getSupports() { return supports; } diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 5b9251f9..69c62472 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -35,6 +35,7 @@ public String getContext() { public NodeSet getPositiveNodes() { return positiveNodes; } + public void setPositiveNodes(NodeSet positiveNodes) { this.positiveNodes = positiveNodes; } diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index b6ff53a2..1df110d9 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -1,11 +1,9 @@ package sneps.snip.classes; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NotAPropositionNodeException; import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.snebr.Support; +import sneps.network.classes.setClasses.PropositionSet; import sneps.snip.matching.Substitutions; /** @@ -155,32 +153,32 @@ public RuleUseInfo combine(RuleUseInfo rui) { //System.out.println(this.isDisjoint(rui) + " " + this.isVarsCompatible(rui)); if (this.isDisjoint(rui) && this.isVarsCompatible(rui)) { return new RuleUseInfo(this.getSubstitutions().union(rui.getSubstitutions()), - this.pos + rui.getPosCount(), this.neg + rui.getNegCount(), this.fns.union(rui.getFlagNodeSet())); + this.pos + rui.getPosCount(), this.neg + rui.getNegCount(), + this.fns.union(rui.getFlagNodeSet())); } return null; } + + /** + * Gets the union of supports of each flag node in this RUI's fns + * @return PropositionSet + */ - public Set getCombinedSupports() { + public PropositionSet getSupports() { if (fns.isNew()) - return new HashSet(); + return new PropositionSet(); if (fns.cardinality() == 1) return fns.iterator().next().getSupports(); - Iterator fnIter = fns.iterator(); - Set res = fnIter.next().getSupports(); - while (fnIter.hasNext()) { - Set toBeCombined = fnIter.next().getSupports(); - res = Support.combine(res, toBeCombined); - } - - /*for(FlagNode fn : fns){ + PropositionSet res = new PropositionSet(); + for(FlagNode fn : fns){ try { res = res.union(fn.getSupports()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { } - }*/ + } return res; } diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index 2b8c5a4c..7f9cb87b 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -1,18 +1,18 @@ package sneps.snip.rules; -import java.util.Iterator; -import java.util.Set; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; import sneps.network.VariableNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; @@ -54,7 +54,7 @@ public AndEntailment(Molecular syn) { public void applyRuleHandler(Report report, Node signature) { String contxt = report.getContextName(); if (report.isPositive()) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), @@ -77,29 +77,19 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { Substitutions sub = Rui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(Rui.getFlagNodeSet()); - //PropositionSet supports = new PropositionSet(); + PropositionSet supports = new PropositionSet(); - /*for(FlagNode fn : justification){ + for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) {} - }*/ - - Iterator fnIter = justification.iterator(); - Set supports = fnIter.next().getSupports(); - while (fnIter.hasNext()) { - Set toBeCombined = fnIter.next().getSupports(); - supports = Support.combine(supports, toBeCombined); } - /*try { + try { supports = supports.union(Rui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {}*/ - - Set RuiSupport = Rui.getCombinedSupports(); - supports = Support.combine(supports, RuiSupport); + | NodeNotFoundInNetworkException e) {} if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index d199db34..bde01162 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -1,26 +1,19 @@ package sneps.snip.rules; import java.util.HashSet; -import java.util.Iterator; import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; -import sneps.network.PropositionNode; import sneps.network.RuleNode; import sneps.network.VariableNode; -import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; -import sneps.snebr.Context; -import sneps.snebr.Controller; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.Channel; import sneps.snip.classes.RuleUseInfo; @@ -88,7 +81,7 @@ public void applyRuleHandler(Report report, Node signature) { int rem = args-(pos+neg); if(remrem) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -99,7 +92,7 @@ public void applyRuleHandler(Report report, Node signature) { if(pos+neg==args) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -141,29 +134,19 @@ protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { Substitutions sub = tRui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(tRui.getFlagNodeSet()); - //PropositionSet supports = new PropositionSet(); + PropositionSet supports = new PropositionSet(); - /*for(FlagNode fn : justification){ + for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) {} - }*/ - - Iterator fnIter = justification.iterator(); - Set supports = fnIter.next().getSupports(); - while (fnIter.hasNext()) { - Set toBeCombined = fnIter.next().getSupports(); - supports = Support.combine(supports, toBeCombined); } - /*try { + try { supports = supports.union(tRui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {}*/ - - Set tRuiSupport = tRui.getCombinedSupports(); - supports = Support.combine(supports, tRuiSupport); + | NodeNotFoundInNetworkException e) {} if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index e731c323..4677b200 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -1,14 +1,10 @@ package sneps.snip.rules; -import java.util.Iterator; -import java.util.Set; - import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; import sneps.network.VariableNode; -import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; @@ -16,8 +12,6 @@ import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; @@ -56,7 +50,7 @@ public NumericalEntailment(Molecular syn) { public void applyRuleHandler(Report report, Node signature) { String contxt = report.getContextName(); if (report.isPositive()) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), @@ -80,9 +74,9 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { Substitutions sub = Rui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(Rui.getFlagNodeSet()); - //PropositionSet supports = new PropositionSet(); + PropositionSet supports = new PropositionSet(); - /*for(FlagNode fn : justification){ + for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException @@ -92,17 +86,7 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { try { supports = supports.union(Rui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {}*/ - - Iterator fnIter = justification.iterator(); - Set supports = fnIter.next().getSupports(); - while (fnIter.hasNext()) { - Set toBeCombined = fnIter.next().getSupports(); - supports = Support.combine(supports, toBeCombined); - } - - Set RuiSupport = Rui.getCombinedSupports(); - supports = Support.combine(supports, RuiSupport); + | NodeNotFoundInNetworkException e) {} if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 053d68f1..f79acaa7 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -1,20 +1,11 @@ package sneps.snip.rules; -import java.util.HashSet; -import java.util.Set; - -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; -import sneps.network.classes.Semantic; -import sneps.network.PropositionNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Term; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.Channel; import sneps.snip.classes.FlagNode; @@ -45,7 +36,7 @@ public void applyRuleHandler(Report report, Node node) { sign = true; - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(node, propSet, 1)); diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index d15c23bc..2e2f8203 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -1,27 +1,19 @@ package sneps.snip.rules; import java.util.HashSet; -import java.util.Iterator; import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; -import sneps.network.PropositionNode; import sneps.network.RuleNode; import sneps.network.VariableNode; -import sneps.network.classes.Semantic; -import sneps.network.PropositionNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; -import sneps.snebr.Context; -import sneps.snebr.Controller; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.Channel; import sneps.snip.classes.RuleUseInfo; @@ -94,7 +86,7 @@ public void applyRuleHandler(Report report, Node signature) { int rem = args-(pos+neg); if(pos>min && posrem) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -105,7 +97,7 @@ public void applyRuleHandler(Report report, Node signature) { if(neg+pos==args) { - Set propSet = report.getSupports(); + PropositionSet propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), @@ -146,9 +138,9 @@ else if (tRui.getPosCount()>= min && tRui.getPosCount() <= max) Substitutions sub = tRui.getSubstitutions(); FlagNodeSet justification = new FlagNodeSet(); justification.addAll(tRui.getFlagNodeSet()); - //PropositionSet supports = new PropositionSet(); + PropositionSet supports = new PropositionSet(); - /*for(FlagNode fn : justification){ + for(FlagNode fn : justification){ try { supports = supports.union(fn.getSupports()); } catch (NotAPropositionNodeException @@ -158,17 +150,7 @@ else if (tRui.getPosCount()>= min && tRui.getPosCount() <= max) try { supports = supports.union(tRui.getSupports()); } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {}*/ - - Iterator fnIter = justification.iterator(); - Set supports = fnIter.next().getSupports(); - while (fnIter.hasNext()) { - Set toBeCombined = fnIter.next().getSupports(); - supports = Support.combine(supports, toBeCombined); - } - - Set tRuiSupport = tRui.getCombinedSupports(); - supports = Support.combine(supports, tRuiSupport); + | NodeNotFoundInNetworkException e) {} if(this.getTerm() instanceof Open){ //knownInstances check this.free vars - > bound From 4a7590cf965b50979dcca2340dd270e33c1acd0c Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sun, 28 Apr 2019 20:30:29 +0200 Subject: [PATCH 05/22] Initial revision --- .classpath | 3 +- src/sneps/network/RuleNode.java | 220 ++++++++++++++---- src/sneps/network/VariableNode.java | 24 +- src/sneps/snip/classes/RuisHandler.java | 7 + src/sneps/snip/classes/SIndex.java | 65 +++--- src/sneps/snip/rules/AndEntailment.java | 2 +- src/sneps/snip/rules/AndOrEntailment.java | 2 +- src/sneps/snip/rules/NumericalEntailment.java | 9 +- src/sneps/snip/rules/ThreshEntailment.java | 2 +- 9 files changed, 245 insertions(+), 89 deletions(-) diff --git a/.classpath b/.classpath index dfc913d2..96023dca 100644 --- a/.classpath +++ b/.classpath @@ -1,10 +1,11 @@ + - \ No newline at end of file + diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 430a8a7f..160018a2 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -12,6 +12,8 @@ import sneps.network.classes.setClasses.ReportSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Base; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; @@ -31,16 +33,36 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; private NodeSet consequents; + /** - * a NodeSet containing all the pattern antecedents attached to this Node + * A NodeSet containing all the pattern antecedents attached to this RuleNode. */ protected NodeSet antNodesWithVars; protected NodeSet antNodesWithoutVars; protected Set antNodesWithVarsIDs; protected Set antNodesWithoutVarsIDs; + + /** + * Set to true if all the antecedents with variables share the same + * variables, false otherwise. + */ protected boolean shareVars; + + /** + * A VarNodeSet of the common free VariableNodes shared between the antecedents. + */ protected VarNodeSet sharedVars; + + /** + * A ContextRuisSet that is used to map each context to its appropriate + * RuiHandler for this RuleNode. + */ protected ContextRuisSet contextRuisSet; + + /** + * A Hashtable used to map each context to a single RuleUseInfo + * that contains all the instances that do not dominate variables. + */ private Hashtable contextConstantRUI; public RuleNode() { @@ -67,6 +89,23 @@ public RuleNode(Molecular syn) { contextRuisSet = new ContextRuisSet(); contextConstantRUI = new Hashtable(); } + + + public NodeSet getConsequents() { + return consequents; + } + + public ContextRuisSet getContextRuisSet() { + return contextRuisSet; + } + + public int getAntSize(){ + return antNodesWithoutVars.size() + antNodesWithVars.size(); + } + + protected NodeSet getPatternNodes() { + return antNodesWithVars; + } protected void sendReportToConsequents(Report reply) { if(!knownInstances.contains(reply)) @@ -76,6 +115,11 @@ protected void sendReportToConsequents(Report reply) { outChannel.addReport(reply); } + /** + * Process antecedent nodes, used for initialization. + * + * @param antNodes + */ protected void processNodes(NodeSet antNodes) { this.splitToNodesWithVarsAndWithout(antNodes, antNodesWithVars, antNodesWithoutVars); for (Node n : antNodesWithVars) { @@ -88,29 +132,47 @@ protected void processNodes(NodeSet antNodes) { sharedVars = getSharedVarsNodes(antNodesWithVars); } + /** + * The main method that does all the inference process in the RuleNode. Creates + * a RUI for the given report, and inserts it into the appropriate RuisHandler + * associated with the report's context, for this RuleNode. It instantiates a + * RuisHandler if this is the first report received in a particular context. It + * then applies the inference rules of this RuleNode on the current stored RUIs. + * + * @param report + * @param signature + * The instance that is being reported by the report. + */ public void applyRuleHandler(Report report, Node signature) { String contextID = report.getContextName(); RuleUseInfo rui; + PropositionSet propSet = report.getSupports(); + FlagNodeSet fns = new FlagNodeSet(); + if (report.isPositive()) { - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns); } else { - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 2)); rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns); } - RuisHandler crtemp = contextRuisSet.getByContext(contextID); + + RuisHandler crtemp = contextRuisSet.getByContext(contextID); + + //This is the first report received by this RuleNode in the context given + //by contextID, so a RuisHandler is created, and added to this RuleNode's + //contextRuisSet if(crtemp == null){ - crtemp = addContextRUIS(contextID); + crtemp = addContextRuiHandler(contextID); } + //The RUI created for the given report is inserted to the corresponding + //RuisHandler for the report's context given by contextID RuleUseInfoSet res = crtemp.insertRUI(rui); if (res == null) res = new RuleUseInfoSet(); + for (RuleUseInfo tRui : res) { applyRuleOnRui(tRui, contextID); } @@ -118,28 +180,34 @@ public void applyRuleHandler(Report report, Node signature) { abstract protected void applyRuleOnRui(RuleUseInfo tRui, String contextID); + /** + * Clears all the information saved by this RuleNode about the instances received. + */ public void clear() { contextRuisSet.clear(); contextConstantRUI.clear(); } - + + /** + * Returns true if all the nodes in the given NodeSet share the same set of + * VariableNodes, and false otherwise. + * + * @param nodes + * @return boolean + */ public boolean allShareVars(NodeSet nodes) { if (nodes.isEmpty()) return false; + //return true; VariableNode n = (VariableNode) nodes.getNode(0); - boolean res = true; for (int i = 1; i < nodes.size(); i++) { if (!n.hasSameFreeVariablesAs((VariableNode) nodes.getNode(i))) { - res = false; - break; + return false; } } - return res; - } - - public NodeSet getConsequents() { - return consequents; + + return true; } public void addAntecedent(Node ant){ @@ -148,14 +216,18 @@ public void addAntecedent(Node ant){ else antNodesWithoutVars.addNode(ant); } - - public ContextRuisSet getContextRuisSet() { - return contextRuisSet; - } - + + /** + * Returns a VarNodeSet of VariableNodes that are shared among some/all the Nodes + * in the given NodeSet. + * + * @param nodes + * @return VarNodeSet + */ public VarNodeSet getSharedVarsNodes(NodeSet nodes) { VarNodeSet res = new VarNodeSet(); VarNodeSet temp = new VarNodeSet(); + if (nodes.isEmpty()) return res; @@ -176,65 +248,127 @@ public VarNodeSet getSharedVarsNodes(NodeSet nodes) { temp.addVarNode(var); } } + return res; } + + /** + * Returns true if the given Node is a constant node i.e. does not have + * free variables. + * + * @param n + * @return boolean + */ + public static boolean isConstantNode(Node n) { + return !(n.getTerm() instanceof Molecular) || (n.getTerm() instanceof Variable); + } + + /*public static boolean isConstantNode(Node n) { + return !((n instanceof VariableNode) || + ((n.getTerm() instanceof Open) && !(((Open) (n.getTerm())).getFreeVariables().isEmpty()))); + }*/ + + //Or call addAntecedent() + /** + * Returns a NodeSet of the nodes that are being pointed at, by this RuleNode, + * with the DownCables that have the same relation name as the given String. + * + * @param name + * Relation name. + * @return NodeSet + */ public NodeSet getDownNodeSet(String name) { if(term != null && term instanceof Molecular) return ((Molecular)term).getDownCableSet().getDownCable(name).getNodeSet(); return null; } + /** + * Returns a NodeSet of the antecedents down cable set, and in case of AndOr or + * Thresh, returns the arguments down cable set. + * + * @return NodeSet + */ public abstract NodeSet getDownAntNodeSet(); public NodeSet getUpNodeSet(String name) { return this.getUpCableSet().getUpCable(name).getNodeSet(); } + /** + * Returns the RuisHandler associated with the given context. + * + * @param cntxt + * @return RuisHandler + */ public RuisHandler getContextRuiHandler(String cntxt) { return contextRuisSet.getByContext(cntxt); } - public RuisHandler addContextRUIS(String contextName) { + /** + * Creates an appropriate RuisHandler for a given context for this RuleNode, + * according to whether all, some or none of the variables in the antecedents are + * shared. Then adds the RuisHandler to this contextRuisSet. + * + * @param contextName + * @return RuisHandler + */ + public RuisHandler addContextRuiHandler(String contextName) { if (sharedVars.size() != 0) { SIndex si = null; + //Antecedents with variables share the same set of variables if (shareVars) - si = new SIndex(contextName, sharedVars, SIndex.SINGLETONRUIS); + si = new SIndex(contextName, sharedVars, SIndex.SINGLETON); + //Some variables are shared else si = new SIndex(contextName, sharedVars, getSIndexContextType()); - return this.addContextRUIS(si); + //return this.addContextRUIS(si); + return this.addContextRuiHandler(contextName, si); } else { - return this.addContextRUIS(contextName, createRuisHandler(contextName)); + //PTree in case of and-entailment + //RUISet otherwise + return this.addContextRuiHandler(contextName, createRuisHandler(contextName)); } } - - private RuleUseInfoSet addContextRUIS(SIndex si) { - // TODO Auto-generated method stub - return null; - } - - public int getAntSize(){ - return antNodesWithoutVars.size() + antNodesWithVars.size(); - } - - public RuisHandler addContextRUIS(String cntxt, RuisHandler cRuis) { + + /** + * Adds the given RuisHandler by the given context name to this contextRuisSet. + * + * @param cntxt + * Context name. + * @param cRuis + * RuisHandler to be added. + * @return RuisHandler + */ + public RuisHandler addContextRuiHandler(String cntxt, RuisHandler cRuis) { return this.contextRuisSet.addHandlerSet(cntxt, cRuis); } protected abstract RuisHandler createRuisHandler(String contextName); - protected RuleUseInfoSet createContextRUISNonShared(String contextName) { + /** + * Returns a RUISet to be used in case all the antecedents do not share a common + * variable, and this RuleNode is not an AndEntailment. + * + * @param contextName + * @return RuleUseInfoSet + */ + protected RuleUseInfoSet createContextRuiHandlerNonShared(String contextName) { return new RuleUseInfoSet(contextName, false); } + /** + * Returns a byte that represents an appropriate SIndex type that is used in case + * the antecedents share some but not all variables.

+ * SIndex.PTree: in AndEntailment
+ * SIndex.RUIS: in other rule nodes + * @return byte + */ protected byte getSIndexContextType() { return SIndex.RUIS; } - protected NodeSet getPatternNodes() { - return antNodesWithVars; - } - public void splitToNodesWithVarsAndWithout(NodeSet allNodes, NodeSet withVars, NodeSet WithoutVars) { for (int i = 0; i < allNodes.size(); i++) { Node n = allNodes.getNode(i); @@ -268,10 +402,6 @@ public RuleUseInfo getConstantRUI(String context) { return contextConstantRUI.get(context); } - public static boolean isConstantNode(Node n) { - return !(n.getTerm() instanceof Molecular) || (n.getTerm() instanceof Variable); - } - @Override public void processRequests() { for (Channel currentChannel : outgoingChannels) { diff --git a/src/sneps/network/VariableNode.java b/src/sneps/network/VariableNode.java index fb8475e5..c5a643f0 100644 --- a/src/sneps/network/VariableNode.java +++ b/src/sneps/network/VariableNode.java @@ -9,25 +9,28 @@ public class VariableNode extends Node implements Serializable{ private VariableSet freeVariables; - private boolean snepslogFlag; public VariableNode() { + freeVariables = new VariableSet(); snepslogFlag = false; } public VariableNode(Term trm) { super(trm); + freeVariables = new VariableSet(); snepslogFlag = false; } public VariableNode(Semantic sem) { super(sem); + freeVariables = new VariableSet(); snepslogFlag = false; } public VariableNode(Semantic sem, Term trm) { super(sem, trm); + freeVariables = new VariableSet(); snepslogFlag = false; } @@ -54,4 +57,23 @@ public void setSnepslogFlag(boolean snepslogFlag) { public VariableSet getFreeVariables() { return freeVariables; } + + /*public static void main(String[] args) { + VariableNode vn1 = new VariableNode(); + VariableNode vn2 = new VariableNode(); + Variable x = new Variable("x"); + Variable y = new Variable("y"); + Variable z = new Variable("z"); + + vn1.getFreeVariables().addVariable(x); + vn1.getFreeVariables().addVariable(y); + vn1.getFreeVariables().addVariable(z); + + vn2.getFreeVariables().addVariable(y); + vn2.getFreeVariables().addVariable(z); + vn2.getFreeVariables().addVariable(x); + + boolean res = vn1.hasSameFreeVariablesAs(vn2); + System.out.print(res); + }*/ } diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 69c62472..a555816f 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -30,6 +30,13 @@ public String getContext() { return context; } + /** + *Inserts the given RuleUseInfo into this RuisHandler and returns the RuleUseInfoSet + *resulted from combining it with the RuleUseInfos in this RuisHandler + * @param rui + * RuleUseInfo + * @return RuleUseInfoSet + */ abstract public RuleUseInfoSet insertRUI(RuleUseInfo rui); public NodeSet getPositiveNodes() { diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index 1a14805f..95e029ba 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -1,7 +1,6 @@ package sneps.snip.classes; import java.util.Hashtable; -import java.util.Set; import sneps.network.VariableNode; import sneps.network.classes.setClasses.NodeSet; @@ -9,30 +8,34 @@ import sneps.network.classes.setClasses.VarNodeSet; import sneps.snip.classes.PTree; import sneps.snip.classes.RuisHandler; -import sneps.snip.matching.Substitutions; public class SIndex extends RuisHandler { - - - private byte ruiType; - public static final byte RUIS = 0, SINGLETONRUIS = 1, PTREE = 2; + + /** + * A Hashtable used to map different substitutions to different RuisHandlers. + */ + private Hashtable map; + private byte ruiHandlerType; + public static final byte RUIS = 0, SINGLETON = 1, PTREE = 2; private VarNodeSet sharedVars; private NodeSet nodesWithVars; /** - * * @param context - * context Name - * @param SharedVars - * set - * @param ruisType - * byte + * Context name. + * @param ruiHandlerType + * The type of RuisHandler used by a rule to be mapped to in this SIndex. + * @param sharedVars + * Set of shared variables whose bindings are used to hash on in this SIndex. + * @param nodesWithVars */ - - public SIndex(String context, VarNodeSet SharedVars, byte ruisType) { + public SIndex(String context, byte ruiHandlerType, VarNodeSet sharedVars, + NodeSet nodesWithVars) { super(context); - this.sharedVars=SharedVars; - this.ruiType=ruisType; + map = new Hashtable(); + this.ruiHandlerType = ruiHandlerType; + this.sharedVars = sharedVars; + this.nodesWithVars = nodesWithVars; } /** @@ -42,13 +45,8 @@ public SIndex(String context, VarNodeSet SharedVars, byte ruisType) { * * @param rui * Rule Use Info - * - * - * */ - - public RuleUseInfoSet insertRUI(RuleUseInfo rui) { - + public RuleUseInfoSet insertRUI(RuleUseInfo rui) { int[] vars = new int[sharedVars.size()]; int index = 0; for (VariableNode varId : sharedVars) { @@ -57,18 +55,15 @@ public RuleUseInfoSet insertRUI(RuleUseInfo rui) { } int x = getIndex(vars); - - RuisHandler trui= SIndexHelper.map.get(x); + RuisHandler trui= map.get(x); if (trui == null) { - trui = getNewRUIS(); - SIndexHelper.map.put(x, trui); - + trui = getNewRUIType(); + map.put(x, trui); } RuleUseInfoSet res = trui.insertRUI(rui); return res; - } private int getIndex(int[] x) { @@ -83,22 +78,20 @@ private int getIndex(int[] x) { hash ^= hash >> 17; hash += hash << 5; return hash; - } - + } /** - * create a new rule use info based on its type + * Creates a new RuisHandler based on the type of SIndex needed. * */ - - private RuisHandler getNewRUIS() { + private RuisHandler getNewRUIType() { RuisHandler tempRui = null; switch (ruiType) { case PTREE: tempRui = new PTree(); ((PTree) tempRui).buildTree(nodesWithVars); break; - case SINGLETONRUIS: + case SINGLETON: tempRui = new RuleUseInfoSet(getContext() , true); break; case RUIS: @@ -116,9 +109,9 @@ private RuisHandler getNewRUIS() { * @return int */ - /*public int getSize() { + public int getSize() { return map.size(); - }*/ + } } \ No newline at end of file diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index 7f9cb87b..ee4570d0 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -149,7 +149,7 @@ public RuisHandler createRuisHandler(String context) { NodeSet ants = antNodesWithoutVars; ants.addAll(antNodesWithVars); tree.buildTree(ants); - this.addContextRUIS(context, tree); + this.addContextRuiHandler(context, tree); return tree; } diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index bde01162..499dc487 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -207,7 +207,7 @@ public NodeSet getDownAntNodeSet() { */ protected RuisHandler createRuisHandler(String contextName) { SIndex index = new SIndex(contextName, getSharedVarsNodes(antNodesWithVars), (byte) 0); - return this.addContextRUIS(contextName, index); + return this.addContextRuiHandler(contextName, index); } /** diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 4677b200..35b965f1 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -41,7 +41,7 @@ public NumericalEntailment(Molecular syn) { } /** - * Creates the first RuleUseInfo from a given Report and stores it(if positive) + * Creates the first RuleUseInfo from a given Report and stores it (if positive) * Also checks if current number of positive Reports satisfy rule * @param report * @param signature @@ -59,6 +59,7 @@ public void applyRuleHandler(Report report, Node signature) { } int curPos = contextRuisSet.getByContext(contxt).getPositiveNodes().size(); int n = antNodesWithoutVars.size()+antNodesWithVars.size(); + //The second cond will be removed if ((curPos >= i) && ((curPos < n-i+1) || (curPos < n-1) ) ) sendSavedRUIs(report.getContextName()); } @@ -143,7 +144,8 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { */ public void addNotSentRui(RuleUseInfo rui, String contxt, Node signature){ SIndex set = (SIndex) contextRuisSet.getByContext(contxt); - if (set == null) + if (set == null) + //Add check for vars set = new SIndex(contxt, getSharedVarsNodes(antNodesWithVars), (byte) 0); set.insertRUI(rui); if(!set.getPositiveNodes().contains(signature)) @@ -162,6 +164,7 @@ private void sendSavedRUIs(String contextID) { if (antNodesWithoutVars.size() != addedConstant.getPosCount()) return; + //Should be a RuisHandler in general, not a PTree RuleUseInfoSet ruis = ((PTree)contextRuisSet.getByContext(contextID)).getAllRootRuis(); if (ruis == null) { applyRuleOnRui(addedConstant, contextID); @@ -184,7 +187,7 @@ private void sendSavedRUIs(String contextID) { @Override public RuisHandler createRuisHandler(String contextName) { SIndex index = new SIndex(contextName, getSharedVarsNodes(antNodesWithVars), (byte) 0); - return this.addContextRUIS(contextName, index); + return this.addContextRuiHandler(contextName, index); } /** diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index 2e2f8203..8f5b2510 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -206,7 +206,7 @@ else if (tRui.getPosCount()>= min && tRui.getPosCount() <= max) */ protected RuisHandler createRuisHandler(String contextName) { SIndex index = new SIndex(contextName, getSharedVarsNodes(antNodesWithVars), (byte) 0); - return this.addContextRUIS(contextName, index); + return this.addContextRuiHandler(contextName, index); } From de1c2f2566c693f3c829b3aebfe116dc732314f8 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sun, 28 Apr 2019 23:45:45 +0200 Subject: [PATCH 06/22] Modify SIndex structure --- src/sneps/network/VariableNode.java | 4 -- .../classes/setClasses/RuleUseInfoSet.java | 34 +++++++++-- src/sneps/snip/classes/RuisHandler.java | 5 +- src/sneps/snip/classes/RuleUseInfo.java | 2 +- src/sneps/snip/classes/SIndex.java | 60 ++++++++----------- 5 files changed, 57 insertions(+), 48 deletions(-) diff --git a/src/sneps/network/VariableNode.java b/src/sneps/network/VariableNode.java index c5a643f0..7bfc91a6 100644 --- a/src/sneps/network/VariableNode.java +++ b/src/sneps/network/VariableNode.java @@ -12,25 +12,21 @@ public class VariableNode extends Node implements Serializable{ private boolean snepslogFlag; public VariableNode() { - freeVariables = new VariableSet(); snepslogFlag = false; } public VariableNode(Term trm) { super(trm); - freeVariables = new VariableSet(); snepslogFlag = false; } public VariableNode(Semantic sem) { super(sem); - freeVariables = new VariableSet(); snepslogFlag = false; } public VariableNode(Semantic sem, Term trm) { super(sem, trm); - freeVariables = new VariableSet(); snepslogFlag = false; } diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 8b8695fd..241c6309 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -15,14 +15,21 @@ */ public class RuleUseInfoSet extends RuisHandler implements Iterable { private HashSet ruis; - - public RuleUseInfoSet(String contextName, boolean b) { - super(contextName); + + /** + * A boolean indicating whether this RuleUseInfoSet is treated as single + * RuleUseInfo. + */ + private boolean singleton; + + public RuleUseInfoSet() { ruis = new HashSet(); } - public RuleUseInfoSet() { + public RuleUseInfoSet(String contextName, boolean singleton) { + super(contextName); ruis = new HashSet(); + this.singleton = singleton; } @Override @@ -34,6 +41,13 @@ public boolean add(RuleUseInfo rui) { return ruis.add(rui); } + /** + * Combines every RUI in this RUISet with every RUI in second. + * @param second + * RUISet + * @return + * Combined RUISet + */ public RuleUseInfoSet combine(RuleUseInfoSet second) { RuleUseInfoSet res = new RuleUseInfoSet(this.context, false); for(RuleUseInfo rui1 : this){ @@ -42,21 +56,29 @@ public RuleUseInfoSet combine(RuleUseInfoSet second) { res.add(rui1.combine(rui2)); } } + return res; } @Override public RuleUseInfoSet insertRUI(RuleUseInfo rui) { - ruis.add(rui); - return this; + if(!singleton) { + ruis.add(rui); + } } + /** + * Adds all the RUIs in rootRUIS to this RUISet. + * @param rootRUIS + * @return boolean + */ public boolean addAll(RuleUseInfoSet rootRUIS) { boolean flag = true; for(RuleUseInfo rui : rootRUIS){ if(!ruis.add(rui)) flag = false; } + return flag; } diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index a555816f..1e9f824c 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -31,8 +31,9 @@ public String getContext() { } /** - *Inserts the given RuleUseInfo into this RuisHandler and returns the RuleUseInfoSet - *resulted from combining it with the RuleUseInfos in this RuisHandler + * Inserts the given RuleUseInfo into this RuisHandler and returns the + * RuleUseInfoSet resulted from combining it with the RuleUseInfos in this + * RuisHandler. * @param rui * RuleUseInfo * @return RuleUseInfoSet diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index 1df110d9..7d43e36c 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -150,12 +150,12 @@ public boolean isDisjoint(RuleUseInfo r) { * @return RuleUseInfo */ public RuleUseInfo combine(RuleUseInfo rui) { - //System.out.println(this.isDisjoint(rui) + " " + this.isVarsCompatible(rui)); if (this.isDisjoint(rui) && this.isVarsCompatible(rui)) { return new RuleUseInfo(this.getSubstitutions().union(rui.getSubstitutions()), this.pos + rui.getPosCount(), this.neg + rui.getNegCount(), this.fns.union(rui.getFlagNodeSet())); } + return null; } diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index 95e029ba..82c56b86 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -1,5 +1,6 @@ package sneps.snip.classes; +import java.util.ArrayList; import java.util.Hashtable; import sneps.network.VariableNode; @@ -12,9 +13,11 @@ public class SIndex extends RuisHandler { /** - * A Hashtable used to map different substitutions to different RuisHandlers. + * A Hashtable used to map different substitutions to different RuisHandlers. + * ArrayList represents the ids of the bound values of the substitutions + * for the shared variables. */ - private Hashtable map; + private Hashtable, RuisHandler> map; private byte ruiHandlerType; public static final byte RUIS = 0, SINGLETON = 1, PTREE = 2; private VarNodeSet sharedVars; @@ -32,53 +35,38 @@ public class SIndex extends RuisHandler { public SIndex(String context, byte ruiHandlerType, VarNodeSet sharedVars, NodeSet nodesWithVars) { super(context); - map = new Hashtable(); + map = new Hashtable, RuisHandler>(); this.ruiHandlerType = ruiHandlerType; this.sharedVars = sharedVars; this.nodesWithVars = nodesWithVars; } /** - * Insert the rule use info in the map based on the substitution. - * If this rule use info is null from the map, a new one is created based on the type - * and if not, it will be replaced by combining this rui and the existing one + * Inserts the RuleUseInfo in the map based on the bound values for the + * substitutions. If the RuisHandler based on the given index is null in the map, + * a new one is created according to the ruiHandlerType. If not, it will be + * replaced by combining the given rui with the existing one. * * @param rui - * Rule Use Info + * RuleUseInfo */ - public RuleUseInfoSet insertRUI(RuleUseInfo rui) { - int[] vars = new int[sharedVars.size()]; - int index = 0; - for (VariableNode varId : sharedVars) { - vars[index] = sharedVars.getVarNode(index).getId(); - index++; + public RuleUseInfoSet insertRUI(RuleUseInfo rui) { + ArrayList boundValues = new ArrayList(); + int boundValueID; + for(VariableNode var : sharedVars) { + boundValueID = rui.getSubstitutions().getBindingByVariable(var).getNode().getId(); + boundValues.add(boundValueID); } - int x = getIndex(vars); - - RuisHandler trui= map.get(x); + RuisHandler trui = map.get(boundValues); if (trui == null) { trui = getNewRUIType(); - map.put(x, trui); + map.put(boundValues, trui); } RuleUseInfoSet res = trui.insertRUI(rui); return res; } - - private int getIndex(int[] x) { - int p = 16777619; - int hash = (int) 2166136261L; - for (int i = 0; i < x.length; ++i) { - hash += (hash ^ x[i]) * p; - } - hash += hash << 13; - hash ^= hash >> 7; - hash += hash << 3; - hash ^= hash >> 17; - hash += hash << 5; - return hash; - } /** * Creates a new RuisHandler based on the type of SIndex needed. @@ -86,19 +74,21 @@ private int getIndex(int[] x) { */ private RuisHandler getNewRUIType() { RuisHandler tempRui = null; - switch (ruiType) { + switch (ruiHandlerType) { + case SINGLETON: + tempRui = new RuleUseInfoSet(getContext() , true); + break; case PTREE: tempRui = new PTree(); ((PTree) tempRui).buildTree(nodesWithVars); break; - case SINGLETON: - tempRui = new RuleUseInfoSet(getContext() , true); - break; case RUIS: tempRui = new RuleUseInfoSet(getContext() , false); + break; default: break; } + return tempRui; } From 670f5a96b031aff79cd93fecb9f5740371c3e382 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Mon, 29 Apr 2019 00:13:37 +0200 Subject: [PATCH 07/22] Implement insertRUI() in RUISet class --- .../classes/setClasses/RuleUseInfoSet.java | 66 ++++++++++++++----- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 241c6309..907051c0 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -1,6 +1,6 @@ package sneps.network.classes.setClasses; -import java.util.HashSet; +import java.util.HashSet; import java.util.Iterator; import sneps.snip.classes.RuisHandler; @@ -37,10 +37,40 @@ public Iterator iterator() { return ruis.iterator(); } - public boolean add(RuleUseInfo rui) { - return ruis.add(rui); + public RuleUseInfoSet add(RuleUseInfo rui) { + ruis.add(rui); + return this; } + @Override + public RuleUseInfoSet insertRUI(RuleUseInfo rui) { + //In case of inserting into a RUI set + if(!singleton) + return combineAdd(rui); + + //In case of inserting into a singleton RUI + if(isEmpty()) + return add(rui); + + return combine(rui); + } + + /** + * Combines rui with every RUI in this RUISet. + * @param rui + * @return RuleUseInfoSet + */ + public RuleUseInfoSet combine(RuleUseInfo rui) { + RuleUseInfoSet res = new RuleUseInfoSet(); + for (RuleUseInfo tRui : ruis) { + RuleUseInfo tmp = rui.combine(tRui); + if (tmp != null) + res.add(tmp); + } + + return res; + } + /** * Combines every RUI in this RUISet with every RUI in second. * @param second @@ -60,26 +90,24 @@ public RuleUseInfoSet combine(RuleUseInfoSet second) { return res; } - @Override - public RuleUseInfoSet insertRUI(RuleUseInfo rui) { - if(!singleton) { - ruis.add(rui); - } - } - /** * Adds all the RUIs in rootRUIS to this RUISet. * @param rootRUIS * @return boolean */ - public boolean addAll(RuleUseInfoSet rootRUIS) { - boolean flag = true; - for(RuleUseInfo rui : rootRUIS){ - if(!ruis.add(rui)) - flag = false; - } + public RuleUseInfoSet addAll(RuleUseInfoSet rootRUIS) { + for(RuleUseInfo rui : rootRUIS) + ruis.add(rui); + + return this; + } + + public RuleUseInfoSet combineAdd(RuleUseInfo rui) { + RuleUseInfoSet temp = this.combine(rui); + temp.add(rui); + this.addAll(temp); - return flag; + return this; } public boolean contains(RuleUseInfo rui){ @@ -89,4 +117,8 @@ public boolean contains(RuleUseInfo rui){ public boolean isEmpty() { return ruis.isEmpty(); } + + public int size() { + return ruis.size(); + } } From f486a83a9c88d0de70b457ef5e8e4ca6bdbb60ce Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Mon, 29 Apr 2019 23:37:19 +0200 Subject: [PATCH 08/22] Add hasSameFreeVariablesAs() in Node class --- src/sneps/network/Node.java | 54 +++++++++++++++- src/sneps/network/PropositionNode.java | 5 +- src/sneps/network/RuleNode.java | 64 ++++++------------- src/sneps/network/VariableNode.java | 34 +--------- .../classes/setClasses/RuleUseInfoSet.java | 5 +- .../classes/setClasses/VarNodeSet.java | 4 +- src/sneps/network/classes/term/Molecular.java | 4 ++ src/sneps/network/classes/term/Open.java | 10 +++ src/sneps/snip/Report.java | 8 ++- src/sneps/snip/classes/FlagNode.java | 8 ++- src/sneps/snip/classes/RuisHandler.java | 10 --- src/sneps/snip/classes/RuleUseInfo.java | 4 +- src/sneps/snip/classes/SIndex.java | 7 +- src/sneps/snip/rules/DoIfNode.java | 7 ++ src/sneps/snip/rules/WhenDoNode.java | 7 ++ 15 files changed, 127 insertions(+), 104 deletions(-) diff --git a/src/sneps/network/Node.java b/src/sneps/network/Node.java index 5ba506c1..a0e7dae2 100644 --- a/src/sneps/network/Node.java +++ b/src/sneps/network/Node.java @@ -6,7 +6,9 @@ import sneps.network.cables.UpCableSet; import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; import sneps.snebr.Context; import sneps.snip.channels.Channel; @@ -94,7 +96,10 @@ public String getSemanticSuperClass() { * @return the name or the label of the current node. */ public String getIdentifier() { - return this.term.getIdentifier(); + if(this.term != null) + return this.term.getIdentifier(); + else + return ""; } /** @@ -266,5 +271,52 @@ public void setTemp(boolean temp) { public void updateUpCables() { ((Molecular) this.getTerm()).updateUpCables(this); } + + public boolean hasSameFreeVariablesAs(Node n) { + //this Node is a VariableNode + if(this instanceof VariableNode) { + //Node to be compared with is also a VariableNode + if(n instanceof VariableNode) { + if(this.equals(((VariableNode) n))) + return true; + } + + //Node to be compared with is an Open node + if(n.getTerm() instanceof Open) { + VarNodeSet freeVars = ((Open) n.getTerm()).getFreeVariables(); + if((freeVars.size() == 1) && (this.equals(freeVars.getVarNode(0)))) + return true; + } + } + + //this Node is an Open node + if(this.getTerm() instanceof Open) { + VarNodeSet n1freeVars = ((Open) this.getTerm()).getFreeVariables(); + + //Node to be compared with is a VariableNode + if(n instanceof VariableNode) { + if((n1freeVars.size() == 1) && + (n1freeVars.getVarNode(0).equals(((VariableNode) n)))) + return true; + } + + //Node to be compared with is an Open node + if(n.getTerm() instanceof Open) { + VarNodeSet n2freeVars = ((Open) n.getTerm()).getFreeVariables(); + if(n1freeVars.size() != n2freeVars.size()) + return false; + + for(int i = 0; i < n1freeVars.size(); i++) { + if(!(n2freeVars.contains(n1freeVars.getVarNode(i)))) { + return false; + } + } + + return true; + } + } + + return false; + } } diff --git a/src/sneps/network/PropositionNode.java b/src/sneps/network/PropositionNode.java index 3b291759..f7b36df0 100644 --- a/src/sneps/network/PropositionNode.java +++ b/src/sneps/network/PropositionNode.java @@ -8,7 +8,6 @@ import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.ReportSet; -import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Term; import sneps.network.Node; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -29,7 +28,9 @@ import sneps.snip.matching.LinearSubstitutions; import sneps.snip.matching.Substitutions; -public class PropositionNode extends Node implements Serializable{ +public class PropositionNode extends Node implements Serializable { + private static final long serialVersionUID = 1L; + private Support basicSupport; protected ChannelSet outgoingChannels; diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 160018a2..fc2e160f 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -1,6 +1,7 @@ package sneps.network; import java.io.Serializable; +import java.util.Collection; import java.util.HashSet; import java.util.Hashtable; import java.util.Set; @@ -12,11 +13,8 @@ import sneps.network.classes.setClasses.ReportSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; -import sneps.network.classes.term.Base; -import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Variable; import sneps.snebr.Context; import sneps.snebr.Controller; import sneps.snip.Report; @@ -146,7 +144,7 @@ protected void processNodes(NodeSet antNodes) { public void applyRuleHandler(Report report, Node signature) { String contextID = report.getContextName(); RuleUseInfo rui; - PropositionSet propSet = report.getSupports(); + Collection propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); if (report.isPositive()) { @@ -198,11 +196,10 @@ public void clear() { public boolean allShareVars(NodeSet nodes) { if (nodes.isEmpty()) return false; - //return true; - VariableNode n = (VariableNode) nodes.getNode(0); + Node n = nodes.getNode(0); for (int i = 1; i < nodes.size(); i++) { - if (!n.hasSameFreeVariablesAs((VariableNode) nodes.getNode(i))) { + if (!(n.hasSameFreeVariablesAs(nodes.getNode(i)))) { return false; } } @@ -210,7 +207,7 @@ public boolean allShareVars(NodeSet nodes) { return true; } - public void addAntecedent(Node ant){ + public void addAntecedent(Node ant) { if(ant instanceof VariableNode || ant.getTerm() instanceof Open) antNodesWithVars.addNode(ant); else @@ -231,15 +228,15 @@ public VarNodeSet getSharedVarsNodes(NodeSet nodes) { if (nodes.isEmpty()) return res; - for(Node curNode : nodes){ - if(curNode instanceof VariableNode){ - if(temp.contains((VariableNode)curNode)) - res.addVarNode((VariableNode)curNode); + for(Node curNode : nodes) { + if(curNode instanceof VariableNode) { + if(temp.contains((VariableNode) curNode)) + res.addVarNode((VariableNode) curNode); else temp.addVarNode((VariableNode) curNode); } - if(curNode.getTerm() instanceof Open){ + if(curNode.getTerm() instanceof Open) { VarNodeSet free = ((Open)curNode.getTerm()).getFreeVariables(); for(VariableNode var : free) if(temp.contains(var)) @@ -251,24 +248,6 @@ public VarNodeSet getSharedVarsNodes(NodeSet nodes) { return res; } - - /** - * Returns true if the given Node is a constant node i.e. does not have - * free variables. - * - * @param n - * @return boolean - */ - public static boolean isConstantNode(Node n) { - return !(n.getTerm() instanceof Molecular) || (n.getTerm() instanceof Variable); - } - - /*public static boolean isConstantNode(Node n) { - return !((n instanceof VariableNode) || - ((n.getTerm() instanceof Open) && !(((Open) (n.getTerm())).getFreeVariables().isEmpty()))); - }*/ - - //Or call addAntecedent() /** * Returns a NodeSet of the nodes that are being pointed at, by this RuleNode, @@ -319,13 +298,15 @@ public RuisHandler addContextRuiHandler(String contextName) { SIndex si = null; //Antecedents with variables share the same set of variables if (shareVars) - si = new SIndex(contextName, sharedVars, SIndex.SINGLETON); - //Some variables are shared + si = new SIndex(SIndex.SINGLETON, sharedVars, antNodesWithVars); + //Antecedents share some but not all variables else - si = new SIndex(contextName, sharedVars, getSIndexContextType()); - //return this.addContextRUIS(si); + si = new SIndex(getSIndexType(), sharedVars, antNodesWithVars); + return this.addContextRuiHandler(contextName, si); - } else { + } + + else { //PTree in case of and-entailment //RUISet otherwise return this.addContextRuiHandler(contextName, createRuisHandler(contextName)); @@ -355,7 +336,7 @@ public RuisHandler addContextRuiHandler(String cntxt, RuisHandler cRuis) { * @return RuleUseInfoSet */ protected RuleUseInfoSet createContextRuiHandlerNonShared(String contextName) { - return new RuleUseInfoSet(contextName, false); + return new RuleUseInfoSet(false); } /** @@ -365,17 +346,12 @@ protected RuleUseInfoSet createContextRuiHandlerNonShared(String contextName) { * SIndex.RUIS: in other rule nodes * @return byte */ - protected byte getSIndexContextType() { - return SIndex.RUIS; - } + protected abstract byte getSIndexType(); public void splitToNodesWithVarsAndWithout(NodeSet allNodes, NodeSet withVars, NodeSet WithoutVars) { for (int i = 0; i < allNodes.size(); i++) { Node n = allNodes.getNode(i); - if (isConstantNode(n)) - WithoutVars.addNode(n); - else - withVars.addNode(n); + addAntecedent(n); } } diff --git a/src/sneps/network/VariableNode.java b/src/sneps/network/VariableNode.java index 7bfc91a6..3b8f0158 100644 --- a/src/sneps/network/VariableNode.java +++ b/src/sneps/network/VariableNode.java @@ -3,7 +3,9 @@ import java.io.Serializable; import sneps.network.classes.Semantic; +import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.setClasses.VariableSet; +import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; import sneps.network.classes.term.Variable; @@ -30,18 +32,6 @@ public VariableNode(Semantic sem, Term trm) { snepslogFlag = false; } - public boolean hasSameFreeVariablesAs(VariableNode node) { - int i = 0; - for (Variable var : freeVariables) { - if (!var.equals(node.getFreeVariables().getVariable(i))) { - return false; - } else { - i++; - } - } - return true; - } - public boolean isSnepslogFlag() { return snepslogFlag; } @@ -53,23 +43,5 @@ public void setSnepslogFlag(boolean snepslogFlag) { public VariableSet getFreeVariables() { return freeVariables; } - - /*public static void main(String[] args) { - VariableNode vn1 = new VariableNode(); - VariableNode vn2 = new VariableNode(); - Variable x = new Variable("x"); - Variable y = new Variable("y"); - Variable z = new Variable("z"); - - vn1.getFreeVariables().addVariable(x); - vn1.getFreeVariables().addVariable(y); - vn1.getFreeVariables().addVariable(z); - - vn2.getFreeVariables().addVariable(y); - vn2.getFreeVariables().addVariable(z); - vn2.getFreeVariables().addVariable(x); - - boolean res = vn1.hasSameFreeVariablesAs(vn2); - System.out.print(res); - }*/ + } diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 907051c0..36fe5130 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -26,8 +26,7 @@ public RuleUseInfoSet() { ruis = new HashSet(); } - public RuleUseInfoSet(String contextName, boolean singleton) { - super(contextName); + public RuleUseInfoSet(boolean singleton) { ruis = new HashSet(); this.singleton = singleton; } @@ -79,7 +78,7 @@ public RuleUseInfoSet combine(RuleUseInfo rui) { * Combined RUISet */ public RuleUseInfoSet combine(RuleUseInfoSet second) { - RuleUseInfoSet res = new RuleUseInfoSet(this.context, false); + RuleUseInfoSet res = new RuleUseInfoSet(false); for(RuleUseInfo rui1 : this){ for(RuleUseInfo rui2 : second){ //if(rui1.isDisjoint(rui2)) diff --git a/src/sneps/network/classes/setClasses/VarNodeSet.java b/src/sneps/network/classes/setClasses/VarNodeSet.java index 12d662c3..88c3a2d0 100644 --- a/src/sneps/network/classes/setClasses/VarNodeSet.java +++ b/src/sneps/network/classes/setClasses/VarNodeSet.java @@ -29,7 +29,9 @@ public VariableNode getVarNode(int index){ } public void addVarNode(VariableNode n) { - variables.add(n); + if(!(variables.contains(n))) { + variables.add(n); + } } public void addAll(VarNodeSet allVariables) { diff --git a/src/sneps/network/classes/term/Molecular.java b/src/sneps/network/classes/term/Molecular.java index 5125c56c..59dc279b 100644 --- a/src/sneps/network/classes/term/Molecular.java +++ b/src/sneps/network/classes/term/Molecular.java @@ -26,6 +26,10 @@ public Molecular(String identifier, DownCableSet downCableSet){ super(identifier); this.downCableSet = downCableSet; } + + public Molecular(String id) { + super(id); + } public DownCableSet getDownCableSet() { return this.downCableSet; diff --git a/src/sneps/network/classes/term/Open.java b/src/sneps/network/classes/term/Open.java index a87ab1d3..a6602b49 100644 --- a/src/sneps/network/classes/term/Open.java +++ b/src/sneps/network/classes/term/Open.java @@ -28,9 +28,19 @@ public Open(String identifier, DownCableSet dCableSet) { this.variables = new VarNodeSet(); this.updateFreeVariables(); } + + public Open(String id) { + super(id); + this.variables = new VarNodeSet(); + } + public VarNodeSet getFreeVariables() { return variables; } + + public void addFreeVariable(VariableNode n) { + variables.addVarNode(n); + } /** * The method that populate the list of free variables by the diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index c0164d10..18c3b1f7 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -1,5 +1,7 @@ package sneps.snip; +import java.util.Collection; + import sneps.network.classes.setClasses.PropositionSet; import sneps.snip.matching.Substitutions; @@ -12,11 +14,11 @@ */ public class Report { private Substitutions substitution; - private PropositionSet supports; + private Collection supports; private boolean sign; private String contextName; - public Report(Substitutions substitution, PropositionSet set, boolean sign, String contextID) { + public Report(Substitutions substitution, Collection set, boolean sign, String contextID) { this.substitution = substitution; this.supports = set; this.sign = sign; @@ -27,7 +29,7 @@ public Substitutions getSubstitutions() { return substitution; } - public PropositionSet getSupports() { + public Collection getSupports() { return supports; } diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index c0833cff..78aae89f 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -1,11 +1,13 @@ package sneps.snip.classes; +import java.util.Collection; + import sneps.network.Node; import sneps.network.classes.setClasses.PropositionSet; public class FlagNode { private Node node; - private PropositionSet supports; + private Collection supports; private int flag; /** @@ -18,7 +20,7 @@ public class FlagNode { * @param f, * true or false */ - public FlagNode(Node n, PropositionSet set, int f) { + public FlagNode(Node n, Collection set, int f) { node = n; supports = set; flag = f; @@ -38,7 +40,7 @@ public Node getNode() { * * @return support */ - public PropositionSet getSupports() { + public Collection getSupports() { return supports; } diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 1e9f824c..3ea9a4c4 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -14,21 +14,11 @@ * @version 3.00 31/5/2018 */ public abstract class RuisHandler { - protected String context; private NodeSet positiveNodes; public RuisHandler(){ positiveNodes = new NodeSet(); } - - public RuisHandler(String contextID) { - this(); - this.context = contextID; - } - - public String getContext() { - return context; - } /** * Inserts the given RuleUseInfo into this RuisHandler and returns the diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index 7d43e36c..768a8354 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -164,7 +164,7 @@ public RuleUseInfo combine(RuleUseInfo rui) { * @return PropositionSet */ - public PropositionSet getSupports() { + /*public PropositionSet getSupports() { if (fns.isNew()) return new PropositionSet(); @@ -181,6 +181,6 @@ public PropositionSet getSupports() { } return res; - } + }*/ } \ No newline at end of file diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index 82c56b86..ba8bdf8e 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -32,9 +32,8 @@ public class SIndex extends RuisHandler { * Set of shared variables whose bindings are used to hash on in this SIndex. * @param nodesWithVars */ - public SIndex(String context, byte ruiHandlerType, VarNodeSet sharedVars, + public SIndex(byte ruiHandlerType, VarNodeSet sharedVars, NodeSet nodesWithVars) { - super(context); map = new Hashtable, RuisHandler>(); this.ruiHandlerType = ruiHandlerType; this.sharedVars = sharedVars; @@ -76,14 +75,14 @@ private RuisHandler getNewRUIType() { RuisHandler tempRui = null; switch (ruiHandlerType) { case SINGLETON: - tempRui = new RuleUseInfoSet(getContext() , true); + tempRui = new RuleUseInfoSet(true); break; case PTREE: tempRui = new PTree(); ((PTree) tempRui).buildTree(nodesWithVars); break; case RUIS: - tempRui = new RuleUseInfoSet(getContext() , false); + tempRui = new RuleUseInfoSet(false); break; default: break; diff --git a/src/sneps/snip/rules/DoIfNode.java b/src/sneps/snip/rules/DoIfNode.java index 89d1b8b8..686338f9 100644 --- a/src/sneps/snip/rules/DoIfNode.java +++ b/src/sneps/snip/rules/DoIfNode.java @@ -34,4 +34,11 @@ protected RuisHandler createRuisHandler(String contextName) { return null; } + + @Override + protected byte getSIndexType() { + // TODO Auto-generated method stub + return 0; + } + } diff --git a/src/sneps/snip/rules/WhenDoNode.java b/src/sneps/snip/rules/WhenDoNode.java index d3646aad..13656310 100644 --- a/src/sneps/snip/rules/WhenDoNode.java +++ b/src/sneps/snip/rules/WhenDoNode.java @@ -34,4 +34,11 @@ protected RuisHandler createRuisHandler(String contextName) { return null; } + + @Override + protected byte getSIndexType() { + // TODO Auto-generated method stub + return 0; + } + } From fc3d5a9ea9fb27d996046fd89f51f23b37f189f9 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Thu, 2 May 2019 18:19:25 +0200 Subject: [PATCH 09/22] SIndex Tests --- src/sneps/network/Node.java | 4 +- src/sneps/network/RuleNode.java | 83 +++--- src/sneps/snip/Report.java | 13 +- src/sneps/snip/channels/Channel.java | 2 +- src/sneps/snip/classes/FlagNode.java | 5 + src/sneps/snip/classes/SIndex.java | 32 ++- src/sneps/snip/rules/NumericalEntailment.java | 70 ++--- tests/SIndexTest.java | 259 ++++-------------- 8 files changed, 175 insertions(+), 293 deletions(-) diff --git a/src/sneps/network/Node.java b/src/sneps/network/Node.java index a0e7dae2..bb329d00 100644 --- a/src/sneps/network/Node.java +++ b/src/sneps/network/Node.java @@ -125,7 +125,9 @@ public NodeSet getParentNodes() { */ @Override public String toString() { - return this.term.toString(); + if(this.term != null) + return this.term.toString(); + return null; } /** diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index fc2e160f..92e98fb6 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -31,6 +31,7 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; private NodeSet consequents; + protected NodeSet antecedents; /** * A NodeSet containing all the pattern antecedents attached to this RuleNode. @@ -55,48 +56,54 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ * A ContextRuisSet that is used to map each context to its appropriate * RuiHandler for this RuleNode. */ - protected ContextRuisSet contextRuisSet; + //protected ContextRuisSet contextRuisSet; + + /** + * A RuisHandler that is used to keep track of all the RUIs for this RuleNode. + */ + protected RuisHandler ruisHandler; /** * A Hashtable used to map each context to a single RuleUseInfo - * that contains all the instances that do not dominate variables. + * that contains all the constant instances that do not dominate variables. */ private Hashtable contextConstantRUI; public RuleNode() { consequents = new NodeSet(); + antecedents = new NodeSet(); antNodesWithoutVars = new NodeSet(); antNodesWithoutVarsIDs = new HashSet(); antNodesWithVars = new NodeSet(); antNodesWithVarsIDs = new HashSet(); shareVars = false; sharedVars = new VarNodeSet(); - contextRuisSet = new ContextRuisSet(); + //contextRuisSet = new ContextRuisSet(); contextConstantRUI = new Hashtable(); } public RuleNode(Molecular syn) { super(syn); - consequents = new NodeSet(); + consequents = new NodeSet(); + antecedents = new NodeSet(); antNodesWithoutVars = new NodeSet(); antNodesWithoutVarsIDs = new HashSet(); antNodesWithVars = new NodeSet(); antNodesWithVarsIDs = new HashSet(); shareVars = false; sharedVars = new VarNodeSet(); - contextRuisSet = new ContextRuisSet(); + //contextRuisSet = new ContextRuisSet(); contextConstantRUI = new Hashtable(); } - public NodeSet getConsequents() { return consequents; } - - public ContextRuisSet getContextRuisSet() { - return contextRuisSet; + + public RuisHandler getRuisHandler() { + return ruisHandler; } - + public int getAntSize(){ return antNodesWithoutVars.size() + antNodesWithVars.size(); } @@ -142,7 +149,7 @@ protected void processNodes(NodeSet antNodes) { * The instance that is being reported by the report. */ public void applyRuleHandler(Report report, Node signature) { - String contextID = report.getContextName(); + //String contextID = report.getContextName(); RuleUseInfo rui; Collection propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); @@ -156,33 +163,31 @@ public void applyRuleHandler(Report report, Node signature) { rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns); } - RuisHandler crtemp = contextRuisSet.getByContext(contextID); + //RuisHandler crtemp = contextRuisSet.getByContext(contextID); - //This is the first report received by this RuleNode in the context given - //by contextID, so a RuisHandler is created, and added to this RuleNode's - //contextRuisSet - if(crtemp == null){ - crtemp = addContextRuiHandler(contextID); + // This is the first report received by this RuleNode, so a RuisHandler is + // created + if(ruisHandler == null){ + ruisHandler = addRuiHandler(); } - //The RUI created for the given report is inserted to the corresponding - //RuisHandler for the report's context given by contextID - RuleUseInfoSet res = crtemp.insertRUI(rui); + // The RUI created for the given report is inserted to the RuisHandler + RuleUseInfoSet res = ruisHandler.insertRUI(rui); if (res == null) res = new RuleUseInfoSet(); for (RuleUseInfo tRui : res) { - applyRuleOnRui(tRui, contextID); + applyRuleOnRui(tRui); } } - abstract protected void applyRuleOnRui(RuleUseInfo tRui, String contextID); + abstract protected void applyRuleOnRui(RuleUseInfo tRui); /** * Clears all the information saved by this RuleNode about the instances received. */ public void clear() { - contextRuisSet.clear(); + //contextRuisSet.clear(); contextConstantRUI.clear(); } @@ -281,39 +286,39 @@ public NodeSet getUpNodeSet(String name) { * @param cntxt * @return RuisHandler */ - public RuisHandler getContextRuiHandler(String cntxt) { + /*public RuisHandler getContextRuiHandler(String cntxt) { return contextRuisSet.getByContext(cntxt); - } + }*/ /** - * Creates an appropriate RuisHandler for a given context for this RuleNode, - * according to whether all, some or none of the variables in the antecedents are - * shared. Then adds the RuisHandler to this contextRuisSet. + * Creates an appropriate RuisHandler for this RuleNode, according to whether all, + * some or none of the variables in the antecedents are shared. * * @param contextName * @return RuisHandler */ - public RuisHandler addContextRuiHandler(String contextName) { + public RuisHandler addRuiHandler() { if (sharedVars.size() != 0) { SIndex si = null; - //Antecedents with variables share the same set of variables + // Antecedents with variables share the same set of variables if (shareVars) si = new SIndex(SIndex.SINGLETON, sharedVars, antNodesWithVars); - //Antecedents share some but not all variables + // Antecedents share some but not all variables else si = new SIndex(getSIndexType(), sharedVars, antNodesWithVars); - return this.addContextRuiHandler(contextName, si); + return si; } else { - //PTree in case of and-entailment - //RUISet otherwise - return this.addContextRuiHandler(contextName, createRuisHandler(contextName)); + // PTree in case of and-entailment + // RUISet otherwise + //return this.addContextRuiHandler(contextName, createRuisHandler(contextName)); + return createRuisHandler(); } } - /** + /*/** * Adds the given RuisHandler by the given context name to this contextRuisSet. * * @param cntxt @@ -322,11 +327,11 @@ public RuisHandler addContextRuiHandler(String contextName) { * RuisHandler to be added. * @return RuisHandler */ - public RuisHandler addContextRuiHandler(String cntxt, RuisHandler cRuis) { + /*public RuisHandler addContextRuiHandler(String cntxt, RuisHandler cRuis) { return this.contextRuisSet.addHandlerSet(cntxt, cRuis); - } + }*/ - protected abstract RuisHandler createRuisHandler(String contextName); + protected abstract RuisHandler createRuisHandler(); /** * Returns a RUISet to be used in case all the antecedents do not share a common diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index 18c3b1f7..cc33987b 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -16,13 +16,11 @@ public class Report { private Substitutions substitution; private Collection supports; private boolean sign; - private String contextName; - public Report(Substitutions substitution, Collection set, boolean sign, String contextID) { + public Report(Substitutions substitution, Collection set, boolean sign) { this.substitution = substitution; this.supports = set; this.sign = sign; - this.contextName = contextID; } public Substitutions getSubstitutions() { @@ -36,8 +34,8 @@ public Collection getSupports() { @Override public boolean equals(Object report) { Report castedReport = (Report) report; - return this.substitution.equals(castedReport.substitution) && this.sign == castedReport.sign - && this.contextName == castedReport.contextName; + return this.substitution.equals(castedReport.substitution) + && this.sign == castedReport.sign; } public boolean getSign() { @@ -53,10 +51,7 @@ public boolean isNegative() { } public String toString() { - return "ContextID : " + contextName + "\nSign: " + sign + "\nSubstitution: " + substitution + "\nSupport: " + supports.toString(); + return "Sign: " + sign + "\nSubstitution: " + substitution + "\nSupport: " + supports.toString(); } - public String getContextName() { - return contextName; - } } diff --git a/src/sneps/snip/channels/Channel.java b/src/sneps/snip/channels/Channel.java index d7259600..ceed8acb 100644 --- a/src/sneps/snip/channels/Channel.java +++ b/src/sneps/snip/channels/Channel.java @@ -42,7 +42,7 @@ public Channel(Substitutions switchSubstitution, public boolean addReport(Report report) { System.out.println("Can pass " + filter.canPass(report)); - if (filter.canPass(report) && contextName == report.getContextName()) { + if (filter.canPass(report)) { System.out.println("\n\nThe Switch data:\n" + switch_); switch_.switchReport(report); reportsBuffer.addReport(report); diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index 78aae89f..94611542 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -25,6 +25,11 @@ public FlagNode(Node n, Collection set, int f) { supports = set; flag = f; } + + public FlagNode(Node n, int f) { + node = n; + flag = f; + } /** * Return the node of the flag node diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index ba8bdf8e..d945357d 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -7,8 +7,6 @@ import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; -import sneps.snip.classes.PTree; -import sneps.snip.classes.RuisHandler; public class SIndex extends RuisHandler { @@ -17,7 +15,7 @@ public class SIndex extends RuisHandler { * ArrayList represents the ids of the bound values of the substitutions * for the shared variables. */ - private Hashtable, RuisHandler> map; + public Hashtable, RuisHandler> map; private byte ruiHandlerType; public static final byte RUIS = 0, SINGLETON = 1, PTREE = 2; private VarNodeSet sharedVars; @@ -39,6 +37,12 @@ public SIndex(byte ruiHandlerType, VarNodeSet sharedVars, this.sharedVars = sharedVars; this.nodesWithVars = nodesWithVars; } + + public SIndex(byte ruiHandlerType, VarNodeSet sharedVars) { + map = new Hashtable, RuisHandler>(); + this.ruiHandlerType = ruiHandlerType; + this.sharedVars = sharedVars; + } /** * Inserts the RuleUseInfo in the map based on the bound values for the @@ -53,18 +57,24 @@ public RuleUseInfoSet insertRUI(RuleUseInfo rui) { ArrayList boundValues = new ArrayList(); int boundValueID; for(VariableNode var : sharedVars) { - boundValueID = rui.getSubstitutions().getBindingByVariable(var).getNode().getId(); - boundValues.add(boundValueID); + if(rui.getSubstitutions().getBindingByVariable(var) != null) { + boundValueID = rui.getSubstitutions().getBindingByVariable(var).getNode().getId(); + boundValues.add(boundValueID); + } } - RuisHandler trui = map.get(boundValues); - if (trui == null) { - trui = getNewRUIType(); - map.put(boundValues, trui); + if(!boundValues.isEmpty()) { + RuisHandler trui = map.get(boundValues); + if (trui == null) { + trui = getNewRUIType(); + map.put(boundValues, trui); + } + + RuleUseInfoSet res = trui.insertRUI(rui); + return res; } - RuleUseInfoSet res = trui.insertRUI(rui); - return res; + return null; } /** diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 35b965f1..6cfe0caa 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -1,5 +1,7 @@ package sneps.snip.rules; +import java.util.Collection; + import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; @@ -34,10 +36,18 @@ */ public class NumericalEntailment extends RuleNode { private static final long serialVersionUID = 3546852401118194013L; + private int i; public NumericalEntailment(Molecular syn) { super(syn); + // Initializing i + NodeSet max = getDownNodeSet("i"); + i = Integer.parseInt(max.getNode(0).getIdentifier()); + + // Initializing the antecedents + antecedents = getDownAntNodeSet(); + processNodes(antecedents); } /** @@ -48,15 +58,24 @@ public NumericalEntailment(Molecular syn) { */ @Override public void applyRuleHandler(Report report, Node signature) { - String contxt = report.getContextName(); if (report.isPositive()) { - PropositionSet propSet = report.getSupports(); + Collection propSet = report.getSupports(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); - RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), - 1, 0, fns); - addNotSentRui(rui, contxt, signature); + RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns); + + // Inserting the RuleUseInfo into the RuleNode's RuisHandler: + // SIndex in case there shared variables between the antecedents, or + // RUISet in case there are no shared variables + if(ruisHandler == null){ + ruisHandler = addRuiHandler(); + } + + ruisHandler.insertRUI(rui); + if(!ruisHandler.getPositiveNodes().contains(signature)) + ruisHandler.getPositiveNodes().addNode(signature); } + int curPos = contextRuisSet.getByContext(contxt).getPositiveNodes().size(); int n = antNodesWithoutVars.size()+antNodesWithVars.size(); //The second cond will be removed @@ -136,22 +155,6 @@ protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { } } - /** - * Inserts given RuleUseInfo into the appropriate SIndex and updates the corresponding SIndex - * @param rui - * @param contxt - * @param signature - */ - public void addNotSentRui(RuleUseInfo rui, String contxt, Node signature){ - SIndex set = (SIndex) contextRuisSet.getByContext(contxt); - if (set == null) - //Add check for vars - set = new SIndex(contxt, getSharedVarsNodes(antNodesWithVars), (byte) 0); - set.insertRUI(rui); - if(!set.getPositiveNodes().contains(signature)) - set.getPositiveNodes().addNode(signature); - contextRuisSet.addHandlerSet(contxt, set); - } /** * Prepares the appropriate SIndex and all its root RuleUseInfo for broadcasting * @param contextID @@ -179,17 +182,6 @@ private void sendSavedRUIs(String contextID) { } } - /** - * Creates an appropriate SIndex as a RuisHandler and inserts it into ContextRuisSet by Context - * @param contextName - * @return - */ - @Override - public RuisHandler createRuisHandler(String contextName) { - SIndex index = new SIndex(contextName, getSharedVarsNodes(antNodesWithVars), (byte) 0); - return this.addContextRuiHandler(contextName, index); - } - /** * Naming convention used to retrieve Nodes in Down Antecedent position is "iant"; * "i" for NumericalEntailment, "ant" for Antecedent @@ -197,9 +189,19 @@ public RuisHandler createRuisHandler(String contextName) { */ @Override public NodeSet getDownAntNodeSet(){ - return this.getDownNodeSet("iant"); + return this.getDownNodeSet("&ant"); } + @Override + protected RuisHandler createRuisHandler() { + return new RuleUseInfoSet(false); + } + + @Override + protected byte getSIndexType() { + return SIndex.RUIS; + } + /** * Getter for i * @return @@ -207,6 +209,7 @@ public NodeSet getDownAntNodeSet(){ public int getI() { return i; } + /** * Setter for i * @param newI @@ -214,4 +217,5 @@ public int getI() { public void setI(int newI){ i = newI; } + } diff --git a/tests/SIndexTest.java b/tests/SIndexTest.java index 5672c6f4..7f951bd7 100644 --- a/tests/SIndexTest.java +++ b/tests/SIndexTest.java @@ -1,18 +1,20 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; +import java.util.ArrayList; + import sneps.network.Node; import sneps.network.PropositionNode; import sneps.network.VariableNode; +import sneps.network.classes.term.Base; import sneps.network.classes.term.Variable; -import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; +import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.classes.SIndexHelper; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; @@ -23,12 +25,16 @@ public class SIndexTest { public void newSIndex() { /** - * Creating Ndes + * Creating Nodes */ - Node n = new Node(); - Node n1 = new Node(); - Node n2 = new Node(); + Base ba = new Base("HEY"); + Base ba1 = new Base("hoooo"); + Base ba2 = new Base("hola"); + + Node n = new Node(ba); + Node n1 = new Node(ba1); + Node n2 = new Node(ba2); Node n3 = new Node(); Node n4 = new Node(); Node n5 = new Node(); @@ -40,6 +46,8 @@ public void newSIndex() { LinearSubstitutions ls1 = new LinearSubstitutions(); LinearSubstitutions ls2 = new LinearSubstitutions(); LinearSubstitutions ls3 = new LinearSubstitutions(); + LinearSubstitutions ls4 = new LinearSubstitutions(); + LinearSubstitutions ls5 = new LinearSubstitutions(); PropositionNode pn = new PropositionNode(); PropositionNode pn2 = new PropositionNode(); @@ -91,36 +99,36 @@ public void newSIndex() { vns5.addVarNode(vn5); vns6.addVarNode(vn6); vns7.addVarNode(vn7); + //RUI set SIndex + SIndex index = new SIndex((byte) 0, vns); - SIndex index = new SIndex("Hello1",vns,(byte) 1); - SIndex index1 = new SIndex("Hello",vns1,(byte) 1); - SIndex index2 = new SIndex("Hell",vns2,(byte) 1); - SIndex index3 = new SIndex("Hel",vns3,(byte) 1); - SIndex index4 = new SIndex("He",vns4,(byte) 1); - SIndex index5 = new SIndex("H",vns5,(byte) 1); - SIndex index6 = new SIndex("Ha",vns6,(byte) 1); - SIndex index7 = new SIndex("Hs",vns7,(byte) 1); - + //Singleton SIndex + SIndex index1 = new SIndex((byte) 1, vns); + Binding b = new Binding(vn, n); Binding b1 = new Binding(vn1, n1); Binding b2 = new Binding(vn2, n); - Binding b3 = new Binding(vn3,n); + Binding b3 = new Binding(vn3, n); Binding b4 = new Binding(vn4, n); Binding b5 = new Binding(vn5, n1); Binding b6 = new Binding(vn6, n); - Binding b7 = new Binding(vn7,n); - - ls.insert(b); - ls1.insert(b1); - - FlagNode fn = new FlagNode(n,ps,1); - FlagNode fn1 = new FlagNode(n1,ps1,1); - FlagNode fn2 = new FlagNode(n2,ps,1); - FlagNode fn3 = new FlagNode(n3,ps,1); - FlagNode fn4 = new FlagNode(n4,ps,1); - FlagNode fn5 = new FlagNode(n5,ps,1); - FlagNode fn6 = new FlagNode(n6,ps,1); - FlagNode fn7 = new FlagNode(n7,ps,1); + Binding b7 = new Binding(vn7, n); + Binding b8 = new Binding(vn, n1); + Binding b9 = new Binding(vn, n2); + + ls.putIn(b); + ls1.putIn(b1); + ls4.putIn(b8); + ls5.putIn(b9); + + FlagNode fn = new FlagNode(n, 1); + FlagNode fn1 = new FlagNode(n1, 1); + FlagNode fn2 = new FlagNode(n2, 1); + FlagNode fn3 = new FlagNode(n3, 1); + FlagNode fn4 = new FlagNode(n4, 1); + FlagNode fn5 = new FlagNode(n5, 1); + FlagNode fn6 = new FlagNode(n6, 1); + FlagNode fn7 = new FlagNode(n7, 1); FlagNodeSet fns = new FlagNodeSet(); FlagNodeSet fns1 = new FlagNodeSet(); @@ -140,8 +148,6 @@ public void newSIndex() { fns5.insert(fn1); fns6.insert(fn1); fns7.insert(fn1); - - /** @@ -150,200 +156,55 @@ public void newSIndex() { RuleUseInfo rui = new RuleUseInfo(ls, 1, 0, fns); index.insertRUI(rui); - + index1.insertRUI(rui); RuleUseInfo rui1 = new RuleUseInfo(ls1, 1, 0, fns1); - index1.insertRUI(rui1); RuleUseInfo rui2 = new RuleUseInfo(ls1, 1, 0, fns2); - index2.insertRUI(rui2); RuleUseInfo rui3 = new RuleUseInfo(ls1, 1, 0, fns2); - index3.insertRUI(rui3); RuleUseInfo rui4 = new RuleUseInfo(ls3, 0, 1, fns3); - index4.insertRUI(rui4); RuleUseInfo rui5 = new RuleUseInfo(ls2, 2, 0, fns4); index.insertRUI(rui5); + index1.insertRUI(rui5); RuleUseInfo rui6 = new RuleUseInfo(ls, 3, 0, fns5); index.insertRUI(rui6); + index1.insertRUI(rui6); - assertEquals(11, SIndexHelper.getSize()); - } - - - - - @Test - public void newSIndexTwo() { + RuleUseInfo rui7 = new RuleUseInfo(ls4, 4, 0, fns5); + index.insertRUI(rui7); + index1.insertRUI(rui7); - /** - * Creating Nodes - */ + RuleUseInfo rui8 = new RuleUseInfo(ls5, 5, 0, fns5); + index.insertRUI(rui8); + index1.insertRUI(rui8); - Node n = new Node(); - Node n1 = new Node(); - Node n2 = new Node(); - Node n3 = new Node(); - Node n4 = new Node(); - Node n5 = new Node(); - Node n6 = new Node(); - Node n7 = new Node(); - - LinearSubstitutions ls = new LinearSubstitutions(); - LinearSubstitutions ls1 = new LinearSubstitutions(); - LinearSubstitutions ls2 = new LinearSubstitutions(); - LinearSubstitutions ls3 = new LinearSubstitutions(); + ArrayList arr = new ArrayList(); + arr.add(0); + ArrayList arr1 = new ArrayList(); + arr1.add(1); + ArrayList arr2 = new ArrayList(); + arr2.add(2); - PropositionNode pn = new PropositionNode(); - PropositionNode pn2 = new PropositionNode(); - PropositionNode pn3 = new PropositionNode(); - PropositionSet ps = new PropositionSet(); - PropositionSet ps1 = new PropositionSet(); - PropositionSet ps2 = new PropositionSet(); - PropositionSet ps3 = new PropositionSet(); - PropositionSet ps4 = new PropositionSet(); - PropositionSet ps5 = new PropositionSet(); - PropositionSet ps6 = new PropositionSet(); - PropositionSet ps7 = new PropositionSet(); - - - Variable v = new Variable("x"); - Variable v1 = new Variable("y"); - Variable v2 = new Variable("z"); - Variable v3 = new Variable("a"); - Variable v4 = new Variable("b"); - Variable v5 = new Variable("c"); - Variable v6 = new Variable("d"); - Variable v7 = new Variable("e"); + assertEquals(3, index.getSize()); + assertEquals(3, ((RuleUseInfoSet) (index.map.get(arr))).size()); + assertEquals(1, ((RuleUseInfoSet) (index.map.get(arr1))).size()); + assertEquals(1, ((RuleUseInfoSet) (index.map.get(arr2))).size()); - VariableNode vn = new VariableNode(v); - VariableNode vn1 = new VariableNode(v1); - VariableNode vn2 = new VariableNode(v2); - VariableNode vn3 = new VariableNode(v3); - VariableNode vn4 = new VariableNode(v4); - VariableNode vn5 = new VariableNode(v5); - VariableNode vn6 = new VariableNode(v6); - VariableNode vn7 = new VariableNode(v7); - - VarNodeSet vns = new VarNodeSet(); - VarNodeSet vns1 = new VarNodeSet(); - VarNodeSet vns2 = new VarNodeSet(); - VarNodeSet vns3 = new VarNodeSet(); - VarNodeSet vns4 = new VarNodeSet(); - VarNodeSet vns5 = new VarNodeSet(); - VarNodeSet vns6 = new VarNodeSet(); - VarNodeSet vns7 = new VarNodeSet(); - - - vns.addVarNode(vn); - vns1.addVarNode(vn1); - vns2.addVarNode(vn2); - vns3.addVarNode(vn3); - vns4.addVarNode(vn4); - vns5.addVarNode(vn5); - vns6.addVarNode(vn6); - vns7.addVarNode(vn7); - - SIndex index = new SIndex("Hello1",vns,(byte) 1); - SIndex index1 = new SIndex("Hello",vns1,(byte) 1); - SIndex index2 = new SIndex("Hell",vns2,(byte) 1); - SIndex index3 = new SIndex("Hel",vns3,(byte) 1); - SIndex index4 = new SIndex("He",vns4,(byte) 1); - SIndex index5 = new SIndex("H",vns5,(byte) 1); - SIndex index6 = new SIndex("Ha",vns6,(byte) 1); - SIndex index7 = new SIndex("Hs",vns7,(byte) 1); - - Binding b = new Binding(vn, n); - Binding b1 = new Binding(vn1, n1); - Binding b2 = new Binding(vn2, n); - Binding b3 = new Binding(vn3,n); - Binding b4 = new Binding(vn4, n); - Binding b5 = new Binding(vn5, n1); - Binding b6 = new Binding(vn6, n); - Binding b7 = new Binding(vn7,n); - - ls.insert(b); - ls1.insert(b1); - - FlagNode fn = new FlagNode(n,ps,1); - FlagNode fn1 = new FlagNode(n1,ps1,1); - FlagNode fn2 = new FlagNode(n2,ps,1); - FlagNode fn3 = new FlagNode(n3,ps,1); - FlagNode fn4 = new FlagNode(n4,ps,1); - FlagNode fn5 = new FlagNode(n5,ps,1); - FlagNode fn6 = new FlagNode(n6,ps,1); - FlagNode fn7 = new FlagNode(n7,ps,1); - - FlagNodeSet fns = new FlagNodeSet(); - FlagNodeSet fns1 = new FlagNodeSet(); - FlagNodeSet fns2 = new FlagNodeSet(); - FlagNodeSet fns3 = new FlagNodeSet(); - FlagNodeSet fns4 = new FlagNodeSet(); - FlagNodeSet fns5 = new FlagNodeSet(); - FlagNodeSet fns6 = new FlagNodeSet(); - FlagNodeSet fns7 = new FlagNodeSet(); - - - fns.insert(fn); - fns1.insert(fn1); - fns2.insert(fn1); - fns3.insert(fn1); - fns4.insert(fn1); - fns5.insert(fn1); - fns6.insert(fn1); - fns7.insert(fn1); - - - - - /** - * Creating new Ruis and inserting them in the map - */ - - RuleUseInfo rui = new RuleUseInfo(ls, 1, 0, fns); - index.insertRUI(rui); - - - - RuleUseInfo rui1 = new RuleUseInfo(ls1, 1, 0, fns1); - index1.insertRUI(rui1); - - - RuleUseInfo rui2 = new RuleUseInfo(ls1, 1, 0, fns2); - index2.insertRUI(rui2); - - - RuleUseInfo rui3 = new RuleUseInfo(ls1, 1, 0, fns2); - index3.insertRUI(rui3); - - - RuleUseInfo rui4 = new RuleUseInfo(ls3, 0, 1, fns3); - index4.insertRUI(rui4); - - - RuleUseInfo rui5 = new RuleUseInfo(ls2, 2, 0, fns4); - index.insertRUI(rui5); - - - RuleUseInfo rui6 = new RuleUseInfo(ls, 3, 0, fns5); - index4.insertRUI(rui6); - - RuleUseInfo rui7 = new RuleUseInfo(ls, 3, 0, fns5); - index5.insertRUI(rui6); - - assertEquals(6, SIndexHelper.getSize()); + assertEquals(3, index1.getSize()); + assertEquals(1, ((RuleUseInfoSet) (index1.map.get(arr))).size()); } - - + + } From 052be6a7f20ddf8c744d13f0fe68c711cce5caa0 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sat, 11 May 2019 01:25:07 +0200 Subject: [PATCH 10/22] Implementation of rule firing for NumericalEntailment --- src/sneps/network/Network.java | 12 +- src/sneps/network/PropositionNode.java | 9 +- src/sneps/network/RuleNode.java | 132 +++++++---- .../classes/setClasses/FlagNodeSet.java | 12 + .../classes/setClasses/RuleUseInfoSet.java | 13 ++ .../classes/setClasses/VarNodeSet.java | 2 +- src/sneps/snebr/Support.java | 4 + src/sneps/snip/InferenceTypes.java | 5 + src/sneps/snip/Report.java | 43 ++-- src/sneps/snip/classes/FlagNode.java | 14 +- src/sneps/snip/classes/RuisHandler.java | 15 +- src/sneps/snip/classes/RuleResponse.java | 44 ++++ src/sneps/snip/classes/RuleUseInfo.java | 48 +--- src/sneps/snip/classes/SIndex.java | 12 + .../snip/matching/LinearSubstitutions.java | 4 + src/sneps/snip/matching/Substitutions.java | 1 + src/sneps/snip/rules/NumericalEntailment.java | 213 ++++++++---------- src/sneps/snip/rules/OrEntailment.java | 3 - tests/NumericalEntailmentTests.java | 177 ++++++++------- tests/SIndexTest.java | 5 +- 20 files changed, 437 insertions(+), 331 deletions(-) create mode 100644 src/sneps/snip/InferenceTypes.java create mode 100644 src/sneps/snip/classes/RuleResponse.java diff --git a/src/sneps/network/Network.java b/src/sneps/network/Network.java index 5a376ca8..fa47dcc0 100644 --- a/src/sneps/network/Network.java +++ b/src/sneps/network/Network.java @@ -1088,6 +1088,8 @@ private static boolean isToBePattern(Object[][] array) { * the case frame implemented by the new pattern or proposition node. * * @return the newly created pattern node or proposition node. + * @throws NodeNotFoundInNetworkException + * @throws NotAPropositionNodeException * * @throws Exception * if the semantic class specified by the case frame was not @@ -1095,7 +1097,7 @@ private static boolean isToBePattern(Object[][] array) { */ @SuppressWarnings("rawtypes") - private static Node createPatNode(Object[][] relNodeSet, CaseFrame caseFrame) { + private static Node createPatNode(Object[][] relNodeSet, CaseFrame caseFrame) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { LinkedList dCables = new LinkedList(); for (int i = 0; i < relNodeSet.length; i++) { @@ -1136,7 +1138,7 @@ else if (caseFrame == RelationsRestrictedCaseFrame.whenDo) } - private static Node createPatNode(Object[][] relNodeSet, RelationsRestrictedCaseFrame caseFrame) { + private static Node createPatNode(Object[][] relNodeSet, RelationsRestrictedCaseFrame caseFrame) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { LinkedList dCables = new LinkedList(); for (int i = 0; i < relNodeSet.length; i++) { dCables.add(new DownCable((Relation) relNodeSet[i][0], (NodeSet) relNodeSet[i][1])); @@ -1188,6 +1190,8 @@ else if (caseFrame == RelationsRestrictedCaseFrame.whenDo) * the case frame implemented by the new closed or proposition node. * * @return the newly created closed or proposition node. + * @throws NodeNotFoundInNetworkException + * @throws NotAPropositionNodeException * * @throws Exception * if the semantic class specified by the case frame was not @@ -1195,7 +1199,7 @@ else if (caseFrame == RelationsRestrictedCaseFrame.whenDo) */ @SuppressWarnings("rawtypes") - private static Node createClosedNode(Object[][] relNodeSet, CaseFrame caseFrame) { + private static Node createClosedNode(Object[][] relNodeSet, CaseFrame caseFrame) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { LinkedList dCables = new LinkedList(); for (int i = 0; i < relNodeSet.length; i++) { dCables.add(new DownCable((Relation) relNodeSet[i][0], (NodeSet) relNodeSet[i][1])); @@ -1231,7 +1235,7 @@ private static Node createClosedNode(Object[][] relNodeSet, CaseFrame caseFrame) } @SuppressWarnings("rawtypes") - private static Node createClosedNode(Object[][] relNodeSet, RelationsRestrictedCaseFrame caseFrame) { + private static Node createClosedNode(Object[][] relNodeSet, RelationsRestrictedCaseFrame caseFrame) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { LinkedList dCables = new LinkedList(); for (int i = 0; i < relNodeSet.length; i++) { dCables.add(new DownCable((Relation) relNodeSet[i][0], (NodeSet) relNodeSet[i][1])); diff --git a/src/sneps/network/PropositionNode.java b/src/sneps/network/PropositionNode.java index f7b36df0..ad703cc7 100644 --- a/src/sneps/network/PropositionNode.java +++ b/src/sneps/network/PropositionNode.java @@ -31,14 +31,14 @@ public class PropositionNode extends Node implements Serializable { private static final long serialVersionUID = 1L; - private Support basicSupport; - + protected Support basicSupport; protected ChannelSet outgoingChannels; protected ChannelSet incomingChannels; protected ReportSet knownInstances; protected ReportSet newInstances; public PropositionNode() { + basicSupport = new Support(); outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); knownInstances = new ReportSet(); @@ -47,6 +47,7 @@ public PropositionNode() { public PropositionNode(Term trm) { super(Semantic.proposition, trm); + basicSupport = new Support(); outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); knownInstances = new ReportSet(); @@ -56,8 +57,8 @@ public PropositionNode(Term trm) { public void processSingleChannelReports(Channel currentChannel) { ReportSet reports = currentChannel.getReportsBuffer(); for (Report currentReport : reports) { - Report alteredReport = new Report(currentReport.getSubstitutions(), currentReport.getSupports(), - currentReport.getSign(), currentReport.getContextName()); + Report alteredReport = new Report(currentReport.getSubstitutions(), currentReport.getSupport(), + currentReport.getSign()); if (knownInstances.contains(alteredReport)) { continue; }else{ diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 92e98fb6..8cedfbd4 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -6,6 +6,8 @@ import java.util.Hashtable; import java.util.Set; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NotAPropositionNodeException; import sneps.network.classes.setClasses.ContextRuisSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; @@ -17,6 +19,7 @@ import sneps.network.classes.term.Open; import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.AntecedentToRuleChannel; import sneps.snip.channels.Channel; @@ -24,13 +27,14 @@ import sneps.snip.channels.RuleToConsequentChannel; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; - private NodeSet consequents; + protected NodeSet consequents; protected NodeSet antecedents; /** @@ -67,9 +71,9 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ * A Hashtable used to map each context to a single RuleUseInfo * that contains all the constant instances that do not dominate variables. */ - private Hashtable contextConstantRUI; + protected RuleUseInfo constantRUI; - public RuleNode() { + public RuleNode() throws NotAPropositionNodeException, NodeNotFoundInNetworkException{ consequents = new NodeSet(); antecedents = new NodeSet(); antNodesWithoutVars = new NodeSet(); @@ -78,11 +82,9 @@ public RuleNode() { antNodesWithVarsIDs = new HashSet(); shareVars = false; sharedVars = new VarNodeSet(); - //contextRuisSet = new ContextRuisSet(); - contextConstantRUI = new Hashtable(); } - public RuleNode(Molecular syn) { + public RuleNode(Molecular syn) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { super(syn); consequents = new NodeSet(); antecedents = new NodeSet(); @@ -92,8 +94,6 @@ public RuleNode(Molecular syn) { antNodesWithVarsIDs = new HashSet(); shareVars = false; sharedVars = new VarNodeSet(); - //contextRuisSet = new ContextRuisSet(); - contextConstantRUI = new Hashtable(); } public NodeSet getConsequents() { @@ -111,6 +111,26 @@ public int getAntSize(){ protected NodeSet getPatternNodes() { return antNodesWithVars; } + + public NodeSet getAntecedents() { + return antecedents; + } + + public NodeSet getAntsWithoutVars() { + return antNodesWithoutVars; + } + + public NodeSet getAntsWithVars() { + return antNodesWithVars; + } + + public void setAntecedents(NodeSet antecedents) { + this.antecedents = antecedents; + } + + public void setConsequents(NodeSet consequents) { + this.consequents = consequents; + } protected void sendReportToConsequents(Report reply) { if(!knownInstances.contains(reply)) @@ -149,9 +169,8 @@ protected void processNodes(NodeSet antNodes) { * The instance that is being reported by the report. */ public void applyRuleHandler(Report report, Node signature) { - //String contextID = report.getContextName(); RuleUseInfo rui; - Collection propSet = report.getSupports(); + Support propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); if (report.isPositive()) { @@ -163,32 +182,49 @@ public void applyRuleHandler(Report report, Node signature) { rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns); } - //RuisHandler crtemp = contextRuisSet.getByContext(contextID); - - // This is the first report received by this RuleNode, so a RuisHandler is - // created - if(ruisHandler == null){ + // This is the first report received by this RuleNode, so + // a RuisHandler is created + if(ruisHandler == null) { ruisHandler = addRuiHandler(); } - - // The RUI created for the given report is inserted to the RuisHandler - RuleUseInfoSet res = ruisHandler.insertRUI(rui); - if (res == null) - res = new RuleUseInfoSet(); - for (RuleUseInfo tRui : res) { - applyRuleOnRui(tRui); + RuleUseInfoSet res = new RuleUseInfoSet(); + if(antNodesWithoutVars.contains(signature)) + addConstantRui(rui); + else { + // The RUI created for the given report is inserted to the RuisHandler + res = ruisHandler.insertRUI(rui); + } + + if (res == null) { + applyRuleOnRui(constantRUI); + return; + } + + if(constantRUI != null) { + RuleUseInfo combined; + for (RuleUseInfo tRui : res) { + combined = tRui.combine(constantRUI); + if(combined != null) + applyRuleOnRui(combined); + } + } + else { + for (RuleUseInfo tRui : res) { + applyRuleOnRui(tRui); + } } } - abstract protected void applyRuleOnRui(RuleUseInfo tRui); + abstract protected RuleResponse applyRuleOnRui(RuleUseInfo tRui); /** + * * Clears all the information saved by this RuleNode about the instances received. */ public void clear() { - //contextRuisSet.clear(); - contextConstantRUI.clear(); + + //contextConstantRUI.clear(); } /** @@ -232,6 +268,20 @@ public VarNodeSet getSharedVarsNodes(NodeSet nodes) { if (nodes.isEmpty()) return res; + + if(nodes.size() == 1) { + if(nodes.getNode(0) instanceof VariableNode) { + res.addVarNode((VariableNode) nodes.getNode(0)); + return res; + } + + if(nodes.getNode(0).getTerm() instanceof Open) { + VarNodeSet free = ((Open) nodes.getNode(0).getTerm()).getFreeVariables(); + for(VariableNode var : free) + res.addVarNode(var); + return res; + } + } for(Node curNode : nodes) { if(curNode instanceof VariableNode) { @@ -263,7 +313,8 @@ public VarNodeSet getSharedVarsNodes(NodeSet nodes) { * @return NodeSet */ public NodeSet getDownNodeSet(String name) { - if(term != null && term instanceof Molecular) + if(term != null && term instanceof Molecular && + ((Molecular) term).getDownCableSet().getDownCable(name) != null) return ((Molecular)term).getDownCableSet().getDownCable(name).getNodeSet(); return null; } @@ -340,7 +391,7 @@ public RuisHandler addRuiHandler() { * @param contextName * @return RuleUseInfoSet */ - protected RuleUseInfoSet createContextRuiHandlerNonShared(String contextName) { + protected RuleUseInfoSet createContextRuiHandlerNonShared() { return new RuleUseInfoSet(false); } @@ -360,27 +411,22 @@ public void splitToNodesWithVarsAndWithout(NodeSet allNodes, NodeSet withVars, N } } - public RuleUseInfo addConstantRuiToContext(String context, RuleUseInfo rui) { - Context contxt = (Context) Controller.getContextByName(context); - RuleUseInfo tRui = contextConstantRUI.get(contxt); - if (tRui != null) - tRui = rui.combine(tRui); + public RuleUseInfo addConstantRui(RuleUseInfo rui) { + //Context contxt = (Context) Controller.getContextByName(context); + //RuleUseInfo tRui = contextConstantRUI.get(contxt); + if (constantRUI != null) + constantRUI = rui.combine(constantRUI); else - tRui = rui; - if (tRui == null) + constantRUI = rui; + if (constantRUI == null) throw new NullPointerException( "The existed RUI could not be merged " + "with the given rui so check your code again"); - contextConstantRUI.put(contxt, tRui); - return tRui; - } - - public RuleUseInfo getConstantRui(Context con) { - RuleUseInfo tRui = contextConstantRUI.get(con); - return tRui; + //contextConstantRUI.put(contxt, tRui); + return constantRUI; } - public RuleUseInfo getConstantRUI(String context) { - return contextConstantRUI.get(context); + public RuleUseInfo getConstantRui() { + return constantRUI; } @Override diff --git a/src/sneps/network/classes/setClasses/FlagNodeSet.java b/src/sneps/network/classes/setClasses/FlagNodeSet.java index 61116f03..10923363 100644 --- a/src/sneps/network/classes/setClasses/FlagNodeSet.java +++ b/src/sneps/network/classes/setClasses/FlagNodeSet.java @@ -94,4 +94,16 @@ public void addAll(FlagNodeSet fns) { for(FlagNode fn : fns) flagNodes.add(fn); } + + public String toString() { + String res = null; + for(FlagNode fn : flagNodes) { + if(res != null) + res += fn.toString() + "\n"; + else + res = fn.toString() + "\n"; + } + + return res; + } } diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 36fe5130..7c09d7ff 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -120,4 +120,17 @@ public boolean isEmpty() { public int size() { return ruis.size(); } + + public void clear() { + ruis.clear(); + } + + public String toString() { + String res = null; + for(RuleUseInfo rui : ruis) { + res = rui.toString() + " "; + } + + return res; + } } diff --git a/src/sneps/network/classes/setClasses/VarNodeSet.java b/src/sneps/network/classes/setClasses/VarNodeSet.java index 88c3a2d0..08d179d9 100644 --- a/src/sneps/network/classes/setClasses/VarNodeSet.java +++ b/src/sneps/network/classes/setClasses/VarNodeSet.java @@ -24,7 +24,7 @@ public Iterator iterator() { return variables.iterator(); } - public VariableNode getVarNode(int index){ + public VariableNode getVarNode(int index) { return variables.get(index); } diff --git a/src/sneps/snebr/Support.java b/src/sneps/snebr/Support.java index 55132e67..029522fa 100644 --- a/src/sneps/snebr/Support.java +++ b/src/sneps/snebr/Support.java @@ -26,6 +26,10 @@ public class Support implements Serializable{ private boolean hasChildren; private boolean TreeComputed; + public Support() { + + } + public Support(int id) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { this.id = id; assumptionBasedSupport = new Hashtable(); diff --git a/src/sneps/snip/InferenceTypes.java b/src/sneps/snip/InferenceTypes.java new file mode 100644 index 00000000..244cc4ec --- /dev/null +++ b/src/sneps/snip/InferenceTypes.java @@ -0,0 +1,5 @@ +package sneps.snip; + +public enum InferenceTypes { + FORWARD, BACKWARD; +} diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index cc33987b..15cd5c55 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -2,40 +2,47 @@ import java.util.Collection; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NotAPropositionNodeException; import sneps.network.classes.setClasses.PropositionSet; +import sneps.snebr.Context; +import sneps.snebr.Support; import sneps.snip.matching.Substitutions; -/** - * @className Report.java - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 - */ public class Report { private Substitutions substitution; - private Collection supports; + private Support support; private boolean sign; - public Report(Substitutions substitution, Collection set, boolean sign) { + public Report(Substitutions substitution, Support suppt, boolean sign) { this.substitution = substitution; - this.supports = set; + this.support = suppt; this.sign = sign; } + public boolean anySupportAssertedInContext(Context reportContext) + throws NotAPropositionNodeException, NodeNotFoundInNetworkException { + Collection reportSupportsSet = support.getAssumptionBasedSupport().values(); + PropositionSet contextHypothesisSet = reportContext.getHypothesisSet(); + for (PropositionSet assumptionHyps : reportSupportsSet) + if (assumptionHyps.isSubSet(contextHypothesisSet)) + return true; + return false; + } + public Substitutions getSubstitutions() { return substitution; } - public Collection getSupports() { - return supports; + public Support getSupport() { + return support; } @Override public boolean equals(Object report) { Report castedReport = (Report) report; - return this.substitution.equals(castedReport.substitution) - && this.sign == castedReport.sign; + return this.substitution.equals(castedReport.substitution) && + this.sign == castedReport.sign; } public boolean getSign() { @@ -50,8 +57,12 @@ public boolean isNegative() { return !sign; } + public void toggleSign() { + this.sign = !this.sign; + } + public String toString() { - return "Sign: " + sign + "\nSubstitution: " + substitution + "\nSupport: " + supports.toString(); + return "Sign: " + sign + "\nSubstitution: " + substitution + "\nSupport: " + support; } -} +} \ No newline at end of file diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index 94611542..5605c726 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -1,13 +1,11 @@ package sneps.snip.classes; -import java.util.Collection; - import sneps.network.Node; -import sneps.network.classes.setClasses.PropositionSet; +import sneps.snebr.Support; public class FlagNode { private Node node; - private Collection supports; + private Support supports; private int flag; /** @@ -20,7 +18,7 @@ public class FlagNode { * @param f, * true or false */ - public FlagNode(Node n, Collection set, int f) { + public FlagNode(Node n, Support set, int f) { node = n; supports = set; flag = f; @@ -45,7 +43,7 @@ public Node getNode() { * * @return support */ - public Collection getSupports() { + public Support getSupports() { return supports; } @@ -69,5 +67,9 @@ public int getFlag() { public boolean isEqual(FlagNode fn) { return fn.node == node && fn.supports == supports && fn.flag == flag; } + + public String toString() { + return "Proposiiton: " + node.toString() + " Flag: " + flag; + } } diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 3ea9a4c4..9bda0e8f 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -14,12 +14,7 @@ * @version 3.00 31/5/2018 */ public abstract class RuisHandler { - private NodeSet positiveNodes; - - public RuisHandler(){ - positiveNodes = new NodeSet(); - } - + /** * Inserts the given RuleUseInfo into this RuisHandler and returns the * RuleUseInfoSet resulted from combining it with the RuleUseInfos in this @@ -30,11 +25,7 @@ public RuisHandler(){ */ abstract public RuleUseInfoSet insertRUI(RuleUseInfo rui); - public NodeSet getPositiveNodes() { - return positiveNodes; - } + abstract public boolean isEmpty(); - public void setPositiveNodes(NodeSet positiveNodes) { - this.positiveNodes = positiveNodes; - } + abstract public void clear(); } diff --git a/src/sneps/snip/classes/RuleResponse.java b/src/sneps/snip/classes/RuleResponse.java new file mode 100644 index 00000000..19fe4353 --- /dev/null +++ b/src/sneps/snip/classes/RuleResponse.java @@ -0,0 +1,44 @@ +package sneps.snip.classes; + +import java.util.ArrayList; + +import sneps.network.classes.setClasses.NodeSet; +import sneps.snip.Report; + +public class RuleResponse { + + private ArrayList reports; + private NodeSet consequents; + + public RuleResponse() { + this.reports = new ArrayList(); + this.consequents = new NodeSet(); + } + + public RuleResponse(ArrayList reports, NodeSet consequents) { + this.reports = reports; + this.consequents = consequents; + } + + public ArrayList getReport() { + return reports; + } + + public void setReport(ArrayList reports) { + this.reports = reports; + } + + public void addReport(Report r) { + reports.add(r); + } + + public NodeSet getConsequents() { + return consequents; + } + + public void setConsequents(NodeSet consequents) { + this.consequents = consequents; + } + + +} diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index 768a8354..89a2ab57 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -1,9 +1,7 @@ package sneps.snip.classes; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.network.classes.setClasses.PropositionSet; +import sneps.snebr.Support; import sneps.snip.matching.Substitutions; /** @@ -20,6 +18,10 @@ public class RuleUseInfo { private int pos; private int neg; private FlagNodeSet fns; + + public RuleUseInfo() { + + } public RuleUseInfo(Substitutions s, int p, int n, FlagNodeSet f) { sub = s; @@ -111,23 +113,14 @@ public boolean isVarsCompatible(RuleUseInfo r) { * @return true or false */ public boolean isJoint(RuleUseInfo r) { - // for (int i = 0; i < fns.cardinality(); i++) { - // for (int j = 0; j < r.getFlagNodeSet().cardinality(); j++) { - // if (fns.getFlagNode(i) - // .getNode() - // .getIdentifier() - // .equals(r.getFlagNodeSet().getFlagNode(j).getNode() - // .getIdentifier())) - // return true; - // } - // } for (FlagNode fn1 : fns) { for (FlagNode fn2 : r.getFlagNodeSet()) { - System.out.println("---->> " + fn1.getNode() + " " + fn1.getNode()); + //System.out.println("---->> " + fn1.getNode() + " " + fn1.getNode()); if (fn1.getNode() == fn2.getNode()) return true; } } + return false; } @@ -159,28 +152,9 @@ public RuleUseInfo combine(RuleUseInfo rui) { return null; } - /** - * Gets the union of supports of each flag node in this RUI's fns - * @return PropositionSet - */ - - /*public PropositionSet getSupports() { - if (fns.isNew()) - return new PropositionSet(); - - if (fns.cardinality() == 1) - return fns.iterator().next().getSupports(); - - PropositionSet res = new PropositionSet(); - for(FlagNode fn : fns){ - try { - res = res.union(fn.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - } - } - - return res; - }*/ + public String toString() { + return "Sub: " + sub.toString() + " Pos: " + pos + " Neg: " + neg + + " Fns: " + fns.toString(); + } } \ No newline at end of file diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index d945357d..0c2d39bc 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -112,5 +112,17 @@ public int getSize() { return map.size(); } + public byte getRuiHandlerType() { + return ruiHandlerType; + } + + public boolean isEmpty() { + return map.isEmpty(); + } + + public void clear() { + map.clear(); + } + } \ No newline at end of file diff --git a/src/sneps/snip/matching/LinearSubstitutions.java b/src/sneps/snip/matching/LinearSubstitutions.java index 3f20fee8..2b1a0a0a 100644 --- a/src/sneps/snip/matching/LinearSubstitutions.java +++ b/src/sneps/snip/matching/LinearSubstitutions.java @@ -461,6 +461,10 @@ public void update(Binding mb , Node mn) } } } + + public int size() { + return sub.size(); + } /*public int termID(int variableID) { diff --git a/src/sneps/snip/matching/Substitutions.java b/src/sneps/snip/matching/Substitutions.java index f6218186..268f39b5 100644 --- a/src/sneps/snip/matching/Substitutions.java +++ b/src/sneps/snip/matching/Substitutions.java @@ -35,6 +35,7 @@ public interface Substitutions { public void unionIn (Substitutions s); public void update(Binding mb , Node mn); public Node value(VariableNode n); + public int size(); } diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 6cfe0caa..0e9933a6 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -1,28 +1,26 @@ package sneps.snip.rules; -import java.util.Collection; +import java.util.ArrayList; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NodeNotFoundInPropSetException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; -import sneps.network.VariableNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; -import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Open; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; -import sneps.snip.classes.PTree; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.matching.Binding; -import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.matching.Substitutions; + /** * @className NumericalEntailment.java * @@ -38,158 +36,127 @@ public class NumericalEntailment extends RuleNode { private static final long serialVersionUID = 3546852401118194013L; private int i; + + // Used for testing + private ArrayList reportsToBeSent; - public NumericalEntailment(Molecular syn) { + public NumericalEntailment(Molecular syn) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { super(syn); // Initializing i NodeSet max = getDownNodeSet("i"); - i = Integer.parseInt(max.getNode(0).getIdentifier()); + if(max != null) + i = Integer.parseInt(max.getNode(0).getIdentifier()); // Initializing the antecedents - antecedents = getDownAntNodeSet(); - processNodes(antecedents); + //antecedents = getDownAntNodeSet(); + //processNodes(antecedents); + + consequents = getDownNodeSet("iconsq"); + + reportsToBeSent = new ArrayList(); } /** * Creates the first RuleUseInfo from a given Report and stores it (if positive) - * Also checks if current number of positive Reports satisfy rule + * Also checks if current number of positive Reports satisfies the rule * @param report * @param signature */ @Override public void applyRuleHandler(Report report, Node signature) { + processNodes(antecedents); + System.out.println("---------------"); if (report.isPositive()) { - Collection propSet = report.getSupports(); + //System.out.println("CREATING POSITIVE RUI"); + Support propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns); + //System.out.println(rui); // Inserting the RuleUseInfo into the RuleNode's RuisHandler: - // SIndex in case there shared variables between the antecedents, or + // SIndex in case there are shared variables between the antecedents, or // RUISet in case there are no shared variables - if(ruisHandler == null){ + if(ruisHandler == null) ruisHandler = addRuiHandler(); - } - ruisHandler.insertRUI(rui); - if(!ruisHandler.getPositiveNodes().contains(signature)) - ruisHandler.getPositiveNodes().addNode(signature); - } - - int curPos = contextRuisSet.getByContext(contxt).getPositiveNodes().size(); - int n = antNodesWithoutVars.size()+antNodesWithVars.size(); - //The second cond will be removed - if ((curPos >= i) && ((curPos < n-i+1) || (curPos < n-1) ) ) - sendSavedRUIs(report.getContextName()); - } - - /** - * Creates a Report from a given RuleUseInfo to be broadcasted to outgoing channels - * @param rui - * @param contextID - */ - @Override - protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { - if (Rui.getPosCount() >= i){ - Substitutions sub = Rui.getSubstitutions(); - FlagNodeSet justification = new FlagNodeSet(); - justification.addAll(Rui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); - - for(FlagNode fn : justification){ - try { - supports = supports.union(fn.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} + RuleResponse response = new RuleResponse(); + response.setConsequents(consequents); + if(antNodesWithoutVars.contains(signature)) { + addConstantRui(rui); + //System.out.println(constantRUI); + if (ruisHandler.isEmpty()) + applyRuleOnRui(constantRUI); + + // What if ruisHandler is not empty? I need to combine constantRUI with + // all the RUIs stored or what? } - - try { - supports = supports.union(Rui.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - - if(this.getTerm() instanceof Open){ - //knownInstances check this.free vars - > bound - VarNodeSet freeVars = ((Open)this.getTerm()).getFreeVariables(); - Substitutions ruiSub = Rui.getSubstitutions(); - boolean allBound = true; - - for(Report report : knownInstances){ - //Bound to same thing(if bound) - for(VariableNode var : freeVars){ - if(!report.getSubstitutions().isBound(var)){ - allBound = false; - break; + else { + // The RUI created for the given report is inserted to the RuisHandler + RuleUseInfoSet res = ruisHandler.insertRUI(rui); + //System.out.println(res); + + if(constantRUI != null) { + RuleUseInfo combined; + for (RuleUseInfo tRui : res) { + combined = tRui.combine(constantRUI); + if(combined != null) { + applyRuleOnRui(combined); } } - if(allBound){//if yes - Substitutions instanceSub = report.getSubstitutions(); - - for(int i = 0; i < ruiSub.cardinality(); i++){ - Binding ruiBind = ruiSub.getBinding(i);//if rui also bound - Binding instanceBind = instanceSub. - getBindingByVariable(ruiBind.getVariable()); - if( !((instanceBind != null) && - (instanceBind.isEqual(ruiBind))) ){ - allBound = false; - break; - } - } - if(allBound){ - //combine known with rui - Substitutions newSub = new LinearSubstitutions(); - newSub.insert(instanceSub); - newSub.insert(ruiSub); - - //add to new support and send - Report reply = new Report(newSub, supports, true, contextID); - sendReportToConsequents(reply); - return; - } + } + else { + for (RuleUseInfo tRui : res) { + applyRuleOnRui(tRui); } } } - - Report reply = new Report(sub, supports, true, contextID); - sendReportToConsequents(reply); } + } - - /** - * Prepares the appropriate SIndex and all its root RuleUseInfo for broadcasting - * @param contextID - */ - private void sendSavedRUIs(String contextID) { - RuleUseInfo addedConstant = getConstantRUI(contextID); - if (addedConstant == null && antNodesWithoutVars.size() != 0) - return; - - if (antNodesWithoutVars.size() != addedConstant.getPosCount()) - return; - - //Should be a RuisHandler in general, not a PTree - RuleUseInfoSet ruis = ((PTree)contextRuisSet.getByContext(contextID)).getAllRootRuis(); - if (ruis == null) { - applyRuleOnRui(addedConstant, contextID); - return; - } - - RuleUseInfo combined; - for (RuleUseInfo info : ruis) { - combined = info.combine(addedConstant); - if (combined != null) - applyRuleOnRui(combined, contextID); + + protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + if(rui.getPosCount() < i) + return null; + + Support replySupport = new Support(); + /*PropositionSet propSet = new PropositionSet(); + for(FlagNode fn : rui.getFlagNodeSet()) + try { + propSet.add(fn.getNode().getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + + try { + propSet.add(this.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); } + + try { + replySupport.addJustificationBasedSupport(propSet); + } catch (NodeNotFoundInPropSetException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + + Report reply = new Report(rui.getSubstitutions(), replySupport, true); + reportsToBeSent.add(reply); + + return new RuleResponse(); } - + /** - * Naming convention used to retrieve Nodes in Down Antecedent position is "iant"; + * Naming convention used to retrieve Nodes in Down Antecedent position is "iant" * "i" for NumericalEntailment, "ant" for Antecedent * @return */ @Override public NodeSet getDownAntNodeSet(){ - return this.getDownNodeSet("&ant"); + return this.getDownNodeSet("iant"); } @Override @@ -214,8 +181,12 @@ public int getI() { * Setter for i * @param newI */ - public void setI(int newI){ + public void setI(int newI) { i = newI; } + + public ArrayList getReplies() { + return reportsToBeSent; + } } diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index f79acaa7..b0f3ef0d 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -13,9 +13,6 @@ import sneps.snip.classes.RuleUseInfo; public class OrEntailment extends RuleNode { - - boolean sign = false; - private int ant,cq; /** diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index c11b0879..a77587b6 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -1,11 +1,11 @@ import java.util.ArrayList; + import java.util.LinkedList; import org.junit.Test; import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -19,13 +19,16 @@ import sneps.network.classes.Relation; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; +import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; @@ -45,7 +48,7 @@ public class NumericalEntailmentTests extends TestCase { private static RuleUseInfo rui; private static Report report; - public void setUp() { + public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkException { try { context = Controller.createContext(contextName); } catch (DuplicateContextNameException e1) { @@ -55,7 +58,7 @@ public void setUp() { LinearSubstitutions sub = new LinearSubstitutions(); FlagNodeSet fns = new FlagNodeSet(); FlagNode fn; - PropositionSet support = new PropositionSet(); + Support support = new Support(); ArrayList wires = new ArrayList(); LinkedList dc = new LinkedList(); LinkedList rels = new LinkedList(); @@ -110,6 +113,8 @@ public void setUp() { } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); + } + LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); DownCable dc1; DownCableSet dcs; @@ -121,7 +126,7 @@ public void setUp() { dc1 = new DownCable(classRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Open("Prop1", dcs)); + prop1 = new Node(new Closed("Prop1", dcs)); dcList.clear(); //------------------------------------------------------------// nodeSet1.clear(); nodeSet1.addNode(dog); @@ -131,7 +136,7 @@ public void setUp() { dc1 = new DownCable(doesRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Open("Prop2", dcs)); + prop2 = new Node(new Closed("Prop2", dcs)); dcList.clear(); //------------------------------------------------------------// nodeSet1.clear(); nodeSet1.addNode(var); @@ -140,8 +145,9 @@ public void setUp() { nodeSet1.clear(); nodeSet1.addNode(dog); dc1 = new DownCable(classRel, nodeSet1); dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); + dcs = new DownCableSet(dcList, caseFrameMC); prop3 = new Node(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// nodeSet1.clear(); nodeSet1.addNode(fido); @@ -151,41 +157,50 @@ public void setUp() { dc1 = new DownCable(doesRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Open("Prop4", dcs)); + prop4 = new Node(new Closed("Prop4", dcs)); dcList.clear(); //------------------------------------------------------------// - } //------------------------------------- Numerical Supports ----------------------------------------------// - try { + /*try { support.add(prop1.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(prop1, support, 1); + assumptionBasedSupport.put("key", support); + fn = new FlagNode(prop1, assumptionBasedSupport.values(), 1); fns.insert(fn); support.clearSet(); + try { support.add(prop2.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(prop2, support, 1); + assumptionBasedSupport.clear(); + assumptionBasedSupport.put("key", support); + fn = new FlagNode(prop2, assumptionBasedSupport.values(), 1); fns.insert(fn); support.clearSet(); + try { support.add(prop3.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(prop3, support, 1); + support.clearSet(); + assumptionBasedSupport.clear(); + assumptionBasedSupport.put("key", support); + fn = new FlagNode(prop3, assumptionBasedSupport.values(), 1); fns.insert(fn); + + support.clearSet();*/ nodeSet.addNode(prop1); dc.add(new DownCable(antsRel, nodeSet)); @@ -202,97 +217,93 @@ public void setUp() { nodeSet.addNode(prop4); dc.add(new DownCable(consRel, nodeSet)); - DownCableSet dcs = new DownCableSet(dc, caseFrameAC); + + DownCableSet dcss = new DownCableSet(dc, caseFrameAC); + //System.out.println(dcss.getDownCable("iant")); + //System.out.println(dcss.getDownCable("iconsq")); + NodeSet a = new NodeSet(); + a.addNode(prop1); + a.addNode(prop2); + a.addNode(prop3); + + NodeSet c = new NodeSet(); + c.addNode(prop4); //------------------------------------- AND ----------------------------------------------// - numerical = new NumericalEntailment(new Open("Open", dcs)); + numerical = new NumericalEntailment(new Open("Open", dcss)); + //System.out.println(numerical.getId()); numerical.setI(1); - - try { - support.add(dog.getId()); - support.add(fido.getId()); - support.add(var.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - - sub.insert(new Binding((VariableNode) var,fido)); - rui = new RuleUseInfo(sub, 1, 0, fns); - report = new Report(sub, support, true, contextName); + numerical.setAntecedents(a); + numerical.setConsequents(c); + + sub = new LinearSubstitutions(); + fns = new FlagNodeSet(); + fn = new FlagNode(prop1, new Support(), 1); + fns.insert(fn); + report = new Report(sub, new Support(), true); } + /*@Test + public void testProcessNodes() { + assertEquals(1, numerical.getConsequents().size()); + assertEquals(3, numerical.getAntecedents().size()); + assertEquals(2, numerical.getAntsWithoutVars().size()); + assertEquals(1, numerical.getAntsWithVars().size()); + }*/ + @Test public void testApplyRuleHandler() { - numerical.setKnownInstances(numerical.getNewInstances()); - numerical.getNewInstances().clear(); - - numerical.applyRuleHandler(report, fido); - if(numerical.getAntSize() <= 1) - assertNotNull("NumericalEntailment: ApplyRuleHandler doesn't broadcast reports", - numerical.getNewInstances()); - else - assertTrue("NumericalEntailment: ApplyRuleHandler broacdcasts final report without waiting for enough positive antecedents reports", - numerical.getNewInstances().isEmpty()); - - - numerical.setKnownInstances(numerical.getNewInstances()); - numerical.getNewInstances().clear(); + numerical.applyRuleHandler(report, prop1); + + assertEquals(1, numerical.getReplies().size()); + assertEquals(numerical.getReplies().get(0), report); + } + + @Test + public void testApplyRuleHandler2() { + numerical.setI(2); + numerical.getReplies().clear(); + + numerical.applyRuleHandler(report, prop1); + + assertEquals(0, numerical.getReplies().size()); + } + + @Test + public void testApplyRuleHandler3() { + numerical.setI(2); + numerical.getReplies().clear(); + + numerical.applyRuleHandler(report, prop1); + + assertEquals(0, numerical.getReplies().size()); + assertNotNull("NumericalEntailment: Null RuisHandler", numerical.getRuisHandler()); + assertTrue("NumericalEntailment: addRuiHandler doesn't create an SIndex as a RuisHandler", + numerical.getRuisHandler() instanceof SIndex); + assertEquals(SIndex.SINGLETON, ((SIndex) (numerical.getRuisHandler())).getRuiHandlerType()); + LinearSubstitutions sub = new LinearSubstitutions(); + sub.putIn(new Binding((VariableNode) var, fido)); FlagNodeSet fns = new FlagNodeSet(); - PropositionSet support = new PropositionSet(); - - try { - support.add(dog.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - - FlagNode fn = new FlagNode(dog, support, 1); + FlagNode fn = new FlagNode(prop3, new Support(), 1); fns.insert(fn); - report = new Report(sub, support, true, contextName); + rui = new RuleUseInfo(sub, 1, 0, fns); + report = new Report(sub, new Support(), true); - numerical.applyRuleHandler(report, dog); - if(numerical.getAntSize() >= numerical.getI()) - assertTrue( - "NumericalEntailment: ApplyRuleHandler doesn't broadcast reports", - numerical.getNewInstances().isEmpty()); + numerical.applyRuleHandler(report, prop3); + assertEquals(1, numerical.getReplies().size()); } @Test - public void testGetDownAntNodeSet() { + public void testGetDownNodeSet() { NodeSet downAntNodeSet = numerical.getDownAntNodeSet(); assertNotNull("NumericalEntailment: getDownAntNodeSet retuns null", downAntNodeSet); assertFalse("NumericalEntailment: getDownAntNodeSet retuns an empty NodeSet", downAntNodeSet.isEmpty()); - } - - @Test - public void testCreateRuisHandler() { - numerical.createRuisHandler(contextName); - RuisHandler handler = numerical.getContextRuiHandler(contextName); - assertNotNull("NumericalEntailment: CreateRuisHandler creats a null RuisHandler", - handler); - assertTrue("NumericalEntailment: CreateRuisHandler doesn't create an SIndex as a Handler", - handler instanceof SIndex); - } - - @Test - public void testAddNotSentRui() { - numerical.addNotSentRui(rui, contextName, dog); - assertNotNull("NumericalEntailment: addNotSentRui doesn't add a RuiHandler in contextRuisHandlers", - numerical.getContextRuiHandler(contextName)); - - NodeSet positives = numerical.getContextRuiHandler(contextName).getPositiveNodes(); - assertTrue("NumericalEntailment: addNotSentRui doesn't add signature to positiveNodes set", - positives.contains(dog)); - assertTrue("NumericalEntailment: addNotSentRui adds wrong signatures to positiveNodes set", - !positives.contains(fido)); - - assertTrue("AndEntailment: addNotSentRui doesn't add an SIndex in contextRuisHandlers", - numerical.getContextRuiHandler(contextName)instanceof SIndex); - + //assertEquals(1, numerical.getDownNodeSet("iconsq")); + //assertEquals(3, numerical.getDownAntNodeSet()); } public void tearDown(){ diff --git a/tests/SIndexTest.java b/tests/SIndexTest.java index 7f951bd7..10decb5a 100644 --- a/tests/SIndexTest.java +++ b/tests/SIndexTest.java @@ -3,6 +3,8 @@ import java.util.ArrayList; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.PropositionNode; import sneps.network.VariableNode; @@ -22,7 +24,7 @@ public class SIndexTest { @Test - public void newSIndex() { + public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetworkException { /** * Creating Nodes @@ -99,6 +101,7 @@ public void newSIndex() { vns5.addVarNode(vn5); vns6.addVarNode(vn6); vns7.addVarNode(vn7); + //RUI set SIndex SIndex index = new SIndex((byte) 0, vns); From 7a22146b2676dfd1cae0b9c08a7a17e49627f431 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sat, 11 May 2019 05:15:32 +0200 Subject: [PATCH 11/22] Basic implementation of OrEntailment --- src/sneps/snip/rules/NumericalEntailment.java | 1 + src/sneps/snip/rules/OrEntailment.java | 85 +++++---- tests/NumericalEntailmentTests.java | 46 +---- tests/OrTest.java | 175 ++++++------------ 4 files changed, 115 insertions(+), 192 deletions(-) diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 0e9933a6..6e6b5af4 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -51,6 +51,7 @@ public NumericalEntailment(Molecular syn) throws NotAPropositionNodeException, N //antecedents = getDownAntNodeSet(); //processNodes(antecedents); + // Initializing the consequents consequents = getDownNodeSet("iconsq"); reportsToBeSent = new ArrayList(); diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index b0f3ef0d..255de151 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -1,71 +1,86 @@ package sneps.snip.rules; +import java.util.ArrayList; + +import sneps.exceptions.DuplicatePropositionException; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NodeNotFoundInPropSetException; +import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; -import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.term.Molecular; +import sneps.snebr.Support; import sneps.snip.Report; -import sneps.snip.channels.Channel; -import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; public class OrEntailment extends RuleNode { + private static final long serialVersionUID = 1L; + // Used for testing + private ArrayList reportsToBeSent; - /** - *Constructor for the Or Entailment - * @param syn - */ - public OrEntailment(Molecular syn) { + public OrEntailment(Molecular syn) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { super(syn); + + // Initializing the antecedents + antecedents = getDownAntNodeSet(); + processNodes(antecedents); + + // Initializing the consequents + consequents = getDownNodeSet("Vconsq"); + + reportsToBeSent = new ArrayList(); } - /** - * Checks if the report received is true - * If yes, the report is sent to the ants with the true report - */ public void applyRuleHandler(Report report, Node node) { - if(report.isPositive()) { + Support replySupport = new Support(); + /*PropositionSet propSet = new PropositionSet(); + try { + propSet.add(node.getId()); + propSet.add(this.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + try { + replySupport.addJustificationBasedSupport(propSet); + } catch (NodeNotFoundInPropSetException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ - sign = true; - - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(node, propSet, 1)); - - Report reply = new Report(report.getSubstitutions(), propSet, sign, report.getContextName()); - - for (Channel outChannel : outgoingChannels) - outChannel.addReport(reply); - + Report reply = new Report(report.getSubstitutions(), replySupport, true); + reportsToBeSent.add(reply); } - -} + } + + @Override + protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { + return null; + } - - @Override public NodeSet getDownAntNodeSet() { return this.getDownNodeSet("Vant"); } @Override - protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { - + protected RuisHandler createRuisHandler() { + return null; } @Override - protected RuisHandler createRuisHandler(String contextName) { - // TODO Auto-generated method stub - return null; + protected byte getSIndexType() { + return 0; } - public boolean getReply() { - return sign; + public ArrayList getSentReports() { + return reportsToBeSent; } } diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index a77587b6..d6127ed0 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -93,8 +93,9 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); - var = new VariableNode(new Variable("X")); } + + var = new VariableNode(new Variable("X")); //------------------------------------- prop1, prop2, prop3, prop4 ----------------------------------------------// @@ -161,46 +162,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx dcList.clear(); //------------------------------------------------------------// -//------------------------------------- Numerical Supports ----------------------------------------------// - - /*try { - support.add(prop1.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - assumptionBasedSupport.put("key", support); - fn = new FlagNode(prop1, assumptionBasedSupport.values(), 1); - fns.insert(fn); - - support.clearSet(); - - try { - support.add(prop2.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - assumptionBasedSupport.clear(); - assumptionBasedSupport.put("key", support); - fn = new FlagNode(prop2, assumptionBasedSupport.values(), 1); - fns.insert(fn); - - support.clearSet(); - - try { - support.add(prop3.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - support.clearSet(); - assumptionBasedSupport.clear(); - assumptionBasedSupport.put("key", support); - fn = new FlagNode(prop3, assumptionBasedSupport.values(), 1); - fns.insert(fn); - - support.clearSet();*/ +//------------------------------------- Numerical ----------------------------------------------// nodeSet.addNode(prop1); dc.add(new DownCable(antsRel, nodeSet)); @@ -229,7 +191,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx NodeSet c = new NodeSet(); c.addNode(prop4); -//------------------------------------- AND ----------------------------------------------// +//------------------------------------- NUMERICAL ----------------------------------------------// numerical = new NumericalEntailment(new Open("Open", dcss)); //System.out.println(numerical.getId()); diff --git a/tests/OrTest.java b/tests/OrTest.java index 8220b9ee..63196d19 100644 --- a/tests/OrTest.java +++ b/tests/OrTest.java @@ -24,6 +24,7 @@ import sneps.network.classes.RelationsRestrictedCaseFrame; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Term; @@ -33,6 +34,7 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuleUseInfo; @@ -49,30 +51,11 @@ public class OrTest { private static Node fido, var, dog, barks; private static Node prop1, prop2, prop3, prop4; private static RuleUseInfo rui; - private static Report report; + private static Report report, report1; @BeforeClass public static void BuildNetwork() throws Exception { - /** - * build context - */ - try { - context = Controller.createContext(contextName); - } catch (DuplicateContextNameException e1) { - assertNotNull(e1.getMessage(), e1); - } - - /** - * Create substitutions, - * FlagNodeSet, - * FlagNode, - * PropositionSet, - * wires, - * relations, - * caseFrames - */ - LinearSubstitutions sub = new LinearSubstitutions(); FlagNodeSet fns = new FlagNodeSet(); FlagNode fn; @@ -98,7 +81,6 @@ public static void BuildNetwork() throws Exception { rels.clear(); - /** * Building propositions & base nodes, * adding wires @@ -116,11 +98,9 @@ public static void BuildNetwork() throws Exception { } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); - var = new VariableNode(new Variable("X")); } - - + var = new VariableNode(new Variable("X")); try { wires.clear(); wires.add(wire1); wires.add(wire2); @@ -137,82 +117,53 @@ public static void BuildNetwork() throws Exception { } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); - LinkedList dcList = new LinkedList(); - NodeSet nodeSet1 = new NodeSet(); - DownCable dc1; DownCableSet dcs; - - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Open("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Open("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Open("Prop4", dcs)); - dcList.clear(); } + LinkedList dcList = new LinkedList(); + NodeSet nodeSet1 = new NodeSet(); + DownCable dc1; DownCableSet dcs; + + nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop1 = new Node(new Closed("Prop1", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameCD); + prop2 = new Node(new Closed("Prop2", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new Node(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMD); + prop4 = new Node(new Closed("Prop4", dcs)); + dcList.clear(); - - - try { - support.add(prop1.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop1, support, 1); - fns.insert(fn); - - support.clearSet(); - try { - support.add(prop2.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop2, support, 1); - fns.insert(fn); - - support.clearSet(); - try { - support.add(prop3.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop3, support, 1); - fns.insert(fn); nodeSet.addNode(prop1); dc.add(new DownCable(antsRel, nodeSet)); @@ -229,35 +180,29 @@ public static void BuildNetwork() throws Exception { nodeSet.addNode(prop4); dc.add(new DownCable(consRel, nodeSet)); - DownCableSet dcs = new DownCableSet(dc, caseFrameAC); + DownCableSet dcss = new DownCableSet(dc, caseFrameAC); /** - * Or-entailment + * Or-Entailment */ - or = new OrEntailment(new Open("Open", dcs)); - - try { - support.add(dog.getId()); - support.add(fido.getId()); - support.add(var.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } + or = new OrEntailment(new Open("Open", dcss)); - sub.insert(new Binding((VariableNode) var,fido)); - rui = new RuleUseInfo(sub, 1, 0, fns); - report = new Report(sub, support, true, contextName); + sub.insert(new Binding((VariableNode) var, fido)); + LinearSubstitutions s = new LinearSubstitutions(); + report = new Report(sub, new Support(), true); + report1 = new Report(s, new Support(), true); } @Test - public void applyRuleHandler() { + public void testApplyRuleHandler() { + or.applyRuleHandler(report, prop3); + assertEquals(1, or.getSentReports().size()); - or.applyRuleHandler(report,fido); - assertEquals(true,or.getReply()); + or.applyRuleHandler(report1, prop1); + assertEquals(2, or.getSentReports().size()); } From 3d0145b520ba468a6cd87c204765634f0bed3917 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Wed, 15 May 2019 00:01:22 +0200 Subject: [PATCH 12/22] Change support in Report, and create outgoingChannels for sending reports --- src/sneps/network/PropositionNode.java | 56 ++- src/sneps/network/RuleNode.java | 25 +- .../classes/setClasses/ChannelSet.java | 82 +++- .../classes/setClasses/PropositionSet.java | 2 +- .../network/classes/setClasses/ReportSet.java | 5 +- .../classes/setClasses/RuleUseInfoSet.java | 12 +- src/sneps/snip/Report.java | 58 ++- src/sneps/snip/channels/Channel.java | 5 + src/sneps/snip/classes/FlagNode.java | 19 +- src/sneps/snip/classes/RuisHandler.java | 3 +- src/sneps/snip/classes/RuleResponse.java | 50 ++- src/sneps/snip/classes/RuleUseInfo.java | 26 +- src/sneps/snip/classes/SIndex.java | 13 + src/sneps/snip/rules/AndOrEntailment.java | 221 +--------- src/sneps/snip/rules/NumericalEntailment.java | 203 +++++---- src/sneps/snip/rules/OrEntailment.java | 70 ++- tests/NumericalEntailmentTests.java | 82 ++-- tests/{OrTest.java => OrTests.java} | 398 +++++++++--------- 18 files changed, 671 insertions(+), 659 deletions(-) rename tests/{OrTest.java => OrTests.java} (76%) diff --git a/src/sneps/network/PropositionNode.java b/src/sneps/network/PropositionNode.java index ad703cc7..fe0e262f 100644 --- a/src/sneps/network/PropositionNode.java +++ b/src/sneps/network/PropositionNode.java @@ -15,8 +15,11 @@ import sneps.exceptions.NotAPropositionNodeException; import java.util.Hashtable; +import java.util.Set; +import sneps.snebr.Context; import sneps.snebr.Support; +import sneps.snip.InferenceTypes; import sneps.snip.Pair; import sneps.snip.Report; import sneps.snip.Runner; @@ -38,7 +41,6 @@ public class PropositionNode extends Node implements Serializable { protected ReportSet newInstances; public PropositionNode() { - basicSupport = new Support(); outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); knownInstances = new ReportSet(); @@ -47,7 +49,6 @@ public PropositionNode() { public PropositionNode(Term trm) { super(Semantic.proposition, trm); - basicSupport = new Support(); outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); knownInstances = new ReportSet(); @@ -58,7 +59,7 @@ public void processSingleChannelReports(Channel currentChannel) { ReportSet reports = currentChannel.getReportsBuffer(); for (Report currentReport : reports) { Report alteredReport = new Report(currentReport.getSubstitutions(), currentReport.getSupport(), - currentReport.getSign()); + currentReport.getSign(), InferenceTypes.BACKWARD); if (knownInstances.contains(alteredReport)) { continue; }else{ @@ -205,43 +206,88 @@ public boolean alreadyWorking(Channel channel) { public Support getBasicSupport() { return basicSupport; } + public void setBasicSupport() throws NotAPropositionNodeException, NodeNotFoundInNetworkException { this.basicSupport = new Support(this.getId()); } + public ChannelSet getOutgoingChannels() { return outgoingChannels; } + public void setOutgoingChannels(ChannelSet outgoingChannels) { this.outgoingChannels = outgoingChannels; } + public ChannelSet getIncomingChannels() { return incomingChannels; } + public void setIncomingChannels(ChannelSet incomingChannels) { this.incomingChannels = incomingChannels; } + public ReportSet getKnownInstances() { return knownInstances; } + public void setKnownInstances(ReportSet knownInstances) { this.knownInstances = knownInstances; } + public Hashtable getAssumptionBasedSupport() { return basicSupport.getAssumptionBasedSupport(); } + public Hashtable getJustificationSupport() { return basicSupport.getJustificationSupport(); } + public void addJustificationBasedSupport(PropositionSet propSet) throws NodeNotFoundInPropSetException, NotAPropositionNodeException, NodeNotFoundInNetworkException{ basicSupport.addJustificationBasedSupport(propSet); } + public boolean removeNodeFromSupports(PropositionNode propNode) { - return basicSupport.removeNodeFromSupports(propNode); - + return basicSupport.removeNodeFromSupports(propNode); } public ReportSet getNewInstances() { return newInstances; } + + protected Set getOutgoingAntecedentRuleChannels() { + return outgoingChannels.getAntRuleChannels(); + } + + protected Set getOutgoingRuleConsequentChannels() { + return outgoingChannels.getRuleConsChannels(); + } + + protected Set getOutgoingMatchChannels() { + return outgoingChannels.getMatchChannels(); + } + + protected Set getIncomingAntecedentRuleChannels() { + return incomingChannels.getAntRuleChannels(); + } + + protected Set getIncomingRuleConsequentChannels() { + return incomingChannels.getRuleConsChannels(); + } + + protected Set getIncomingMatchChannels() { + return incomingChannels.getMatchChannels(); + } + + public boolean assertedInContext(Context reportContext) { + // TODO Auto-generated method stub + return false; + } + + public Channel establishChannel(ChannelTypes rulecons, Node n, LinearSubstitutions linearSubstitutions, + LinearSubstitutions linearSubstitutions2, Context currentContext, int j) { + // TODO Auto-generated method stub + return null; + } } diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 8cedfbd4..aaa38f87 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -1,6 +1,7 @@ package sneps.network; import java.io.Serializable; +import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.util.Hashtable; @@ -30,6 +31,7 @@ import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; +import sneps.snip.matching.LinearSubstitutions; public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; @@ -73,7 +75,7 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ */ protected RuleUseInfo constantRUI; - public RuleNode() throws NotAPropositionNodeException, NodeNotFoundInNetworkException{ + public RuleNode() { consequents = new NodeSet(); antecedents = new NodeSet(); antNodesWithoutVars = new NodeSet(); @@ -84,7 +86,7 @@ public RuleNode() throws NotAPropositionNodeException, NodeNotFoundInNetworkExce sharedVars = new VarNodeSet(); } - public RuleNode(Molecular syn) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { + public RuleNode(Molecular syn) { super(syn); consequents = new NodeSet(); antecedents = new NodeSet(); @@ -145,7 +147,7 @@ protected void sendReportToConsequents(Report reply) { * * @param antNodes */ - protected void processNodes(NodeSet antNodes) { + public void processNodes(NodeSet antNodes) { this.splitToNodesWithVarsAndWithout(antNodes, antNodesWithVars, antNodesWithoutVars); for (Node n : antNodesWithVars) { antNodesWithVarsIDs.add(n.getId()); @@ -167,8 +169,9 @@ protected void processNodes(NodeSet antNodes) { * @param report * @param signature * The instance that is being reported by the report. + * @return */ - public void applyRuleHandler(Report report, Node signature) { + /*public ArrayList applyRuleHandler(Report report, Node signature) { RuleUseInfo rui; Support propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); @@ -214,17 +217,17 @@ public void applyRuleHandler(Report report, Node signature) { applyRuleOnRui(tRui); } } - } + }*/ - abstract protected RuleResponse applyRuleOnRui(RuleUseInfo tRui); + abstract protected Report applyRuleOnRui(RuleUseInfo tRui); /** * * Clears all the information saved by this RuleNode about the instances received. */ public void clear() { - - //contextConstantRUI.clear(); + if(ruisHandler != null) + ruisHandler.clear(); } /** @@ -326,6 +329,8 @@ public NodeSet getDownNodeSet(String name) { * @return NodeSet */ public abstract NodeSet getDownAntNodeSet(); + + public abstract NodeSet getDownConsqNodeSet(); public NodeSet getUpNodeSet(String name) { return this.getUpCableSet().getUpCable(name).getNodeSet(); @@ -428,6 +433,8 @@ public RuleUseInfo addConstantRui(RuleUseInfo rui) { public RuleUseInfo getConstantRui() { return constantRUI; } + + protected abstract Set getOutgoingChannelsForReport(Report r); @Override public void processRequests() { @@ -474,7 +481,7 @@ public void processReports() { ReportSet channelReports = currentChannel.getReportsBuffer(); for (Report currentReport : channelReports) { if (currentChannel instanceof AntecedentToRuleChannel) { - applyRuleHandler(currentReport, currentChannel.getReporter()); + //applyRuleHandler(currentReport, currentChannel.getReporter()); } } currentChannel.clearReportsBuffer(); diff --git a/src/sneps/network/classes/setClasses/ChannelSet.java b/src/sneps/network/classes/setClasses/ChannelSet.java index f9d3b4b9..c78a1e46 100644 --- a/src/sneps/network/classes/setClasses/ChannelSet.java +++ b/src/sneps/network/classes/setClasses/ChannelSet.java @@ -1,34 +1,82 @@ package sneps.network.classes.setClasses; +import java.util.Collection; +import java.util.HashSet; +import java.util.Hashtable; import java.util.Iterator; +import java.util.Set; +import sneps.snip.channels.AntecedentToRuleChannel; import sneps.snip.channels.Channel; +import sneps.snip.channels.ChannelTypes; +import sneps.snip.channels.RuleToConsequentChannel; -import java.io.Serializable; -import java.util.HashSet; - -/** - * @className ChannelSet.java - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 - */ -public class ChannelSet implements Iterable, Serializable { - private static final long serialVersionUID = -7019589602404627084L; - private HashSet channels; +public class ChannelSet implements Iterable { + private Hashtable> channels; public ChannelSet() { - channels = new HashSet(); + channels = new Hashtable>(); + } + + public void addChannel(Channel channel) { + ChannelTypes channelType; + if (channel instanceof AntecedentToRuleChannel) + channelType = ChannelTypes.RuleAnt; + else if (channel instanceof RuleToConsequentChannel) + channelType = ChannelTypes.RuleCons; + else + channelType = ChannelTypes.MATCHED; + Set targetSet = channels.get(channelType); + targetSet.add(channel); + channels.put(channelType, targetSet); } @Override public Iterator iterator() { - return channels.iterator(); + Set allMergedChannels = new HashSet(); + Collection> collectionOfSets = channels.values(); + for (Set set : collectionOfSets) + allMergedChannels.addAll(set); + return allMergedChannels.iterator(); + } + + /*** + * Method acting as a filter for quick HashSet filtering applied on channels + * based on request processing status. + * + * @param processedRequest boolean expressing filter criteria + * @return newly created ChannelSet + */ + public ChannelSet getFilteredRequestChannels(boolean processedRequest) { + ChannelSet processedRequestsChannels = new ChannelSet(); + Set allMergedChannels = new HashSet(); + Collection> collectionOfSets = channels.values(); + for (Set set : collectionOfSets) + allMergedChannels.addAll(set); + for (Channel channel : allMergedChannels) { + if (channel.isRequestProcessed() == processedRequest) + processedRequestsChannels.addChannel(channel); + } + return processedRequestsChannels; + } + + public Set getChannels() { + Set allMergedChannels = new HashSet(); + Collection> collectionOfSets = channels.values(); + for (Set set : collectionOfSets) + allMergedChannels.addAll(set); + return allMergedChannels; } - public void addChannel(Channel newChannel) { - channels.add(newChannel); + public Set getAntRuleChannels() { + return channels.get(ChannelTypes.RuleAnt); } + public Set getRuleConsChannels() { + return channels.get(ChannelTypes.RuleCons); + } + + public Set getMatchChannels() { + return channels.get(ChannelTypes.MATCHED); + } } diff --git a/src/sneps/network/classes/setClasses/PropositionSet.java b/src/sneps/network/classes/setClasses/PropositionSet.java index f9d9ac02..baaa1936 100644 --- a/src/sneps/network/classes/setClasses/PropositionSet.java +++ b/src/sneps/network/classes/setClasses/PropositionSet.java @@ -89,7 +89,7 @@ public static int[] removeDuplicates(int[] props) { * * @return an int array containing the props */ - private int[] getProps() { + public int[] getProps() { return props; } diff --git a/src/sneps/network/classes/setClasses/ReportSet.java b/src/sneps/network/classes/setClasses/ReportSet.java index 3abadb94..8a910331 100644 --- a/src/sneps/network/classes/setClasses/ReportSet.java +++ b/src/sneps/network/classes/setClasses/ReportSet.java @@ -21,12 +21,12 @@ public ReportSet() { reports = new HashSet(); } - public void addReport(Report rport){ + public void addReport(Report rport) { reports.add(rport); } @Override - public Iterator iterator(){ + public Iterator iterator() { return reports.iterator(); } @@ -37,7 +37,6 @@ public boolean contains(Report report) { public void clear() { reports = new HashSet(); } - public boolean isEmpty() { return reports.isEmpty(); diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 7c09d7ff..f8e22f45 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -51,7 +51,8 @@ public RuleUseInfoSet insertRUI(RuleUseInfo rui) { if(isEmpty()) return add(rui); - return combine(rui); + ruis = combine(rui).getRuis(); + return this; } /** @@ -125,6 +126,10 @@ public void clear() { ruis.clear(); } + public HashSet getRuis() { + return ruis; + } + public String toString() { String res = null; for(RuleUseInfo rui : ruis) { @@ -133,4 +138,9 @@ public String toString() { return res; } + + @Override + public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui) { + return combine(rui); + } } diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index 15cd5c55..a551ca83 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -1,40 +1,49 @@ package sneps.snip; -import java.util.Collection; - import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; +import sneps.network.Network; +import sneps.network.PropositionNode; import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; -import sneps.snebr.Support; import sneps.snip.matching.Substitutions; public class Report { private Substitutions substitution; - private Support support; + + /** + * Contains the id of the proposition node denoting the actual instance + * represented by this Report + */ + private PropositionSet support; private boolean sign; + private InferenceTypes inferenceType; - public Report(Substitutions substitution, Support suppt, boolean sign) { - this.substitution = substitution; - this.support = suppt; - this.sign = sign; - } + public Report(Substitutions substitution, PropositionSet suppt, boolean sign, InferenceTypes inference) { + this.substitution = substitution; + this.support = suppt; + this.sign = sign; + this.inferenceType = inference; + } - public boolean anySupportAssertedInContext(Context reportContext) - throws NotAPropositionNodeException, NodeNotFoundInNetworkException { - Collection reportSupportsSet = support.getAssumptionBasedSupport().values(); - PropositionSet contextHypothesisSet = reportContext.getHypothesisSet(); - for (PropositionSet assumptionHyps : reportSupportsSet) - if (assumptionHyps.isSubSet(contextHypothesisSet)) - return true; - return false; - } + public boolean anySupportAssertedInContext(Context reportContext) + throws NotAPropositionNodeException, NodeNotFoundInNetworkException { + int[] reportSupportsSet = support.getProps(); + int currentPropNodeId; + for (int i = 0; i < reportSupportsSet.length; i++) { + currentPropNodeId = reportSupportsSet[i]; + PropositionNode retrievedNode = (PropositionNode) Network.getNodeById(currentPropNodeId); + if (retrievedNode.assertedInContext(reportContext)) + return true; + } + return false; + } public Substitutions getSubstitutions() { return substitution; } - public Support getSupport() { + public PropositionSet getSupport() { return support; } @@ -62,7 +71,16 @@ public void toggleSign() { } public String toString() { - return "Sign: " + sign + "\nSubstitution: " + substitution + "\nSupport: " + support; + return "Sign: " + sign + "\nSubstitution: " + substitution + "\nSupport: " + + support + "\nType: " + inferenceType; } + + public InferenceTypes getInferenceType() { + return inferenceType; + } + + public void setInferenceType(InferenceTypes inferenceType) { + this.inferenceType = inferenceType; + } } \ No newline at end of file diff --git a/src/sneps/snip/channels/Channel.java b/src/sneps/snip/channels/Channel.java index ceed8acb..290e9203 100644 --- a/src/sneps/snip/channels/Channel.java +++ b/src/sneps/snip/channels/Channel.java @@ -80,4 +80,9 @@ public void setValve(boolean valve) { public void clearReportsBuffer() { reportsBuffer.clear(); } + + public boolean isRequestProcessed() { + // TODO Auto-generated method stub + return false; + } } diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index 5605c726..60ff0f84 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -1,11 +1,11 @@ package sneps.snip.classes; import sneps.network.Node; -import sneps.snebr.Support; +import sneps.network.classes.setClasses.PropositionSet; public class FlagNode { private Node node; - private Support supports; + private PropositionSet support; private int flag; /** @@ -18,14 +18,9 @@ public class FlagNode { * @param f, * true or false */ - public FlagNode(Node n, Support set, int f) { - node = n; - supports = set; - flag = f; - } - - public FlagNode(Node n, int f) { + public FlagNode(Node n, PropositionSet set, int f) { node = n; + support= set; flag = f; } @@ -43,8 +38,8 @@ public Node getNode() { * * @return support */ - public Support getSupports() { - return supports; + public PropositionSet getSupport() { + return support; } /** @@ -65,7 +60,7 @@ public int getFlag() { * @return true or false */ public boolean isEqual(FlagNode fn) { - return fn.node == node && fn.supports == supports && fn.flag == flag; + return fn.node == node && fn.support == support && fn.flag == flag; } public String toString() { diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 9bda0e8f..37049e7b 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -1,6 +1,5 @@ package sneps.snip.classes; -import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.RuleUseInfoSet; /** @@ -25,6 +24,8 @@ public abstract class RuisHandler { */ abstract public RuleUseInfoSet insertRUI(RuleUseInfo rui); + abstract public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui); + abstract public boolean isEmpty(); abstract public void clear(); diff --git a/src/sneps/snip/classes/RuleResponse.java b/src/sneps/snip/classes/RuleResponse.java index 19fe4353..9dda975d 100644 --- a/src/sneps/snip/classes/RuleResponse.java +++ b/src/sneps/snip/classes/RuleResponse.java @@ -1,44 +1,48 @@ package sneps.snip.classes; -import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; -import sneps.network.classes.setClasses.NodeSet; import sneps.snip.Report; +import sneps.snip.channels.Channel; public class RuleResponse { - private ArrayList reports; - private NodeSet consequents; + private Report report; + private Set consequentChannels; public RuleResponse() { - this.reports = new ArrayList(); - this.consequents = new NodeSet(); - } - - public RuleResponse(ArrayList reports, NodeSet consequents) { - this.reports = reports; - this.consequents = consequents; + consequentChannels = new HashSet(); } - public ArrayList getReport() { - return reports; + public Report getReport() { + return report; } - public void setReport(ArrayList reports) { - this.reports = reports; - } - - public void addReport(Report r) { - reports.add(r); + public void setReport(Report report) { + this.report = report; } - public NodeSet getConsequents() { - return consequents; + public Set getConsequentChannels() { + return consequentChannels; } - public void setConsequents(NodeSet consequents) { - this.consequents = consequents; + public void setConsequentChannels(Set consequentChannels) { + this.consequentChannels = consequentChannels; } + public void addChannel(Channel c) { + this.consequentChannels.add(c); + } + + public void addAllChannels(Set channels) { + for(Channel c : channels) { + this.consequentChannels.add(c); + } + } + + public void clear() { + consequentChannels.clear(); + } } diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index 89a2ab57..92f26fe3 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -1,7 +1,7 @@ package sneps.snip.classes; import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.snebr.Support; +import sneps.snip.InferenceTypes; import sneps.snip.matching.Substitutions; /** @@ -19,15 +19,21 @@ public class RuleUseInfo { private int neg; private FlagNodeSet fns; + /** + * Type of Report this RUI is produced from. + */ + private InferenceTypes type; + public RuleUseInfo() { } - public RuleUseInfo(Substitutions s, int p, int n, FlagNodeSet f) { + public RuleUseInfo(Substitutions s, int p, int n, FlagNodeSet f, InferenceTypes t) { sub = s; pos = p; neg = n; fns = f; + type = t; } /** @@ -94,6 +100,10 @@ public Substitutions getSubstitutions() { return sub; } + public InferenceTypes getType() { + return type; + } + /** * Check if this and r have no binding conflicts * @@ -115,7 +125,6 @@ public boolean isVarsCompatible(RuleUseInfo r) { public boolean isJoint(RuleUseInfo r) { for (FlagNode fn1 : fns) { for (FlagNode fn2 : r.getFlagNodeSet()) { - //System.out.println("---->> " + fn1.getNode() + " " + fn1.getNode()); if (fn1.getNode() == fn2.getNode()) return true; } @@ -144,9 +153,16 @@ public boolean isDisjoint(RuleUseInfo r) { */ public RuleUseInfo combine(RuleUseInfo rui) { if (this.isDisjoint(rui) && this.isVarsCompatible(rui)) { + InferenceTypes resultingType; + if(this.getType().equals(InferenceTypes.FORWARD) || + rui.getType().equals(InferenceTypes.FORWARD)) + resultingType = InferenceTypes.FORWARD; + else + resultingType = InferenceTypes.BACKWARD; + return new RuleUseInfo(this.getSubstitutions().union(rui.getSubstitutions()), this.pos + rui.getPosCount(), this.neg + rui.getNegCount(), - this.fns.union(rui.getFlagNodeSet())); + this.fns.union(rui.getFlagNodeSet()), resultingType); } return null; @@ -154,7 +170,7 @@ public RuleUseInfo combine(RuleUseInfo rui) { public String toString() { return "Sub: " + sub.toString() + " Pos: " + pos + " Neg: " + neg + - " Fns: " + fns.toString(); + " Fns: " + fns.toString() + "Type: " + type; } } \ No newline at end of file diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index 0c2d39bc..7a0d7969 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Hashtable; +import java.util.Set; import sneps.network.VariableNode; import sneps.network.classes.setClasses.NodeSet; @@ -123,6 +124,18 @@ public boolean isEmpty() { public void clear() { map.clear(); } + + public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui) { + RuleUseInfoSet res = new RuleUseInfoSet(); + Set> keys = map.keySet(); + for(ArrayList key : keys) { + RuisHandler handler = map.get(key); + RuleUseInfoSet temp = handler.combineConstantRUI(rui); + res.addAll(temp); + } + + return res; + } } \ No newline at end of file diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index 499dc487..5b061984 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -25,216 +25,35 @@ import sneps.snip.classes.RuisHandler; public class AndOrEntailment extends RuleNode { - boolean sign = false; - private int min, max, args; - private int received=0; - private static int pos=0; - private static int neg=0; - public int getAndOrMin() { - return min; + @Override + protected Report applyRuleOnRui(RuleUseInfo tRui) { + // TODO Auto-generated method stub + return null; } - public int getAndOrMax() { - return max; - } - - public int getAndOrArgs() { - return args; - } - - public void setAndOrMin(int min) { - this.min = min; - } - - public void setAndOrMax(int max) { - this.max = max; - } - - public void setAndOrArgs(int args) { - this.args = args; - } - - /** - * Constructor for the AndOr Entailment - * @param syn - */ - - public AndOrEntailment(Molecular syn) { - super(syn); - } - - /** - * When a report is received, it checks whether it is true or false - * Then the positive or negative will be updated accordingly - * When there is enough args received to create a RUI, for the rule to check, - * A RUI will be created and apply the rule on this RUI - */ - public void applyRuleHandler(Report report, Node signature) { - String contextID = report.getContextName(); - RuleUseInfo rui; - if (report.isPositive()) { - pos++; - } else { - neg++; - } - - int rem = args-(pos+neg); - if(remrem) { - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(signature, propSet, 1)); - rui = new RuleUseInfo(report.getSubstitutions(), - pos, neg, fns); - applyRuleOnRui(rui, contextID); - } - - - - if(pos+neg==args) { - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(signature, propSet, 1)); - rui = new RuleUseInfo(report.getSubstitutions(), - pos, neg, fns); - applyRuleOnRui(rui, contextID); - } - } - - - /** - * Checks the condition for firing the rule. - * If the conditions are true, the sign is set to true - * Then a new report is created with the sign that was set. - * The report is broadcasted to the ants. - */ - protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { - - if(tRui.getPosCount()>=min&&tRui.getPosCount()<=max) { - sign=true; - }else if(tRui.getPosCount()>max||tRui.getPosCount()rem) { - sign=false; - } - - - Set nodesSentReports = new HashSet(); - for (FlagNode fn : tRui.getFlagNodeSet()) { - nodesSentReports.add(fn.getNode().getId()); - } - - - - - - Substitutions sub = tRui.getSubstitutions(); - FlagNodeSet justification = new FlagNodeSet(); - justification.addAll(tRui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); - - for(FlagNode fn : justification){ - try { - supports = supports.union(fn.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - } - - try { - supports = supports.union(tRui.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - - if(this.getTerm() instanceof Open){ - //knownInstances check this.free vars - > bound - VarNodeSet freeVars = ((Open)this.getTerm()).getFreeVariables(); - Substitutions ruiSub = tRui.getSubstitutions(); - boolean allBound = true; - - for(Report report : knownInstances){ - //Bound to same thing(if bound) - for(VariableNode var : freeVars){ - if(!report.getSubstitutions().isBound(var)){ - allBound = false; - break; - } - } - if(allBound){//if yes - Substitutions instanceSub = report.getSubstitutions(); - - for(int i = 0; i < ruiSub.cardinality(); i++){ - Binding ruiBind = ruiSub.getBinding(i);//if rui also bound - Binding instanceBind = instanceSub. - getBindingByVariable(ruiBind.getVariable()); - if( !((instanceBind != null) && - (instanceBind.isEqual(ruiBind))) ){ - allBound = false; - break; - } - } - if(allBound){ - //combine known with rui - Substitutions newSub = new LinearSubstitutions(); - newSub.insert(instanceSub); - newSub.insert(ruiSub); - - } - } - } - } - - - Report forwardReport = new Report(sub, supports, sign, contextID); - - for (Channel outChannel : outgoingChannels) { - if(!nodesSentReports.contains(outChannel.getRequester().getId())) - outChannel.addReport(forwardReport); - } - - } - - + @Override public NodeSet getDownAntNodeSet() { - return this.getDownNodeSet("Xant"); + // TODO Auto-generated method stub + return null; } - /** - * Create the SIndex within the context - * @param ContextName - */ - protected RuisHandler createRuisHandler(String contextName) { - SIndex index = new SIndex(contextName, getSharedVarsNodes(antNodesWithVars), (byte) 0); - return this.addContextRuiHandler(contextName, index); + @Override + public NodeSet getDownConsqNodeSet() { + // TODO Auto-generated method stub + return null; } - /** - * Getters of positive, negative, sign - * used in testing - */ - public static int getPos() { - return pos; + @Override + protected RuisHandler createRuisHandler() { + // TODO Auto-generated method stub + return null; } - public static int getNeg() { - return neg; - } - - public boolean isSign() { - return sign; - } - - /** - * Clear all the values - * Used in testing - */ - public void clrAll() { - min=0; - max=0; - pos=0; - neg=0; + @Override + protected byte getSIndexType() { + // TODO Auto-generated method stub + return 0; } -} +} \ No newline at end of file diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 6e6b5af4..62c18d28 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -1,10 +1,10 @@ package sneps.snip.rules; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; -import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NodeNotFoundInPropSetException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; @@ -13,13 +13,16 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.term.Molecular; -import sneps.snebr.Support; +import sneps.snebr.Controller; import sneps.snip.Report; +import sneps.snip.channels.Channel; +import sneps.snip.channels.ChannelTypes; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; +import sneps.snip.matching.LinearSubstitutions; /** * @className NumericalEntailment.java @@ -38,9 +41,9 @@ public class NumericalEntailment extends RuleNode { private int i; // Used for testing - private ArrayList reportsToBeSent; + protected ArrayList reportsToBeSent; - public NumericalEntailment(Molecular syn) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { + public NumericalEntailment(Molecular syn) { super(syn); // Initializing i NodeSet max = getDownNodeSet("i"); @@ -48,106 +51,134 @@ public NumericalEntailment(Molecular syn) throws NotAPropositionNodeException, N i = Integer.parseInt(max.getNode(0).getIdentifier()); // Initializing the antecedents - //antecedents = getDownAntNodeSet(); + antecedents = getDownAntNodeSet(); //processNodes(antecedents); // Initializing the consequents - consequents = getDownNodeSet("iconsq"); + consequents = getDownConsqNodeSet(); reportsToBeSent = new ArrayList(); } /** - * Creates the first RuleUseInfo from a given Report and stores it (if positive) - * Also checks if current number of positive Reports satisfies the rule + * Creates the first RuleUseInfo from a given Report and stores it (if positive), + * also checks if current number of positive Reports satisfies the rule. * @param report * @param signature */ - @Override - public void applyRuleHandler(Report report, Node signature) { + //@Override + public ArrayList applyRuleHandler(Report report, Node signature) { processNodes(antecedents); System.out.println("---------------"); - if (report.isPositive()) { - //System.out.println("CREATING POSITIVE RUI"); - Support propSet = report.getSupport(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(signature, propSet, 1)); - RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns); - //System.out.println(rui); - - // Inserting the RuleUseInfo into the RuleNode's RuisHandler: - // SIndex in case there are shared variables between the antecedents, or - // RUISet in case there are no shared variables - if(ruisHandler == null) - ruisHandler = addRuiHandler(); - - RuleResponse response = new RuleResponse(); - response.setConsequents(consequents); - if(antNodesWithoutVars.contains(signature)) { - addConstantRui(rui); - //System.out.println(constantRUI); - if (ruisHandler.isEmpty()) - applyRuleOnRui(constantRUI); - - // What if ruisHandler is not empty? I need to combine constantRUI with - // all the RUIs stored or what? + + if(report.isNegative()) + return null; + + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + Report reply; + Set forwardChannels; + + //System.out.println("CREATING POSITIVE RUI"); + PropositionSet propSet = report.getSupport(); + FlagNodeSet fns = new FlagNodeSet(); + fns.insert(new FlagNode(signature, propSet, 1)); + RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns, + report.getInferenceType()); + //System.out.println(rui); + + // Inserting the RuleUseInfo into the RuleNode's RuisHandler: + // SIndex in case there are shared variables between the antecedents, or + // RUISet in case there are no shared variables + if(ruisHandler == null) + ruisHandler = addRuiHandler(); + + if(antNodesWithoutVars.contains(signature)) { + addConstantRui(rui); + if (ruisHandler.isEmpty()) { + reply = applyRuleOnRui(constantRUI); + /*if(reply != null) { + forwardChannels = getOutgoingChannelsForReport(reply); + response.setReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + }*/ } else { - // The RUI created for the given report is inserted to the RuisHandler - RuleUseInfoSet res = ruisHandler.insertRUI(rui); - //System.out.println(res); - - if(constantRUI != null) { - RuleUseInfo combined; - for (RuleUseInfo tRui : res) { - combined = tRui.combine(constantRUI); - if(combined != null) { - applyRuleOnRui(combined); - } + RuleUseInfoSet combined = ruisHandler.combineConstantRUI(constantRUI); + for (RuleUseInfo tRui : combined) { + reply = applyRuleOnRui(tRui); + if(reply != null) { + forwardChannels = getOutgoingChannelsForReport(reply); + response.clear(); + response.setReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); } } - else { - for (RuleUseInfo tRui : res) { - applyRuleOnRui(tRui); + } + } + else { + // The RUI created for the given report is inserted to the RuisHandler + RuleUseInfoSet res = ruisHandler.insertRUI(rui); + //System.out.println(res); + + if(constantRUI != null) { + RuleUseInfo combined; + for (RuleUseInfo tRui : res) { + combined = tRui.combine(constantRUI); + if(combined != null) { + reply = applyRuleOnRui(combined); + /*if(reply != null) { + forwardChannels = getOutgoingChannelsForReport(reply); + response.clear(); + response.setReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + }*/ + } + } + } + else { + for (RuleUseInfo tRui : res) { + reply = applyRuleOnRui(tRui); + if(reply != null) { + forwardChannels = getOutgoingChannelsForReport(reply); + response.clear(); + response.setReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); } } } } - } + if(responseList.isEmpty()) + return null; - protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + return responseList; + } + + protected Report applyRuleOnRui(RuleUseInfo rui) { if(rui.getPosCount() < i) return null; - Support replySupport = new Support(); - /*PropositionSet propSet = new PropositionSet(); + PropositionSet replySupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { - propSet.add(fn.getNode().getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { + replySupport.union(fn.getSupport()); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { e.printStackTrace(); } + + // TODO + // Add rule node to replySupport - try { - propSet.add(this.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - e.printStackTrace(); - } - - try { - replySupport.addJustificationBasedSupport(propSet); - } catch (NodeNotFoundInPropSetException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - e.printStackTrace(); - }*/ - - Report reply = new Report(rui.getSubstitutions(), replySupport, true); + Report reply = new Report(rui.getSubstitutions(), replySupport, true, + rui.getType()); + System.out.println(reply); reportsToBeSent.add(reply); - - return new RuleResponse(); + return reply; } /** @@ -159,6 +190,11 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { public NodeSet getDownAntNodeSet(){ return this.getDownNodeSet("iant"); } + + @Override + public NodeSet getDownConsqNodeSet() { + return this.getDownNodeSet("iconsq"); + } @Override protected RuisHandler createRuisHandler() { @@ -170,6 +206,27 @@ protected byte getSIndexType() { return SIndex.RUIS; } + protected Set getOutgoingChannelsForReport(Report r) { + Set outgoingChannels = getOutgoingRuleConsequentChannels(); + Set replyChannels = new HashSet(); + for(Node n : consequents) { + for(Channel c : outgoingChannels) { + if(c.getRequester().getId() == n.getId() && + r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { + replyChannels.add(c); + break; + } + } + + Channel ch = establishChannel(ChannelTypes.RuleCons, n, + new LinearSubstitutions(), (LinearSubstitutions) + r.getSubstitutions(), Controller.getCurrentContext(), -1); + replyChannels.add(ch); + } + + return replyChannels; + } + /** * Getter for i * @return diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 255de151..1d527ad7 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -2,65 +2,52 @@ import java.util.ArrayList; -import sneps.exceptions.DuplicatePropositionException; + import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NodeNotFoundInPropSetException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; -import sneps.network.RuleNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.term.Molecular; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; -public class OrEntailment extends RuleNode { +public class OrEntailment extends NumericalEntailment { private static final long serialVersionUID = 1L; - // Used for testing - private ArrayList reportsToBeSent; - - public OrEntailment(Molecular syn) throws NotAPropositionNodeException, NodeNotFoundInNetworkException { + public OrEntailment(Molecular syn) { super(syn); - - // Initializing the antecedents antecedents = getDownAntNodeSet(); - processNodes(antecedents); - - // Initializing the consequents - consequents = getDownNodeSet("Vconsq"); + consequents = getDownConsqNodeSet(); reportsToBeSent = new ArrayList(); } - public void applyRuleHandler(Report report, Node node) { - if(report.isPositive()) { - Support replySupport = new Support(); - /*PropositionSet propSet = new PropositionSet(); - try { - propSet.add(node.getId()); - propSet.add(this.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - e.printStackTrace(); - } - try { - replySupport.addJustificationBasedSupport(propSet); - } catch (NodeNotFoundInPropSetException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - e.printStackTrace(); - }*/ - - Report reply = new Report(report.getSubstitutions(), replySupport, true); - reportsToBeSent.add(reply); + public ArrayList applyRuleHandler(Report report, Node signature) { + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + PropositionSet replySupport = new PropositionSet(); + try { + replySupport.union(report.getSupport()); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); } + // TODO + // Add rule node to replySupport + Report reply = new Report(report.getSubstitutions(), replySupport, + true, report.getInferenceType()); + reportsToBeSent.add(reply); + response.setReport(reply); + //Set outgoingChannels = getOutgoingChannelsForReport(reply); + //response.addAllChannels(outgoingChannels); + responseList.add(response); + return responseList; } - + @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { + protected Report applyRuleOnRui(RuleUseInfo tRui) { return null; } @@ -68,6 +55,11 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { public NodeSet getDownAntNodeSet() { return this.getDownNodeSet("Vant"); } + + @Override + public NodeSet getDownConsqNodeSet() { + return this.getDownNodeSet("Vconsq"); + } @Override protected RuisHandler createRuisHandler() { @@ -78,9 +70,5 @@ protected RuisHandler createRuisHandler() { protected byte getSIndexType() { return 0; } - - public ArrayList getSentReports() { - return reportsToBeSent; - } } diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index d6127ed0..268a7a4b 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -6,12 +6,14 @@ import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Network; import sneps.network.Node; +import sneps.network.PropositionNode; import sneps.network.VariableNode; import sneps.network.cables.DownCable; import sneps.network.cables.DownCableSet; @@ -29,6 +31,7 @@ import sneps.snebr.Context; import sneps.snebr.Controller; import sneps.snebr.Support; +import sneps.snip.InferenceTypes; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; @@ -48,7 +51,7 @@ public class NumericalEntailmentTests extends TestCase { private static RuleUseInfo rui; private static Report report; - public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkException { + public void setUp() { try { context = Controller.createContext(contextName); } catch (DuplicateContextNameException e1) { @@ -56,9 +59,9 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx } LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); FlagNodeSet fns = new FlagNodeSet(); FlagNode fn; - Support support = new Support(); ArrayList wires = new ArrayList(); LinkedList dc = new LinkedList(); LinkedList rels = new LinkedList(); @@ -79,13 +82,13 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; rels.clear(); -//-------------------------------------- fido, dog, barks, X ---------------------------------------------// +//----------------------------- fido, dog, barks, X -----------------------------// try { var = Network.buildVariableNode("X"); fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - barks = Network.buildBaseNode("Barks", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); + dog = Network.buildBaseNode("Dog", new Semantic("Base")); + barks = Network.buildBaseNode("Barks", new Semantic("Base")); wire1 = new Wire(memberRel, fido); wire2 = new Wire(classRel, dog); wire3 = new Wire(doesRel, barks); @@ -97,7 +100,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx var = new VariableNode(new Variable("X")); -//------------------------------------- prop1, prop2, prop3, prop4 ----------------------------------------------// +//--------------------------- prop1, prop2, prop3, prop4 -----------------------------// try { wires.clear(); wires.add(wire1); wires.add(wire2); @@ -127,7 +130,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx dc1 = new DownCable(classRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Closed("Prop1", dcs)); + prop1 = new PropositionNode(new Closed("Prop1", dcs)); dcList.clear(); //------------------------------------------------------------// nodeSet1.clear(); nodeSet1.addNode(dog); @@ -137,7 +140,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx dc1 = new DownCable(doesRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Closed("Prop2", dcs)); + prop2 = new PropositionNode(new Closed("Prop2", dcs)); dcList.clear(); //------------------------------------------------------------// nodeSet1.clear(); nodeSet1.addNode(var); @@ -147,7 +150,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx dc1 = new DownCable(classRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); + prop3 = new PropositionNode(new Open("Prop3", dcs)); ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// @@ -158,28 +161,21 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx dc1 = new DownCable(doesRel, nodeSet1); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Closed("Prop4", dcs)); + prop4 = new PropositionNode(new Closed("Prop4", dcs)); dcList.clear(); //------------------------------------------------------------// -//------------------------------------- Numerical ----------------------------------------------// +//------------------------- Numerical Setup ------------------------------------// nodeSet.addNode(prop1); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); nodeSet.addNode(prop2); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); nodeSet.addNode(prop3); dc.add(new DownCable(antsRel, nodeSet)); - + nodeSet.clear(); nodeSet.addNode(prop4); dc.add(new DownCable(consRel, nodeSet)); - DownCableSet dcss = new DownCableSet(dc, caseFrameAC); //System.out.println(dcss.getDownCable("iant")); //System.out.println(dcss.getDownCable("iconsq")); @@ -191,7 +187,7 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx NodeSet c = new NodeSet(); c.addNode(prop4); -//------------------------------------- NUMERICAL ----------------------------------------------// +//---------------------------- NUMERICAL -----------------------------------// numerical = new NumericalEntailment(new Open("Open", dcss)); //System.out.println(numerical.getId()); @@ -200,29 +196,36 @@ public void setUp() throws NotAPropositionNodeException, NodeNotFoundInNetworkEx numerical.setConsequents(c); sub = new LinearSubstitutions(); - fns = new FlagNodeSet(); - fn = new FlagNode(prop1, new Support(), 1); - fns.insert(fn); - report = new Report(sub, new Support(), true); + support = new PropositionSet(); + /*try { + support.add(prop1.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }*/ + //fns = new FlagNodeSet(); + //fn = new FlagNode(prop1, new Support(), 1); + //fns.insert(fn); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); } /*@Test public void testProcessNodes() { assertEquals(1, numerical.getConsequents().size()); assertEquals(3, numerical.getAntecedents().size()); - assertEquals(2, numerical.getAntsWithoutVars().size()); assertEquals(1, numerical.getAntsWithVars().size()); + assertEquals(2, numerical.getAntsWithoutVars().size()); }*/ - @Test + /*@Test public void testApplyRuleHandler() { numerical.applyRuleHandler(report, prop1); assertEquals(1, numerical.getReplies().size()); assertEquals(numerical.getReplies().get(0), report); - } + }*/ - @Test + /*@Test public void testApplyRuleHandler2() { numerical.setI(2); numerical.getReplies().clear(); @@ -230,7 +233,7 @@ public void testApplyRuleHandler2() { numerical.applyRuleHandler(report, prop1); assertEquals(0, numerical.getReplies().size()); - } + }*/ @Test public void testApplyRuleHandler3() { @@ -246,27 +249,30 @@ public void testApplyRuleHandler3() { assertEquals(SIndex.SINGLETON, ((SIndex) (numerical.getRuisHandler())).getRuiHandlerType()); LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); + /*try { + support.add(prop3.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }*/ sub.putIn(new Binding((VariableNode) var, fido)); - FlagNodeSet fns = new FlagNodeSet(); - FlagNode fn = new FlagNode(prop3, new Support(), 1); - fns.insert(fn); - rui = new RuleUseInfo(sub, 1, 0, fns); - report = new Report(sub, new Support(), true); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); numerical.applyRuleHandler(report, prop3); assertEquals(1, numerical.getReplies().size()); } - @Test + /*@Test public void testGetDownNodeSet() { NodeSet downAntNodeSet = numerical.getDownAntNodeSet(); assertNotNull("NumericalEntailment: getDownAntNodeSet retuns null", downAntNodeSet); assertFalse("NumericalEntailment: getDownAntNodeSet retuns an empty NodeSet", downAntNodeSet.isEmpty()); - //assertEquals(1, numerical.getDownNodeSet("iconsq")); - //assertEquals(3, numerical.getDownAntNodeSet()); - } + assertEquals(1, numerical.getDownConsqNodeSet().size()); + assertEquals(3, numerical.getDownAntNodeSet().size()); + }*/ public void tearDown(){ Network.clearNetwork(); diff --git a/tests/OrTest.java b/tests/OrTests.java similarity index 76% rename from tests/OrTest.java rename to tests/OrTests.java index 63196d19..c91458d5 100644 --- a/tests/OrTest.java +++ b/tests/OrTests.java @@ -1,209 +1,189 @@ -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.LinkedList; - -import org.junit.*; - -import sneps.exceptions.CannotBuildNodeException; -import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; -import sneps.exceptions.EquivalentNodeException; -import sneps.exceptions.IllegalIdentifierException; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; -import sneps.network.Network; -import sneps.network.Node; -import sneps.network.RuleNode; -import sneps.network.VariableNode; -import sneps.network.cables.DownCable; -import sneps.network.cables.DownCableSet; -import sneps.network.classes.CaseFrame; -import sneps.network.classes.RCFP; -import sneps.network.classes.Relation; -import sneps.network.classes.RelationsRestrictedCaseFrame; -import sneps.network.classes.Semantic; -import sneps.network.classes.Wire; -import sneps.network.classes.term.Closed; -import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; -import sneps.network.classes.term.Variable; -import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.network.classes.setClasses.NodeSet; -import sneps.network.classes.setClasses.PropositionSet; -import sneps.snebr.Context; -import sneps.snebr.Controller; -import sneps.snebr.Support; -import sneps.snip.Report; -import sneps.snip.classes.FlagNode; -import sneps.snip.classes.RuleUseInfo; -import sneps.snip.matching.Binding; -import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.rules.AndEntailment; -import sneps.snip.rules.OrEntailment; - -public class OrTest { - - private static Context context; - private static String contextName = "TempContext"; - private static OrEntailment or; - private static Node fido, var, dog, barks; - private static Node prop1, prop2, prop3, prop4; - private static RuleUseInfo rui; - private static Report report, report1; - - - @BeforeClass - public static void BuildNetwork() throws Exception { - LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - FlagNode fn; - PropositionSet support = new PropositionSet(); - ArrayList wires = new ArrayList(); - LinkedList dc = new LinkedList(); - LinkedList rels = new LinkedList(); - NodeSet nodeSet = new NodeSet(); - Relation memberRel = Network.defineRelation("Member", "NodeSet"); - Relation classRel = Network.defineRelation("Class", "NodeSet"); - Relation doesRel = Network.defineRelation("Does", "NodeSet"); - Relation antsRel = Network.defineRelation("Vant", "Vant"); - Relation consRel = Network.defineRelation("Vconsq", "Vconsq"); - rels.add(memberRel); rels.add(classRel); - CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); - rels.clear(); rels.add(classRel); rels.add(doesRel); - CaseFrame caseFrameCD = Network.defineCaseFrame("ClassDoes", rels); - rels.clear(); rels.add(memberRel); rels.add(doesRel); - CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); - rels.clear(); rels.add(antsRel); rels.add(consRel); - CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); - Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; - rels.clear(); - - - /** - * Building propositions & base nodes, - * adding wires - */ - - try { - var = Network.buildVariableNode("X"); - fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - barks = Network.buildBaseNode("Barks", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - wire1 = new Wire(memberRel, fido); - wire2 = new Wire(classRel, dog); - wire3 = new Wire(doesRel, barks); - wire4 = new Wire(memberRel, var); - } catch (IllegalIdentifierException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e1) { - assertNotNull(e1.getMessage(), e1); - } - - var = new VariableNode(new Variable("X")); - - try { - wires.clear(); wires.add(wire1); wires.add(wire2); - prop1 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire2); wires.add(wire3); - prop2 = Network.buildMolecularNode(wires, caseFrameCD); - - wires.clear(); wires.add(wire4); wires.add(wire2); - prop3 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire1); wires.add(wire3); - prop4 = Network.buildMolecularNode(wires, caseFrameMD); - } catch (CannotBuildNodeException | EquivalentNodeException - | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { - assertNotNull(e1.getMessage(), e1); - } - - LinkedList dcList = new LinkedList(); - NodeSet nodeSet1 = new NodeSet(); - DownCable dc1; DownCableSet dcs; - - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Closed("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Closed("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Closed("Prop4", dcs)); - dcList.clear(); - - - nodeSet.addNode(prop1); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop2); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop3); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop4); - dc.add(new DownCable(consRel, nodeSet)); - - DownCableSet dcss = new DownCableSet(dc, caseFrameAC); - - - /** - * Or-Entailment - */ - - or = new OrEntailment(new Open("Open", dcss)); - - sub.insert(new Binding((VariableNode) var, fido)); - LinearSubstitutions s = new LinearSubstitutions(); - - report = new Report(sub, new Support(), true); - report1 = new Report(s, new Support(), true); - } - - @Test - public void testApplyRuleHandler() { - or.applyRuleHandler(report, prop3); - assertEquals(1, or.getSentReports().size()); - - or.applyRuleHandler(report1, prop1); - assertEquals(2, or.getSentReports().size()); - } - - -} \ No newline at end of file +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.LinkedList; + +import org.junit.*; + +import sneps.exceptions.CannotBuildNodeException; +import sneps.exceptions.EquivalentNodeException; +import sneps.exceptions.IllegalIdentifierException; +import sneps.exceptions.NodeNotFoundInNetworkException; +import sneps.exceptions.NotAPropositionNodeException; +import sneps.network.Network; +import sneps.network.Node; +import sneps.network.PropositionNode; +import sneps.network.VariableNode; +import sneps.network.cables.DownCable; +import sneps.network.cables.DownCableSet; +import sneps.network.classes.CaseFrame; +import sneps.network.classes.Relation; +import sneps.network.classes.Semantic; +import sneps.network.classes.Wire; +import sneps.network.classes.term.Closed; +import sneps.network.classes.term.Open; +import sneps.network.classes.term.Variable; +import sneps.network.classes.setClasses.NodeSet; +import sneps.network.classes.setClasses.PropositionSet; +import sneps.snip.InferenceTypes; +import sneps.snip.Report; +import sneps.snip.matching.Binding; +import sneps.snip.matching.LinearSubstitutions; +import sneps.snip.rules.OrEntailment; + +public class OrTests { + + private static OrEntailment or; + private static Node fido, var, dog, barks; + private static Node prop1, prop2, prop3, prop4; + private static Report report, report1; + + + @BeforeClass + public static void SetUp() { + LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); + ArrayList wires = new ArrayList(); + LinkedList dc = new LinkedList(); + LinkedList rels = new LinkedList(); + NodeSet nodeSet = new NodeSet(); + Relation memberRel = Network.defineRelation("Member", "NodeSet"); + Relation classRel = Network.defineRelation("Class", "NodeSet"); + Relation doesRel = Network.defineRelation("Does", "NodeSet"); + Relation antsRel = Network.defineRelation("Vant", "Vant"); + Relation consRel = Network.defineRelation("Vconsq", "Vconsq"); + rels.add(memberRel); rels.add(classRel); + CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); + rels.clear(); rels.add(classRel); rels.add(doesRel); + CaseFrame caseFrameCD = Network.defineCaseFrame("ClassDoes", rels); + rels.clear(); rels.add(memberRel); rels.add(doesRel); + CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); + rels.clear(); rels.add(antsRel); rels.add(consRel); + CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); + Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; + rels.clear(); + +//----------------------------- fido, dog, barks, X -----------------------------// + + + try { + var = Network.buildVariableNode("X"); + fido = Network.buildBaseNode("Fido", new Semantic("Base")); + dog = Network.buildBaseNode("Dog", new Semantic("Base")); + barks = Network.buildBaseNode("Barks", new Semantic("Base")); + wire1 = new Wire(memberRel, fido); + wire2 = new Wire(classRel, dog); + wire3 = new Wire(doesRel, barks); + wire4 = new Wire(memberRel, var); + } catch (IllegalIdentifierException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e1) { + assertNotNull(e1.getMessage(), e1); + } + + var = new VariableNode(new Variable("X")); + +//--------------------------- prop1, prop2, prop3, prop4 -----------------------------// + + try { + wires.clear(); wires.add(wire1); wires.add(wire2); + prop1 = Network.buildMolecularNode(wires, caseFrameMC); + + wires.clear(); wires.add(wire2); wires.add(wire3); + prop2 = Network.buildMolecularNode(wires, caseFrameCD); + + wires.clear(); wires.add(wire4); wires.add(wire2); + prop3 = Network.buildMolecularNode(wires, caseFrameMC); + + wires.clear(); wires.add(wire1); wires.add(wire3); + prop4 = Network.buildMolecularNode(wires, caseFrameMD); + } catch (CannotBuildNodeException | EquivalentNodeException + | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { + assertNotNull(e1.getMessage(), e1); + } + + LinkedList dcList = new LinkedList(); + NodeSet nodeSet1 = new NodeSet(); + DownCable dc1; DownCableSet dcs; + + nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop1 = new PropositionNode(new Closed("Prop1", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameCD); + prop2 = new PropositionNode(new Closed("Prop2", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new PropositionNode(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMD); + prop4 = new PropositionNode(new Closed("Prop4", dcs)); + dcList.clear(); + + + nodeSet.addNode(prop1); + dc.add(new DownCable(antsRel, nodeSet)); + + nodeSet.clear(); + nodeSet.addNode(prop2); + dc.add(new DownCable(antsRel, nodeSet)); + + nodeSet.clear(); + nodeSet.addNode(prop3); + dc.add(new DownCable(antsRel, nodeSet)); + + nodeSet.clear(); + nodeSet.addNode(prop4); + dc.add(new DownCable(consRel, nodeSet)); + + DownCableSet dcss = new DownCableSet(dc, caseFrameAC); + + + /** + * Or-Entailment + */ + or = new OrEntailment(new Open("Open", dcss)); + + sub.putIn(new Binding((VariableNode) var, fido)); + LinearSubstitutions s = new LinearSubstitutions(); + + report = new Report(sub, support, true, InferenceTypes.BACKWARD); + report1 = new Report(s, support, true, InferenceTypes.BACKWARD); + } + + @Test + public void testApplyRuleHandler() { + or.applyRuleHandler(report, prop3); + assertEquals(1, or.getReplies().size()); + + or.applyRuleHandler(report1, prop1); + assertEquals(2, or.getReplies().size()); + } + +} From 58e327e9cfd44d30e4ec00732bef274f2cc72121 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Wed, 15 May 2019 00:34:14 +0200 Subject: [PATCH 13/22] Remove SIndex errors --- src/sneps/snip/rules/OrEntailment.java | 2 -- tests/NumericalEntailmentTests.java | 4 +-- tests/SIndexTest.java | 43 +++++++++++--------------- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 1d527ad7..6e65e83d 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -21,8 +21,6 @@ public OrEntailment(Molecular syn) { super(syn); antecedents = getDownAntNodeSet(); consequents = getDownConsqNodeSet(); - - reportsToBeSent = new ArrayList(); } public ArrayList applyRuleHandler(Report report, Node signature) { diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index 268a7a4b..2e3597e5 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -203,9 +203,7 @@ public void setUp() { // TODO Auto-generated catch block e.printStackTrace(); }*/ - //fns = new FlagNodeSet(); - //fn = new FlagNode(prop1, new Support(), 1); - //fns.insert(fn); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); } diff --git a/tests/SIndexTest.java b/tests/SIndexTest.java index 10decb5a..bff4473a 100644 --- a/tests/SIndexTest.java +++ b/tests/SIndexTest.java @@ -14,6 +14,7 @@ import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.setClasses.FlagNodeSet; +import sneps.snip.InferenceTypes; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; @@ -56,14 +57,6 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo PropositionNode pn3 = new PropositionNode(); PropositionSet ps = new PropositionSet(); - PropositionSet ps1 = new PropositionSet(); - PropositionSet ps2 = new PropositionSet(); - PropositionSet ps3 = new PropositionSet(); - PropositionSet ps4 = new PropositionSet(); - PropositionSet ps5 = new PropositionSet(); - PropositionSet ps6 = new PropositionSet(); - PropositionSet ps7 = new PropositionSet(); - Variable v = new Variable("x"); Variable v1 = new Variable("y"); @@ -124,14 +117,14 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo ls4.putIn(b8); ls5.putIn(b9); - FlagNode fn = new FlagNode(n, 1); - FlagNode fn1 = new FlagNode(n1, 1); - FlagNode fn2 = new FlagNode(n2, 1); - FlagNode fn3 = new FlagNode(n3, 1); - FlagNode fn4 = new FlagNode(n4, 1); - FlagNode fn5 = new FlagNode(n5, 1); - FlagNode fn6 = new FlagNode(n6, 1); - FlagNode fn7 = new FlagNode(n7, 1); + FlagNode fn = new FlagNode(n, ps, 1); + FlagNode fn1 = new FlagNode(n1, ps, 1); + FlagNode fn2 = new FlagNode(n2, ps, 1); + FlagNode fn3 = new FlagNode(n3, ps, 1); + FlagNode fn4 = new FlagNode(n4, ps, 1); + FlagNode fn5 = new FlagNode(n5, ps, 1); + FlagNode fn6 = new FlagNode(n6, ps, 1); + FlagNode fn7 = new FlagNode(n7, ps, 1); FlagNodeSet fns = new FlagNodeSet(); FlagNodeSet fns1 = new FlagNodeSet(); @@ -157,37 +150,37 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo * Creating new Ruis and inserting them in the map */ - RuleUseInfo rui = new RuleUseInfo(ls, 1, 0, fns); + RuleUseInfo rui = new RuleUseInfo(ls, 1, 0, fns, InferenceTypes.BACKWARD); index.insertRUI(rui); index1.insertRUI(rui); - RuleUseInfo rui1 = new RuleUseInfo(ls1, 1, 0, fns1); + RuleUseInfo rui1 = new RuleUseInfo(ls1, 1, 0, fns1, InferenceTypes.BACKWARD); - RuleUseInfo rui2 = new RuleUseInfo(ls1, 1, 0, fns2); + RuleUseInfo rui2 = new RuleUseInfo(ls1, 1, 0, fns2, InferenceTypes.BACKWARD); - RuleUseInfo rui3 = new RuleUseInfo(ls1, 1, 0, fns2); + RuleUseInfo rui3 = new RuleUseInfo(ls1, 1, 0, fns2, InferenceTypes.BACKWARD); - RuleUseInfo rui4 = new RuleUseInfo(ls3, 0, 1, fns3); + RuleUseInfo rui4 = new RuleUseInfo(ls3, 0, 1, fns3, InferenceTypes.BACKWARD); - RuleUseInfo rui5 = new RuleUseInfo(ls2, 2, 0, fns4); + RuleUseInfo rui5 = new RuleUseInfo(ls2, 2, 0, fns4, InferenceTypes.BACKWARD); index.insertRUI(rui5); index1.insertRUI(rui5); - RuleUseInfo rui6 = new RuleUseInfo(ls, 3, 0, fns5); + RuleUseInfo rui6 = new RuleUseInfo(ls, 3, 0, fns5, InferenceTypes.BACKWARD); index.insertRUI(rui6); index1.insertRUI(rui6); - RuleUseInfo rui7 = new RuleUseInfo(ls4, 4, 0, fns5); + RuleUseInfo rui7 = new RuleUseInfo(ls4, 4, 0, fns5, InferenceTypes.BACKWARD); index.insertRUI(rui7); index1.insertRUI(rui7); - RuleUseInfo rui8 = new RuleUseInfo(ls5, 5, 0, fns5); + RuleUseInfo rui8 = new RuleUseInfo(ls5, 5, 0, fns5, InferenceTypes.BACKWARD); index.insertRUI(rui8); index1.insertRUI(rui8); From ad3ceab81c990d262b12ec61063ab2d4ce761184 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Fri, 17 May 2019 23:32:17 +0200 Subject: [PATCH 14/22] Implementation of AndOr, along with some of its tests --- src/sneps/network/RuleNode.java | 94 ++-- .../classes/setClasses/FlagNodeSet.java | 9 + src/sneps/snip/rules/AndOrEntailment.java | 100 ++++- src/sneps/snip/rules/DoIfNode.java | 29 +- src/sneps/snip/rules/NumericalEntailment.java | 59 +-- src/sneps/snip/rules/OrEntailment.java | 2 +- src/sneps/snip/rules/ThreshEntailment.java | 272 ++++-------- src/sneps/snip/rules/WhenDoNode.java | 31 +- tests/AndOrTest.java | 403 +++++++----------- tests/NumericalEntailmentTests.java | 118 ++--- 10 files changed, 515 insertions(+), 602 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index aaa38f87..6e50bd92 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -74,6 +74,12 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ * that contains all the constant instances that do not dominate variables. */ protected RuleUseInfo constantRUI; + + /** + * Used for testing. + */ + protected ArrayList reportsToBeSent; + public RuleNode() { consequents = new NodeSet(); @@ -84,6 +90,7 @@ public RuleNode() { antNodesWithVarsIDs = new HashSet(); shareVars = false; sharedVars = new VarNodeSet(); + reportsToBeSent = new ArrayList(); } public RuleNode(Molecular syn) { @@ -96,10 +103,7 @@ public RuleNode(Molecular syn) { antNodesWithVarsIDs = new HashSet(); shareVars = false; sharedVars = new VarNodeSet(); - } - - public NodeSet getConsequents() { - return consequents; + reportsToBeSent = new ArrayList(); } public RuisHandler getRuisHandler() { @@ -133,6 +137,10 @@ public void setAntecedents(NodeSet antecedents) { public void setConsequents(NodeSet consequents) { this.consequents = consequents; } + + public ArrayList getReplies() { + return reportsToBeSent; + } protected void sendReportToConsequents(Report reply) { if(!knownInstances.contains(reply)) @@ -171,18 +179,23 @@ public void processNodes(NodeSet antNodes) { * The instance that is being reported by the report. * @return */ - /*public ArrayList applyRuleHandler(Report report, Node signature) { + public ArrayList applyRuleHandler(Report report, Node signature) { + System.out.println("---------------------"); + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + RuleUseInfo rui; - Support propSet = report.getSupport(); + PropositionSet propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); if (report.isPositive()) { fns.insert(new FlagNode(signature, propSet, 1)); - rui = new RuleUseInfo(report.getSubstitutions(), - 1, 0, fns); + rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns, + report.getInferenceType()); } else { fns.insert(new FlagNode(signature, propSet, 2)); - rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns); + rui = new RuleUseInfo(report.getSubstitutions(), 0, 1, fns, + report.getInferenceType()); } // This is the first report received by this RuleNode, so @@ -191,35 +204,53 @@ public void processNodes(NodeSet antNodes) { ruisHandler = addRuiHandler(); } - RuleUseInfoSet res = new RuleUseInfoSet(); - if(antNodesWithoutVars.contains(signature)) + if(antNodesWithoutVars.contains(signature)) { addConstantRui(rui); - else { - // The RUI created for the given report is inserted to the RuisHandler - res = ruisHandler.insertRUI(rui); - } - - if (res == null) { - applyRuleOnRui(constantRUI); - return; - } - - if(constantRUI != null) { - RuleUseInfo combined; - for (RuleUseInfo tRui : res) { - combined = tRui.combine(constantRUI); - if(combined != null) - applyRuleOnRui(combined); + if (ruisHandler.isEmpty()) { + response = applyRuleOnRui(constantRUI); + if(response != null) + responseList.add(response); + } + else { + RuleUseInfoSet combined = ruisHandler.combineConstantRUI(constantRUI); + for (RuleUseInfo tRui : combined) { + response = applyRuleOnRui(tRui); + if(response != null) + responseList.add(response); + } } } else { - for (RuleUseInfo tRui : res) { - applyRuleOnRui(tRui); + // The RUI created for the given report is inserted to the RuisHandler + RuleUseInfoSet res = ruisHandler.insertRUI(rui); + + if(constantRUI != null) { + RuleUseInfo combined; + for (RuleUseInfo tRui : res) { + combined = tRui.combine(constantRUI); + if(combined != null) { + response = applyRuleOnRui(combined); + if(response != null) + responseList.add(response); + } + } + } + else { + for (RuleUseInfo tRui : res) { + response = applyRuleOnRui(tRui); + if(response != null) + responseList.add(response); + } } } - }*/ + + if(responseList.isEmpty()) + return null; + + return responseList; + } - abstract protected Report applyRuleOnRui(RuleUseInfo tRui); + abstract protected RuleResponse applyRuleOnRui(RuleUseInfo tRui); /** * @@ -228,6 +259,7 @@ public void processNodes(NodeSet antNodes) { public void clear() { if(ruisHandler != null) ruisHandler.clear(); + constantRUI = null; } /** diff --git a/src/sneps/network/classes/setClasses/FlagNodeSet.java b/src/sneps/network/classes/setClasses/FlagNodeSet.java index 10923363..32cc0da9 100644 --- a/src/sneps/network/classes/setClasses/FlagNodeSet.java +++ b/src/sneps/network/classes/setClasses/FlagNodeSet.java @@ -106,4 +106,13 @@ public String toString() { return res; } + + public NodeSet getAllNodes() { + NodeSet res = new NodeSet(); + for(FlagNode fn : flagNodes) { + res.addNode(fn.getNode()); + } + + return res; + } } diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index 5b061984..5acb9f5e 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -7,53 +7,121 @@ import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; -import sneps.network.VariableNode; -import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; -import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Open; +import sneps.snebr.Controller; import sneps.snip.Report; import sneps.snip.channels.Channel; +import sneps.snip.channels.ChannelTypes; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.matching.Substitutions; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; public class AndOrEntailment extends RuleNode { + private static final long serialVersionUID = 1L; + + private int min; + private int max; + + public AndOrEntailment(Molecular syn) { + super(syn); + /*NodeSet minNode = getDownNodeSet("min"); + min = Integer.parseInt(minNode.getNode(0).getIdentifier()); + NodeSet maxNode = getDownNodeSet("max"); + max = Integer.parseInt(maxNode.getNode(0).getIdentifier());*/ + antecedents = getDownAntNodeSet(); + processNodes(antecedents); + } @Override - protected Report applyRuleOnRui(RuleUseInfo tRui) { - // TODO Auto-generated method stub - return null; + protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + boolean reportSign = false; + if (rui.getNegCount() == getAntSize() - min) + reportSign = true; + else if (rui.getPosCount() != max) + return null; + + PropositionSet replySupport = new PropositionSet(); + for(FlagNode fn : rui.getFlagNodeSet()) + try { + replySupport.union(fn.getSupport()); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + + // TODO + // Add rule node to replySupport + + consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); + + System.out.println(rui.getFlagNodeSet().getAllNodes()); + System.out.println(consequents); + Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, + rui.getType()); + System.out.println(reply); + reportsToBeSent.add(reply); + + RuleResponse r = new RuleResponse(); + r.setReport(reply); + //Set forwardChannels = getOutgoingChannelsForReport(reply); + //r.addAllChannels(forwardChannels); + + return r; } @Override public NodeSet getDownAntNodeSet() { - // TODO Auto-generated method stub - return null; + return getDownNodeSet("arg"); } @Override public NodeSet getDownConsqNodeSet() { - // TODO Auto-generated method stub return null; } @Override protected RuisHandler createRuisHandler() { - // TODO Auto-generated method stub - return null; + return new RuleUseInfoSet(false); } @Override protected byte getSIndexType() { - // TODO Auto-generated method stub - return 0; + return SIndex.RUIS; + } + + @Override + protected Set getOutgoingChannelsForReport(Report r) { + Set outgoingChannels = getOutgoingRuleConsequentChannels(); + Set replyChannels = new HashSet(); + for(Node n : consequents) { + for(Channel c : outgoingChannels) { + if(c.getRequester().getId() == n.getId() && + r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { + replyChannels.add(c); + break; + } + } + + Channel ch = establishChannel(ChannelTypes.RuleCons, n, + new LinearSubstitutions(), (LinearSubstitutions) + r.getSubstitutions(), Controller.getCurrentContext(), -1); + replyChannels.add(ch); + } + + return replyChannels; + } + + public void setMin(int min) { + this.min = min; + } + + public void setMax(int max) { + this.max = max; } } \ No newline at end of file diff --git a/src/sneps/snip/rules/DoIfNode.java b/src/sneps/snip/rules/DoIfNode.java index 686338f9..22d32961 100644 --- a/src/sneps/snip/rules/DoIfNode.java +++ b/src/sneps/snip/rules/DoIfNode.java @@ -1,11 +1,14 @@ package sneps.snip.rules; +import java.util.Set; + import sneps.network.RuleNode; -import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Term; +import sneps.snip.Report; +import sneps.snip.channels.Channel; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; public class DoIfNode extends RuleNode { @@ -15,30 +18,40 @@ public DoIfNode(Molecular syn) { super(syn); } + @Override + public NodeSet getDownAntNodeSet() { + // TODO Auto-generated method stub + return null; + } @Override - protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { + protected byte getSIndexType() { // TODO Auto-generated method stub - + return 0; } @Override - public NodeSet getDownAntNodeSet() { + protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { // TODO Auto-generated method stub return null; } @Override - protected RuisHandler createRuisHandler(String contextName) { + public NodeSet getDownConsqNodeSet() { // TODO Auto-generated method stub return null; } + @Override + protected RuisHandler createRuisHandler() { + // TODO Auto-generated method stub + return null; + } @Override - protected byte getSIndexType() { + protected Set getOutgoingChannelsForReport(Report r) { // TODO Auto-generated method stub - return 0; + return null; } } diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 62c18d28..dcc1a800 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -40,9 +40,6 @@ public class NumericalEntailment extends RuleNode { private int i; - // Used for testing - protected ArrayList reportsToBeSent; - public NumericalEntailment(Molecular syn) { super(syn); // Initializing i @@ -56,8 +53,6 @@ public NumericalEntailment(Molecular syn) { // Initializing the consequents consequents = getDownConsqNodeSet(); - - reportsToBeSent = new ArrayList(); } /** @@ -76,10 +71,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { ArrayList responseList = new ArrayList(); RuleResponse response = new RuleResponse(); - Report reply; - Set forwardChannels; - //System.out.println("CREATING POSITIVE RUI"); PropositionSet propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); @@ -96,59 +88,40 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if(antNodesWithoutVars.contains(signature)) { addConstantRui(rui); if (ruisHandler.isEmpty()) { - reply = applyRuleOnRui(constantRUI); - /*if(reply != null) { - forwardChannels = getOutgoingChannelsForReport(reply); - response.setReport(reply); - response.addAllChannels(forwardChannels); + response = applyRuleOnRui(constantRUI); + if(response != null) responseList.add(response); - }*/ } else { RuleUseInfoSet combined = ruisHandler.combineConstantRUI(constantRUI); for (RuleUseInfo tRui : combined) { - reply = applyRuleOnRui(tRui); - if(reply != null) { - forwardChannels = getOutgoingChannelsForReport(reply); - response.clear(); - response.setReport(reply); - response.addAllChannels(forwardChannels); + response = applyRuleOnRui(tRui); + if(response != null) responseList.add(response); - } } } } else { // The RUI created for the given report is inserted to the RuisHandler RuleUseInfoSet res = ruisHandler.insertRUI(rui); - //System.out.println(res); + System.out.println(res); if(constantRUI != null) { RuleUseInfo combined; for (RuleUseInfo tRui : res) { combined = tRui.combine(constantRUI); if(combined != null) { - reply = applyRuleOnRui(combined); - /*if(reply != null) { - forwardChannels = getOutgoingChannelsForReport(reply); - response.clear(); - response.setReport(reply); - response.addAllChannels(forwardChannels); + response = applyRuleOnRui(combined); + if(response != null) responseList.add(response); - }*/ } } } else { for (RuleUseInfo tRui : res) { - reply = applyRuleOnRui(tRui); - if(reply != null) { - forwardChannels = getOutgoingChannelsForReport(reply); - response.clear(); - response.setReport(reply); - response.addAllChannels(forwardChannels); + response = applyRuleOnRui(tRui); + if(response != null) responseList.add(response); - } } } } @@ -159,7 +132,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { return responseList; } - protected Report applyRuleOnRui(RuleUseInfo rui) { + protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { if(rui.getPosCount() < i) return null; @@ -178,7 +151,13 @@ protected Report applyRuleOnRui(RuleUseInfo rui) { rui.getType()); System.out.println(reply); reportsToBeSent.add(reply); - return reply; + + RuleResponse r = new RuleResponse(); + r.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + r.addAllChannels(forwardChannels); + + return r; } /** @@ -242,9 +221,5 @@ public int getI() { public void setI(int newI) { i = newI; } - - public ArrayList getReplies() { - return reportsToBeSent; - } } diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 6e65e83d..0fdce066 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -45,7 +45,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { } @Override - protected Report applyRuleOnRui(RuleUseInfo tRui) { + protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { return null; } diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index 8f5b2510..797e75bf 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -7,236 +7,120 @@ import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; -import sneps.network.VariableNode; -import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; -import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Open; +import sneps.snebr.Controller; import sneps.snip.Report; import sneps.snip.channels.Channel; +import sneps.snip.channels.ChannelTypes; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.matching.Substitutions; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; public class ThreshEntailment extends RuleNode { - - private static boolean sign = false; + private static final long serialVersionUID = 1L; - private int min, max, args; - private int pos=0; - private int neg=0; - public int getThreshMin() { - return min; - } - - public int getThreshMax() { - return max; - } - - public int getThreshArgs() { - return args; - } + private int min; + private int max; - public void setThreshMin(int min) { - this.min = min; - } - - public void setThreshMax(int max) { - this.max = max; - } - - public void setThreshArgs(int args) { - this.args = args; - } - - - /** - * Constructor for the Thresh Entailment - * @param syn - */ public ThreshEntailment(Molecular syn) { super(syn); + NodeSet minNode = getDownNodeSet("min"); + min = Integer.parseInt(minNode.getNode(0).getIdentifier()); + NodeSet maxNode = getDownNodeSet("max"); + max = Integer.parseInt(maxNode.getNode(0).getIdentifier()); + antecedents = getDownAntNodeSet(); + processNodes(antecedents); } - - /** - * When a report is received, it checks whether it is true or false - * Then the positive or negative will be updated accordingly - * When there is enough args received to create a RUI, for the rule to check, - * A RUI will be created and apply the rule on this RUI - */ - - public void applyRuleHandler(Report report, Node signature) { + + @Override + protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + boolean reportSign = false; + if (rui.getPosCount() == min + && rui.getNegCount() == getAntSize() - max - 1) + reportSign = true; + else if (rui.getPosCount() != min - 1 + || rui.getNegCount() != getAntSize() - max) + return null; - String contextID = report.getContextName(); - RuleUseInfo rui; + PropositionSet replySupport = new PropositionSet(); + for(FlagNode fn : rui.getFlagNodeSet()) + try { + replySupport.union(fn.getSupport()); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + + // TODO + // Add rule node to replySupport - if(report.isPositive()) { - pos++; - } - if(report.isNegative()) { - neg++; - } + consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); - int rem = args-(pos+neg); - if(pos>min && posrem) { - - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(signature, propSet, 1)); - rui = new RuleUseInfo(report.getSubstitutions(), - pos, neg, fns); - applyRuleOnRui(rui, contextID); - - } + Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, + rui.getType()); + reportsToBeSent.add(reply); - if(neg+pos==args) { - - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(signature, propSet, 1)); - rui = new RuleUseInfo(report.getSubstitutions(), - pos, neg, fns); - applyRuleOnRui(rui, contextID); - - } + RuleResponse r = new RuleResponse(); + r.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + r.addAllChannels(forwardChannels); + return r; } - - - - - /** - * Checks the condition for firing the rule. - * If the conditions are true, the sign is set to true - * Then a new report is created with the sign that was set. - * The report is broadcasted to the ants. - */ - protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { - - if (tRui.getPosCount() < min || tRui.getPosCount()>max) - sign = true; - else if (tRui.getPosCount()>= min && tRui.getPosCount() <= max) - sign = false; - - int rem = args-(tRui.getPosCount()+tRui.getNegCount()); - if(tRui.getPosCount()>min && tRui.getPosCount()rem) { - sign=false; - } - - Set nodesSentReports = new HashSet(); - for (FlagNode fn : tRui.getFlagNodeSet()) { - nodesSentReports.add(fn.getNode().getId()); - } - - - Substitutions sub = tRui.getSubstitutions(); - FlagNodeSet justification = new FlagNodeSet(); - justification.addAll(tRui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); - for(FlagNode fn : justification){ - try { - supports = supports.union(fn.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - } - - try { - supports = supports.union(tRui.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} + @Override + public NodeSet getDownAntNodeSet() { + return getDownNodeSet("arg"); + } - if(this.getTerm() instanceof Open){ - //knownInstances check this.free vars - > bound - VarNodeSet freeVars = ((Open)this.getTerm()).getFreeVariables(); - Substitutions ruiSub = tRui.getSubstitutions(); - boolean allBound = true; + @Override + public NodeSet getDownConsqNodeSet() { + return null; + } - for(Report report : knownInstances){ - //Bound to same thing(if bound) - for(VariableNode var : freeVars){ - if(!report.getSubstitutions().isBound(var)){ - allBound = false; - break; - } - } - if(allBound){//if yes - Substitutions instanceSub = report.getSubstitutions(); + @Override + protected RuisHandler createRuisHandler() { + return new RuleUseInfoSet(false); + } - for(int i = 0; i < ruiSub.cardinality(); i++){ - Binding ruiBind = ruiSub.getBinding(i);//if rui also bound - Binding instanceBind = instanceSub. - getBindingByVariable(ruiBind.getVariable()); - if( !((instanceBind != null) && - (instanceBind.isEqual(ruiBind))) ){ - allBound = false; - break; - } - } - if(allBound){ - //combine known with rui - Substitutions newSub = new LinearSubstitutions(); - newSub.insert(instanceSub); - newSub.insert(ruiSub); + @Override + protected byte getSIndexType() { + return SIndex.RUIS; + } - } + @Override + protected Set getOutgoingChannelsForReport(Report r) { + Set outgoingChannels = getOutgoingRuleConsequentChannels(); + Set replyChannels = new HashSet(); + for(Node n : consequents) { + for(Channel c : outgoingChannels) { + if(c.getRequester().getId() == n.getId() && + r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { + replyChannels.add(c); + break; } } + + Channel ch = establishChannel(ChannelTypes.RuleCons, n, + new LinearSubstitutions(), (LinearSubstitutions) + r.getSubstitutions(), Controller.getCurrentContext(), -1); + replyChannels.add(ch); } - - Report forwardReport = new Report(sub, supports, sign, contextID); - - for (Channel outChannel : outgoingChannels) { - if(!nodesSentReports.contains(outChannel.getRequester().getId())) - outChannel.addReport(forwardReport); - } - - } - - /** - * Create the SIndex within the context - * @param ContextName - */ - protected RuisHandler createRuisHandler(String contextName) { - SIndex index = new SIndex(contextName, getSharedVarsNodes(antNodesWithVars), (byte) 0); - return this.addContextRuiHandler(contextName, index); - } - - - @Override - public NodeSet getDownAntNodeSet() { - return this.getDownNodeSet("Tant"); + return replyChannels; } - public boolean getSign() { - return sign; + public void setMin(int min) { + this.min = min; } - - public int getPos() { - return pos; - } - - public int getNeg() { - return neg; + public void setMax(int max) { + this.max = max; } - /** - * Clears all the variables. - * Used in testing - */ - public void clrAll() { - min=0; - max=0; - pos=0; - neg=0; - - } } diff --git a/src/sneps/snip/rules/WhenDoNode.java b/src/sneps/snip/rules/WhenDoNode.java index 13656310..ebba9e01 100644 --- a/src/sneps/snip/rules/WhenDoNode.java +++ b/src/sneps/snip/rules/WhenDoNode.java @@ -1,11 +1,14 @@ package sneps.snip.rules; +import java.util.Set; + import sneps.network.RuleNode; -import sneps.network.classes.Semantic; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Term; +import sneps.snip.Report; +import sneps.snip.channels.Channel; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; public class WhenDoNode extends RuleNode { @@ -15,30 +18,40 @@ public WhenDoNode(Molecular syn) { super(syn); } - @Override - protected void applyRuleOnRui(RuleUseInfo tRui, String contextID) { + public NodeSet getDownAntNodeSet() { // TODO Auto-generated method stub - + return null; } @Override - public NodeSet getDownAntNodeSet() { + protected byte getSIndexType() { + // TODO Auto-generated method stub + return 0; + } + + @Override + protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { // TODO Auto-generated method stub return null; } @Override - protected RuisHandler createRuisHandler(String contextName) { + public NodeSet getDownConsqNodeSet() { // TODO Auto-generated method stub return null; } + @Override + protected RuisHandler createRuisHandler() { + // TODO Auto-generated method stub + return null; + } @Override - protected byte getSIndexType() { + protected Set getOutgoingChannelsForReport(Report r) { // TODO Auto-generated method stub - return 0; + return null; } } diff --git a/tests/AndOrTest.java b/tests/AndOrTest.java index c3a999e5..72ee407a 100644 --- a/tests/AndOrTest.java +++ b/tests/AndOrTest.java @@ -25,6 +25,7 @@ import sneps.network.classes.RelationsRestrictedCaseFrame; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; @@ -33,6 +34,7 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snip.InferenceTypes; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuleUseInfo; @@ -42,64 +44,26 @@ import sneps.snip.rules.OrEntailment; public class AndOrTest { - private static Context context; private static String contextName = "TempContext"; private static AndOrEntailment andor; - private static Node fido, var, dog, barks; - private static Node prop1, prop2, prop3, prop4; - private static RuleUseInfo rui; + private static Node fido, var, dog, barks, animal, veg, mineral; + private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7; private static Report report; - private static Report report1; - private static Report report2; - private static Report report3; - private static Report report4; - private static Report report5; - private static Report report6; - private static Report report7; - private static Report report8; - private static Report report9; - private static Report report10; - private static Report report11; - private static Report report12; - private static Report report13; - private static Report report14; - private static Report report15; - private static Report report16; - private static Report report17; - private static Report report18; - private static Report report19; - private static Report report20; - private static Report report21; - private static Report report22; - private static Report report23; - private static Report report24; - private static Report report25; - private static Report report26; - private static Report report27; - private static Report report28; + private static Report report1, report2, report3, report4, report5, report6, + report7, report8, report9, report10, report11, report12, report13, report14, + report15, report16, report17, report18, report19, report20, report21, report22, + report23, report24, report25, report26, report27, report28; @BeforeClass - public static void setUpBeforeClass() throws Exception { + public static void setUp() throws Exception { try { context = Controller.createContext(contextName); } catch (DuplicateContextNameException e1) { assertNotNull(e1.getMessage(), e1); } - - /** - * Create substitutions, - * FlagNodeSet, - * FlagNode, - * PropositionSet, - * wires, - * relations, - * caseFrames - */ - - LinearSubstitutions sub = new LinearSubstitutions(); FlagNodeSet fns = new FlagNodeSet(); FlagNode fn; @@ -113,6 +77,7 @@ public static void setUpBeforeClass() throws Exception { Relation doesRel = Network.defineRelation("Does", "NodeSet"); Relation antsRel = Network.defineRelation("Xant", "Xant"); Relation consRel = Network.defineRelation("Xconsq", "Xconsq"); + Relation argsRel = Network.defineRelation("arg", "arg"); rels.add(memberRel); rels.add(classRel); CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); rels.clear(); rels.add(classRel); rels.add(doesRel); @@ -121,26 +86,29 @@ public static void setUpBeforeClass() throws Exception { CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); rels.clear(); rels.add(antsRel); rels.add(consRel); CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); + rels.clear(); rels.add(argsRel); + CaseFrame caseFrameArgs = Network.defineCaseFrame("Args", rels); Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; + Wire wire5 = null, wire6 = null, wire7 = null; rels.clear(); - - - /** - * Building propositions & base nodes, - * adding wires - */ - try { var = Network.buildVariableNode("X"); fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - barks = Network.buildBaseNode("Barks", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); + dog = Network.buildBaseNode("Dog", new Semantic("Base")); + barks = Network.buildBaseNode("Barks", new Semantic("Base")); wire1 = new Wire(memberRel, fido); wire2 = new Wire(classRel, dog); wire3 = new Wire(doesRel, barks); wire4 = new Wire(memberRel, var); + + animal = Network.buildBaseNode("Animal", new Semantic("Base")); + veg = Network.buildBaseNode("Vegetable", new Semantic("Base")); + mineral = Network.buildBaseNode("Mineral", new Semantic("Base")); + wire5 = new Wire(classRel, animal); + wire6 = new Wire(classRel, veg); + wire7 = new Wire(classRel, mineral); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); @@ -148,9 +116,6 @@ public static void setUpBeforeClass() throws Exception { } - - - try { wires.clear(); wires.add(wire1); wires.add(wire2); prop1 = Network.buildMolecularNode(wires, caseFrameMC); @@ -163,214 +128,171 @@ public static void setUpBeforeClass() throws Exception { wires.clear(); wires.add(wire1); wires.add(wire3); prop4 = Network.buildMolecularNode(wires, caseFrameMD); + + wires.clear(); wires.add(wire4); wires.add(wire5); + prop5 = Network.buildMolecularNode(wires, caseFrameArgs); + + wires.clear(); wires.add(wire4); wires.add(wire6); + prop6 = Network.buildMolecularNode(wires, caseFrameArgs); + + wires.clear(); wires.add(wire4); wires.add(wire7); + prop7 = Network.buildMolecularNode(wires, caseFrameArgs); } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); - LinkedList dcList = new LinkedList(); - NodeSet nodeSet1 = new NodeSet(); - DownCable dc1; DownCableSet dcs; - - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Open("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Open("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Open("Prop4", dcs)); - dcList.clear(); } - - - - try { - support.add(prop1.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop1, support, 1); - fns.insert(fn); + LinkedList dcList = new LinkedList(); + NodeSet nodeSet1 = new NodeSet(); + DownCable dc1; DownCableSet dcs; - support.clearSet(); - try { - support.add(prop2.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop2, support, 1); - fns.insert(fn); + nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop1 = new Node(new Closed("Prop1", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameCD); + prop2 = new Node(new Closed("Prop2", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new Node(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMD); + prop4 = new Node(new Closed("Prop4", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(animal); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop5 = new Node(new Open("Prop5", dcs)); + ((Open) (prop5.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(veg); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop6 = new Node(new Open("Prop6", dcs)); + ((Open) (prop6.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(mineral); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop7 = new Node(new Open("Prop7", dcs)); + ((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + +//---------------------------------------------------------------------------------// - support.clearSet(); - try { - support.add(prop3.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop3, support, 1); - fns.insert(fn); - - nodeSet.addNode(prop1); - dc.add(new DownCable(antsRel, nodeSet)); + /*nodeSet.addNode(prop1); + dc.add(new DownCable(argsRel, nodeSet)); nodeSet.clear(); nodeSet.addNode(prop2); - dc.add(new DownCable(antsRel, nodeSet)); + dc.add(new DownCable(argsRel, nodeSet)); nodeSet.clear(); nodeSet.addNode(prop3); - dc.add(new DownCable(antsRel, nodeSet)); + dc.add(new DownCable(argsRel, nodeSet)); nodeSet.clear(); nodeSet.addNode(prop4); - dc.add(new DownCable(consRel, nodeSet)); - - DownCableSet dcs = new DownCableSet(dc, caseFrameAC); + dc.add(new DownCable(argsRel, nodeSet));*/ - /** - * AndOr-Entailment - */ - - - andor = new AndOrEntailment(new Open("Open", dcs)); + nodeSet.addNode(prop5); + nodeSet.addNode(prop6); + nodeSet.addNode(prop7); + dc.add(new DownCable(argsRel, nodeSet)); - try { - support.add(dog.getId()); - support.add(fido.getId()); - support.add(var.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } + DownCableSet dcss = new DownCableSet(dc, caseFrameArgs); + + NodeSet a = new NodeSet(); + a.addNode(prop5); + a.addNode(prop6); + a.addNode(prop7); - sub.insert(new Binding((VariableNode) var,fido)); - rui = new RuleUseInfo(sub, 1, 0, fns); - report = new Report(sub, support, true, contextName); - report1 = new Report(sub, support, true, contextName); - report2 = new Report(sub, support, true, contextName); - report3 = new Report(sub, support, true, contextName); - report4 = new Report(sub, support, false, contextName); - report5 = new Report(sub, support, false, contextName); - report6 = new Report(sub, support, false, contextName); - report7 = new Report(sub, support, false, contextName); - report8 = new Report(sub, support, false, contextName); - report9 = new Report(sub, support, false, contextName); - +//---------------------------- ANDOR -----------------------------------// - report10 = new Report(sub, support, true, contextName); - report11 = new Report(sub, support, true, contextName); - report12 = new Report(sub, support, true, contextName); - report13 = new Report(sub, support, true, contextName); - report14 = new Report(sub, support, true, contextName); - report15 = new Report(sub, support, true, contextName); - report16 = new Report(sub, support, true, contextName); - report17 = new Report(sub, support, true, contextName); - report18 = new Report(sub, support, false, contextName); - report19 = new Report(sub, support, false, contextName); + andor = new AndOrEntailment(new Open("Open", dcss)); + andor.setAntecedents(a); + andor.setMax(2); + andor.setMin(1); + sub = new LinearSubstitutions(); + support = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); - report20 = new Report(sub, support, true, contextName); - report21 = new Report(sub, support, false, contextName); - report22 = new Report(sub, support, false, contextName); - report23 = new Report(sub, support, false, contextName); - report24 = new Report(sub, support, false, contextName); - report25 = new Report(sub, support, false, contextName); - report26 = new Report(sub, support, false, contextName); - report27 = new Report(sub, support, false, contextName); - report28 = new Report(sub, support, false, contextName); - - andor.setAndOrArgs(10); - andor.setAndOrMax(5); - andor.setAndOrMin(3); + support = new PropositionSet(); + /*try { + support.add(prop6.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + report1 = new Report(sub, support, true, InferenceTypes.BACKWARD); } - @Test public void test() { - andor.setAndOrArgs(10); - andor.setAndOrMax(5); - andor.setAndOrMin(3); - andor.applyRuleHandler(report, fido); - andor.applyRuleHandler(report1, fido); - andor.applyRuleHandler(report2, fido); - andor.applyRuleHandler(report3, fido); - andor.applyRuleHandler(report4, fido); - andor.applyRuleHandler(report5, fido); - andor.applyRuleHandler(report6, fido); - andor.applyRuleHandler(report7, fido); - andor.applyRuleHandler(report8, fido); - andor.applyRuleHandler(report9, fido); - //assertEquals(4, andor.getPos()); - //assertEquals(6, andor.getNeg()); - assertEquals(3,andor.getAndOrMin()); - assertEquals(5,andor.getAndOrMax()); - assertEquals(10,andor.getAndOrArgs()); - assertEquals(true, andor.isSign()); - andor.clrAll(); + andor.applyRuleHandler(report, prop5); + assertEquals(0, andor.getReplies().size()); + + andor.applyRuleHandler(report1, prop6); + assertEquals(1, andor.getReplies().size()); } @Test - public void testTwo() { - andor.setAndOrArgs(10); - andor.setAndOrMax(5); - andor.setAndOrMin(3); - andor.applyRuleHandler(report10, fido); - andor.applyRuleHandler(report11, fido); - andor.applyRuleHandler(report12, fido); - andor.applyRuleHandler(report13, fido); - andor.applyRuleHandler(report14, fido); - andor.applyRuleHandler(report15, fido); - andor.applyRuleHandler(report16, fido); - andor.applyRuleHandler(report17, fido); - andor.applyRuleHandler(report18, fido); - andor.applyRuleHandler(report19, fido); - //assertEquals(8, andor.getPos()); - //assertEquals(2, andor.getNeg()); - assertEquals(3,andor.getAndOrMin()); - assertEquals(5,andor.getAndOrMax()); - assertEquals(10,andor.getAndOrArgs()); - assertEquals(false, andor.isSign()); - andor.clrAll(); + public void test2() { + } - @Test - public void testThree() { - andor.setAndOrArgs(10); - andor.setAndOrMax(5); - andor.setAndOrMin(3); + /*@Test + public void test3() { andor.applyRuleHandler(report20, fido); andor.applyRuleHandler(report21, fido); andor.applyRuleHandler(report22, fido); @@ -382,13 +304,10 @@ public void testThree() { andor.applyRuleHandler(report28, fido); //assertEquals(1, andor.getPos()); //assertEquals(8, andor.getNeg()); - assertEquals(3,andor.getAndOrMin()); - assertEquals(5,andor.getAndOrMax()); - assertEquals(10,andor.getAndOrArgs()); - assertEquals(false, andor.isSign()); - andor.clrAll(); - } - - + }*/ + public void tearDown() { + Network.clearNetwork(); + andor.clear(); + } } \ No newline at end of file diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index 2e3597e5..4f15cf1e 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -49,7 +49,7 @@ public class NumericalEntailmentTests extends TestCase { private static Node fido, var, dog, barks; private static Node prop1, prop2, prop3, prop4; private static RuleUseInfo rui; - private static Report report; + private static Report report, report1; public void setUp() { try { @@ -119,51 +119,51 @@ public void setUp() { assertNotNull(e1.getMessage(), e1); } - LinkedList dcList = new LinkedList(); - NodeSet nodeSet1 = new NodeSet(); - DownCable dc1; DownCableSet dcs; + LinkedList dcList = new LinkedList(); + NodeSet nodeSet1 = new NodeSet(); + DownCable dc1; DownCableSet dcs; - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new PropositionNode(new Closed("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new PropositionNode(new Closed("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new PropositionNode(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new PropositionNode(new Closed("Prop4", dcs)); - dcList.clear(); - //------------------------------------------------------------// + nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop1 = new PropositionNode(new Closed("Prop1", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameCD); + prop2 = new PropositionNode(new Closed("Prop2", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new PropositionNode(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMD); + prop4 = new PropositionNode(new Closed("Prop4", dcs)); + dcList.clear(); + //------------------------------------------------------------// //------------------------- Numerical Setup ------------------------------------// @@ -200,11 +200,22 @@ public void setUp() { /*try { support.add(prop1.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - // TODO Auto-generated catch block e.printStackTrace(); }*/ report = new Report(sub, support, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub1 = new LinearSubstitutions(); + PropositionSet support1 = new PropositionSet(); + /*try { + support.add(prop3.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }*/ + sub1.putIn(new Binding((VariableNode) var, fido)); + report1 = new Report(sub1, support1, true, InferenceTypes.BACKWARD); + } /*@Test @@ -246,18 +257,7 @@ public void testApplyRuleHandler3() { numerical.getRuisHandler() instanceof SIndex); assertEquals(SIndex.SINGLETON, ((SIndex) (numerical.getRuisHandler())).getRuiHandlerType()); - LinearSubstitutions sub = new LinearSubstitutions(); - PropositionSet support = new PropositionSet(); - /*try { - support.add(prop3.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - }*/ - sub.putIn(new Binding((VariableNode) var, fido)); - report = new Report(sub, support, true, InferenceTypes.BACKWARD); - - numerical.applyRuleHandler(report, prop3); + numerical.applyRuleHandler(report1, prop3); assertEquals(1, numerical.getReplies().size()); } @@ -272,7 +272,7 @@ public void testGetDownNodeSet() { assertEquals(3, numerical.getDownAntNodeSet().size()); }*/ - public void tearDown(){ + public void tearDown() { Network.clearNetwork(); numerical.clear(); fido = null; From 27fe49ec1ade2c8614432bdd6dab97a8c3bd84be Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sat, 18 May 2019 00:41:40 +0200 Subject: [PATCH 15/22] AndOr tests --- src/sneps/network/RuleNode.java | 33 +++- src/sneps/snip/rules/AndOrEntailment.java | 22 --- src/sneps/snip/rules/NumericalEntailment.java | 25 +-- src/sneps/snip/rules/ThreshEntailment.java | 27 ---- tests/AndOrTest.java | 150 +++++++++++++----- tests/NumericalEntailmentTests.java | 4 +- 6 files changed, 144 insertions(+), 117 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 6e50bd92..1d166cab 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -133,6 +133,10 @@ public NodeSet getAntsWithVars() { public void setAntecedents(NodeSet antecedents) { this.antecedents = antecedents; } + + public NodeSet getConsequents() { + return consequents; + } public void setConsequents(NodeSet consequents) { this.consequents = consequents; @@ -200,11 +204,11 @@ public ArrayList applyRuleHandler(Report report, Node signature) { // This is the first report received by this RuleNode, so // a RuisHandler is created - if(ruisHandler == null) { + if(ruisHandler == null) ruisHandler = addRuiHandler(); - } if(antNodesWithoutVars.contains(signature)) { + System.out.println("CONSTANT"); addConstantRui(rui); if (ruisHandler.isEmpty()) { response = applyRuleOnRui(constantRUI); @@ -221,6 +225,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { } } else { + System.out.println("VAR"); // The RUI created for the given report is inserted to the RuisHandler RuleUseInfoSet res = ruisHandler.insertRUI(rui); @@ -259,7 +264,8 @@ public ArrayList applyRuleHandler(Report report, Node signature) { public void clear() { if(ruisHandler != null) ruisHandler.clear(); - constantRUI = null; + constantRUI = null; + reportsToBeSent.clear(); } /** @@ -466,7 +472,26 @@ public RuleUseInfo getConstantRui() { return constantRUI; } - protected abstract Set getOutgoingChannelsForReport(Report r); + protected Set getOutgoingChannelsForReport(Report r) { + Set outgoingChannels = getOutgoingRuleConsequentChannels(); + Set replyChannels = new HashSet(); + for(Node n : consequents) { + for(Channel c : outgoingChannels) { + if(c.getRequester().getId() == n.getId() && + r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { + replyChannels.add(c); + break; + } + } + + Channel ch = establishChannel(ChannelTypes.RuleCons, n, + new LinearSubstitutions(), (LinearSubstitutions) + r.getSubstitutions(), Controller.getCurrentContext(), -1); + replyChannels.add(ch); + } + + return replyChannels; + } @Override public void processRequests() { diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index 5acb9f5e..abf7c4da 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -94,28 +94,6 @@ protected byte getSIndexType() { return SIndex.RUIS; } - @Override - protected Set getOutgoingChannelsForReport(Report r) { - Set outgoingChannels = getOutgoingRuleConsequentChannels(); - Set replyChannels = new HashSet(); - for(Node n : consequents) { - for(Channel c : outgoingChannels) { - if(c.getRequester().getId() == n.getId() && - r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { - replyChannels.add(c); - break; - } - } - - Channel ch = establishChannel(ChannelTypes.RuleCons, n, - new LinearSubstitutions(), (LinearSubstitutions) - r.getSubstitutions(), Controller.getCurrentContext(), -1); - replyChannels.add(ch); - } - - return replyChannels; - } - public void setMin(int min) { this.min = min; } diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index dcc1a800..57ab2b7c 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -154,8 +154,8 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { RuleResponse r = new RuleResponse(); r.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - r.addAllChannels(forwardChannels); + //Set forwardChannels = getOutgoingChannelsForReport(reply); + //r.addAllChannels(forwardChannels); return r; } @@ -185,27 +185,6 @@ protected byte getSIndexType() { return SIndex.RUIS; } - protected Set getOutgoingChannelsForReport(Report r) { - Set outgoingChannels = getOutgoingRuleConsequentChannels(); - Set replyChannels = new HashSet(); - for(Node n : consequents) { - for(Channel c : outgoingChannels) { - if(c.getRequester().getId() == n.getId() && - r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { - replyChannels.add(c); - break; - } - } - - Channel ch = establishChannel(ChannelTypes.RuleCons, n, - new LinearSubstitutions(), (LinearSubstitutions) - r.getSubstitutions(), Controller.getCurrentContext(), -1); - replyChannels.add(ch); - } - - return replyChannels; - } - /** * Getter for i * @return diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index 797e75bf..3b64b0b2 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -1,23 +1,18 @@ package sneps.snip.rules; -import java.util.HashSet; import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; -import sneps.network.Node; import sneps.network.RuleNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.term.Molecular; -import sneps.snebr.Controller; import sneps.snip.Report; import sneps.snip.channels.Channel; -import sneps.snip.channels.ChannelTypes; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.matching.LinearSubstitutions; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; @@ -92,28 +87,6 @@ protected RuisHandler createRuisHandler() { protected byte getSIndexType() { return SIndex.RUIS; } - - @Override - protected Set getOutgoingChannelsForReport(Report r) { - Set outgoingChannels = getOutgoingRuleConsequentChannels(); - Set replyChannels = new HashSet(); - for(Node n : consequents) { - for(Channel c : outgoingChannels) { - if(c.getRequester().getId() == n.getId() && - r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { - replyChannels.add(c); - break; - } - } - - Channel ch = establishChannel(ChannelTypes.RuleCons, n, - new LinearSubstitutions(), (LinearSubstitutions) - r.getSubstitutions(), Controller.getCurrentContext(), -1); - replyChannels.add(ch); - } - - return replyChannels; - } public void setMin(int min) { this.min = min; diff --git a/tests/AndOrTest.java b/tests/AndOrTest.java index 72ee407a..7fc22d00 100644 --- a/tests/AndOrTest.java +++ b/tests/AndOrTest.java @@ -1,5 +1,4 @@ -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.*; import java.util.ArrayList; import java.util.LinkedList; @@ -9,7 +8,6 @@ import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -20,13 +18,10 @@ import sneps.network.cables.DownCable; import sneps.network.cables.DownCableSet; import sneps.network.classes.CaseFrame; -import sneps.network.classes.RCFP; import sneps.network.classes.Relation; -import sneps.network.classes.RelationsRestrictedCaseFrame; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; import sneps.network.classes.term.Closed; -import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.FlagNodeSet; @@ -37,25 +32,20 @@ import sneps.snip.InferenceTypes; import sneps.snip.Report; import sneps.snip.classes.FlagNode; -import sneps.snip.classes.RuleUseInfo; +import sneps.snip.classes.SIndex; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; import sneps.snip.rules.AndOrEntailment; -import sneps.snip.rules.OrEntailment; public class AndOrTest { private static Context context; private static String contextName = "TempContext"; private static AndOrEntailment andor; - private static Node fido, var, dog, barks, animal, veg, mineral; - private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7; - private static Report report; - private static Report report1, report2, report3, report4, report5, report6, - report7, report8, report9, report10, report11, report12, report13, report14, - report15, report16, report17, report18, report19, report20, report21, report22, - report23, report24, report25, report26, report27, report28; - + private static Node fido, var, dog, barks, animal, veg, mineral, human; + private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8; + private static Report report, report1, report2; + @BeforeClass public static void setUp() throws Exception { try { @@ -65,8 +55,6 @@ public static void setUp() throws Exception { } LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - FlagNode fn; PropositionSet support = new PropositionSet(); ArrayList wires = new ArrayList(); LinkedList dc = new LinkedList(); @@ -89,7 +77,7 @@ public static void setUp() throws Exception { rels.clear(); rels.add(argsRel); CaseFrame caseFrameArgs = Network.defineCaseFrame("Args", rels); Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; - Wire wire5 = null, wire6 = null, wire7 = null; + Wire wire5 = null, wire6 = null, wire7 = null, wire8 = null; rels.clear(); @@ -106,9 +94,11 @@ public static void setUp() throws Exception { animal = Network.buildBaseNode("Animal", new Semantic("Base")); veg = Network.buildBaseNode("Vegetable", new Semantic("Base")); mineral = Network.buildBaseNode("Mineral", new Semantic("Base")); + human = Network.buildBaseNode("Human", new Semantic("Base")); wire5 = new Wire(classRel, animal); wire6 = new Wire(classRel, veg); wire7 = new Wire(classRel, mineral); + wire8 = new Wire(classRel, human); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); @@ -137,6 +127,9 @@ public static void setUp() throws Exception { wires.clear(); wires.add(wire4); wires.add(wire7); prop7 = Network.buildMolecularNode(wires, caseFrameArgs); + + wires.clear(); wires.add(wire4); wires.add(wire8); + prop8 = Network.buildMolecularNode(wires, caseFrameArgs); } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); @@ -219,7 +212,17 @@ public static void setUp() throws Exception { prop7 = new Node(new Open("Prop7", dcs)); ((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); - + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(human); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop8 = new Node(new Open("Prop8", dcs)); + ((Open) (prop8.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); //---------------------------------------------------------------------------------// /*nodeSet.addNode(prop1); @@ -240,19 +243,14 @@ public static void setUp() throws Exception { nodeSet.addNode(prop5); nodeSet.addNode(prop6); nodeSet.addNode(prop7); + nodeSet.addNode(prop8); dc.add(new DownCable(argsRel, nodeSet)); DownCableSet dcss = new DownCableSet(dc, caseFrameArgs); - - NodeSet a = new NodeSet(); - a.addNode(prop5); - a.addNode(prop6); - a.addNode(prop7); //---------------------------- ANDOR -----------------------------------// andor = new AndOrEntailment(new Open("Open", dcss)); - andor.setAntecedents(a); andor.setMax(2); andor.setMin(1); @@ -277,34 +275,108 @@ public static void setUp() throws Exception { report1 = new Report(sub, support, true, InferenceTypes.BACKWARD); } + @Test + public void testProcessNodes() { + assertEquals(0, andor.getConsequents().size()); + assertEquals(4, andor.getAntecedents().size()); + assertEquals(4, andor.getAntsWithVars().size()); + assertEquals(0, andor.getAntsWithoutVars().size()); + } + + /** + * First inference rule allowed by andor (j args are true, then the other n-j + * have to be false). + */ @Test public void test() { andor.applyRuleHandler(report, prop5); + assertNotNull("Null RuisHandler", andor.getRuisHandler()); + assertTrue("addRuiHandler doesn't create an SIndex as a RuisHandler", + andor.getRuisHandler() instanceof SIndex); + assertEquals(SIndex.SINGLETON, ((SIndex) (andor.getRuisHandler())).getRuiHandlerType()); assertEquals(0, andor.getReplies().size()); andor.applyRuleHandler(report1, prop6); assertEquals(1, andor.getReplies().size()); + assertEquals(false, andor.getReplies().get(0).getSign()); + assertEquals(2, andor.getConsequents().size()); + assertEquals(prop7, andor.getConsequents().getNode(0)); + assertEquals(prop8, andor.getConsequents().getNode(1)); } + /** + * Second inference rule allowed by andor (n-i args are false, then the other i + * have to be true). + */ @Test public void test2() { + andor.clear(); + LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report = new Report(sub, support, false, InferenceTypes.FORWARD); + + support = new PropositionSet(); + /*try { + support.add(prop7.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + report1 = new Report(sub, support, false, InferenceTypes.BACKWARD); + support = new PropositionSet(); + /*try { + support.add(prop8.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + report2 = new Report(sub, support, false, InferenceTypes.BACKWARD); + + andor.applyRuleHandler(report, prop5); + assertEquals(0, andor.getReplies().size()); + + andor.applyRuleHandler(report1, prop7); + assertEquals(0, andor.getReplies().size()); + + andor.applyRuleHandler(report2, prop8); + assertEquals(1, andor.getReplies().size()); + assertEquals(true, andor.getReplies().get(0).getSign()); + assertEquals(1, andor.getConsequents().size()); + assertEquals(prop6, andor.getConsequents().getNode(0)); } - /*@Test + /** + * Min and Max are both equal to 1. + */ + @Test public void test3() { - andor.applyRuleHandler(report20, fido); - andor.applyRuleHandler(report21, fido); - andor.applyRuleHandler(report22, fido); - andor.applyRuleHandler(report23, fido); - andor.applyRuleHandler(report24, fido); - andor.applyRuleHandler(report25, fido); - andor.applyRuleHandler(report26, fido); - andor.applyRuleHandler(report27, fido); - andor.applyRuleHandler(report28, fido); - //assertEquals(1, andor.getPos()); - //assertEquals(8, andor.getNeg()); - }*/ + andor.clear(); + andor.setMax(1); + andor.setMin(1); + LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); + + andor.applyRuleHandler(report, prop5); + assertEquals(1, andor.getReplies().size()); + assertEquals(false, andor.getReplies().get(0).getSign()); + assertEquals(3, andor.getConsequents().size()); + } public void tearDown() { Network.clearNetwork(); diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index 4f15cf1e..992008f9 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -237,7 +237,7 @@ public void testApplyRuleHandler() { /*@Test public void testApplyRuleHandler2() { numerical.setI(2); - numerical.getReplies().clear(); + numerical.clear(); numerical.applyRuleHandler(report, prop1); @@ -247,7 +247,7 @@ public void testApplyRuleHandler2() { @Test public void testApplyRuleHandler3() { numerical.setI(2); - numerical.getReplies().clear(); + numerical.clear(); numerical.applyRuleHandler(report, prop1); From b76161ecd8676f5b7b281e9d17f20fae8e6e263a Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sat, 18 May 2019 03:37:20 +0200 Subject: [PATCH 16/22] Thresh and some tests for it --- src/sneps/snip/rules/ThreshEntailment.java | 10 +- tests/AndOrTest.java | 23 + tests/ThreshTest.java | 517 +++++++++------------ 3 files changed, 246 insertions(+), 304 deletions(-) diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index 3b64b0b2..6f8676cb 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -25,10 +25,10 @@ public class ThreshEntailment extends RuleNode { public ThreshEntailment(Molecular syn) { super(syn); - NodeSet minNode = getDownNodeSet("min"); + /*NodeSet minNode = getDownNodeSet("min"); min = Integer.parseInt(minNode.getNode(0).getIdentifier()); NodeSet maxNode = getDownNodeSet("max"); - max = Integer.parseInt(maxNode.getNode(0).getIdentifier()); + max = Integer.parseInt(maxNode.getNode(0).getIdentifier());*/ antecedents = getDownAntNodeSet(); processNodes(antecedents); } @@ -55,15 +55,17 @@ else if (rui.getPosCount() != min - 1 // Add rule node to replySupport consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); + System.out.println(consequents); Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, rui.getType()); reportsToBeSent.add(reply); + System.out.println(reply); RuleResponse r = new RuleResponse(); r.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - r.addAllChannels(forwardChannels); + //Set forwardChannels = getOutgoingChannelsForReport(reply); + //r.addAllChannels(forwardChannels); return r; } diff --git a/tests/AndOrTest.java b/tests/AndOrTest.java index 7fc22d00..2750388c 100644 --- a/tests/AndOrTest.java +++ b/tests/AndOrTest.java @@ -378,6 +378,29 @@ public void test3() { assertEquals(3, andor.getConsequents().size()); } + /** + * Min and Max are both equal to 0. + */ + @Test + public void test4() { + andor.clear(); + andor.setMax(0); + andor.setMin(0); + LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); + + andor.applyRuleHandler(report, prop5); + assertEquals(0, andor.getReplies().size()); + } + public void tearDown() { Network.clearNetwork(); andor.clear(); diff --git a/tests/ThreshTest.java b/tests/ThreshTest.java index 790b1113..310fbbda 100644 --- a/tests/ThreshTest.java +++ b/tests/ThreshTest.java @@ -1,123 +1,61 @@ import static org.junit.Assert.*; -import java.lang.reflect.Field; import java.util.ArrayList; -import java.util.HashSet; import java.util.LinkedList; -import java.util.Set; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Network; import sneps.network.Node; -import sneps.network.PropositionNode; -import sneps.network.RuleNode; import sneps.network.VariableNode; import sneps.network.cables.DownCable; import sneps.network.cables.DownCableSet; import sneps.network.classes.CaseFrame; -import sneps.network.classes.RCFP; import sneps.network.classes.Relation; -import sneps.network.classes.RelationsRestrictedCaseFrame; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; -import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Term; import sneps.network.classes.term.Variable; -import sneps.network.classes.setClasses.ContextRuisSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; import sneps.snebr.Controller; -import sneps.snebr.Support; +import sneps.snip.InferenceTypes; import sneps.snip.Report; import sneps.snip.classes.FlagNode; -import sneps.snip.classes.RuisHandler; -import sneps.snip.classes.RuleUseInfo; +import sneps.snip.classes.SIndex; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.rules.OrEntailment; +import sneps.snip.rules.AndOrEntailment; import sneps.snip.rules.ThreshEntailment; public class ThreshTest { - private static Context context; private static String contextName = "TempContext"; private static ThreshEntailment thresh; - private static Node fido, var, dog, barks; - private static Node prop1, prop2, prop3, prop4; - private static RuleUseInfo rui; - private static Report report; - private static Report report1; - private static Report report2; - private static Report report3; - private static Report report4; - private static Report report5; - private static Report report6; - private static Report report7; - private static Report report8; - private static Report report9; - private static Report report10; - private static Report report11; - private static Report report12; - private static Report report13; - private static Report report14; - private static Report report15; - private static Report report16; - private static Report report17; - private static Report report18; - private static Report report19; - private static Report report20; - private static Report report21; - private static Report report22; - private static Report report23; - private static Report report24; - private static Report report25; - private static Report report26; - private static Report report27; - private static Report report28; - + private static Node fido, var, dog, barks, animal, veg, mineral, human; + private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8; + private static Report report, report1, report2; @BeforeClass - public static void BuildNetwork() throws Exception { - - - /** - * building context - */ - + public static void setUp() throws Exception { try { context = Controller.createContext(contextName); } catch (DuplicateContextNameException e1) { assertNotNull(e1.getMessage(), e1); } - - /** - * Create substitutions, - * FlagNodeSet, - * FlagNode, - * PropositionSet, - * wires, - * relations, - * caseFrames - */ - LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - FlagNode fn; PropositionSet support = new PropositionSet(); ArrayList wires = new ArrayList(); LinkedList dc = new LinkedList(); @@ -126,8 +64,9 @@ public static void BuildNetwork() throws Exception { Relation memberRel = Network.defineRelation("Member", "NodeSet"); Relation classRel = Network.defineRelation("Class", "NodeSet"); Relation doesRel = Network.defineRelation("Does", "NodeSet"); - Relation antsRel = Network.defineRelation("Tant", "Tant"); - Relation consRel = Network.defineRelation("Tconsq", "Tconsq"); + Relation antsRel = Network.defineRelation("Xant", "Xant"); + Relation consRel = Network.defineRelation("Xconsq", "Xconsq"); + Relation argsRel = Network.defineRelation("arg", "arg"); rels.add(memberRel); rels.add(classRel); CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); rels.clear(); rels.add(classRel); rels.add(doesRel); @@ -136,31 +75,31 @@ public static void BuildNetwork() throws Exception { CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); rels.clear(); rels.add(antsRel); rels.add(consRel); CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); + rels.clear(); rels.add(argsRel); + CaseFrame caseFrameArgs = Network.defineCaseFrame("Args", rels); Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; + Wire wire5 = null, wire6 = null, wire7 = null, wire8 = null; rels.clear(); - - - /** - * Building propositions & base nodes, - * adding wires - */ - - - - - - try { var = Network.buildVariableNode("X"); fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - barks = Network.buildBaseNode("Barks", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); + dog = Network.buildBaseNode("Dog", new Semantic("Base")); + barks = Network.buildBaseNode("Barks", new Semantic("Base")); wire1 = new Wire(memberRel, fido); wire2 = new Wire(classRel, dog); wire3 = new Wire(doesRel, barks); wire4 = new Wire(memberRel, var); + + animal = Network.buildBaseNode("Animal", new Semantic("Base")); + veg = Network.buildBaseNode("Vegetable", new Semantic("Base")); + mineral = Network.buildBaseNode("Mineral", new Semantic("Base")); + human = Network.buildBaseNode("Human", new Semantic("Base")); + wire5 = new Wire(classRel, animal); + wire6 = new Wire(classRel, veg); + wire7 = new Wire(classRel, mineral); + wire8 = new Wire(classRel, human); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); @@ -168,14 +107,6 @@ public static void BuildNetwork() throws Exception { } - - - - - - - - try { wires.clear(); wires.add(wire1); wires.add(wire2); prop1 = Network.buildMolecularNode(wires, caseFrameMC); @@ -188,230 +119,216 @@ public static void BuildNetwork() throws Exception { wires.clear(); wires.add(wire1); wires.add(wire3); prop4 = Network.buildMolecularNode(wires, caseFrameMD); + + wires.clear(); wires.add(wire4); wires.add(wire5); + prop5 = Network.buildMolecularNode(wires, caseFrameArgs); + + wires.clear(); wires.add(wire4); wires.add(wire6); + prop6 = Network.buildMolecularNode(wires, caseFrameArgs); + + wires.clear(); wires.add(wire4); wires.add(wire7); + prop7 = Network.buildMolecularNode(wires, caseFrameArgs); + + wires.clear(); wires.add(wire4); wires.add(wire8); + prop8 = Network.buildMolecularNode(wires, caseFrameArgs); } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); - LinkedList dcList = new LinkedList(); - NodeSet nodeSet1 = new NodeSet(); - DownCable dc1; DownCableSet dcs; - - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Open("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Open("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Open("Prop4", dcs)); - dcList.clear(); } - - try { - support.add(prop1.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop1, support, 1); - fns.insert(fn); + LinkedList dcList = new LinkedList(); + NodeSet nodeSet1 = new NodeSet(); + DownCable dc1; DownCableSet dcs; - support.clearSet(); - try { - support.add(prop2.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop2, support, 1); - fns.insert(fn); - - support.clearSet(); - try { - support.add(prop3.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(prop3, support, 1); - fns.insert(fn); - - nodeSet.addNode(prop1); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop2); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop3); - dc.add(new DownCable(antsRel, nodeSet)); + nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop1 = new Node(new Closed("Prop1", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameCD); + prop2 = new Node(new Closed("Prop2", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(dog); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new Node(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMD); + prop4 = new Node(new Closed("Prop4", dcs)); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(animal); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop5 = new Node(new Open("Prop5", dcs)); + ((Open) (prop5.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(veg); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop6 = new Node(new Open("Prop6", dcs)); + ((Open) (prop6.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(mineral); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop7 = new Node(new Open("Prop7", dcs)); + ((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(human); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop8 = new Node(new Open("Prop8", dcs)); + ((Open) (prop8.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + dcList.clear(); +//---------------------------------------------------------------------------------// + + nodeSet.addNode(prop5); + nodeSet.addNode(prop6); + nodeSet.addNode(prop7); + nodeSet.addNode(prop8); + dc.add(new DownCable(argsRel, nodeSet)); - nodeSet.clear(); - nodeSet.addNode(prop4); - dc.add(new DownCable(consRel, nodeSet)); + DownCableSet dcss = new DownCableSet(dc, caseFrameArgs); - DownCableSet dcs = new DownCableSet(dc, caseFrameAC); +//---------------------------- THRESH -----------------------------------// + thresh = new ThreshEntailment(new Open("Open", dcss)); + thresh.setMax(3); + thresh.setMin(2); - /** - * Thresh-entailment - */ - - - thresh= new ThreshEntailment(new Open("Open", dcs)); - - try { - support.add(dog.getId()); - support.add(fido.getId()); - support.add(var.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException + sub = new LinearSubstitutions(); + support = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - - sub.insert(new Binding((VariableNode) var,fido)); - rui = new RuleUseInfo(sub, 2, 8, fns); - report = new Report(sub, support, false, contextName); - - report = new Report(sub, support, true, contextName); - report1 = new Report(sub, support, true, contextName); - report2 = new Report(sub, support, false, contextName); - report3 = new Report(sub, support, false, contextName); - report4 = new Report(sub, support, false, contextName); - report5 = new Report(sub, support, false, contextName); - report6 = new Report(sub, support, false, contextName); - report7 = new Report(sub, support, false, contextName); - report8 = new Report(sub, support, false, contextName); - report9 = new Report(sub, support, false, contextName); - - - report10 = new Report(sub, support, true, contextName); - report11 = new Report(sub, support, true, contextName); - report12 = new Report(sub, support, true, contextName); - report13 = new Report(sub, support, true, contextName); - report14 = new Report(sub, support, false, contextName); - report15 = new Report(sub, support, false, contextName); - report16 = new Report(sub, support, false, contextName); - report17 = new Report(sub, support, false, contextName); - report18 = new Report(sub, support, false, contextName); - report19 = new Report(sub, support, false, contextName); - - - report20 = new Report(sub, support, true, contextName); - report21 = new Report(sub, support, true, contextName); - report22 = new Report(sub, support, true, contextName); - report23 = new Report(sub, support, true, contextName); - report24 = new Report(sub, support, false, contextName); - report25 = new Report(sub, support, false, contextName); - report26 = new Report(sub, support, false, contextName); - report27 = new Report(sub, support, false, contextName); - report28 = new Report(sub, support, false, contextName); - - thresh.setThreshArgs(10); - thresh.setThreshMax(6); - thresh.setThreshMin(3); + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report = new Report(sub, support, false, InferenceTypes.BACKWARD); + + support = new PropositionSet(); + /*try { + support.add(prop6.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + report1 = new Report(sub, support, true, InferenceTypes.BACKWARD); } - @Test + public void testProcessNodes() { + assertEquals(0, thresh.getConsequents().size()); + assertEquals(4, thresh.getAntecedents().size()); + assertEquals(4, thresh.getAntsWithVars().size()); + assertEquals(0, thresh.getAntsWithoutVars().size()); + } + + /** + * First inference rule allowed by thresh (n-j args are false, and i-1 args are + * true, then the other j-i+1 have to be false). + */ + /*@Test public void test() { - thresh.setThreshArgs(10); - thresh.setThreshMax(6); - thresh.setThreshMin(3); - thresh.applyRuleHandler(report, fido); - thresh.applyRuleHandler(report1, fido); - thresh.applyRuleHandler(report2, fido); - thresh.applyRuleHandler(report3, fido); - thresh.applyRuleHandler(report4, fido); - thresh.applyRuleHandler(report5, fido); - thresh.applyRuleHandler(report6, fido); - thresh.applyRuleHandler(report7, fido); - thresh.applyRuleHandler(report8, fido); - thresh.applyRuleHandler(report9, fido); - //assertEquals(2, thresh.getPos()); - //assertEquals(8, thresh.getNeg()); - assertEquals(3,thresh.getThreshMin()); - assertEquals(6,thresh.getThreshMax()); - assertEquals(10,thresh.getThreshArgs()); - assertEquals(true, thresh.getSign()); - thresh.clrAll(); - } + thresh.applyRuleHandler(report, prop5); + assertNotNull("Null RuisHandler", thresh.getRuisHandler()); + assertTrue("addRuiHandler doesn't create an SIndex as a RuisHandler", + thresh.getRuisHandler() instanceof SIndex); + assertEquals(SIndex.SINGLETON, ((SIndex) (thresh.getRuisHandler())).getRuiHandlerType()); + assertEquals(0, thresh.getReplies().size()); + + thresh.applyRuleHandler(report1, prop6); + assertEquals(1, thresh.getReplies().size()); + assertEquals(false, thresh.getReplies().get(0).getSign()); + assertEquals(2, thresh.getConsequents().size()); + assertEquals(prop7, thresh.getConsequents().getNode(0)); + assertEquals(prop8, thresh.getConsequents().getNode(1)); + }*/ + /** + * Second inference rule allowed by thresh (i args are true, and n-j-1 args are + * false, then the other j-i+1 args have to be true). + */ @Test - public void testTwo() { - thresh.setThreshArgs(10); - thresh.setThreshMax(6); - thresh.setThreshMin(3); - thresh.applyRuleHandler(report10, fido); - thresh.applyRuleHandler(report11, fido); - thresh.applyRuleHandler(report12, fido); - thresh.applyRuleHandler(report13, fido); - thresh.applyRuleHandler(report14, fido); - thresh.applyRuleHandler(report15, fido); - thresh.applyRuleHandler(report16, fido); - thresh.applyRuleHandler(report17, fido); - thresh.applyRuleHandler(report18, fido); - thresh.applyRuleHandler(report19, fido); - //assertEquals(4, thresh.getPos()); - //assertEquals(6, thresh.getNeg()); - assertEquals(3,thresh.getThreshMin()); - assertEquals(6,thresh.getThreshMax()); - assertEquals(10,thresh.getThreshArgs()); - assertEquals(false, thresh.getSign()); - thresh.clrAll(); + public void test2() { + thresh.clear(); + LinearSubstitutions sub = new LinearSubstitutions(); + PropositionSet support = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); + + thresh.applyRuleHandler(report, prop5); + + sub = new LinearSubstitutions(); + support = new PropositionSet(); + /*try { + support.add(prop6.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub.putIn(new Binding((VariableNode) var, fido)); + report1 = new Report(sub, support, true, InferenceTypes.BACKWARD); + + thresh.applyRuleHandler(report, prop6); + assertEquals(1, thresh.getReplies().size()); + assertEquals(true, thresh.getReplies().get(0).getSign()); + assertEquals(2, thresh.getConsequents().size()); } - @Test - public void testThree() { - thresh.setThreshArgs(10); - thresh.setThreshMax(6); - thresh.setThreshMin(3); - thresh.applyRuleHandler(report20, fido); - thresh.applyRuleHandler(report21, fido); - thresh.applyRuleHandler(report22, fido); - thresh.applyRuleHandler(report23, fido); - thresh.applyRuleHandler(report24, fido); - thresh.applyRuleHandler(report25, fido); - thresh.applyRuleHandler(report26, fido); - thresh.applyRuleHandler(report27, fido); - thresh.applyRuleHandler(report28, fido); - //assertEquals(4, thresh.getPos()); - //assertEquals(5, thresh.getNeg()); - assertEquals(3,thresh.getThreshMin()); - assertEquals(6,thresh.getThreshMax()); - assertEquals(10,thresh.getThreshArgs()); - assertEquals(false, thresh.getSign()); - thresh.clrAll(); + public void tearDown() { + Network.clearNetwork(); + thresh.clear(); } } \ No newline at end of file From 10482451d964841141f406af48b732579652cbc4 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Sat, 18 May 2019 05:46:45 +0200 Subject: [PATCH 17/22] Reimplement getSharedVarsNodes() in RuleNode --- src/sneps/network/Node.java | 2 +- src/sneps/network/RuleNode.java | 117 +++++++++++------- .../classes/setClasses/ContextRuisSet.java | 74 ----------- src/sneps/network/classes/term/Molecular.java | 4 +- src/sneps/snip/classes/PTree.java | 3 +- src/sneps/snip/classes/SIndex.java | 7 +- tests/SIndexTest.java | 6 +- 7 files changed, 85 insertions(+), 128 deletions(-) delete mode 100644 src/sneps/network/classes/setClasses/ContextRuisSet.java diff --git a/src/sneps/network/Node.java b/src/sneps/network/Node.java index bb329d00..371ea7a8 100644 --- a/src/sneps/network/Node.java +++ b/src/sneps/network/Node.java @@ -271,7 +271,7 @@ public void setTemp(boolean temp) { * class and pass the current node as a parameter. */ public void updateUpCables() { - ((Molecular) this.getTerm()).updateUpCables(this); + //((Molecular) this.getTerm()).updateUpCables(this); } public boolean hasSameFreeVariablesAs(Node n) { diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 1d166cab..3c5c64ff 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -1,15 +1,11 @@ package sneps.network; import java.io.Serializable; + import java.util.ArrayList; -import java.util.Collection; import java.util.HashSet; -import java.util.Hashtable; import java.util.Set; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; -import sneps.network.classes.setClasses.ContextRuisSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; @@ -18,9 +14,8 @@ import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.snebr.Context; +import sneps.network.classes.term.Variable; import sneps.snebr.Controller; -import sneps.snebr.Support; import sneps.snip.Report; import sneps.snip.channels.AntecedentToRuleChannel; import sneps.snip.channels.Channel; @@ -32,6 +27,7 @@ import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; import sneps.snip.matching.LinearSubstitutions; +import sneps.snip.rules.OrEntailment; public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; @@ -56,7 +52,7 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ /** * A VarNodeSet of the common free VariableNodes shared between the antecedents. */ - protected VarNodeSet sharedVars; + protected Set sharedVars; /** * A ContextRuisSet that is used to map each context to its appropriate @@ -89,7 +85,7 @@ public RuleNode() { antNodesWithVars = new NodeSet(); antNodesWithVarsIDs = new HashSet(); shareVars = false; - sharedVars = new VarNodeSet(); + sharedVars = new HashSet(); reportsToBeSent = new ArrayList(); } @@ -102,7 +98,7 @@ public RuleNode(Molecular syn) { antNodesWithVars = new NodeSet(); antNodesWithVarsIDs = new HashSet(); shareVars = false; - sharedVars = new VarNodeSet(); + sharedVars = new HashSet(); reportsToBeSent = new ArrayList(); } @@ -297,49 +293,40 @@ public void addAntecedent(Node ant) { } /** - * Returns a VarNodeSet of VariableNodes that are shared among some/all the Nodes - * in the given NodeSet. + * Returns a VarNodeSet of VariableNodes that are shared among all the Nodes in + * the given NodeSet. * * @param nodes * @return VarNodeSet */ - public VarNodeSet getSharedVarsNodes(NodeSet nodes) { - VarNodeSet res = new VarNodeSet(); - VarNodeSet temp = new VarNodeSet(); + public static Set getSharedVarsNodes(NodeSet nodes) { + Set res = new HashSet(); if (nodes.isEmpty()) return res; - if(nodes.size() == 1) { - if(nodes.getNode(0) instanceof VariableNode) { - res.addVarNode((VariableNode) nodes.getNode(0)); - return res; - } - - if(nodes.getNode(0).getTerm() instanceof Open) { - VarNodeSet free = ((Open) nodes.getNode(0).getTerm()).getFreeVariables(); - for(VariableNode var : free) - res.addVarNode(var); - return res; - } + if(nodes.getNode(0) instanceof VariableNode) + res.add((VariableNode) nodes.getNode(0)); + else if(nodes.getNode(0).getTerm() instanceof Open) { + VarNodeSet freeVars = ((Open) nodes.getNode(0).getTerm()).getFreeVariables(); + for(VariableNode v : freeVars) + res.add(v); } - - for(Node curNode : nodes) { - if(curNode instanceof VariableNode) { - if(temp.contains((VariableNode) curNode)) - res.addVarNode((VariableNode) curNode); - else - temp.addVarNode((VariableNode) curNode); + + if(nodes.size() == 1) + return res; + + for(int i = 1; i < nodes.size(); i++) { + Set vars = new HashSet(); + if(nodes.getNode(i) instanceof VariableNode) + vars.add((VariableNode) nodes.getNode(i)); + else if(nodes.getNode(i).getTerm() instanceof Open) { + VarNodeSet freeVars = ((Open) nodes.getNode(i).getTerm()).getFreeVariables(); + for(VariableNode v : freeVars) + vars.add(v); } - if(curNode.getTerm() instanceof Open) { - VarNodeSet free = ((Open)curNode.getTerm()).getFreeVariables(); - for(VariableNode var : free) - if(temp.contains(var)) - res.addVarNode(var); - else - temp.addVarNode(var); - } + res.retainAll(vars); } return res; @@ -407,7 +394,6 @@ public RuisHandler addRuiHandler() { else { // PTree in case of and-entailment // RUISet otherwise - //return this.addContextRuiHandler(contextName, createRuisHandler(contextName)); return createRuisHandler(); } } @@ -544,5 +530,50 @@ public void processReports() { currentChannel.clearReportsBuffer(); } } + + public static void main(String[] args) { + Variable v1 = new Variable("x"); + VariableNode x = new VariableNode(v1); + Variable v2 = new Variable("y"); + VariableNode y = new VariableNode(v2); + Variable v3 = new Variable("z"); + VariableNode z = new VariableNode(v3); + Variable v4 = new Variable("v"); + VariableNode v = new VariableNode(v4); + Variable v5 = new Variable("w"); + VariableNode w = new VariableNode(v5); + + Open o1 = new Open("1"); + o1.addFreeVariable(x); + o1.addFreeVariable(y); + o1.addFreeVariable(v); + o1.addFreeVariable(w); + PropositionNode p1 = new PropositionNode(o1); + + Open o2 = new Open("2"); + o2.addFreeVariable(x); + o2.addFreeVariable(v); + o2.addFreeVariable(w); + o2.addFreeVariable(z); + PropositionNode p2 = new PropositionNode(o2); + + Open o3 = new Open("3"); + o3.addFreeVariable(z); + o3.addFreeVariable(v); + o3.addFreeVariable(w); + o3.addFreeVariable(y); + PropositionNode p3 = new PropositionNode(o3); + + NodeSet n = new NodeSet(); + n.addNode(p1); + n.addNode(p2); + n.addNode(p3); + + Set res = getSharedVarsNodes(n); + + for(VariableNode vn : res) { + System.out.println(vn); + } + } } diff --git a/src/sneps/network/classes/setClasses/ContextRuisSet.java b/src/sneps/network/classes/setClasses/ContextRuisSet.java deleted file mode 100644 index 2c06a7cf..00000000 --- a/src/sneps/network/classes/setClasses/ContextRuisSet.java +++ /dev/null @@ -1,74 +0,0 @@ -package sneps.network.classes.setClasses; - -import java.util.Enumeration; -import java.util.Hashtable; - -import sneps.snip.classes.RuisHandler; - -public class ContextRuisSet{ - private Hashtable ruisHandlers; - - public ContextRuisSet() { - ruisHandlers = new Hashtable(); - } - - /** - * Adds the given RuisHandler by contextName into this.ruisHandlers - * @param contextName - * @param handler - * @return - */ - public RuisHandler addHandlerSet(String contextName, RuisHandler handler){ - return ruisHandlers.put(contextName, handler); - } - - /** - * Checks whether this.ruisHandlers has an entry for the given contextName - * @param contextID - * @return - */ - public boolean hasContext(String contextName) { - RuisHandler set = ruisHandlers.get(contextName); - if(set == null) - return false; - return true; - } - - /** - * Gets the stored entry for the given contextName - * @param contextID - * @return - */ - public RuisHandler getByContext(String contextName) { - return ruisHandlers.get(contextName); - } - - /** - * Checks whether this.ruisHandlers contains the given handler - * @param handler - * @return - */ - public boolean contains(RuisHandler handler){ - return ruisHandlers.contains(handler); - } - - public boolean containsKey(String handlerKey){ - return ruisHandlers.containsKey(handlerKey); - } - - public Enumeration getKeys(){ - return ruisHandlers.keys(); - } - - public int size(){ - return ruisHandlers.size(); - } - - public void remove(String contextName, RuisHandler handler){ - ruisHandlers.remove(contextName, handler); - } - - public void clear() { - ruisHandlers.clear(); - } -} diff --git a/src/sneps/network/classes/term/Molecular.java b/src/sneps/network/classes/term/Molecular.java index 59dc279b..ef4df19d 100644 --- a/src/sneps/network/classes/term/Molecular.java +++ b/src/sneps/network/classes/term/Molecular.java @@ -55,7 +55,7 @@ public String toString(){ * the node object having the current molecular object * as its syntactic object. */ - public void updateUpCables(Node node) { + /*public void updateUpCables(Node node) { DownCableSet dCableSet = this.getDownCableSet(); Enumeration dCables = dCableSet.getDownCables().elements(); while(dCables.hasMoreElements()){ @@ -69,6 +69,6 @@ public void updateUpCables(Node node) { n.getUpCableSet().getUpCable(r.getName()).addNode(node); } } - } + }*/ } diff --git a/src/sneps/snip/classes/PTree.java b/src/sneps/snip/classes/PTree.java index 078a9e67..8c74fe4b 100644 --- a/src/sneps/snip/classes/PTree.java +++ b/src/sneps/snip/classes/PTree.java @@ -333,7 +333,8 @@ public PTreeNode getRoot(){ /** * @className * - * @ClassDescription The PTreeNode is the lowest level in the PTree, where RUIs are stored and combined. + * @ClassDescription The PTreeNode is the lowest level in the PTree, + * where RUIs are stored and combined. * * @author Amgad Ashraf * diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index 7a0d7969..df1ae758 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -7,7 +7,6 @@ import sneps.network.VariableNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.RuleUseInfoSet; -import sneps.network.classes.setClasses.VarNodeSet; public class SIndex extends RuisHandler { @@ -19,7 +18,7 @@ public class SIndex extends RuisHandler { public Hashtable, RuisHandler> map; private byte ruiHandlerType; public static final byte RUIS = 0, SINGLETON = 1, PTREE = 2; - private VarNodeSet sharedVars; + private Set sharedVars; private NodeSet nodesWithVars; /** @@ -31,7 +30,7 @@ public class SIndex extends RuisHandler { * Set of shared variables whose bindings are used to hash on in this SIndex. * @param nodesWithVars */ - public SIndex(byte ruiHandlerType, VarNodeSet sharedVars, + public SIndex(byte ruiHandlerType, Set sharedVars, NodeSet nodesWithVars) { map = new Hashtable, RuisHandler>(); this.ruiHandlerType = ruiHandlerType; @@ -39,7 +38,7 @@ public SIndex(byte ruiHandlerType, VarNodeSet sharedVars, this.nodesWithVars = nodesWithVars; } - public SIndex(byte ruiHandlerType, VarNodeSet sharedVars) { + public SIndex(byte ruiHandlerType, Set sharedVars) { map = new Hashtable, RuisHandler>(); this.ruiHandlerType = ruiHandlerType; this.sharedVars = sharedVars; diff --git a/tests/SIndexTest.java b/tests/SIndexTest.java index bff4473a..78ad6411 100644 --- a/tests/SIndexTest.java +++ b/tests/SIndexTest.java @@ -31,9 +31,9 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo * Creating Nodes */ - Base ba = new Base("HEY"); - Base ba1 = new Base("hoooo"); - Base ba2 = new Base("hola"); + Base ba = new Base("A"); + Base ba1 = new Base("B"); + Base ba2 = new Base("C"); Node n = new Node(ba); Node n1 = new Node(ba1); From 4fb22fa761105dc7caaeec5fa9db7a5e2138b174 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Tue, 21 May 2019 23:15:36 +0200 Subject: [PATCH 18/22] Revision of PTree --- src/sneps/network/Node.java | 2 +- src/sneps/network/PropositionNode.java | 19 +- src/sneps/network/RuleNode.java | 64 +--- .../classes/setClasses/FlagNodeSet.java | 4 + .../classes/setClasses/VarNodeSet.java | 37 ++- src/sneps/network/classes/term/Molecular.java | 4 +- src/sneps/snip/Report.java | 24 ++ src/sneps/snip/ReportInstances.java | 37 +++ src/sneps/snip/Runner.java | 139 +++++---- src/sneps/snip/classes/PTree.java | 264 ++++++++++------ src/sneps/snip/classes/RuisHandler.java | 4 - src/sneps/snip/classes/RuleResponse.java | 5 +- src/sneps/snip/classes/SIndexHelper.java | 14 - src/sneps/snip/rules/AndOrEntailment.java | 70 ++++- src/sneps/snip/rules/NumericalEntailment.java | 1 - src/sneps/snip/rules/OrEntailment.java | 3 +- tests/AndOrTest.java | 16 +- tests/OrTests.java | 5 + tests/PSubTreeTests.java | 130 -------- tests/PTreeNodeTests.java | 130 -------- tests/PTreeTests.java | 291 ++++++++++++++---- tests/SIndexTest.java | 42 +-- 22 files changed, 692 insertions(+), 613 deletions(-) create mode 100644 src/sneps/snip/ReportInstances.java delete mode 100644 src/sneps/snip/classes/SIndexHelper.java delete mode 100644 tests/PSubTreeTests.java delete mode 100644 tests/PTreeNodeTests.java diff --git a/src/sneps/network/Node.java b/src/sneps/network/Node.java index 371ea7a8..bb329d00 100644 --- a/src/sneps/network/Node.java +++ b/src/sneps/network/Node.java @@ -271,7 +271,7 @@ public void setTemp(boolean temp) { * class and pass the current node as a parameter. */ public void updateUpCables() { - //((Molecular) this.getTerm()).updateUpCables(this); + ((Molecular) this.getTerm()).updateUpCables(this); } public boolean hasSameFreeVariablesAs(Node n) { diff --git a/src/sneps/network/PropositionNode.java b/src/sneps/network/PropositionNode.java index fe0e262f..8ecd8831 100644 --- a/src/sneps/network/PropositionNode.java +++ b/src/sneps/network/PropositionNode.java @@ -22,6 +22,7 @@ import sneps.snip.InferenceTypes; import sneps.snip.Pair; import sneps.snip.Report; +import sneps.snip.ReportInstances; import sneps.snip.Runner; import sneps.snip.channels.AntecedentToRuleChannel; import sneps.snip.channels.Channel; @@ -37,13 +38,13 @@ public class PropositionNode extends Node implements Serializable { protected Support basicSupport; protected ChannelSet outgoingChannels; protected ChannelSet incomingChannels; - protected ReportSet knownInstances; + protected ReportInstances knownInstances; protected ReportSet newInstances; public PropositionNode() { outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); - knownInstances = new ReportSet(); + knownInstances = new ReportInstances(); newInstances = new ReportSet(); } @@ -51,11 +52,11 @@ public PropositionNode(Term trm) { super(Semantic.proposition, trm); outgoingChannels = new ChannelSet(); incomingChannels = new ChannelSet(); - knownInstances = new ReportSet(); + knownInstances = new ReportInstances(); newInstances = new ReportSet(); } - public void processSingleChannelReports(Channel currentChannel) { + /*public void processSingleChannelReports(Channel currentChannel) { ReportSet reports = currentChannel.getReportsBuffer(); for (Report currentReport : reports) { Report alteredReport = new Report(currentReport.getSubstitutions(), currentReport.getSupport(), @@ -71,12 +72,12 @@ public void processSingleChannelReports(Channel currentChannel) { currentChannel.clearReportsBuffer(); } currentChannel.clearReportsBuffer(); - } + }*/ - public void processReports() { + /*public void processReports() { for (Channel inChannel : incomingChannels) processSingleChannelReports(inChannel); - } + }*/ public void broadcastReport(Report report) { newInstances.addReport(report); @@ -227,11 +228,11 @@ public void setIncomingChannels(ChannelSet incomingChannels) { this.incomingChannels = incomingChannels; } - public ReportSet getKnownInstances() { + public ReportInstances getKnownInstances() { return knownInstances; } - public void setKnownInstances(ReportSet knownInstances) { + public void setKnownInstances(ReportInstances knownInstances) { this.knownInstances = knownInstances; } diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 3c5c64ff..e83a3ac0 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -6,6 +6,7 @@ import java.util.HashSet; import java.util.Set; +import sneps.network.cables.DownCableSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; @@ -14,7 +15,6 @@ import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; import sneps.network.classes.term.Open; -import sneps.network.classes.term.Variable; import sneps.snebr.Controller; import sneps.snip.Report; import sneps.snip.channels.AntecedentToRuleChannel; @@ -27,7 +27,6 @@ import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.rules.OrEntailment; public abstract class RuleNode extends PropositionNode implements Serializable{ private static final long serialVersionUID = 3891988384679269734L; @@ -110,14 +109,6 @@ public int getAntSize(){ return antNodesWithoutVars.size() + antNodesWithVars.size(); } - protected NodeSet getPatternNodes() { - return antNodesWithVars; - } - - public NodeSet getAntecedents() { - return antecedents; - } - public NodeSet getAntsWithoutVars() { return antNodesWithoutVars; } @@ -125,6 +116,10 @@ public NodeSet getAntsWithoutVars() { public NodeSet getAntsWithVars() { return antNodesWithVars; } + + public NodeSet getAntecedents() { + return antecedents; + } public void setAntecedents(NodeSet antecedents) { this.antecedents = antecedents; @@ -142,13 +137,13 @@ public ArrayList getReplies() { return reportsToBeSent; } - protected void sendReportToConsequents(Report reply) { + /*protected void sendReportToConsequents(Report reply) { if(!knownInstances.contains(reply)) newInstances.addReport(reply); for (Channel outChannel : outgoingChannels) if(outChannel instanceof RuleToConsequentChannel) outChannel.addReport(reply); - } + }*/ /** * Process antecedent nodes, used for initialization. @@ -531,49 +526,4 @@ public void processReports() { } } - public static void main(String[] args) { - Variable v1 = new Variable("x"); - VariableNode x = new VariableNode(v1); - Variable v2 = new Variable("y"); - VariableNode y = new VariableNode(v2); - Variable v3 = new Variable("z"); - VariableNode z = new VariableNode(v3); - Variable v4 = new Variable("v"); - VariableNode v = new VariableNode(v4); - Variable v5 = new Variable("w"); - VariableNode w = new VariableNode(v5); - - Open o1 = new Open("1"); - o1.addFreeVariable(x); - o1.addFreeVariable(y); - o1.addFreeVariable(v); - o1.addFreeVariable(w); - PropositionNode p1 = new PropositionNode(o1); - - Open o2 = new Open("2"); - o2.addFreeVariable(x); - o2.addFreeVariable(v); - o2.addFreeVariable(w); - o2.addFreeVariable(z); - PropositionNode p2 = new PropositionNode(o2); - - Open o3 = new Open("3"); - o3.addFreeVariable(z); - o3.addFreeVariable(v); - o3.addFreeVariable(w); - o3.addFreeVariable(y); - PropositionNode p3 = new PropositionNode(o3); - - NodeSet n = new NodeSet(); - n.addNode(p1); - n.addNode(p2); - n.addNode(p3); - - Set res = getSharedVarsNodes(n); - - for(VariableNode vn : res) { - System.out.println(vn); - } - } - } diff --git a/src/sneps/network/classes/setClasses/FlagNodeSet.java b/src/sneps/network/classes/setClasses/FlagNodeSet.java index 32cc0da9..8117db29 100644 --- a/src/sneps/network/classes/setClasses/FlagNodeSet.java +++ b/src/sneps/network/classes/setClasses/FlagNodeSet.java @@ -115,4 +115,8 @@ public NodeSet getAllNodes() { return res; } + + public void clear() { + flagNodes.clear(); + } } diff --git a/src/sneps/network/classes/setClasses/VarNodeSet.java b/src/sneps/network/classes/setClasses/VarNodeSet.java index 08d179d9..5072bcb3 100644 --- a/src/sneps/network/classes/setClasses/VarNodeSet.java +++ b/src/sneps/network/classes/setClasses/VarNodeSet.java @@ -5,13 +5,6 @@ import sneps.network.VariableNode; -/** - * @className VarNodeSet.java - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 - */ public class VarNodeSet implements Iterable { protected Vector variables; @@ -35,9 +28,8 @@ public void addVarNode(VariableNode n) { } public void addAll(VarNodeSet allVariables) { - for (int i = 0; i < variables.size(); i++) { - this.addVarNode(allVariables.getVarNode(i)); - } + for (VariableNode v : allVariables) + this.addVarNode(v); } public int size() { @@ -56,11 +48,24 @@ public void remove(VariableNode variable) { variables.remove(variable); } - public static VarNodeSet union(VarNodeSet v1, VarNodeSet v2){ - VarNodeSet v3 = v2; - for(VariableNode var : v1) - if(!v3.contains(var)) - v3.addVarNode(var); - return v3; + public static VarNodeSet union(VarNodeSet v1, VarNodeSet v2) { + VarNodeSet union = new VarNodeSet(); + for(VariableNode v : v1) + union.addVarNode(v); + for(VariableNode v : v2) + union.addVarNode(v); + + return union; + } + + public String toString() { + String res = null; + for(VariableNode v : variables) { + if(res == null) + res = v + " "; + else + res += v + " "; + } + return res; } } diff --git a/src/sneps/network/classes/term/Molecular.java b/src/sneps/network/classes/term/Molecular.java index ef4df19d..59dc279b 100644 --- a/src/sneps/network/classes/term/Molecular.java +++ b/src/sneps/network/classes/term/Molecular.java @@ -55,7 +55,7 @@ public String toString(){ * the node object having the current molecular object * as its syntactic object. */ - /*public void updateUpCables(Node node) { + public void updateUpCables(Node node) { DownCableSet dCableSet = this.getDownCableSet(); Enumeration dCables = dCableSet.getDownCables().elements(); while(dCables.hasMoreElements()){ @@ -69,6 +69,6 @@ public String toString(){ n.getUpCableSet().getUpCable(r.getName()).addNode(node); } } - }*/ + } } diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index a551ca83..a186f6e8 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -38,6 +38,30 @@ public boolean anySupportAssertedInContext(Context reportContext) } return false; } + + public Report combine(Report r) { + if(substitution.isCompatible(r.getSubstitutions())) { + PropositionSet combinedSupport = new PropositionSet(); + try { + combinedSupport = support.union(r.support); + } catch (NotAPropositionNodeException | + NodeNotFoundInNetworkException e1) { + e1.printStackTrace(); + } + + InferenceTypes resultingType; + if(inferenceType.equals(InferenceTypes.FORWARD) || + r.getInferenceType().equals(InferenceTypes.FORWARD)) + resultingType = InferenceTypes.FORWARD; + else + resultingType = InferenceTypes.BACKWARD; + + return new Report(substitution.union(r.getSubstitutions()), + combinedSupport, sign, resultingType); + } + + return null; + } public Substitutions getSubstitutions() { return substitution; diff --git a/src/sneps/snip/ReportInstances.java b/src/sneps/snip/ReportInstances.java new file mode 100644 index 00000000..45aa726e --- /dev/null +++ b/src/sneps/snip/ReportInstances.java @@ -0,0 +1,37 @@ +package sneps.snip; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Set; + +import sneps.snip.matching.Substitutions; + +public class ReportInstances implements Iterable { + Hashtable> instances; + + public ReportInstances() { + instances = new Hashtable>(); + } + + public void addReport(Report report) { + Substitutions reportSubs = report.getSubstitutions(); + Set reportsSet = instances.remove(reportSubs); + reportsSet.add(report); + instances.put(reportSubs, reportsSet); + } + + public Set getReportBySubstitutions(Substitutions subs) { + return instances.get(subs); + } + + public Iterator iterator() { + Set allMergedReports = new HashSet(); + Collection> collectionOfSets = instances.values(); + for (Set set : collectionOfSets) + allMergedReports.addAll(set); + return allMergedReports.iterator(); + } + +} \ No newline at end of file diff --git a/src/sneps/snip/Runner.java b/src/sneps/snip/Runner.java index a5d97de5..69527e44 100644 --- a/src/sneps/snip/Runner.java +++ b/src/sneps/snip/Runner.java @@ -2,68 +2,93 @@ import java.util.ArrayDeque; import java.util.Deque; +import java.util.Hashtable; import java.util.Queue; import sneps.network.ActNode; import sneps.network.Node; +import sneps.network.PropositionNode; public class Runner { - - private static Queue highQueue; - private static Queue lowQueue; - private static Deque actQueue; - - public static void initiate() { - highQueue = new ArrayDeque(); - lowQueue = new ArrayDeque(); - actQueue = new ArrayDeque(); - } - public static String run() { - String sequence = ""; - main: while(!highQueue.isEmpty() || !lowQueue.isEmpty() || !actQueue.isEmpty()) { - while(!highQueue.isEmpty()) { - System.out.println("\n\n"); - System.out.println(" AT HIGH QUEUE "); - Node toRunNext = highQueue.poll(); - System.out.println(toRunNext); - System.out.println("\n\n"); - toRunNext.processReports(); - sequence += 'H'; - } - while(!lowQueue.isEmpty()) { - System.out.println("in"); - Node toRunNext = lowQueue.poll(); - toRunNext.processRequests(); - sequence += 'L'; - if(!highQueue.isEmpty()) - continue main; - } - while(!actQueue.isEmpty()) { - System.out.println("AT ACT QUEUE"); - ActNode toRunNext = actQueue.removeLast(); - System.out.println(toRunNext + " agenda: " + toRunNext.getAgenda()); - System.out.println("\n\n"); - toRunNext.processIntends(); - sequence += 'A'; - if(!highQueue.isEmpty() || !lowQueue.isEmpty()) { - continue main; - } - } - } - return sequence; - } - - public static void addToHighQueue(Node node) { - highQueue.add(node); - } - - public static void addToLowQueue(Node node) { - lowQueue.add(node); - } - - public static void addToActStack(ActNode node) { - actQueue.addLast(node); - } + private static Queue highQueue; + private static Queue lowQueue; + private static Deque actQueue; + private static Hashtable forwardAssertedNodes; -} + public static void initiate() { + highQueue = new ArrayDeque(); + lowQueue = new ArrayDeque(); + actQueue = new ArrayDeque(); + forwardAssertedNodes = new Hashtable(); + } + + public static String run() { + String sequence = ""; + main: while (!highQueue.isEmpty() || !lowQueue.isEmpty() || !actQueue.isEmpty()) { + while (!highQueue.isEmpty()) { + System.out.println("\n\n"); + System.out.println(" AT HIGH QUEUE "); + Node toRunNext = highQueue.poll(); + System.out.println(toRunNext); + System.out.println("\n\n"); + toRunNext.processReports(); + sequence += 'H'; + } + while (!lowQueue.isEmpty()) { + System.out.println("in"); + Node toRunNext = lowQueue.poll(); + toRunNext.processRequests(); + sequence += 'L'; + if (!highQueue.isEmpty()) + continue main; + } + while (!actQueue.isEmpty()) { + System.out.println("AT ACT QUEUE"); + ActNode toRunNext = actQueue.removeLast(); + System.out.println(toRunNext + " agenda: " + toRunNext.getAgenda()); + System.out.println("\n\n"); + toRunNext.processIntends(); + sequence += 'A'; + if (!highQueue.isEmpty() || !lowQueue.isEmpty()) { + continue main; + } + } + } + return sequence; + } + + public static void addToHighQueue(Node node) { + highQueue.add(node); + } + + public static void addToLowQueue(Node node) { + lowQueue.add(node); + } + + public static void addToActStack(ActNode node) { + actQueue.addLast(node); + } + + /*** + * Method used to keep track of asserted nodes with a specific certain report as + * its hash in the hashtable forwardAssertedNodes instance + * + * @param report + * @param node + */ + public static void addNodeAssertionThroughFReport(Report report, PropositionNode node) { + forwardAssertedNodes.put(report, node); + } + + /*** + * Method checks if the given node was asserted (added in the + * forwardAssertedNodes instance) with a forward report + * + * @param node + * @return + */ + public static boolean isNodeAssertedThroughForwardInf(PropositionNode node) { + return forwardAssertedNodes.containsValue(node); + } +} \ No newline at end of file diff --git a/src/sneps/snip/classes/PTree.java b/src/sneps/snip/classes/PTree.java index 8c74fe4b..602714e8 100644 --- a/src/sneps/snip/classes/PTree.java +++ b/src/sneps/snip/classes/PTree.java @@ -24,20 +24,19 @@ * * @ClassDescription A PTree is a binary tree structure. Every AndEntailment rule node stores combinations of antecedent RUIs inside a PTree because of its optimized structure. * A PTree class follows an algorithm for construction and insertion, having leaf nodes representing RUIs of rule antecedents and root nodes representing combined RUIs of the rule itself. - * - * @author Amgad Ashraf - * @version 3.00 31/5/2018 */ public class PTree extends RuisHandler { private Hashtable patternVariables;//PatternId, VariableNodes private Hashtable> variablePatterns;//VariableNode, PatternIds private VarNodeSet notProccessed; private HashSet subTrees; + /** + * Maps every pattern to a subTree. + */ private Hashtable subTreesMap; /** * Constructor for the PTree - * @param context */ public PTree() { super(); @@ -52,11 +51,11 @@ public PTree() { * Builds the PTree by preparing pattern variables set, variable patterns set, pattern sequence and finally constructing the tree bottom up * @param ants */ - public void buildTree(NodeSet ants){ + public void buildTree(NodeSet ants) { fillPVandVP(ants); - Queue patternSequence = getPatternSequence(); - + Queue patternSequence = getPatternSequence(); + constructBottomUp(patternSequence); patternVariables = new Hashtable(); @@ -68,82 +67,86 @@ public void buildTree(NodeSet ants){ * Prepares both pattern variables set and variable patterns set * @param ants */ - private void fillPVandVP(NodeSet ants) { - for(Node pattern : ants){ - int id = pattern.getId(); + public void fillPVandVP(NodeSet ants) { + for(Node pattern : ants) { + int patId = pattern.getId(); Term term = pattern.getTerm(); - VarNodeSet vars = patternVariables.get(id); + VarNodeSet vars = patternVariables.get(patId); if(vars == null || vars.isEmpty()) vars = new VarNodeSet(); - Set pats = variablePatterns.get(id); - if(pats == null || pats.isEmpty()) - pats = new HashSet(); - if(term instanceof Variable){ + if(term instanceof Variable) { vars.addVarNode((VariableNode) pattern); notProccessed.addVarNode((VariableNode) pattern); } - if(term instanceof Open){ - VarNodeSet freeVars = ((Open)term).getFreeVariables(); + if(term instanceof Open) { + VarNodeSet freeVars = ((Open) term).getFreeVariables(); vars.addAll(freeVars); notProccessed.addAll(freeVars); } - patternVariables.put(id, vars); - } - - Set pats = patternVariables.keySet(); - for(int curPat : pats){ - VarNodeSet vars = patternVariables.get(curPat); - if(!(vars.isEmpty()) && !(vars == null)){ - for(VariableNode curVar : vars){ - Set pat = variablePatterns.get(curVar); - if(pat == null){ - pat = new HashSet(); - pat.add(curPat); - variablePatterns.put(curVar, pat); - continue; - } - if(!pat.contains(curPat)){ - pat.add(curPat); - variablePatterns.put(curVar, pat); - } + + patternVariables.put(patId, vars); + + for(VariableNode curVar : vars) { + Set pats = variablePatterns.get(curVar); + if(pats == null) { + pats = new HashSet(); + pats.add(patId); + variablePatterns.put(curVar, pats); + continue; + } + + if(!pats.contains(patId)) { + pats.add(patId); + variablePatterns.put(curVar, pats); } } } } + /** * Prepares pattern sequence into a sequence of PTreeNodes ready for construction * @return */ - private Queue getPatternSequence() { + public Queue getPatternSequence() { LinkedHashSet res = new LinkedHashSet(); Queue treeNodes = new LinkedList(); - - for(VariableNode currentVar : notProccessed){ - Set vPatternsIds = variablePatterns.get(currentVar); - - for(int currentPatId : vPatternsIds) - if(!res.contains(currentPatId)){ - res.add(currentPatId); - - VarNodeSet vs = new VarNodeSet(); - vs.addVarNode(currentVar); - Set pats = new HashSet(); - pats.add(currentPatId); - - treeNodes.add(new PTreeNode(pats, vs)); + VarNodeSet toBeProcessed = new VarNodeSet(); + + while(!notProccessed.isEmpty()) { + toBeProcessed.addVarNode(notProccessed.iterator().next()); + while (!toBeProcessed.isEmpty()) { + VariableNode var = toBeProcessed.iterator().next(); + Set vPatternsIds = variablePatterns.get(var); + for(int currentPatId : vPatternsIds) { + if(!res.contains(currentPatId)) { + res.add(currentPatId); + VarNodeSet patVarSet = patternVariables.get(currentPatId); + toBeProcessed.addAll(patVarSet); + } } + + toBeProcessed.remove(var); + notProccessed.remove(var); + } } + + for (int pat : res) { + Set pats = new HashSet(); + pats.add(pat); + treeNodes.add(new PTreeNode(pats, patternVariables.get(pat))); + } + return treeNodes; - } + /** * Uses given ordered PTreeNodes to construct the PTree * If no two adjacent nodes in the sequence share variables, a PSubTree is handled * @param treeNodes */ - private void constructBottomUp(Queue treeNodes) { + public void constructBottomUp(Queue treeNodes) { PTreeNode head = treeNodes.poll(); if (treeNodes.isEmpty()) { processSubTree(head); @@ -167,20 +170,23 @@ private void constructBottomUp(Queue treeNodes) { head = second; } } + if (head != null) newTreeNodes.add(head); + if (sharing) constructBottomUp(newTreeNodes); else for(PTreeNode subHead : newTreeNodes) processSubTree(subHead); } + /** * Creates a PSubTree and inserts it into subTrees and subTreesMap * @param subHead */ private void processSubTree(PTreeNode subHead) { - if(subHead != null){ + if(subHead != null) { PSubTree subTree = new PSubTree(subHead); subTrees.add(subTree); for (int id : subHead.getPats()) { @@ -194,9 +200,8 @@ private void processSubTree(PTreeNode subHead) { public RuleUseInfoSet insertRUI(RuleUseInfo rui) { Stack stack = new Stack(); FlagNodeSet fns = rui.getFlagNodeSet(); - for(FlagNode node : fns){ + for(FlagNode node : fns) { int pattern = node.getNode().getId(); - PSubTree subTree = subTreesMap.get(pattern); RuleUseInfoSet returned = new RuleUseInfoSet(); if(subTree != null) @@ -209,8 +214,10 @@ public RuleUseInfoSet insertRUI(RuleUseInfo rui) { tSet = new RuleUseInfoSet(); stack.push(tSet); } + stack.push(returned); } + return multiply(stack); } @@ -237,6 +244,7 @@ private boolean sharingVars(PTreeNode head, PTreeNode second) { return true; return false; } + private VarNodeSet getSharedVars(PTreeNode first, PTreeNode second) { VarNodeSet smaller = null, bigger = null, intersection = new VarNodeSet(); if (first.getVars().size() > second.getVars().size()) { @@ -249,23 +257,43 @@ private VarNodeSet getSharedVars(PTreeNode first, PTreeNode second) { for (VariableNode i : smaller) if (bigger.contains(i)) intersection.addVarNode(i); + return intersection; } + private Set union(Set vars1, Set vars2) { Set union = new HashSet(); - for(int strng : vars1){ - if(vars2.contains(strng)) - union.add(strng); - } + for(int strng : vars1) + union.add(strng); + for(int strng : vars2) + union.add(strng); return union; } + public VarNodeSet getNotProccessed() { + return notProccessed; + } + + public void setNotProccessed(VarNodeSet notProccessed) { + this.notProccessed = notProccessed; + } + + public Hashtable getPatternVariables() { + return patternVariables; + } + + public Hashtable> getVariablePatterns() { + return variablePatterns; + } + public HashSet getSubTrees() { return subTrees; } + public void setSubTrees(HashSet subTrees) { this.subTrees = subTrees; } + public Hashtable getSubTreesMap() { return subTreesMap; } @@ -273,38 +301,53 @@ public Hashtable getSubTreesMap() { public void setSubTreesMap(Hashtable subTreesMap) { this.subTreesMap = subTreesMap; } - public RuleUseInfoSet getAllRootRuis(){ + + public RuleUseInfoSet getAllRootRuis() { RuleUseInfoSet res = new RuleUseInfoSet(); for(PSubTree subtree : subTrees) res.addAll(subtree.getRootRUIS()); return res; } + + @Override + public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui) { + return null; + } + + @Override + public boolean isEmpty() { + return false; + } + + @Override + public void clear() { + + } /** * @className PSubTree * - * @ClassDescription The PSubTree is used to handle the case where antecedent patterns have disjoint variables, by keeping track of multiple PSubTrees for every disjoint variable inside the same PTree. - * - * @author Amgad Ashraf + * @ClassDescription The PSubTree is used to handle the case where antecedent + * patterns have disjoint variable unions, by keeping track of multiple PSubTrees + * for every disjoint variable union inside the same PTree. * - * @version 3.00 31/5/2018 */ public class PSubTree { private PTreeNode root; - public PSubTree(PTreeNode rot){ - root = rot; + public PSubTree(PTreeNode root){ + this.root = root; } public RuleUseInfoSet insert(RuleUseInfo rui) { FlagNodeSet fns = rui.getFlagNodeSet(); RuleUseInfoSet res = new RuleUseInfoSet(); - for(FlagNode node : fns){ + for(FlagNode node : fns) { int pattern = node.getNode().getId(); - PTreeNode leaf = getLeafPattern(pattern, root); res = leaf.insertIntoTree(rui, res); } + return res; } @@ -331,36 +374,34 @@ public PTreeNode getRoot(){ } /** - * @className + * @className PTreeNode * * @ClassDescription The PTreeNode is the lowest level in the PTree, * where RUIs are stored and combined. - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 */ public class PTreeNode { private PTreeNode parent, sibling; private PTreeNode leftChild, rightChild; - private Hashtable ruisMap;//Substitutions node IDs + /** + * Maps every set of bindings to a RUISet. + */ + private Hashtable ruisMap; private Set pats; private VarNodeSet vars; private VarNodeSet siblingIntersection; - public PTreeNode(){ - ruisMap = new Hashtable(); - parent = null; sibling = null; - leftChild = null; rightChild = null; - pats = null; vars = null; + public PTreeNode() { + ruisMap = new Hashtable(); } - public PTreeNode(Set p, VarNodeSet v){ + + public PTreeNode(Set p, VarNodeSet v) { this(); - pats = p; vars = v; + pats = p; + vars = v; } public RuleUseInfoSet insertIntoTree(RuleUseInfo rui, RuleUseInfoSet ruiSet) { - Integer[] key = insertRUI(rui); + int key = insertRUI(rui); if (sibling == null) { ruiSet.add(rui); return ruiSet; @@ -374,6 +415,7 @@ public RuleUseInfoSet insertIntoTree(RuleUseInfo rui, RuleUseInfoSet ruiSet) { continue; parent.insertIntoTree(combinedRui, ruiSet); } + return ruiSet; } @@ -389,58 +431,88 @@ public void insertLeftAndRight(PTreeNode leftNode, PTreeNode rightNode, rightChild = rightNode; } - public Integer[] insertRUI(RuleUseInfo rui) { + public int insertRUI(RuleUseInfo rui) { if (sibling == null) { - RuleUseInfoSet ruiSet = ruisMap.get(new Integer[]{0}); + RuleUseInfoSet ruiSet = ruisMap.get(0); if (ruiSet == null) { ruiSet = new RuleUseInfoSet(); - ruisMap.put(new Integer[]{0}, ruiSet); + ruisMap.put(0, ruiSet); } ruiSet.add(rui); - return new Integer[]{0}; + return 0; } - - Integer[] vs = new Integer[siblingIntersection.size()]; + + // The case when the node is other than the root + int[] vs = new int[siblingIntersection.size()]; int index = 0; - for (VariableNode var : siblingIntersection) - vs[index++] = rui.getSubstitutions().getBindingByVariable(var).getNode().getId(); + for (VariableNode var : siblingIntersection) { + if(rui.getSubstitutions().getBindingByVariable(var) != null) + vs[index++] = rui.getSubstitutions().getBindingByVariable(var).getNode().getId(); + } + int key = getKey(vs); RuleUseInfoSet ruis = ruisMap.get(vs); if (ruis == null) { ruis = new RuleUseInfoSet(); - ruisMap.put(vs, ruis); + ruisMap.put(key, ruis); } ruis.add(rui); - return vs; + return key; } - public RuleUseInfoSet getRUIS(){ + public RuleUseInfoSet getRUIS() { RuleUseInfoSet ruiSet = new RuleUseInfoSet(); Collection mappedRuis = ruisMap.values(); for(RuleUseInfoSet singleSet : mappedRuis) ruiSet.addAll(singleSet); return ruiSet; } + + /** + * Get the index of a RuleUseInfo in the table by the ids of its + * substitutions + * + * @param x + * int[] + * @return int + */ + private int getKey(int[] x) { + int p = 16777619; + int hash = (int) 2166136261L; + for (int i = 0; i < x.length; ++i) { + hash += (hash ^ x[i]) * p; + } + hash += hash << 13; + hash ^= hash >> 7; + hash += hash << 3; + hash ^= hash >> 17; + hash += hash << 5; + return hash; + } + - public RuleUseInfoSet getRUIS(Integer[] index) { + public RuleUseInfoSet getRUIS(int index) { return ruisMap.get(index); } public PTreeNode getLeftChild() { return leftChild; } + public PTreeNode getRightChild() { return rightChild; } + public Set getPats() { return pats; } + public VarNodeSet getVars() { return vars; } + @Override public String toString(){ return pats.toString(); } - } } \ No newline at end of file diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 37049e7b..a1f73151 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -7,10 +7,6 @@ * * @ClassDescription To deal with the large number of RUIs generated as more nodes are built in the network, RuisHandler classes are built to store and combine RUIs. * A RuisHandler is stored in a rule Node and a single rule Node can store multiple instances of a single RuisHandler class. - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 */ public abstract class RuisHandler { diff --git a/src/sneps/snip/classes/RuleResponse.java b/src/sneps/snip/classes/RuleResponse.java index 9dda975d..b5bbcfe2 100644 --- a/src/sneps/snip/classes/RuleResponse.java +++ b/src/sneps/snip/classes/RuleResponse.java @@ -1,5 +1,6 @@ package sneps.snip.classes; +import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -9,7 +10,7 @@ public class RuleResponse { private Report report; - private Set consequentChannels; + private Collection consequentChannels; public RuleResponse() { consequentChannels = new HashSet(); @@ -23,7 +24,7 @@ public void setReport(Report report) { this.report = report; } - public Set getConsequentChannels() { + public Collection getConsequentChannels() { return consequentChannels; } diff --git a/src/sneps/snip/classes/SIndexHelper.java b/src/sneps/snip/classes/SIndexHelper.java deleted file mode 100644 index 10ba3d5e..00000000 --- a/src/sneps/snip/classes/SIndexHelper.java +++ /dev/null @@ -1,14 +0,0 @@ -package sneps.snip.classes; - -import java.util.Hashtable; - -public class SIndexHelper { - - public static Hashtable map = new Hashtable(); - - - public static int getSize() { - return map.size(); - } - -} diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index abf7c4da..6eb0529c 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -1,23 +1,19 @@ package sneps.snip.rules; -import java.util.HashSet; import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; -import sneps.network.Node; import sneps.network.RuleNode; +import sneps.network.cables.DownCableSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.term.Molecular; -import sneps.snebr.Controller; import sneps.snip.Report; import sneps.snip.channels.Channel; -import sneps.snip.channels.ChannelTypes; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.matching.LinearSubstitutions; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; @@ -46,6 +42,57 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { else if (rui.getPosCount() != max) return null; + /*consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); + ArrayList res = new ArrayList(); + RuleResponse response = new RuleResponse(); + Report reply; + PropositionSet replySupport = new PropositionSet(); + PropositionSet ruleSupport = new PropositionSet(); + for(FlagNode fn : rui.getFlagNodeSet()) + try { + replySupport.union(fn.getSupport()); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + + if(this.getTerm() instanceof Closed) { + try { + ruleSupport = ruleSupport.add(this.getId()); + replySupport.union(ruleSupport); + } catch (DuplicatePropositionException | NotAPropositionNodeException | + NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + + if(Runner.isNodeAssertedThroughForwardInf(this)) + reply = new Report(rui.getSubstitutions(), replySupport, reportSign, + InferenceTypes.FORWARD); + else + reply = new Report(rui.getSubstitutions(), replySupport, reportSign, + rui.getType()); + + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + res.add(response); + } + else if(this.getTerm() instanceof Open) { + Report ruiReport = new Report(rui.getSubstitutions(), replySupport, + reportSign, rui.getType()); + for(Report r : knownInstances) { + reply = ruiReport.combine(r); + if(reply != null) { + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + res.add(response); + response.clear(); + } + } + } + + return res;*/ + PropositionSet replySupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { @@ -56,14 +103,19 @@ else if (rui.getPosCount() != max) // TODO // Add rule node to replySupport - + // If Closed, just return this + // Else, from knownInstances which contains instances found for the rule node itself + // Check for report type, get support to union with rui, and get subs to union with rui + + // If node is Closed, need to check if it was asserted thru forward inference, + // by calling method isAssertedthruForwardInference(), to get type of replyReport consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); - System.out.println(rui.getFlagNodeSet().getAllNodes()); - System.out.println(consequents); + //System.out.println(rui.getFlagNodeSet().getAllNodes()); + //System.out.println(consequents); Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, rui.getType()); - System.out.println(reply); + //System.out.println(reply); reportsToBeSent.add(reply); RuleResponse r = new RuleResponse(); diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index 57ab2b7c..e97f942e 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -77,7 +77,6 @@ public ArrayList applyRuleHandler(Report report, Node signature) { fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns, report.getInferenceType()); - //System.out.println(rui); // Inserting the RuleUseInfo into the RuleNode's RuisHandler: // SIndex in case there are shared variables between the antecedents, or diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 0fdce066..9a7e1f02 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -6,6 +6,7 @@ import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; +import sneps.network.RuleNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.term.Molecular; @@ -14,7 +15,7 @@ import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; -public class OrEntailment extends NumericalEntailment { +public class OrEntailment extends RuleNode { private static final long serialVersionUID = 1L; public OrEntailment(Molecular syn) { diff --git a/tests/AndOrTest.java b/tests/AndOrTest.java index 2750388c..03c3e421 100644 --- a/tests/AndOrTest.java +++ b/tests/AndOrTest.java @@ -24,14 +24,12 @@ import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; -import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; import sneps.snebr.Controller; import sneps.snip.InferenceTypes; import sneps.snip.Report; -import sneps.snip.classes.FlagNode; import sneps.snip.classes.SIndex; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; @@ -42,9 +40,9 @@ public class AndOrTest { private static Context context; private static String contextName = "TempContext"; private static AndOrEntailment andor; - private static Node fido, var, dog, barks, animal, veg, mineral, human; + private static Node john, fido, var, dog, barks, animal, veg, mineral, human; private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8; - private static Report report, report1, report2; + private static Report report, report1, report2, report3; @BeforeClass public static void setUp() throws Exception { @@ -83,6 +81,7 @@ public static void setUp() throws Exception { try { var = Network.buildVariableNode("X"); + john = Network.buildBaseNode("John", new Semantic("Base")); fido = Network.buildBaseNode("Fido", new Semantic("Base")); dog = Network.buildBaseNode("Dog", new Semantic("Base")); barks = Network.buildBaseNode("Barks", new Semantic("Base")); @@ -312,6 +311,10 @@ public void test() { public void test2() { andor.clear(); LinearSubstitutions sub = new LinearSubstitutions(); + LinearSubstitutions sub1 = new LinearSubstitutions(); + sub.putIn(new Binding((VariableNode) var, fido)); + sub1.putIn(new Binding((VariableNode) var, john)); + PropositionSet support = new PropositionSet(); /*try { support.add(prop5.getId()); @@ -319,7 +322,6 @@ public void test2() { | NodeNotFoundInNetworkException e) { e.printStackTrace(); }*/ - sub.putIn(new Binding((VariableNode) var, fido)); report = new Report(sub, support, false, InferenceTypes.FORWARD); support = new PropositionSet(); @@ -338,6 +340,7 @@ public void test2() { | NodeNotFoundInNetworkException e) { e.printStackTrace(); }*/ + report3 = new Report(sub1, support, false, InferenceTypes.BACKWARD); report2 = new Report(sub, support, false, InferenceTypes.BACKWARD); andor.applyRuleHandler(report, prop5); @@ -346,6 +349,9 @@ public void test2() { andor.applyRuleHandler(report1, prop7); assertEquals(0, andor.getReplies().size()); + andor.applyRuleHandler(report3, prop8); + assertEquals(0, andor.getReplies().size()); + andor.applyRuleHandler(report2, prop8); assertEquals(1, andor.getReplies().size()); assertEquals(true, andor.getReplies().get(0).getSign()); diff --git a/tests/OrTests.java b/tests/OrTests.java index c91458d5..5cb946f8 100644 --- a/tests/OrTests.java +++ b/tests/OrTests.java @@ -186,4 +186,9 @@ public void testApplyRuleHandler() { assertEquals(2, or.getReplies().size()); } + public void tearDown() { + Network.clearNetwork(); + or.clear(); + } + } diff --git a/tests/PSubTreeTests.java b/tests/PSubTreeTests.java deleted file mode 100644 index 63c5fcfc..00000000 --- a/tests/PSubTreeTests.java +++ /dev/null @@ -1,130 +0,0 @@ -import static org.junit.Assert.*; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; -import sneps.exceptions.IllegalIdentifierException; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; -import sneps.network.Network; -import sneps.network.Node; -import sneps.network.VariableNode; -import sneps.network.classes.Semantic; -import sneps.network.classes.term.Base; -import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.network.classes.setClasses.NodeSet; -import sneps.network.classes.setClasses.PropositionSet; -import sneps.network.classes.setClasses.RuleUseInfoSet; -import sneps.snebr.Context; -import sneps.snebr.Controller; -import sneps.snip.classes.FlagNode; -import sneps.snip.classes.PTree; -import sneps.snip.classes.RuleUseInfo; -import sneps.snip.classes.PTree.PSubTree; -import sneps.snip.classes.PTree.PTreeNode; -import sneps.snip.matching.Binding; -import sneps.snip.matching.LinearSubstitutions; - - -public class PSubTreeTests { - private static Context context; - private static String contextName = "TempContext"; - private static NodeSet ants; - private static PTree tree; - private static RuleUseInfo rui; - - @Before - public void setUp() { - try { - Controller.createContext("default"); - } catch (DuplicateContextNameException e1) { - assertNotNull(e1.getMessage(), e1); - } - - ants = new NodeSet(); - try { - context = Controller.createContext(contextName); - } catch (DuplicateContextNameException e1) { - assertNotNull(e1.getMessage(), e1); - } - VariableNode var; - Node fido,dog; - try { - var = Network.buildVariableNode("X"); - fido = Network.buildBaseNode("Fido", new Semantic("Member")); - dog = Network.buildBaseNode("Dog", new Semantic("Class")); - ants.addNode(var); ants.addNode(fido); ants.addNode(dog); - } catch (IllegalIdentifierException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - var = new VariableNode(); - fido = new Node(new Base("Fido")); - dog = new Node(new Base("Dog")); - ants.addNode(var); ants.addNode(fido); ants.addNode(dog); - } - - LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - PropositionSet supports = new PropositionSet(); - FlagNode fn; - - try { - supports.add(var.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(var, supports, 1); - fns.insert(fn); - - supports.clearSet(); - try { - supports.add(dog.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(dog, supports, 1); - fns.insert(fn); - sub.insert(new Binding(var, dog)); - - rui = new RuleUseInfo(sub, 1, 0, fns); - tree = new PTree(); - } - - @After - public void tearDown(){ - Network.clearNetwork(); - ants.clear(); - tree = null; - rui = null; - } - - @Test - public void testPSubTreeInsert(){ - tree.buildTree(ants); - tree.insertRUI(rui); - RuleUseInfoSet ruiSet = new RuleUseInfoSet(); - for(PSubTree subTree : tree.getSubTrees()){ - subTree.insert(rui); - ruiSet.addAll(subTree.getRootRUIS()); - } - assertFalse( - "PSubTree: PSubTree RootRUIS cannot be empty", - ruiSet.isEmpty()); - } - @Test - public void testPSubTreeGetLeafPattern(){ - tree.buildTree(ants); - tree.insertRUI(rui); - for(PSubTree subTree : tree.getSubTrees()){ - PTreeNode node = subTree.getLeafPattern(1, subTree.getRoot()); - assertNotNull( - "PSubTree: PSubTree GetLeafPattern cannot return null", - node); - } - } -} diff --git a/tests/PTreeNodeTests.java b/tests/PTreeNodeTests.java deleted file mode 100644 index 3dab6528..00000000 --- a/tests/PTreeNodeTests.java +++ /dev/null @@ -1,130 +0,0 @@ -import static org.junit.Assert.*; - -import java.util.HashSet; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; -import sneps.exceptions.IllegalIdentifierException; -import sneps.exceptions.NodeNotFoundInNetworkException; -import sneps.exceptions.NotAPropositionNodeException; -import sneps.network.Network; -import sneps.network.Node; -import sneps.network.VariableNode; -import sneps.network.classes.Semantic; -import sneps.network.classes.term.Base; -import sneps.network.classes.setClasses.FlagNodeSet; -import sneps.network.classes.setClasses.NodeSet; -import sneps.network.classes.setClasses.PropositionSet; -import sneps.snebr.Context; -import sneps.snebr.Controller; -import sneps.snip.classes.FlagNode; -import sneps.snip.classes.PTree; -import sneps.snip.classes.PTree.PSubTree; -import sneps.snip.classes.RuleUseInfo; -import sneps.snip.matching.Binding; -import sneps.snip.matching.LinearSubstitutions; - - -public class PTreeNodeTests { - private static Context context; - private static String contextName = "TempContext"; - private static NodeSet ants; - private static PTree tree; - private static HashSet subTrees; - private static RuleUseInfo rui; - - @Before - public void setUp() { - try { - Controller.createContext("default"); - } catch (DuplicateContextNameException e1) { - assertNotNull(e1.getMessage(), e1); - } - - ants = new NodeSet(); - try { - context = Controller.createContext(contextName); - } catch (DuplicateContextNameException e1) { - assertNotNull(e1.getMessage(), e1); - } - VariableNode var; - Node fido,dog; - try { - var = Network.buildVariableNode("X"); - fido = Network.buildBaseNode("Fido", new Semantic("Member")); - dog = Network.buildBaseNode("Dog", new Semantic("Class")); - ants.addNode(var); ants.addNode(fido); ants.addNode(dog); - } catch (IllegalIdentifierException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - var = new VariableNode(); - fido = new Node(new Base("Fido")); - dog = new Node(new Base("Dog")); - ants.addNode(var); ants.addNode(fido); ants.addNode(dog); - } - - LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - PropositionSet supports = new PropositionSet(); - FlagNode fn; - - try { - supports.add(var.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(var, supports, 1); - fns.insert(fn); - - supports.clearSet(); - try { - supports.add(dog.getId()); - } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) { - assertNotNull(e.getMessage(), e); - } - fn = new FlagNode(dog, supports, 1); - fns.insert(fn); - sub.insert(new Binding(var, dog)); - - rui = new RuleUseInfo(sub, 1, 0, fns); - tree = new PTree(); - tree.buildTree(ants); - subTrees = tree.getSubTrees(); - } - - @After - public void tearDown() { - Network.clearNetwork(); - ants.clear(); - tree = null; - rui = null; - } - - @Test - public void testPTreeNodeInsertRUI(){ - for(PSubTree subTree : subTrees){ - subTree.insert(rui); - assertNotNull( - "PTreeNode: PSubTree RootRUIS cannot be null", - subTree.getRootRUIS()); - } - } - - @Test - public void testPTreeNodeInsertIntoTree(){ - for(PSubTree subTree : subTrees){ - subTree.insert(rui); - assertNotNull( - "PTreeNode: PSubTree RootRUIS cannot be null", - subTree.getRootRUIS()); - } - - } - -} diff --git a/tests/PTreeTests.java b/tests/PTreeTests.java index 072d9d0f..c6011cbb 100644 --- a/tests/PTreeTests.java +++ b/tests/PTreeTests.java @@ -1,42 +1,58 @@ -import java.util.HashSet; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; + +import java.util.LinkedList; +import java.util.Queue; import org.junit.After; import org.junit.Before; import org.junit.Test; +import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; import sneps.exceptions.DuplicatePropositionException; +import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Network; import sneps.network.Node; +import sneps.network.PropositionNode; import sneps.network.VariableNode; +import sneps.network.cables.DownCable; +import sneps.network.cables.DownCableSet; +import sneps.network.classes.CaseFrame; +import sneps.network.classes.Relation; import sneps.network.classes.Semantic; +import sneps.network.classes.Wire; import sneps.network.classes.term.Base; +import sneps.network.classes.term.Open; +import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; -import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snip.InferenceTypes; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; import sneps.snip.classes.PTree.PSubTree; +import sneps.snip.classes.PTree.PTreeNode; import sneps.snip.classes.RuleUseInfo; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; import junit.framework.TestCase; public class PTreeTests extends TestCase { - private static Context context; - private static String contextName = "TempContext"; private static NodeSet ants; + Node prop1, prop2, prop3; private static PTree tree; - private static RuleUseInfo rui; + private static RuleUseInfo rui, rui1, rui2; @Before - public void setUp(){ + public void setUp() { try { Controller.createContext("default"); } catch (DuplicateContextNameException e1) { @@ -44,26 +60,110 @@ public void setUp(){ } ants = new NodeSet(); - try { - context = Controller.createContext(contextName); - } catch (DuplicateContextNameException e1) { - assertNotNull(e1.getMessage(), e1); - } - VariableNode var; - Node fido,dog; + VariableNode var1, var2; + Node fido, dog; + Node man, woman, married; + Node john, mary; + //Node prop1, prop2, prop3; + ArrayList wires = new ArrayList(); + LinkedList rels = new LinkedList(); + Relation memberRel = Network.defineRelation("Member", "NodeSet"); + Relation classRel = Network.defineRelation("Class", "NodeSet"); + rels.add(memberRel); rels.add(classRel); + CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); + Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null, wire5 = null; + rels.clear(); + try { - var = Network.buildVariableNode("X"); - fido = Network.buildBaseNode("Fido", new Semantic("Member")); - dog = Network.buildBaseNode("Dog", new Semantic("Class")); - ants.addNode(var); ants.addNode(fido); ants.addNode(dog); + var1 = Network.buildVariableNode("X"); + var2 = Network.buildVariableNode("Y"); + + //fido = Network.buildBaseNode("Fido", new Semantic("Member")); + //dog = Network.buildBaseNode("Dog", new Semantic("Class")); + //ants.addNode(var); //ants.addNode(fido); //ants.addNode(dog); + + man = Network.buildBaseNode("Man", new Semantic("Class")); + woman = Network.buildBaseNode("Woman", new Semantic("Class")); + married = Network.buildBaseNode("Married", new Semantic("Class")); + john = Network.buildBaseNode("John", new Semantic("Class")); + mary = Network.buildBaseNode("Mary", new Semantic("Class")); + wire1 = new Wire(memberRel, var1); + wire2 = new Wire(memberRel, var2); + wire3 = new Wire(classRel, man); + wire4 = new Wire(memberRel, woman); + wire5 = new Wire(memberRel, married); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); - var = new VariableNode(); - fido = new Node(new Base("Fido")); - dog = new Node(new Base("Dog")); - ants.addNode(fido); ants.addNode(var); ants.addNode(dog); + var1 = new VariableNode(new Variable("X")); + var2 = new VariableNode(new Variable("Y")); + man = new Node(new Base("Man")); + woman = new Node(new Base("Woman")); + married = new Node(new Base("Married")); + john = new Node(new Base("John")); + mary = new Node(new Base("Mary")); + + //fido = new Node(new Base("Fido")); + //dog = new Node(new Base("Dog")); + //ants.addNode(fido); ants.addNode(var); ants.addNode(dog); + } + + try { + wires.clear(); wires.add(wire1); wires.add(wire3); + prop1 = Network.buildMolecularNode(wires, caseFrameMC); + + wires.clear(); wires.add(wire2); wires.add(wire4); + prop2 = Network.buildMolecularNode(wires, caseFrameMC); + + wires.clear(); wires.add(wire1); wires.add(wire2); wires.add(wire5); + prop3 = Network.buildMolecularNode(wires, caseFrameMC); + } catch (CannotBuildNodeException | EquivalentNodeException + | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { + assertNotNull(e1.getMessage(), e1); } + + LinkedList dcList = new LinkedList(); + NodeSet nodeSet1 = new NodeSet(); + DownCable dc1; DownCableSet dcs; + + nodeSet1.addNode(var1); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(man); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop1 = new PropositionNode(new Open("Prop1", dcs)); + ((Open) (prop1.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(woman); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop2 = new PropositionNode(new Open("Prop2", dcs)); + ((Open) (prop2.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); + dcList.clear(); + //------------------------------------------------------------// + nodeSet1.clear(); nodeSet1.addNode(var1); nodeSet1.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet1); + dcList.add(dc1); + nodeSet1.clear(); nodeSet1.addNode(married); + dc1 = new DownCable(classRel, nodeSet1); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new PropositionNode(new Open("Prop3", dcs)); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); + ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); + dcList.clear(); + //------------------------------------------------------------// + + ants.addNode(prop1); + ants.addNode(prop2); + ants.addNode(prop3); LinearSubstitutions sub = new LinearSubstitutions(); FlagNodeSet fns = new FlagNodeSet(); @@ -71,38 +171,100 @@ public void setUp(){ FlagNode fn; try { - supports.add(var.getId()); + supports.add(prop1.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(var, supports, 1); + fn = new FlagNode(prop1, supports, 1); fns.insert(fn); - - supports.clearSet(); + sub.putIn(new Binding(var1, john)); + rui = new RuleUseInfo(sub, 1, 0, fns, InferenceTypes.BACKWARD); + + LinearSubstitutions sub1 = new LinearSubstitutions(); + FlagNodeSet fns1 = new FlagNodeSet(); + PropositionSet supports1 = new PropositionSet(); + FlagNode fn1; try { - supports.add(dog.getId()); + supports.add(prop2.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(dog, supports, 1); - fns.insert(fn); - sub.insert(new Binding(var, dog)); + fn1 = new FlagNode(prop2, supports1, 1); + fns1.insert(fn1); + sub1.putIn(new Binding(var2, mary)); + rui1 = new RuleUseInfo(sub1, 1, 0, fns1, InferenceTypes.BACKWARD); - supports.clearSet(); + LinearSubstitutions sub2 = new LinearSubstitutions(); + FlagNodeSet fns2 = new FlagNodeSet(); + PropositionSet supports2 = new PropositionSet(); + FlagNode fn2; try { - supports.add(fido.getId()); + supports.add(prop3.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(fido, supports, 1); - fns.insert(fn); + fn2 = new FlagNode(prop3, supports2, 1); + fns2.insert(fn2); + sub2.putIn(new Binding(var1, john)); + sub2.putIn(new Binding(var2, mary)); + rui2 = new RuleUseInfo(sub2, 1, 0, fns2, InferenceTypes.BACKWARD); - rui = new RuleUseInfo(sub, 1, 0, fns); tree = new PTree(); } + + @Test + public void testBuildTree() { + // Filling Pattern Variables and Variable Patterns + tree.fillPVandVP(ants); + assertEquals(3, tree.getPatternVariables().size()); + assertEquals(2, tree.getVariablePatterns().size()); + + // Computing Pattern sequence + Queue res = tree.getPatternSequence(); + assertEquals(3, res.size()); + + // Constructing the tree + tree.constructBottomUp(res); + assertNotNull("PTree: Built Sub Trees is null", + tree.getSubTrees()); + assertNotNull("PTree: Built Sub Trees Map is null", + tree.getSubTreesMap()); + + assertFalse("PTree: Built Sub Trees is empty", + tree.getSubTrees().isEmpty()); + assertEquals(1, tree.getSubTrees().size()); + + assertFalse("PTree: Built Sub Trees Map is empty", + tree.getSubTreesMap().isEmpty()); + assertEquals(3, tree.getSubTreesMap().size()); + + PTreeNode root = tree.getSubTrees().iterator().next().getRoot(); + + // Root node represents the conjunction of Man(x), Woman(x), Married(x, y) + assertEquals(3, root.getPats().size()); + assertEquals(2, root.getVars().size()); + + // Conjunction of Man(x) and Married(x, y) + assertEquals(2, root.getLeftChild().getPats().size()); + assertEquals(2, root.getLeftChild().getVars().size()); + + // Man(x) is a leaf node + assertEquals(1, root.getLeftChild().getLeftChild().getPats().size()); + assertEquals(1, root.getLeftChild().getLeftChild().getVars().size()); + assertEquals(null, root.getLeftChild().getLeftChild().getLeftChild()); + + // Married(x, y) is a leaf node + assertEquals(1, root.getLeftChild().getRightChild().getPats().size()); + assertEquals(2, root.getLeftChild().getRightChild().getVars().size()); + assertEquals(null, root.getLeftChild().getRightChild().getLeftChild()); + + // Woman(x) is a leaf node + assertEquals(1, root.getRightChild().getPats().size()); + assertEquals(1, root.getRightChild().getVars().size()); + } @Test public void testInsertRUI() { @@ -110,37 +272,48 @@ public void testInsertRUI() { RuleUseInfoSet ruiSet = new RuleUseInfoSet(); tree.insertRUI(rui); - HashSet set = tree.getSubTrees(); - assertNotNull( - "PTree: PSubTrees cannot be null", - set); - - for(PSubTree subTree : set) - ruiSet.addAll(subTree.getRootRUIS()); - - assertFalse( - "PTree: PSubTree getRootRUIS cannot return empty", - ruiSet.isEmpty()); - assertTrue( - "PTree: PSubTree getRootRUI doesn't contain created RUI", - ruiSet.contains(rui)); + tree.insertRUI(rui1); + PTreeNode root = tree.getSubTrees().iterator().next().getRoot(); + + // Since rui is received from the antecedent Man(x), then it should be + // inserted in the Man(x) leaf node + assertTrue("Rui is not inserted in the correct PTree node", + root.getLeftChild().getLeftChild().getRUIS().contains(rui)); + + // Since rui1 is received from the antecedent Woman(x), then it should be + // inserted in the Woman(x) leaf node + assertTrue("Rui is not inserted in the correct PTree node", + root.getRightChild().getRUIS().contains(rui1)); + + ruiSet = root.getRUIS(); + assertTrue(ruiSet.isEmpty()); + + tree.insertRUI(rui2); + ruiSet = root.getRUIS(); + assertEquals(1, ruiSet.size()); + // Value of pcount of rui stored at the root should be equal to the num of + // antecedents + assertEquals(3, ruiSet.iterator().next().getPosCount()); } - + + /** + * PSubTree Tests. + */ + @Test - public void testBuildTree() { + public void testPSubTreeGetLeafPattern() { tree.buildTree(ants); - assertNotNull("PTree: Built Sub Trees is null", - tree.getSubTrees()); - assertNotNull("PTree: Built Sub Trees Map is null", - tree.getSubTreesMap()); -/* assertEquals("PTree: Built Sub Trees is empty", - false, tree.getSubTrees().isEmpty()); - assertEquals("PTree: Built Sub Trees Map is empty", - false, tree.getSubTreesMap().isEmpty());*/ + tree.insertRUI(rui); + PSubTree subTree = tree.getSubTrees().iterator().next(); + assertNotNull("PSubTree: PSubTree GetLeafPattern cannot return null", + subTree.getLeafPattern(prop1.getId(), subTree.getRoot())); + assertFalse("PSubTree: Leaf pattern node returned from GetLeafPattern should " + + "contain the rui inserted", + subTree.getLeafPattern(prop1.getId(), subTree.getRoot()).getRUIS().isEmpty()); } @After - public void tearDown(){ + public void tearDown() { Network.clearNetwork(); ants.clear(); tree = null; diff --git a/tests/SIndexTest.java b/tests/SIndexTest.java index 78ad6411..6172bbd2 100644 --- a/tests/SIndexTest.java +++ b/tests/SIndexTest.java @@ -2,6 +2,8 @@ import static org.junit.Assert.assertEquals; import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; @@ -12,7 +14,6 @@ import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; -import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.snip.InferenceTypes; import sneps.snip.classes.FlagNode; @@ -76,24 +77,24 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo VariableNode vn6 = new VariableNode(v6); VariableNode vn7 = new VariableNode(v7); - VarNodeSet vns = new VarNodeSet(); - VarNodeSet vns1 = new VarNodeSet(); - VarNodeSet vns2 = new VarNodeSet(); - VarNodeSet vns3 = new VarNodeSet(); - VarNodeSet vns4 = new VarNodeSet(); - VarNodeSet vns5 = new VarNodeSet(); - VarNodeSet vns6 = new VarNodeSet(); - VarNodeSet vns7 = new VarNodeSet(); - - - vns.addVarNode(vn); - vns1.addVarNode(vn1); - vns2.addVarNode(vn2); - vns3.addVarNode(vn3); - vns4.addVarNode(vn4); - vns5.addVarNode(vn5); - vns6.addVarNode(vn6); - vns7.addVarNode(vn7); + Set vns = new HashSet(); + Set vns1 = new HashSet(); + Set vns2 = new HashSet(); + Set vns3 = new HashSet(); + Set vns4 = new HashSet(); + Set vns5 = new HashSet(); + Set vns6 = new HashSet(); + Set vns7 = new HashSet(); + + + vns.add(vn); + vns1.add(vn1); + vns2.add(vn2); + vns3.add(vn3); + vns4.add(vn4); + vns5.add(vn5); + vns6.add(vn6); + vns7.add(vn7); //RUI set SIndex SIndex index = new SIndex((byte) 0, vns); @@ -103,7 +104,7 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo Binding b = new Binding(vn, n); Binding b1 = new Binding(vn1, n1); - Binding b2 = new Binding(vn2, n); + Binding b2 = new Binding(vn2, n1); Binding b3 = new Binding(vn3, n); Binding b4 = new Binding(vn4, n); Binding b5 = new Binding(vn5, n1); @@ -113,6 +114,7 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo Binding b9 = new Binding(vn, n2); ls.putIn(b); + ls.putIn(b2); ls1.putIn(b1); ls4.putIn(b8); ls5.putIn(b9); From a65f2a510211d55ee3b573d31df33d0f02d3d35c Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Wed, 22 May 2019 15:00:18 +0200 Subject: [PATCH 19/22] AndEntailment and its tests --- src/sneps/network/RuleNode.java | 6 +- src/sneps/snip/classes/FlagNode.java | 3 +- src/sneps/snip/rules/AndEntailment.java | 269 ++++++------- src/sneps/snip/rules/NumericalEntailment.java | 16 +- tests/AndEntailmentTests.java | 361 ++++++++++-------- tests/NumericalEntailmentTests.java | 30 +- tests/PTreeTests.java | 10 +- 7 files changed, 346 insertions(+), 349 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index e83a3ac0..1280daaa 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -436,16 +436,14 @@ public void splitToNodesWithVarsAndWithout(NodeSet allNodes, NodeSet withVars, N } public RuleUseInfo addConstantRui(RuleUseInfo rui) { - //Context contxt = (Context) Controller.getContextByName(context); - //RuleUseInfo tRui = contextConstantRUI.get(contxt); if (constantRUI != null) constantRUI = rui.combine(constantRUI); else constantRUI = rui; if (constantRUI == null) throw new NullPointerException( - "The existed RUI could not be merged " + "with the given rui so check your code again"); - //contextConstantRUI.put(contxt, tRui); + "The existed RUI could not be merged " + + "with the given rui so check your code again"); return constantRUI; } diff --git a/src/sneps/snip/classes/FlagNode.java b/src/sneps/snip/classes/FlagNode.java index 60ff0f84..989531cc 100644 --- a/src/sneps/snip/classes/FlagNode.java +++ b/src/sneps/snip/classes/FlagNode.java @@ -64,7 +64,8 @@ public boolean isEqual(FlagNode fn) { } public String toString() { - return "Proposiiton: " + node.toString() + " Flag: " + flag; + return "Proposiiton: " + node.toString() + " Supports: " + support + + " Flag: " + flag; } } diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index ee4570d0..66fba863 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -1,26 +1,26 @@ package sneps.snip.rules; +import java.util.ArrayList; +import java.util.Set; + import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; -import sneps.network.VariableNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; -import sneps.network.classes.setClasses.VarNodeSet; import sneps.network.classes.term.Molecular; -import sneps.network.classes.term.Open; import sneps.snip.Report; +import sneps.snip.channels.Channel; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; import sneps.snip.classes.RuisHandler; +import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; -import sneps.snip.matching.Binding; -import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.matching.Substitutions; +import sneps.snip.classes.SIndex; /** * @className AndEntailment.java @@ -28,180 +28,157 @@ * @ClassDescription The AndEntailment is an inference rule that asserts the conjunction of all the nodes in its antecedent position to imply the conjunction of all the nodes in its consequent position. * When the rule node receives a request from a node in its consequent position, it sends requests to all its nodes in its antecedent position. * Generally, when a rule node has enough reports, it creates a reply report and broadcasts it to all consequent nodes. - * In the case of the AndEntailment rule, the reply report is created and sent when all antecedent nodes sent their respective reports. + * In the case of the AndEntailment rule, the reply report is created and sent when all antecedent nodes send their respective reports. * - * @author Amgad Ashraf - * @version 3.00 31/5/2018 */ public class AndEntailment extends RuleNode { private static final long serialVersionUID = -8545987005610860977L; - /** - * Constructor for the AndEntailment rule node - * @param syn - */ public AndEntailment(Molecular syn) { super(syn); + antecedents = getDownAntNodeSet(); + processNodes(antecedents); + consequents = getDownConsqNodeSet(); } /** - * Creates the first RuleUseInfo from a given Report and stores it(if positive) - * Also checks if current number of positive Reports satisfy rule + * Creates the first RuleUseInfo from a given Report and stores it (if positive). + * Also checks if current number of positive Reports satisfies the rule. * @param report * @param signature */ @Override - public void applyRuleHandler(Report report, Node signature) { - String contxt = report.getContextName(); - if (report.isPositive()) { - PropositionSet propSet = report.getSupports(); - FlagNodeSet fns = new FlagNodeSet(); - fns.insert(new FlagNode(signature, propSet, 1)); - RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), - 1, 0, fns); - addNotSentRui(rui, contxt, signature); - } - if (contextRuisSet.getByContext(contxt).getPositiveNodes().size() >= getAntSize()) - sendSavedRUIs(report.getContextName()); - } - - /** - * Creates a Report from a given RuleUseInfo to be broadcasted to outgoing channels - * Also checks report supports and modifies accordingly - * @param Rui - * @param contextID - */ - @Override - protected void applyRuleOnRui(RuleUseInfo Rui, String contextID) { - if (Rui.getPosCount() >= getAntSize()){ - Substitutions sub = Rui.getSubstitutions(); - FlagNodeSet justification = new FlagNodeSet(); - justification.addAll(Rui.getFlagNodeSet()); - PropositionSet supports = new PropositionSet(); - - for(FlagNode fn : justification){ - try { - supports = supports.union(fn.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} + public ArrayList applyRuleHandler(Report report, Node signature) { + if(report.isNegative()) + return null; + + //System.out.println("------------------------------"); + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + + PropositionSet propSet = report.getSupport(); + FlagNodeSet fns = new FlagNodeSet(); + fns.insert(new FlagNode(signature, propSet, 1)); + RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns, + report.getInferenceType()); + + if(antNodesWithoutVars.contains(signature)) { + addConstantRui(rui); + if (constantRUI.getPosCount() != antNodesWithoutVars.size()) + return null; + + if(ruisHandler == null) { + response = applyRuleOnRui(constantRUI); + if(response != null) + responseList.add(response); } - - try { - supports = supports.union(Rui.getSupports()); - } catch (NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - if(this.getTerm() instanceof Open){ - //knownInstances check this.free vars - > bound - VarNodeSet freeVars = ((Open)this.getTerm()).getFreeVariables(); - Substitutions ruiSub = Rui.getSubstitutions(); - boolean allBound = true; - - for(Report report : knownInstances){ - //Bound to same thing(if bound) - for(VariableNode var : freeVars){ - if(!report.getSubstitutions().isBound(var)){ - allBound = false; - break; - } + RuleUseInfoSet ruis = ((PTree) ruisHandler).getAllRootRuis(); + if (ruis != null) { + RuleUseInfo combined; + for (RuleUseInfo r : ruis) { + combined = r.combine(constantRUI); + if (combined != null) { + response = applyRuleOnRui(combined); + if(response != null) + responseList.add(response); } - if(allBound){//if yes - Substitutions instanceSub = report.getSubstitutions(); - - for(int i = 0; i < ruiSub.cardinality(); i++){ - Binding ruiBind = ruiSub.getBinding(i);//if rui also bound - Binding instanceBind = instanceSub. - getBindingByVariable(ruiBind.getVariable()); - if( !((instanceBind != null) && - (instanceBind.isEqual(ruiBind))) ){ - allBound = false; - break; - } - } - if(allBound){ - //combine known with rui - Substitutions newSub = new LinearSubstitutions(); - newSub.insert(instanceSub); - newSub.insert(ruiSub); - - //add to new support and send - Report reply = new Report(newSub, supports, true, contextID); - sendReportToConsequents(reply); - return; - } + } + } + } + else { + if (ruisHandler == null) + ruisHandler = addRuiHandler(); + RuleUseInfoSet res = ruisHandler.insertRUI(rui); + if (res == null) + res = new RuleUseInfoSet(); + for (RuleUseInfo tRui : res) { + if (tRui.getPosCount() != antNodesWithVars.size()) + return null; + + if(constantRUI == null) { + response = applyRuleOnRui(tRui); + if(response != null) + responseList.add(response); + } + else { + RuleUseInfo combined; + combined = tRui.combine(constantRUI); + if (combined != null) { + response = applyRuleOnRui(combined); + if(response != null) + responseList.add(response); } } } - - Report reply = new Report(sub, supports, true, contextID); - sendReportToConsequents(reply); + } + + if(responseList.isEmpty()) + return null; + + return responseList; } /** - * Creates an appropriate PTree as a RuisHandler, builds it and inserts it into ContextRuisSet by Context - * @param context - * @return + * Creates a Report from a given RuleUseInfo to be broadcasted to outgoing channels */ @Override - public RuisHandler createRuisHandler(String context) { - PTree tree = new PTree(); - NodeSet ants = antNodesWithoutVars; - ants.addAll(antNodesWithVars); - tree.buildTree(ants); - this.addContextRuiHandler(context, tree); - return tree; + protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + if (rui.getPosCount() < getAntSize()) + return null; + + PropositionSet replySupport = new PropositionSet(); + for(FlagNode fn : rui.getFlagNodeSet()) + try { + //System.out.println("HERE"); + //System.out.println(fn.getSupport()); + replySupport.union(fn.getSupport()); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + } + //System.out.println(replySupport); + + // TODO + // Add rule node to replySupport + + Report reply = new Report(rui.getSubstitutions(), replySupport, true, + rui.getType()); + reportsToBeSent.add(reply); + + RuleResponse r = new RuleResponse(); + r.setReport(reply); + //Set forwardChannels = getOutgoingChannelsForReport(reply); + //r.addAllChannels(forwardChannels); + + return r; } - + /** - * Inserts given RuleUseInfo into the appropriate PTree and updates the corresponding PTree - * @param rui - * @param contxt - * @param signature + * Naming convention used to retrieve Nodes in Down Antecedent position is "&ant"; + * "&" for AndEntailment, "ant" for Antecedent + * @return NodeSet */ - public void addNotSentRui(RuleUseInfo rui, String contxt, Node signature){ - PTree tree = (PTree) contextRuisSet.getByContext(contxt); - if (tree == null) - tree = (PTree) createRuisHandler(contxt); - tree.insertRUI(rui); - if(!tree.getPositiveNodes().contains(signature)) - tree.getPositiveNodes().addNode(signature); - contextRuisSet.addHandlerSet(contxt, tree); + @Override + public NodeSet getDownAntNodeSet() { + return this.getDownNodeSet("&ant"); + } + + @Override + public NodeSet getDownConsqNodeSet() { + return this.getDownNodeSet("&consq"); } - /** - * Prepares the appropriate PTree and all its root RuleUseInfo for broadcasting - * @param contextID - */ - private void sendSavedRUIs(String contextID) { - RuleUseInfo addedConstant = getConstantRUI(contextID); - if (addedConstant == null && antNodesWithoutVars.size() != 0) - return; - - if( (addedConstant != null) &&(antNodesWithoutVars.size() != addedConstant.getPosCount())) - return; - - RuleUseInfoSet ruis = ((PTree)contextRuisSet.getByContext(contextID)).getAllRootRuis(); - if (ruis == null) { - applyRuleOnRui(addedConstant, contextID); - return; - } - RuleUseInfo combined; - for (RuleUseInfo info : ruis) { - combined = info.combine(addedConstant); - if (combined != null) - applyRuleOnRui(combined, contextID); - } + @Override + protected RuisHandler createRuisHandler() { + PTree tree = new PTree(); + tree.buildTree(antNodesWithVars); + return tree; } - /** - * Naming convention used to retrieve Nodes in Down Antecedent position is "&ant"; - * "&" for AndEntailment, "ant" for Antecedent - * @return - */ @Override - public NodeSet getDownAntNodeSet() { - return this.getDownNodeSet("&ant");//ants for & name convention + protected byte getSIndexType() { + return SIndex.PTREE; } } diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index e97f942e..f0b1b927 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -61,7 +61,7 @@ public NumericalEntailment(Molecular syn) { * @param report * @param signature */ - //@Override + @Override public ArrayList applyRuleHandler(Report report, Node signature) { processNodes(antecedents); System.out.println("---------------"); @@ -78,15 +78,9 @@ public ArrayList applyRuleHandler(Report report, Node signature) { RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns, report.getInferenceType()); - // Inserting the RuleUseInfo into the RuleNode's RuisHandler: - // SIndex in case there are shared variables between the antecedents, or - // RUISet in case there are no shared variables - if(ruisHandler == null) - ruisHandler = addRuiHandler(); - if(antNodesWithoutVars.contains(signature)) { addConstantRui(rui); - if (ruisHandler.isEmpty()) { + if (ruisHandler == null) { response = applyRuleOnRui(constantRUI); if(response != null) responseList.add(response); @@ -101,6 +95,12 @@ public ArrayList applyRuleHandler(Report report, Node signature) { } } else { + // Inserting the RuleUseInfo into the RuleNode's RuisHandler: + // SIndex in case there are shared variables between the antecedents, or + // RUISet in case there are no shared variables + if(ruisHandler == null) + ruisHandler = addRuiHandler(); + // The RUI created for the given report is inserted to the RuisHandler RuleUseInfoSet res = ruisHandler.insertRUI(rui); System.out.println(res); diff --git a/tests/AndEntailmentTests.java b/tests/AndEntailmentTests.java index b0a289ec..e838bf18 100644 --- a/tests/AndEntailmentTests.java +++ b/tests/AndEntailmentTests.java @@ -16,6 +16,7 @@ import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Network; import sneps.network.Node; +import sneps.network.PropositionNode; import sneps.network.VariableNode; import sneps.network.cables.DownCable; import sneps.network.cables.DownCableSet; @@ -23,6 +24,7 @@ import sneps.network.classes.Relation; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; +import sneps.network.classes.term.Base; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.FlagNodeSet; @@ -30,6 +32,7 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; import sneps.snebr.Controller; +import sneps.snip.InferenceTypes; import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; @@ -40,17 +43,17 @@ import sneps.snip.rules.AndEntailment; -public class AndEntailmentTests extends TestCase{ +public class AndEntailmentTests extends TestCase { private static Context context; private static String contextName = "TempContext"; private static AndEntailment and; - private static Node fido, var, dog, barks; + private static Node fido, var1, var2, dog, barks; + private static Node man, woman, married, husband, john, mary, steve, sue; private static Node prop1, prop2, prop3, prop4; - private static RuleUseInfo rui; - private static Report report; + private static Report report, report1, report2, report3, report4, report5; @Before - public void setUp(){ + public void setUp() { try { context = Controller.createContext(contextName); } catch (DuplicateContextNameException e1) { @@ -58,13 +61,12 @@ public void setUp(){ } LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - FlagNode fn; PropositionSet support = new PropositionSet(); ArrayList wires = new ArrayList(); LinkedList dc = new LinkedList(); LinkedList rels = new LinkedList(); NodeSet nodeSet = new NodeSet(); + NodeSet nodeSett = new NodeSet(); Relation memberRel = Network.defineRelation("Member", "NodeSet"); Relation classRel = Network.defineRelation("Class", "NodeSet"); Relation doesRel = Network.defineRelation("Does", "NodeSet"); @@ -73,211 +75,226 @@ public void setUp(){ rels.add(memberRel); rels.add(classRel); CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); rels.clear(); rels.add(classRel); rels.add(doesRel); - CaseFrame caseFrameCD = Network.defineCaseFrame("ClassDoes", rels); - rels.clear(); rels.add(memberRel); rels.add(doesRel); - CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); rels.clear(); rels.add(antsRel); rels.add(consRel); CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); - Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; + Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null, wire5 = null; + Wire wire6 = null, wire7 = null, wire8 = null, wire9 = null, wire10 = null; rels.clear(); -//-------------------------------------- fido, dog, barks, X ---------------------------------------------// +//----------------------- SETTING UP NODES --------------------------------// try { - var = Network.buildVariableNode("X"); - fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - barks = Network.buildBaseNode("Barks", new Semantic("Proposition"));//MolecularNode(wires, caseFrame); - wire1 = new Wire(memberRel, fido); - wire2 = new Wire(classRel, dog); - wire3 = new Wire(doesRel, barks); - wire4 = new Wire(memberRel, var); + var1 = Network.buildVariableNode("X"); + var2 = Network.buildVariableNode("Y"); + + man = Network.buildBaseNode("Man", new Semantic("Class")); + woman = Network.buildBaseNode("Woman", new Semantic("Class")); + married = Network.buildBaseNode("Married", new Semantic("Class")); + husband = Network.buildBaseNode("Husband", new Semantic("Class")); + john = Network.buildBaseNode("John", new Semantic("Class")); + mary = Network.buildBaseNode("Mary", new Semantic("Class")); + steve = Network.buildBaseNode("Steve", new Semantic("Class")); + sue = Network.buildBaseNode("Sue", new Semantic("Class")); + wire5 = new Wire(memberRel, var1); + wire6 = new Wire(memberRel, var2); + wire7 = new Wire(classRel, man); + wire8 = new Wire(classRel, woman); + wire9 = new Wire(classRel, married); + wire10 = new Wire(classRel, husband); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); - var = new VariableNode(new Variable("X")); + var1 = new VariableNode(new Variable("X")); + var2 = new VariableNode(new Variable("Y")); + man = new Node(new Base("Man")); + woman = new Node(new Base("Woman")); + married = new Node(new Base("Married")); + husband = new Node(new Base("Husband")); + john = new Node(new Base("John")); + mary = new Node(new Base("Mary")); + steve = new Node(new Base("Steve")); + sue = new Node(new Base("Sue")); } -//------------------------------------- prop1, prop2, prop3, prop4 ----------------------------------------------// +//---------------------- PROPOSITION NODES SETUP -------------------------------// try { - wires.clear(); wires.add(wire1); wires.add(wire2); + wires.clear(); wires.add(wire5); wires.add(wire7); prop1 = Network.buildMolecularNode(wires, caseFrameMC); - wires.clear(); wires.add(wire2); wires.add(wire3); - prop2 = Network.buildMolecularNode(wires, caseFrameCD); + wires.clear(); wires.add(wire6); wires.add(wire8); + prop2 = Network.buildMolecularNode(wires, caseFrameMC); - wires.clear(); wires.add(wire4); wires.add(wire2); + wires.clear(); wires.add(wire5); wires.add(wire6); wires.add(wire9); prop3 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire1); wires.add(wire3); - prop4 = Network.buildMolecularNode(wires, caseFrameMD); + + wires.clear(); wires.add(wire5); wires.add(wire6); wires.add(wire10); + prop4 = Network.buildMolecularNode(wires, caseFrameMC); } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + NodeSet nodeSet3 = new NodeSet(); + NodeSet nodeSet4 = new NodeSet(); + NodeSet nodeSet5 = new NodeSet(); + NodeSet nodeSet6 = new NodeSet(); + NodeSet nodeSet7 = new NodeSet(); + NodeSet nodeSet8 = new NodeSet(); DownCable dc1; DownCableSet dcs; - nodeSet1.addNode(fido); + nodeSet1.addNode(var1); dc1 = new DownCable(memberRel, nodeSet1); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet2.addNode(man); + dc1 = new DownCable(classRel, nodeSet2); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Open("Prop1", dcs)); + prop1 = new PropositionNode(new Open("Prop1", dcs)); + //((Open) (prop1.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet3.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet3); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); + nodeSet4.addNode(woman); + dc1 = new DownCable(classRel, nodeSet4); dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Open("Prop2", dcs)); + dcs = new DownCableSet(dcList, caseFrameMC); + prop2 = new PropositionNode(new Open("Prop2", dcs)); + //((Open) (prop2.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet5.addNode(var1); nodeSet5.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet5); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet6.addNode(married); + dc1 = new DownCable(classRel, nodeSet6); dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); + dcs = new DownCableSet(dcList, caseFrameMC); + prop3 = new PropositionNode(new Open("Prop3", dcs)); + //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); + //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet7.addNode(var1); nodeSet7.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet7); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); + nodeSet8.addNode(husband); + dc1 = new DownCable(classRel, nodeSet8); dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Open("Prop4", dcs)); + dcs = new DownCableSet(dcList, caseFrameMC); + prop4 = new PropositionNode(new Open("Prop4", dcs)); + //((Open) (prop4.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); + //((Open) (prop4.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); dcList.clear(); //------------------------------------------------------------// } -//------------------------------------- AND Supports ----------------------------------------------// +//----------------------- AND SETUP -----------------------------------// + nodeSet.addNode(prop1); + nodeSet.addNode(prop2); + nodeSet.addNode(prop3); + dc.add(new DownCable(antsRel, nodeSet)); + + nodeSett.addNode(prop4); + dc.add(new DownCable(consRel, nodeSett)); + + DownCableSet dcs = new DownCableSet(dc, caseFrameAC); + +//------------------------ AND ---------------------------// + and = new AndEntailment(new Open("Open", dcs)); + + sub.putIn(new Binding((VariableNode) var1, john)); + support = new PropositionSet(); try { support.add(prop1.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(prop1, support, 1); - fns.insert(fn); - - support.clearSet(); + report = new Report(sub, support, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub1 = new LinearSubstitutions(); + FlagNodeSet fns1 = new FlagNodeSet(); + PropositionSet support1 = new PropositionSet(); + FlagNode fn1; try { - support.add(prop2.getId()); + support1.add(prop2.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(prop2, support, 1); - fns.insert(fn); - - support.clearSet(); + fn1 = new FlagNode(prop2, support1, 1); + fns1.insert(fn1); + sub1.putIn(new Binding((VariableNode) var2, mary)); + report1 = new Report(sub1, support1, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub2 = new LinearSubstitutions(); + FlagNodeSet fns2 = new FlagNodeSet(); + PropositionSet support2 = new PropositionSet(); + FlagNode fn2; try { - support.add(prop3.getId()); + support2.add(prop1.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - fn = new FlagNode(prop3, support, 1); - fns.insert(fn); - - nodeSet.addNode(prop1); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop2); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop3); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop4); - dc.add(new DownCable(consRel, nodeSet)); - - DownCableSet dcs = new DownCableSet(dc, caseFrameAC); - -//------------------------------------- AND ----------------------------------------------// - - and = new AndEntailment(new Open("Open", dcs)); - + fn2 = new FlagNode(prop1, support2, 1); + fns2.insert(fn2); + sub2.putIn(new Binding((VariableNode) var1, steve)); + report2 = new Report(sub2, support2, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub3 = new LinearSubstitutions(); + FlagNodeSet fns3 = new FlagNodeSet(); + PropositionSet support3 = new PropositionSet(); + FlagNode fn3; try { - support.add(dog.getId()); - support.add(fido.getId()); - support.add(var.getId()); + support3.add(prop2.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); } - - sub.insert(new Binding((VariableNode) var,fido)); - rui = new RuleUseInfo(sub, 1, 0, fns); - report = new Report(sub, support, true, contextName); - } - - - @Test - public void testCreateRuisHandler() { - RuisHandler createdHandler = and.createRuisHandler(contextName); - RuisHandler retrievedHandler = and.getContextRuiHandler(contextName); - assertNotNull( - "AndEntailment: CreateRuisHandler creates a null RuisHandler", - createdHandler); - assertTrue( - "AndEntailment: CreateRuisHandler doesn't creates a PTree as a Handler", - createdHandler instanceof PTree); - assertEquals("AndEntailment: GetRuisHandler retrieves a different RuisHandler from CreateRuisHandler a RuisHandler", - createdHandler, retrievedHandler); - } - - @Test - public void testApplyRuleHandler() { - and.setKnownInstances(and.getNewInstances()); - and.getNewInstances().clear(); - - and.applyRuleHandler(report, fido); - if(and.getAntSize() <= 1) - assertNotNull("AndEntailment: ApplyRuleHandler doesn't broadcast report", - and.getNewInstances()); - else - assertTrue("AndEntailment: ApplyRuleHandler broacdcasts final report without waiting for enough positive antecedents reports", - and.getNewInstances().isEmpty()); - - - and.setKnownInstances(and.getNewInstances()); - and.getNewInstances().clear(); - LinearSubstitutions sub = new LinearSubstitutions(); - FlagNodeSet fns = new FlagNodeSet(); - PropositionSet support = new PropositionSet(); - + fn3 = new FlagNode(prop2, support3, 1); + fns3.insert(fn3); + sub3.putIn(new Binding((VariableNode) var2, sue)); + report3 = new Report(sub3, support3, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub4 = new LinearSubstitutions(); + FlagNodeSet fns4 = new FlagNodeSet(); + PropositionSet support4 = new PropositionSet(); + FlagNode fn4; try { - support.add(dog.getId()); + support4.add(prop3.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException - | NodeNotFoundInNetworkException e) {} - - FlagNode fn = new FlagNode(dog, support, 1); - fns.insert(fn); - report = new Report(sub, support, false, contextName); - - and.applyRuleHandler(report, dog); - if(and.getAntSize() >= 1) - assertTrue( - "AndEntailment: ApplyRuleHandler broadcasts negative report", - and.getNewInstances().isEmpty()); + | NodeNotFoundInNetworkException e) { + assertNotNull(e.getMessage(), e); + } + fn4 = new FlagNode(prop3, support4, 1); + fns4.insert(fn4); + sub4.putIn(new Binding((VariableNode) var1, john)); + sub4.putIn(new Binding((VariableNode) var2, mary)); + report4 = new Report(sub4, support4, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub5 = new LinearSubstitutions(); + FlagNodeSet fns5 = new FlagNodeSet(); + PropositionSet support5 = new PropositionSet(); + FlagNode fn5; + try { + support5.add(prop3.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException + | NodeNotFoundInNetworkException e) { + assertNotNull(e.getMessage(), e); + } + fn5 = new FlagNode(prop3, support5, 1); + fns5.insert(fn5); + sub5.putIn(new Binding((VariableNode) var1, steve)); + sub5.putIn(new Binding((VariableNode) var2, sue)); + report5 = new Report(sub5, support5, true, InferenceTypes.BACKWARD); } - + @Test public void testGetDownAntNodeSet() { NodeSet downAntNodeSet = and.getDownAntNodeSet(); @@ -286,32 +303,52 @@ public void testGetDownAntNodeSet() { assertFalse("AndEntailment: getDownAntNodeSet retuns an empty NodeSet", downAntNodeSet.isEmpty()); } - + @Test - public void testAddNotSentRui() { - and.addNotSentRui(rui, contextName, dog); - assertNotNull("AndEntailment: addNotSentRui doesn't add a RuiHandler in contextRuisHandlers", - and.getContextRuiHandler(contextName)); - - NodeSet positives = and.getContextRuiHandler(contextName).getPositiveNodes(); - assertTrue("AndEntailment: addNotSentRui doesn't add signature to positiveNodes set", - positives.contains(dog)); - assertFalse("AndEntailment: addNotSentRui adds wrong signatures to positiveNodes set", - positives.contains(fido)); - - assertTrue("AndEntailment: addNotSentRui doesn't add a PTree in contextRuisHandlers", - and.getContextRuiHandler(contextName)instanceof PTree); + public void testAntsandConsq() { + and.processNodes(and.getAntecedents()); + assertEquals(1, and.getConsequents().size()); + assertEquals(3, and.getAntecedents().size()); + assertEquals(3, and.getAntsWithVars().size()); + assertEquals(0, and.getAntsWithoutVars().size()); + } + @Test + public void testApplyRuleHandler() { + // Man(john) + and.applyRuleHandler(report, prop1); + assertNotNull(and.getRuisHandler()); + assertTrue("AndEntailment: RuisHandler added is not a PTree", + and.getRuisHandler() instanceof PTree); + assertEquals(0, and.getReplies().size()); + + // Woman(mary) + and.applyRuleHandler(report1, prop2); + assertEquals(0, and.getReplies().size()); + + // Man(steve) + and.applyRuleHandler(report2, prop1); + assertEquals(0, and.getReplies().size()); + + // Married(john, mary) + and.applyRuleHandler(report4, prop3); + assertEquals(1, and.getReplies().size()); + + // Married(steve, sue) + and.applyRuleHandler(report5, prop3); + assertEquals(1, and.getReplies().size()); + + // Woman(sue) + and.applyRuleHandler(report3, prop2); + assertEquals(2, and.getReplies().size()); + + for(Report r : and.getReplies()) + System.out.println(r); } @After public void tearDown(){ Network.clearNetwork(); and.clear(); - fido = null; - var = null; - dog = null; - rui = null; - report = null; } } diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index 992008f9..5ff6774c 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -6,7 +6,6 @@ import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; -import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -24,18 +23,12 @@ import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; -import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; -import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.snebr.Context; import sneps.snebr.Controller; -import sneps.snebr.Support; import sneps.snip.InferenceTypes; import sneps.snip.Report; -import sneps.snip.classes.FlagNode; -import sneps.snip.classes.RuisHandler; -import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; @@ -48,7 +41,6 @@ public class NumericalEntailmentTests extends TestCase { private static NumericalEntailment numerical; private static Node fido, var, dog, barks; private static Node prop1, prop2, prop3, prop4; - private static RuleUseInfo rui; private static Report report, report1; public void setUp() { @@ -60,8 +52,6 @@ public void setUp() { LinearSubstitutions sub = new LinearSubstitutions(); PropositionSet support = new PropositionSet(); - FlagNodeSet fns = new FlagNodeSet(); - FlagNode fn; ArrayList wires = new ArrayList(); LinkedList dc = new LinkedList(); LinkedList rels = new LinkedList(); @@ -190,7 +180,6 @@ public void setUp() { //---------------------------- NUMERICAL -----------------------------------// numerical = new NumericalEntailment(new Open("Open", dcss)); - //System.out.println(numerical.getId()); numerical.setI(1); numerical.setAntecedents(a); numerical.setConsequents(c); @@ -210,7 +199,6 @@ public void setUp() { /*try { support.add(prop3.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - // TODO Auto-generated catch block e.printStackTrace(); }*/ sub1.putIn(new Binding((VariableNode) var, fido)); @@ -219,22 +207,22 @@ public void setUp() { } /*@Test - public void testProcessNodes() { + public void testAntsandConsq() { assertEquals(1, numerical.getConsequents().size()); assertEquals(3, numerical.getAntecedents().size()); assertEquals(1, numerical.getAntsWithVars().size()); assertEquals(2, numerical.getAntsWithoutVars().size()); }*/ - /*@Test + @Test public void testApplyRuleHandler() { numerical.applyRuleHandler(report, prop1); assertEquals(1, numerical.getReplies().size()); assertEquals(numerical.getReplies().get(0), report); - }*/ + } - /*@Test + @Test public void testApplyRuleHandler2() { numerical.setI(2); numerical.clear(); @@ -242,7 +230,7 @@ public void testApplyRuleHandler2() { numerical.applyRuleHandler(report, prop1); assertEquals(0, numerical.getReplies().size()); - }*/ + } @Test public void testApplyRuleHandler3() { @@ -252,12 +240,13 @@ public void testApplyRuleHandler3() { numerical.applyRuleHandler(report, prop1); assertEquals(0, numerical.getReplies().size()); - assertNotNull("NumericalEntailment: Null RuisHandler", numerical.getRuisHandler()); + + numerical.applyRuleHandler(report1, prop3); + assertNotNull("NumericalEntailment: Null RuisHandler", + numerical.getRuisHandler()); assertTrue("NumericalEntailment: addRuiHandler doesn't create an SIndex as a RuisHandler", numerical.getRuisHandler() instanceof SIndex); assertEquals(SIndex.SINGLETON, ((SIndex) (numerical.getRuisHandler())).getRuiHandlerType()); - - numerical.applyRuleHandler(report1, prop3); assertEquals(1, numerical.getReplies().size()); } @@ -278,7 +267,6 @@ public void tearDown() { fido = null; var = null; dog = null; - rui = null; report = null; } } diff --git a/tests/PTreeTests.java b/tests/PTreeTests.java index c6011cbb..2feaa786 100644 --- a/tests/PTreeTests.java +++ b/tests/PTreeTests.java @@ -1,6 +1,3 @@ -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - import java.util.ArrayList; import java.util.LinkedList; @@ -64,7 +61,6 @@ public void setUp() { Node fido, dog; Node man, woman, married; Node john, mary; - //Node prop1, prop2, prop3; ArrayList wires = new ArrayList(); LinkedList rels = new LinkedList(); Relation memberRel = Network.defineRelation("Member", "NodeSet"); @@ -87,11 +83,12 @@ public void setUp() { married = Network.buildBaseNode("Married", new Semantic("Class")); john = Network.buildBaseNode("John", new Semantic("Class")); mary = Network.buildBaseNode("Mary", new Semantic("Class")); + wire1 = new Wire(memberRel, var1); wire2 = new Wire(memberRel, var2); wire3 = new Wire(classRel, man); - wire4 = new Wire(memberRel, woman); - wire5 = new Wire(memberRel, married); + wire4 = new Wire(classRel, woman); + wire5 = new Wire(classRel, married); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { assertNotNull(e.getMessage(), e); @@ -169,7 +166,6 @@ public void setUp() { FlagNodeSet fns = new FlagNodeSet(); PropositionSet supports = new PropositionSet(); FlagNode fn; - try { supports.add(prop1.getId()); } catch (DuplicatePropositionException | NotAPropositionNodeException From 1be9fc556a751ec35ffbd34d3e052dc46c8944a1 Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Wed, 22 May 2019 16:53:30 +0200 Subject: [PATCH 20/22] Some modifications to the tests --- src/sneps/network/RuleNode.java | 23 ++- .../classes/setClasses/RuleUseInfoSet.java | 7 - src/sneps/snip/classes/PTree.java | 24 ++- src/sneps/snip/classes/RuisHandler.java | 2 - src/sneps/snip/classes/SIndex.java | 4 - src/sneps/snip/rules/AndEntailment.java | 24 +-- src/sneps/snip/rules/AndOrEntailment.java | 9 +- src/sneps/snip/rules/NumericalEntailment.java | 20 +-- src/sneps/snip/rules/OrEntailment.java | 10 +- src/sneps/snip/rules/ThreshEntailment.java | 4 +- tests/AndOrTest.java | 142 ++++-------------- tests/NumericalEntailmentTests.java | 62 ++++---- tests/OrTests.java | 6 +- tests/PTreeTests.java | 42 +++--- tests/ThreshTest.java | 127 ++++------------ 15 files changed, 170 insertions(+), 336 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index 1280daaa..c9570e99 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -193,15 +193,10 @@ public ArrayList applyRuleHandler(Report report, Node signature) { report.getInferenceType()); } - // This is the first report received by this RuleNode, so - // a RuisHandler is created - if(ruisHandler == null) - ruisHandler = addRuiHandler(); - if(antNodesWithoutVars.contains(signature)) { System.out.println("CONSTANT"); addConstantRui(rui); - if (ruisHandler.isEmpty()) { + if (ruisHandler == null) { response = applyRuleOnRui(constantRUI); if(response != null) responseList.add(response); @@ -216,7 +211,9 @@ public ArrayList applyRuleHandler(Report report, Node signature) { } } else { - System.out.println("VAR"); + if(ruisHandler == null) + ruisHandler = addRuiHandler(); + // The RUI created for the given report is inserted to the RuisHandler RuleUseInfoSet res = ruisHandler.insertRUI(rui); @@ -455,11 +452,13 @@ protected Set getOutgoingChannelsForReport(Report r) { Set outgoingChannels = getOutgoingRuleConsequentChannels(); Set replyChannels = new HashSet(); for(Node n : consequents) { - for(Channel c : outgoingChannels) { - if(c.getRequester().getId() == n.getId() && - r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { - replyChannels.add(c); - break; + if(outgoingChannels != null) { + for(Channel c : outgoingChannels) { + if(c.getRequester().getId() == n.getId() && + r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { + replyChannels.add(c); + break; + } } } diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index f8e22f45..9af5c8bb 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -6,13 +6,6 @@ import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleUseInfo; -/** - * @className RuleUseInfoSet.java - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 - */ public class RuleUseInfoSet extends RuisHandler implements Iterable { private HashSet ruis; diff --git a/src/sneps/snip/classes/PTree.java b/src/sneps/snip/classes/PTree.java index 602714e8..c4d13948 100644 --- a/src/sneps/snip/classes/PTree.java +++ b/src/sneps/snip/classes/PTree.java @@ -311,17 +311,27 @@ public RuleUseInfoSet getAllRootRuis() { @Override public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui) { - return null; - } - - @Override - public boolean isEmpty() { - return false; + RuleUseInfoSet res = new RuleUseInfoSet(); + RuleUseInfoSet ruis = getAllRootRuis(); + if (ruis != null) { + RuleUseInfo combined; + for (RuleUseInfo r : ruis) { + combined = r.combine(rui); + if (combined != null) + res.add(combined); + } + } + + return res; } @Override public void clear() { - + patternVariables = new Hashtable(); + variablePatterns = new Hashtable>(); + notProccessed = new VarNodeSet(); + subTrees = new HashSet(); + subTreesMap = new Hashtable(); } /** diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index a1f73151..7a929108 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -22,7 +22,5 @@ public abstract class RuisHandler { abstract public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui); - abstract public boolean isEmpty(); - abstract public void clear(); } diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index df1ae758..ed26d1a2 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -116,10 +116,6 @@ public byte getRuiHandlerType() { return ruiHandlerType; } - public boolean isEmpty() { - return map.isEmpty(); - } - public void clear() { map.clear(); } diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index 66fba863..0c211254 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -73,17 +73,11 @@ public ArrayList applyRuleHandler(Report report, Node signature) { responseList.add(response); } - RuleUseInfoSet ruis = ((PTree) ruisHandler).getAllRootRuis(); - if (ruis != null) { - RuleUseInfo combined; - for (RuleUseInfo r : ruis) { - combined = r.combine(constantRUI); - if (combined != null) { - response = applyRuleOnRui(combined); - if(response != null) - responseList.add(response); - } - } + RuleUseInfoSet ruis = ruisHandler.combineConstantRUI(constantRUI); + for (RuleUseInfo tRui : ruis) { + response = applyRuleOnRui(tRui); + if(response != null) + responseList.add(response); } } else { @@ -131,14 +125,10 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { PropositionSet replySupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { - //System.out.println("HERE"); - //System.out.println(fn.getSupport()); replySupport.union(fn.getSupport()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { e.printStackTrace(); } - //System.out.println(replySupport); - // TODO // Add rule node to replySupport @@ -148,8 +138,8 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { RuleResponse r = new RuleResponse(); r.setReport(reply); - //Set forwardChannels = getOutgoingChannelsForReport(reply); - //r.addAllChannels(forwardChannels); + Set forwardChannels = getOutgoingChannelsForReport(reply); + r.addAllChannels(forwardChannels); return r; } diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index 6eb0529c..b9f40912 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -5,7 +5,6 @@ import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.RuleNode; -import sneps.network.cables.DownCableSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; @@ -109,19 +108,17 @@ else if(this.getTerm() instanceof Open) { // If node is Closed, need to check if it was asserted thru forward inference, // by calling method isAssertedthruForwardInference(), to get type of replyReport + consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); - //System.out.println(rui.getFlagNodeSet().getAllNodes()); - //System.out.println(consequents); Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, rui.getType()); - //System.out.println(reply); reportsToBeSent.add(reply); RuleResponse r = new RuleResponse(); r.setReport(reply); - //Set forwardChannels = getOutgoingChannelsForReport(reply); - //r.addAllChannels(forwardChannels); + Set forwardChannels = getOutgoingChannelsForReport(reply); + r.addAllChannels(forwardChannels); return r; } diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index f0b1b927..f9900846 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -1,7 +1,7 @@ package sneps.snip.rules; import java.util.ArrayList; -import java.util.HashSet; + import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -13,27 +13,21 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; import sneps.network.classes.term.Molecular; -import sneps.snebr.Controller; import sneps.snip.Report; import sneps.snip.channels.Channel; -import sneps.snip.channels.ChannelTypes; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; -import sneps.snip.matching.LinearSubstitutions; /** * @className NumericalEntailment.java * * @ClassDescription The Numerical-Entailment is a rule node that asserts the conjunction of at least i nodes in its antecedent position to imply the conjunction of all the nodes in its consequent position. * When the rule node receives a request from a node in its consequent position, it sends requests to all its nodes in antecedent positions. - * Generally, when a rule node has enough reports, it creates a reply report and broadcasts it to all requesting consequent nodes. - * In the case of the Numerical-Entailment rule, the reply report is created and sent when a minimum of i antecedent nodes sent their respective positive reports. - * - * @author Amgad Ashraf - * @version 3.00 31/5/2018 + * Generally, when a rule node has enough reports, it creates a reply report and broadcasts it to all consequent nodes. + * In the case of the Numerical-Entailment rule, the reply report is created and sent when a minimum of i antecedent nodes send their respective positive reports. */ public class NumericalEntailment extends RuleNode { private static final long serialVersionUID = 3546852401118194013L; @@ -49,7 +43,7 @@ public NumericalEntailment(Molecular syn) { // Initializing the antecedents antecedents = getDownAntNodeSet(); - //processNodes(antecedents); + processNodes(antecedents); // Initializing the consequents consequents = getDownConsqNodeSet(); @@ -63,7 +57,7 @@ public NumericalEntailment(Molecular syn) { */ @Override public ArrayList applyRuleHandler(Report report, Node signature) { - processNodes(antecedents); + //processNodes(antecedents); System.out.println("---------------"); if(report.isNegative()) @@ -153,8 +147,8 @@ protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { RuleResponse r = new RuleResponse(); r.setReport(reply); - //Set forwardChannels = getOutgoingChannelsForReport(reply); - //r.addAllChannels(forwardChannels); + Set forwardChannels = getOutgoingChannelsForReport(reply); + r.addAllChannels(forwardChannels); return r; } diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 9a7e1f02..2c4ea272 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -1,7 +1,7 @@ package sneps.snip.rules; import java.util.ArrayList; - +import java.util.Set; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; @@ -11,6 +11,7 @@ import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.term.Molecular; import sneps.snip.Report; +import sneps.snip.channels.Channel; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; import sneps.snip.classes.RuleUseInfo; @@ -25,6 +26,9 @@ public OrEntailment(Molecular syn) { } public ArrayList applyRuleHandler(Report report, Node signature) { + if (report.isNegative()) + return null; + ArrayList responseList = new ArrayList(); RuleResponse response = new RuleResponse(); PropositionSet replySupport = new PropositionSet(); @@ -39,8 +43,8 @@ public ArrayList applyRuleHandler(Report report, Node signature) { true, report.getInferenceType()); reportsToBeSent.add(reply); response.setReport(reply); - //Set outgoingChannels = getOutgoingChannelsForReport(reply); - //response.addAllChannels(outgoingChannels); + Set outgoingChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(outgoingChannels); responseList.add(response); return responseList; } diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index 6f8676cb..d4a2f676 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -64,8 +64,8 @@ else if (rui.getPosCount() != min - 1 RuleResponse r = new RuleResponse(); r.setReport(reply); - //Set forwardChannels = getOutgoingChannelsForReport(reply); - //r.addAllChannels(forwardChannels); + Set forwardChannels = getOutgoingChannelsForReport(reply); + r.addAllChannels(forwardChannels); return r; } diff --git a/tests/AndOrTest.java b/tests/AndOrTest.java index 03c3e421..24dffa98 100644 --- a/tests/AndOrTest.java +++ b/tests/AndOrTest.java @@ -21,7 +21,6 @@ import sneps.network.classes.Relation; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; -import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.NodeSet; @@ -40,8 +39,8 @@ public class AndOrTest { private static Context context; private static String contextName = "TempContext"; private static AndOrEntailment andor; - private static Node john, fido, var, dog, barks, animal, veg, mineral, human; - private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8; + private static Node john, fido, var, animal, veg, mineral, human; + private static Node prop5, prop6, prop7, prop8; private static Report report, report1, report2, report3; @BeforeClass @@ -60,40 +59,23 @@ public static void setUp() throws Exception { NodeSet nodeSet = new NodeSet(); Relation memberRel = Network.defineRelation("Member", "NodeSet"); Relation classRel = Network.defineRelation("Class", "NodeSet"); - Relation doesRel = Network.defineRelation("Does", "NodeSet"); - Relation antsRel = Network.defineRelation("Xant", "Xant"); - Relation consRel = Network.defineRelation("Xconsq", "Xconsq"); Relation argsRel = Network.defineRelation("arg", "arg"); rels.add(memberRel); rels.add(classRel); CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); - rels.clear(); rels.add(classRel); rels.add(doesRel); - CaseFrame caseFrameCD = Network.defineCaseFrame("ClassDoes", rels); - rels.clear(); rels.add(memberRel); rels.add(doesRel); - CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); - rels.clear(); rels.add(antsRel); rels.add(consRel); - CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); rels.clear(); rels.add(argsRel); CaseFrame caseFrameArgs = Network.defineCaseFrame("Args", rels); - Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; - Wire wire5 = null, wire6 = null, wire7 = null, wire8 = null; + Wire wire4 = null, wire5 = null, wire6 = null, wire7 = null, wire8 = null; rels.clear(); - try { var = Network.buildVariableNode("X"); - john = Network.buildBaseNode("John", new Semantic("Base")); fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Base")); - barks = Network.buildBaseNode("Barks", new Semantic("Base")); - wire1 = new Wire(memberRel, fido); - wire2 = new Wire(classRel, dog); - wire3 = new Wire(doesRel, barks); - wire4 = new Wire(memberRel, var); - + john = Network.buildBaseNode("John", new Semantic("Base")); animal = Network.buildBaseNode("Animal", new Semantic("Base")); veg = Network.buildBaseNode("Vegetable", new Semantic("Base")); mineral = Network.buildBaseNode("Mineral", new Semantic("Base")); human = Network.buildBaseNode("Human", new Semantic("Base")); + wire4 = new Wire(memberRel, var); wire5 = new Wire(classRel, animal); wire6 = new Wire(classRel, veg); wire7 = new Wire(classRel, mineral); @@ -104,20 +86,7 @@ public static void setUp() throws Exception { var = new VariableNode(new Variable("X")); } - try { - wires.clear(); wires.add(wire1); wires.add(wire2); - prop1 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire2); wires.add(wire3); - prop2 = Network.buildMolecularNode(wires, caseFrameCD); - - wires.clear(); wires.add(wire4); wires.add(wire2); - prop3 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire1); wires.add(wire3); - prop4 = Network.buildMolecularNode(wires, caseFrameMD); - wires.clear(); wires.add(wire4); wires.add(wire5); prop5 = Network.buildMolecularNode(wires, caseFrameArgs); @@ -136,108 +105,59 @@ public static void setUp() throws Exception { LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + NodeSet nodeSet3 = new NodeSet(); + NodeSet nodeSet4 = new NodeSet(); + NodeSet nodeSet5 = new NodeSet(); + NodeSet nodeSet6 = new NodeSet(); + NodeSet nodeSet7 = new NodeSet(); + NodeSet nodeSet8 = new NodeSet(); DownCable dc1; DownCableSet dcs; - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Closed("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Closed("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); + nodeSet1.addNode(var); dc1 = new DownCable(memberRel, nodeSet1); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Closed("Prop4", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(animal); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet2.addNode(animal); + dc1 = new DownCable(classRel, nodeSet2); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop5 = new Node(new Open("Prop5", dcs)); - ((Open) (prop5.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop5.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet3.addNode(var); + dc1 = new DownCable(memberRel, nodeSet3); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(veg); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet4.addNode(veg); + dc1 = new DownCable(classRel, nodeSet4); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop6 = new Node(new Open("Prop6", dcs)); - ((Open) (prop6.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop6.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet5.addNode(var); + dc1 = new DownCable(memberRel, nodeSet5); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(mineral); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet6.addNode(mineral); + dc1 = new DownCable(classRel, nodeSet6); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop7 = new Node(new Open("Prop7", dcs)); - ((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet7.addNode(var); + dc1 = new DownCable(memberRel, nodeSet7); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(human); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet8.addNode(human); + dc1 = new DownCable(classRel, nodeSet8); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop8 = new Node(new Open("Prop8", dcs)); - ((Open) (prop8.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop8.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //---------------------------------------------------------------------------------// - - /*nodeSet.addNode(prop1); - dc.add(new DownCable(argsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop2); - dc.add(new DownCable(argsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop3); - dc.add(new DownCable(argsRel, nodeSet)); - - nodeSet.clear(); - nodeSet.addNode(prop4); - dc.add(new DownCable(argsRel, nodeSet));*/ nodeSet.addNode(prop5); nodeSet.addNode(prop6); diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index 5ff6774c..83fd780b 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -56,6 +56,7 @@ public void setUp() { LinkedList dc = new LinkedList(); LinkedList rels = new LinkedList(); NodeSet nodeSet = new NodeSet(); + NodeSet nodeSett = new NodeSet(); Relation memberRel = Network.defineRelation("Member", "NodeSet"); Relation classRel = Network.defineRelation("Class", "NodeSet"); Relation doesRel = Network.defineRelation("Does", "NodeSet"); @@ -111,44 +112,51 @@ public void setUp() { LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + NodeSet nodeSet3 = new NodeSet(); + NodeSet nodeSet4 = new NodeSet(); + NodeSet nodeSet5 = new NodeSet(); + NodeSet nodeSet6 = new NodeSet(); + NodeSet nodeSet7 = new NodeSet(); + NodeSet nodeSet8 = new NodeSet(); DownCable dc1; DownCableSet dcs; nodeSet1.addNode(fido); dc1 = new DownCable(memberRel, nodeSet1); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet2.addNode(dog); + dc1 = new DownCable(classRel, nodeSet2); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop1 = new PropositionNode(new Closed("Prop1", dcs)); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet3.addNode(dog); + dc1 = new DownCable(classRel, nodeSet3); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); + nodeSet4.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet4); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameCD); prop2 = new PropositionNode(new Closed("Prop2", dcs)); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet5.addNode(var); + dc1 = new DownCable(memberRel, nodeSet5); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet6.addNode(dog); + dc1 = new DownCable(classRel, nodeSet6); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop3 = new PropositionNode(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet7.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet7); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); + nodeSet8.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet8); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMD); prop4 = new PropositionNode(new Closed("Prop4", dcs)); @@ -162,27 +170,15 @@ public void setUp() { nodeSet.addNode(prop3); dc.add(new DownCable(antsRel, nodeSet)); - nodeSet.clear(); - nodeSet.addNode(prop4); - dc.add(new DownCable(consRel, nodeSet)); + nodeSett.addNode(prop4); + dc.add(new DownCable(consRel, nodeSett)); DownCableSet dcss = new DownCableSet(dc, caseFrameAC); - //System.out.println(dcss.getDownCable("iant")); - //System.out.println(dcss.getDownCable("iconsq")); - NodeSet a = new NodeSet(); - a.addNode(prop1); - a.addNode(prop2); - a.addNode(prop3); - - NodeSet c = new NodeSet(); - c.addNode(prop4); //---------------------------- NUMERICAL -----------------------------------// numerical = new NumericalEntailment(new Open("Open", dcss)); numerical.setI(1); - numerical.setAntecedents(a); - numerical.setConsequents(c); sub = new LinearSubstitutions(); support = new PropositionSet(); @@ -206,13 +202,13 @@ public void setUp() { } - /*@Test + @Test public void testAntsandConsq() { assertEquals(1, numerical.getConsequents().size()); assertEquals(3, numerical.getAntecedents().size()); assertEquals(1, numerical.getAntsWithVars().size()); assertEquals(2, numerical.getAntsWithoutVars().size()); - }*/ + } @Test public void testApplyRuleHandler() { @@ -250,7 +246,7 @@ public void testApplyRuleHandler3() { assertEquals(1, numerical.getReplies().size()); } - /*@Test + @Test public void testGetDownNodeSet() { NodeSet downAntNodeSet = numerical.getDownAntNodeSet(); assertNotNull("NumericalEntailment: getDownAntNodeSet retuns null", @@ -259,7 +255,7 @@ public void testGetDownNodeSet() { downAntNodeSet.isEmpty()); assertEquals(1, numerical.getDownConsqNodeSet().size()); assertEquals(3, numerical.getDownAntNodeSet().size()); - }*/ + } public void tearDown() { Network.clearNetwork(); diff --git a/tests/OrTests.java b/tests/OrTests.java index 5cb946f8..a16774ea 100644 --- a/tests/OrTests.java +++ b/tests/OrTests.java @@ -36,7 +36,7 @@ public class OrTests { private static OrEntailment or; private static Node fido, var, dog, barks; private static Node prop1, prop2, prop3, prop4; - private static Report report, report1; + private static Report report, report1, report2; @BeforeClass @@ -175,6 +175,7 @@ public static void SetUp() { report = new Report(sub, support, true, InferenceTypes.BACKWARD); report1 = new Report(s, support, true, InferenceTypes.BACKWARD); + report2 = new Report(s, support, false, InferenceTypes.BACKWARD); } @Test @@ -184,6 +185,9 @@ public void testApplyRuleHandler() { or.applyRuleHandler(report1, prop1); assertEquals(2, or.getReplies().size()); + + or.applyRuleHandler(report2, prop1); + assertEquals(2, or.getReplies().size()); } public void tearDown() { diff --git a/tests/PTreeTests.java b/tests/PTreeTests.java index 2feaa786..ee24ceee 100644 --- a/tests/PTreeTests.java +++ b/tests/PTreeTests.java @@ -58,7 +58,6 @@ public void setUp() { ants = new NodeSet(); VariableNode var1, var2; - Node fido, dog; Node man, woman, married; Node john, mary; ArrayList wires = new ArrayList(); @@ -74,10 +73,6 @@ public void setUp() { var1 = Network.buildVariableNode("X"); var2 = Network.buildVariableNode("Y"); - //fido = Network.buildBaseNode("Fido", new Semantic("Member")); - //dog = Network.buildBaseNode("Dog", new Semantic("Class")); - //ants.addNode(var); //ants.addNode(fido); //ants.addNode(dog); - man = Network.buildBaseNode("Man", new Semantic("Class")); woman = Network.buildBaseNode("Woman", new Semantic("Class")); married = Network.buildBaseNode("Married", new Semantic("Class")); @@ -99,10 +94,6 @@ public void setUp() { married = new Node(new Base("Married")); john = new Node(new Base("John")); mary = new Node(new Base("Mary")); - - //fido = new Node(new Base("Fido")); - //dog = new Node(new Base("Dog")); - //ants.addNode(fido); ants.addNode(var); ants.addNode(dog); } try { @@ -121,40 +112,45 @@ public void setUp() { LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + NodeSet nodeSet3 = new NodeSet(); + NodeSet nodeSet4 = new NodeSet(); + NodeSet nodeSet5 = new NodeSet(); + NodeSet nodeSet6 = new NodeSet(); DownCable dc1; DownCableSet dcs; nodeSet1.addNode(var1); dc1 = new DownCable(memberRel, nodeSet1); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(man); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet2.addNode(man); + dc1 = new DownCable(classRel, nodeSet2); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop1 = new PropositionNode(new Open("Prop1", dcs)); - ((Open) (prop1.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); + //((Open) (prop1.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); dcList.clear(); //------------------------------------------------------------// - nodeSet1.addNode(var2); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet3.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet3); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(woman); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet4.addNode(woman); + dc1 = new DownCable(classRel, nodeSet4); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop2 = new PropositionNode(new Open("Prop2", dcs)); - ((Open) (prop2.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); + //((Open) (prop2.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var1); nodeSet1.addNode(var2); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet5.addNode(var1); nodeSet5.addNode(var2); + dc1 = new DownCable(memberRel, nodeSet5); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(married); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet6.addNode(married); + dc1 = new DownCable(classRel, nodeSet6); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop3 = new PropositionNode(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); + //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var1); + //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var2); dcList.clear(); //------------------------------------------------------------// diff --git a/tests/ThreshTest.java b/tests/ThreshTest.java index 310fbbda..175f3f91 100644 --- a/tests/ThreshTest.java +++ b/tests/ThreshTest.java @@ -21,21 +21,17 @@ import sneps.network.classes.Relation; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; -import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; -import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.snebr.Context; import sneps.snebr.Controller; import sneps.snip.InferenceTypes; import sneps.snip.Report; -import sneps.snip.classes.FlagNode; import sneps.snip.classes.SIndex; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; -import sneps.snip.rules.AndOrEntailment; import sneps.snip.rules.ThreshEntailment; public class ThreshTest { @@ -43,8 +39,8 @@ public class ThreshTest { private static Context context; private static String contextName = "TempContext"; private static ThreshEntailment thresh; - private static Node fido, var, dog, barks, animal, veg, mineral, human; - private static Node prop1, prop2, prop3, prop4, prop5, prop6, prop7, prop8; + private static Node fido, var, animal, veg, mineral, human; + private static Node prop5, prop6, prop7, prop8; private static Report report, report1, report2; @BeforeClass @@ -63,33 +59,20 @@ public static void setUp() throws Exception { NodeSet nodeSet = new NodeSet(); Relation memberRel = Network.defineRelation("Member", "NodeSet"); Relation classRel = Network.defineRelation("Class", "NodeSet"); - Relation doesRel = Network.defineRelation("Does", "NodeSet"); - Relation antsRel = Network.defineRelation("Xant", "Xant"); - Relation consRel = Network.defineRelation("Xconsq", "Xconsq"); Relation argsRel = Network.defineRelation("arg", "arg"); rels.add(memberRel); rels.add(classRel); CaseFrame caseFrameMC = Network.defineCaseFrame("MemberClass", rels); - rels.clear(); rels.add(classRel); rels.add(doesRel); - CaseFrame caseFrameCD = Network.defineCaseFrame("ClassDoes", rels); - rels.clear(); rels.add(memberRel); rels.add(doesRel); - CaseFrame caseFrameMD = Network.defineCaseFrame("MemberDoes", rels); - rels.clear(); rels.add(antsRel); rels.add(consRel); - CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); rels.clear(); rels.add(argsRel); CaseFrame caseFrameArgs = Network.defineCaseFrame("Args", rels); - Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; - Wire wire5 = null, wire6 = null, wire7 = null, wire8 = null; + Wire wire1 = null, wire4 = null, wire5 = null, wire6 = null, wire7 = null; + Wire wire8 = null; rels.clear(); try { var = Network.buildVariableNode("X"); fido = Network.buildBaseNode("Fido", new Semantic("Base")); - dog = Network.buildBaseNode("Dog", new Semantic("Base")); - barks = Network.buildBaseNode("Barks", new Semantic("Base")); wire1 = new Wire(memberRel, fido); - wire2 = new Wire(classRel, dog); - wire3 = new Wire(doesRel, barks); wire4 = new Wire(memberRel, var); animal = Network.buildBaseNode("Animal", new Semantic("Base")); @@ -108,18 +91,6 @@ public static void setUp() throws Exception { try { - wires.clear(); wires.add(wire1); wires.add(wire2); - prop1 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire2); wires.add(wire3); - prop2 = Network.buildMolecularNode(wires, caseFrameCD); - - wires.clear(); wires.add(wire4); wires.add(wire2); - prop3 = Network.buildMolecularNode(wires, caseFrameMC); - - wires.clear(); wires.add(wire1); wires.add(wire3); - prop4 = Network.buildMolecularNode(wires, caseFrameMD); - wires.clear(); wires.add(wire4); wires.add(wire5); prop5 = Network.buildMolecularNode(wires, caseFrameArgs); @@ -138,91 +109,57 @@ public static void setUp() throws Exception { LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + NodeSet nodeSet3 = new NodeSet(); + NodeSet nodeSet4 = new NodeSet(); + NodeSet nodeSet5 = new NodeSet(); + NodeSet nodeSet6 = new NodeSet(); + NodeSet nodeSet7 = new NodeSet(); + NodeSet nodeSet8 = new NodeSet(); DownCable dc1; DownCableSet dcs; - nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop1 = new Node(new Closed("Prop1", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameCD); - prop2 = new Node(new Closed("Prop2", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMC); - prop3 = new Node(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); + nodeSet1.addNode(var); dc1 = new DownCable(memberRel, nodeSet1); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); - dcList.add(dc1); - dcs = new DownCableSet(dcList, caseFrameMD); - prop4 = new Node(new Closed("Prop4", dcs)); - dcList.clear(); - //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); - dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(animal); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet2.addNode(animal); + dc1 = new DownCable(classRel, nodeSet2); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop5 = new Node(new Open("Prop5", dcs)); - ((Open) (prop5.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop5.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet3.addNode(var); + dc1 = new DownCable(memberRel, nodeSet3); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(veg); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet4.addNode(veg); + dc1 = new DownCable(classRel, nodeSet4); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop6 = new Node(new Open("Prop6", dcs)); - ((Open) (prop6.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop6.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet5.addNode(var); + dc1 = new DownCable(memberRel, nodeSet5); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(mineral); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet6.addNode(mineral); + dc1 = new DownCable(classRel, nodeSet6); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop7 = new Node(new Open("Prop7", dcs)); - ((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop7.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet7.addNode(var); + dc1 = new DownCable(memberRel, nodeSet7); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(human); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet8.addNode(human); + dc1 = new DownCable(classRel, nodeSet8); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop8 = new Node(new Open("Prop8", dcs)); - ((Open) (prop8.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop8.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //---------------------------------------------------------------------------------// @@ -273,7 +210,7 @@ public void testProcessNodes() { * First inference rule allowed by thresh (n-j args are false, and i-1 args are * true, then the other j-i+1 have to be false). */ - /*@Test + @Test public void test() { thresh.applyRuleHandler(report, prop5); assertNotNull("Null RuisHandler", thresh.getRuisHandler()); @@ -288,7 +225,7 @@ public void test() { assertEquals(2, thresh.getConsequents().size()); assertEquals(prop7, thresh.getConsequents().getNode(0)); assertEquals(prop8, thresh.getConsequents().getNode(1)); - }*/ + } /** * Second inference rule allowed by thresh (i args are true, and n-j-1 args are From 894fd9cc85f8dfd12d3ae09306e1ef9d20e2c39c Mon Sep 17 00:00:00 2001 From: Sarah Ayman Date: Fri, 31 May 2019 02:11:48 +0200 Subject: [PATCH 21/22] Combine a reply report with knownInstances of a RuleNode --- src/sneps/network/RuleNode.java | 80 +++++--------- .../classes/setClasses/FlagNodeSet.java | 7 -- .../network/classes/setClasses/ReportSet.java | 7 -- .../classes/setClasses/RuleUseInfoSet.java | 4 +- src/sneps/snip/Report.java | 19 ++-- src/sneps/snip/ReportInstances.java | 4 + src/sneps/snip/Runner.java | 5 +- src/sneps/snip/classes/PTree.java | 2 +- src/sneps/snip/classes/RuisHandler.java | 12 +- src/sneps/snip/classes/RuleUseInfo.java | 8 +- src/sneps/snip/classes/SIndex.java | 7 +- src/sneps/snip/rules/AndEntailment.java | 100 +++++++++++++---- src/sneps/snip/rules/AndOrEntailment.java | 97 ++++++++-------- src/sneps/snip/rules/DoIfNode.java | 3 +- src/sneps/snip/rules/NumericalEntailment.java | 104 ++++++++++++++---- src/sneps/snip/rules/OrEntailment.java | 86 ++++++++++++--- src/sneps/snip/rules/ThreshEntailment.java | 93 +++++++++++++--- src/sneps/snip/rules/WhenDoNode.java | 3 +- tests/AndEntailmentTests.java | 5 +- tests/AndOrTest.java | 11 +- tests/NumericalEntailmentTests.java | 84 +++++++++++--- tests/OrTests.java | 47 ++++---- tests/SIndexTest.java | 36 +----- tests/ThreshTest.java | 5 +- 24 files changed, 532 insertions(+), 297 deletions(-) diff --git a/src/sneps/network/RuleNode.java b/src/sneps/network/RuleNode.java index c9570e99..2771a52f 100644 --- a/src/sneps/network/RuleNode.java +++ b/src/sneps/network/RuleNode.java @@ -6,7 +6,6 @@ import java.util.HashSet; import java.util.Set; -import sneps.network.cables.DownCableSet; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; @@ -65,8 +64,8 @@ public abstract class RuleNode extends PropositionNode implements Serializable{ protected RuisHandler ruisHandler; /** - * A Hashtable used to map each context to a single RuleUseInfo - * that contains all the constant instances that do not dominate variables. + * A single RUI that contains all the constant instances found that do not + * dominate variables for this RuleNode. */ protected RuleUseInfo constantRUI; @@ -165,19 +164,19 @@ public void processNodes(NodeSet antNodes) { /** * The main method that does all the inference process in the RuleNode. Creates * a RUI for the given report, and inserts it into the appropriate RuisHandler - * associated with the report's context, for this RuleNode. It instantiates a - * RuisHandler if this is the first report received in a particular context. It - * then applies the inference rules of this RuleNode on the current stored RUIs. + * for this RuleNode. It instantiates a RuisHandler if this is the first report + * from a pattern antecedent received. It then applies the inference rules of this + * RuleNode on the current stored RUIs. * * @param report * @param signature * The instance that is being reported by the report. - * @return + * @return */ public ArrayList applyRuleHandler(Report report, Node signature) { - System.out.println("---------------------"); + //System.out.println("---------------------"); ArrayList responseList = new ArrayList(); - RuleResponse response = new RuleResponse(); + ArrayList response = new ArrayList(); RuleUseInfo rui; PropositionSet propSet = report.getSupport(); @@ -193,24 +192,27 @@ public ArrayList applyRuleHandler(Report report, Node signature) { report.getInferenceType()); } + //System.out.println(rui); + if(antNodesWithoutVars.contains(signature)) { - System.out.println("CONSTANT"); addConstantRui(rui); if (ruisHandler == null) { response = applyRuleOnRui(constantRUI); if(response != null) - responseList.add(response); + responseList.addAll(response); } else { RuleUseInfoSet combined = ruisHandler.combineConstantRUI(constantRUI); for (RuleUseInfo tRui : combined) { response = applyRuleOnRui(tRui); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } else { + // This is the first report received from a pattern antecedent, so a + // ruisHandler is created if(ruisHandler == null) ruisHandler = addRuiHandler(); @@ -224,7 +226,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if(combined != null) { response = applyRuleOnRui(combined); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } @@ -232,7 +234,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { for (RuleUseInfo tRui : res) { response = applyRuleOnRui(tRui); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } @@ -243,7 +245,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { return responseList; } - abstract protected RuleResponse applyRuleOnRui(RuleUseInfo tRui); + abstract protected ArrayList applyRuleOnRui(RuleUseInfo tRui); /** * @@ -291,7 +293,7 @@ public void addAntecedent(Node ant) { * @param nodes * @return VarNodeSet */ - public static Set getSharedVarsNodes(NodeSet nodes) { + public Set getSharedVarsNodes(NodeSet nodes) { Set res = new HashSet(); if (nodes.isEmpty()) @@ -353,21 +355,10 @@ public NodeSet getUpNodeSet(String name) { return this.getUpCableSet().getUpCable(name).getNodeSet(); } - /** - * Returns the RuisHandler associated with the given context. - * - * @param cntxt - * @return RuisHandler - */ - /*public RuisHandler getContextRuiHandler(String cntxt) { - return contextRuisSet.getByContext(cntxt); - }*/ - /** * Creates an appropriate RuisHandler for this RuleNode, according to whether all, * some or none of the variables in the antecedents are shared. * - * @param contextName * @return RuisHandler */ public RuisHandler addRuiHandler() { @@ -389,33 +380,9 @@ public RuisHandler addRuiHandler() { return createRuisHandler(); } } - - /*/** - * Adds the given RuisHandler by the given context name to this contextRuisSet. - * - * @param cntxt - * Context name. - * @param cRuis - * RuisHandler to be added. - * @return RuisHandler - */ - /*public RuisHandler addContextRuiHandler(String cntxt, RuisHandler cRuis) { - return this.contextRuisSet.addHandlerSet(cntxt, cRuis); - }*/ protected abstract RuisHandler createRuisHandler(); - /** - * Returns a RUISet to be used in case all the antecedents do not share a common - * variable, and this RuleNode is not an AndEntailment. - * - * @param contextName - * @return RuleUseInfoSet - */ - protected RuleUseInfoSet createContextRuiHandlerNonShared() { - return new RuleUseInfoSet(false); - } - /** * Returns a byte that represents an appropriate SIndex type that is used in case * the antecedents share some but not all variables.

@@ -448,11 +415,22 @@ public RuleUseInfo getConstantRui() { return constantRUI; } + /** + * This method returns all the rule to consequent channels corresponding to a + * given report. The send method filters which reports should actually be sent. + * @param r + * Report + * @return + * Set + */ protected Set getOutgoingChannelsForReport(Report r) { + // getOutgoingRuleConsequentChannels() returns all the RuleToConsequent + // channels already established from before Set outgoingChannels = getOutgoingRuleConsequentChannels(); Set replyChannels = new HashSet(); for(Node n : consequents) { if(outgoingChannels != null) { + // Checking that the same channel has not already been established before for(Channel c : outgoingChannels) { if(c.getRequester().getId() == n.getId() && r.getSubstitutions().isSubSet(c.getFilter().getSubstitution())) { diff --git a/src/sneps/network/classes/setClasses/FlagNodeSet.java b/src/sneps/network/classes/setClasses/FlagNodeSet.java index 8117db29..e683cb21 100644 --- a/src/sneps/network/classes/setClasses/FlagNodeSet.java +++ b/src/sneps/network/classes/setClasses/FlagNodeSet.java @@ -5,13 +5,6 @@ import sneps.snip.classes.FlagNode; -/** - * @className FlagNodeSet.java - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 - */ public class FlagNodeSet implements Iterable { private HashSet flagNodes; diff --git a/src/sneps/network/classes/setClasses/ReportSet.java b/src/sneps/network/classes/setClasses/ReportSet.java index 8a910331..8f33f9e3 100644 --- a/src/sneps/network/classes/setClasses/ReportSet.java +++ b/src/sneps/network/classes/setClasses/ReportSet.java @@ -6,13 +6,6 @@ import sneps.snip.Report; -/** - * @className ReportSet.java - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 - */ public class ReportSet implements Iterable, Serializable { private static final long serialVersionUID = 508832573392879123L; private HashSet reports; diff --git a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java index 9af5c8bb..094377a7 100644 --- a/src/sneps/network/classes/setClasses/RuleUseInfoSet.java +++ b/src/sneps/network/classes/setClasses/RuleUseInfoSet.java @@ -6,11 +6,11 @@ import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleUseInfo; -public class RuleUseInfoSet extends RuisHandler implements Iterable { +public class RuleUseInfoSet implements RuisHandler, Iterable { private HashSet ruis; /** - * A boolean indicating whether this RuleUseInfoSet is treated as single + * A boolean indicating whether this RuleUseInfoSet is treated as a single * RuleUseInfo. */ private boolean singleton; diff --git a/src/sneps/snip/Report.java b/src/sneps/snip/Report.java index a186f6e8..b621d830 100644 --- a/src/sneps/snip/Report.java +++ b/src/sneps/snip/Report.java @@ -10,11 +10,6 @@ public class Report { private Substitutions substitution; - - /** - * Contains the id of the proposition node denoting the actual instance - * represented by this Report - */ private PropositionSet support; private boolean sign; private InferenceTypes inferenceType; @@ -39,14 +34,22 @@ public boolean anySupportAssertedInContext(Context reportContext) return false; } + /** + * This method first checks if the substitutions of this report and the given + * report are compatible. If they are, it returns a new report with the combined + * subs and supports of the two reports. + * @param r + * Report + * @return + */ public Report combine(Report r) { if(substitution.isCompatible(r.getSubstitutions())) { + Substitutions combinedSubs = substitution.union(r.getSubstitutions()); PropositionSet combinedSupport = new PropositionSet(); try { - combinedSupport = support.union(r.support); + combinedSupport = support.union(r.getSupport()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { - e1.printStackTrace(); } InferenceTypes resultingType; @@ -56,7 +59,7 @@ public Report combine(Report r) { else resultingType = InferenceTypes.BACKWARD; - return new Report(substitution.union(r.getSubstitutions()), + return new Report(combinedSubs, combinedSupport, sign, resultingType); } diff --git a/src/sneps/snip/ReportInstances.java b/src/sneps/snip/ReportInstances.java index 45aa726e..5f30ed36 100644 --- a/src/sneps/snip/ReportInstances.java +++ b/src/sneps/snip/ReportInstances.java @@ -33,5 +33,9 @@ public Iterator iterator() { allMergedReports.addAll(set); return allMergedReports.iterator(); } + + public boolean isEmpty() { + return instances.isEmpty(); + } } \ No newline at end of file diff --git a/src/sneps/snip/Runner.java b/src/sneps/snip/Runner.java index 69527e44..16f70eb8 100644 --- a/src/sneps/snip/Runner.java +++ b/src/sneps/snip/Runner.java @@ -89,6 +89,9 @@ public static void addNodeAssertionThroughFReport(Report report, PropositionNode * @return */ public static boolean isNodeAssertedThroughForwardInf(PropositionNode node) { - return forwardAssertedNodes.containsValue(node); + if(forwardAssertedNodes != null) + return forwardAssertedNodes.containsValue(node); + else + return false; } } \ No newline at end of file diff --git a/src/sneps/snip/classes/PTree.java b/src/sneps/snip/classes/PTree.java index c4d13948..0f5921ab 100644 --- a/src/sneps/snip/classes/PTree.java +++ b/src/sneps/snip/classes/PTree.java @@ -25,7 +25,7 @@ * @ClassDescription A PTree is a binary tree structure. Every AndEntailment rule node stores combinations of antecedent RUIs inside a PTree because of its optimized structure. * A PTree class follows an algorithm for construction and insertion, having leaf nodes representing RUIs of rule antecedents and root nodes representing combined RUIs of the rule itself. */ -public class PTree extends RuisHandler { +public class PTree implements RuisHandler { private Hashtable patternVariables;//PatternId, VariableNodes private Hashtable> variablePatterns;//VariableNode, PatternIds private VarNodeSet notProccessed; diff --git a/src/sneps/snip/classes/RuisHandler.java b/src/sneps/snip/classes/RuisHandler.java index 7a929108..04ee673d 100644 --- a/src/sneps/snip/classes/RuisHandler.java +++ b/src/sneps/snip/classes/RuisHandler.java @@ -5,10 +5,10 @@ /** * @className RuisHandler.java * - * @ClassDescription To deal with the large number of RUIs generated as more nodes are built in the network, RuisHandler classes are built to store and combine RUIs. - * A RuisHandler is stored in a rule Node and a single rule Node can store multiple instances of a single RuisHandler class. + * @ClassDescription To deal with the large number of RUIs generated as more nodes are built in the network, + * RuisHandler classes are built to store and combine RUIs. */ -public abstract class RuisHandler { +public interface RuisHandler { /** * Inserts the given RuleUseInfo into this RuisHandler and returns the @@ -18,9 +18,9 @@ public abstract class RuisHandler { * RuleUseInfo * @return RuleUseInfoSet */ - abstract public RuleUseInfoSet insertRUI(RuleUseInfo rui); + public RuleUseInfoSet insertRUI(RuleUseInfo rui); - abstract public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui); + public RuleUseInfoSet combineConstantRUI(RuleUseInfo rui); - abstract public void clear(); + public void clear(); } diff --git a/src/sneps/snip/classes/RuleUseInfo.java b/src/sneps/snip/classes/RuleUseInfo.java index 92f26fe3..79adbb50 100644 --- a/src/sneps/snip/classes/RuleUseInfo.java +++ b/src/sneps/snip/classes/RuleUseInfo.java @@ -5,13 +5,9 @@ import sneps.snip.matching.Substitutions; /** - * @className RuleUseInfo.java + * @ClassDescription The RuleUseInfo is a data structure used to save instances of + * antecedents of rule nodes. * - * @ClassDescription The RuleUseInfo is a data structure used to save instances of antecedents of rule nodes. - * - * @author Amgad Ashraf - * - * @version 3.00 31/5/2018 */ public class RuleUseInfo { private Substitutions sub; diff --git a/src/sneps/snip/classes/SIndex.java b/src/sneps/snip/classes/SIndex.java index ed26d1a2..5b968ef2 100644 --- a/src/sneps/snip/classes/SIndex.java +++ b/src/sneps/snip/classes/SIndex.java @@ -8,7 +8,7 @@ import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.RuleUseInfoSet; -public class SIndex extends RuisHandler { +public class SIndex implements RuisHandler { /** * A Hashtable used to map different substitutions to different RuisHandlers. @@ -47,8 +47,9 @@ public SIndex(byte ruiHandlerType, Set sharedVars) { /** * Inserts the RuleUseInfo in the map based on the bound values for the * substitutions. If the RuisHandler based on the given index is null in the map, - * a new one is created according to the ruiHandlerType. If not, it will be - * replaced by combining the given rui with the existing one. + * a new one is created according to the ruiHandlerType. If not, the RUI will be + * inserted to the corresponding RuisHandler according to the implementation of + * its insert method. * * @param rui * RuleUseInfo diff --git a/src/sneps/snip/rules/AndEntailment.java b/src/sneps/snip/rules/AndEntailment.java index 0c211254..b0fa0fbc 100644 --- a/src/sneps/snip/rules/AndEntailment.java +++ b/src/sneps/snip/rules/AndEntailment.java @@ -3,17 +3,24 @@ import java.util.ArrayList; import java.util.Set; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; +import sneps.network.VariableNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; +import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; +import sneps.snip.InferenceTypes; import sneps.snip.Report; +import sneps.snip.Runner; import sneps.snip.channels.Channel; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; @@ -52,16 +59,15 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if(report.isNegative()) return null; - //System.out.println("------------------------------"); ArrayList responseList = new ArrayList(); - RuleResponse response = new RuleResponse(); + ArrayList response = new ArrayList(); PropositionSet propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); fns.insert(new FlagNode(signature, propSet, 1)); RuleUseInfo rui = new RuleUseInfo(report.getSubstitutions(), 1, 0, fns, report.getInferenceType()); - + if(antNodesWithoutVars.contains(signature)) { addConstantRui(rui); if (constantRUI.getPosCount() != antNodesWithoutVars.size()) @@ -70,14 +76,14 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if(ruisHandler == null) { response = applyRuleOnRui(constantRUI); if(response != null) - responseList.add(response); + responseList.addAll(response); } RuleUseInfoSet ruis = ruisHandler.combineConstantRUI(constantRUI); for (RuleUseInfo tRui : ruis) { response = applyRuleOnRui(tRui); if(response != null) - responseList.add(response); + responseList.addAll(response); } } else { @@ -93,7 +99,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if(constantRUI == null) { response = applyRuleOnRui(tRui); if(response != null) - responseList.add(response); + responseList.addAll(response); } else { RuleUseInfo combined; @@ -101,11 +107,10 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if (combined != null) { response = applyRuleOnRui(combined); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } - } if(responseList.isEmpty()) @@ -118,30 +123,85 @@ public ArrayList applyRuleHandler(Report report, Node signature) { * Creates a Report from a given RuleUseInfo to be broadcasted to outgoing channels */ @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + protected ArrayList applyRuleOnRui(RuleUseInfo rui) { if (rui.getPosCount() < getAntSize()) return null; + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + Report reply; + PropositionSet replySupport = new PropositionSet(); + PropositionSet ruleSupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { - replySupport.union(fn.getSupport()); + replySupport = replySupport.union(fn.getSupport()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { e.printStackTrace(); } - // TODO - // Add rule node to replySupport - Report reply = new Report(rui.getSubstitutions(), replySupport, true, - rui.getType()); - reportsToBeSent.add(reply); + if(this.getTerm() instanceof Closed) { + try { + ruleSupport = ruleSupport.add(this.getId()); + replySupport = replySupport.union(ruleSupport); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException | + DuplicatePropositionException e) { + } + + if(Runner.isNodeAssertedThroughForwardInf(this)) + reply = new Report(rui.getSubstitutions(), replySupport, true, + InferenceTypes.FORWARD); + else + reply = new Report(rui.getSubstitutions(), replySupport, true, + rui.getType()); + + reportsToBeSent.add(reply); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + else if(this.getTerm() instanceof Open) { + // knownInstances contain instances found for the rule node itself + if(knownInstances.isEmpty()) + return null; + VarNodeSet freeVars = ((Open) this.getTerm()).getFreeVariables(); + boolean allBound; + Report ruiReport = new Report(rui.getSubstitutions(), replySupport, + true, rui.getType()); + for(Report r : knownInstances) { + // Only positive reports should be considered + if(r.getSign() == false) + continue; + + allBound = true; + // Check that each free variable in this rule node is bound in the + // current report + for(VariableNode var : freeVars) { + if(!r.getSubstitutions().isBound(var)) { + allBound = false; + break; + } + } + + // All free variables of this rule node are bound in the current report + if(allBound) { + reply = ruiReport.combine(r); + if(reply != null) { + response.clear(); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + } + } + } - RuleResponse r = new RuleResponse(); - r.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - r.addAllChannels(forwardChannels); + if(responseList.isEmpty()) + return null; - return r; + return responseList; } /** diff --git a/src/sneps/snip/rules/AndOrEntailment.java b/src/sneps/snip/rules/AndOrEntailment.java index b9f40912..c0e218cc 100644 --- a/src/sneps/snip/rules/AndOrEntailment.java +++ b/src/sneps/snip/rules/AndOrEntailment.java @@ -1,15 +1,23 @@ package sneps.snip.rules; +import java.util.ArrayList; import java.util.Set; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.RuleNode; +import sneps.network.VariableNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; +import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; +import sneps.snip.InferenceTypes; import sneps.snip.Report; +import sneps.snip.Runner; import sneps.snip.channels.Channel; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; @@ -34,33 +42,32 @@ public AndOrEntailment(Molecular syn) { } @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + protected ArrayList applyRuleOnRui(RuleUseInfo rui) { boolean reportSign = false; if (rui.getNegCount() == getAntSize() - min) reportSign = true; else if (rui.getPosCount() != max) return null; - /*consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); - ArrayList res = new ArrayList(); + consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); + ArrayList responseList = new ArrayList(); RuleResponse response = new RuleResponse(); Report reply; + PropositionSet replySupport = new PropositionSet(); PropositionSet ruleSupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { - replySupport.union(fn.getSupport()); + replySupport = replySupport.union(fn.getSupport()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - e.printStackTrace(); } if(this.getTerm() instanceof Closed) { try { ruleSupport = ruleSupport.add(this.getId()); - replySupport.union(ruleSupport); - } catch (DuplicatePropositionException | NotAPropositionNodeException | - NodeNotFoundInNetworkException e) { - e.printStackTrace(); + replySupport = replySupport.union(ruleSupport); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException | + DuplicatePropositionException e) { } if(Runner.isNodeAssertedThroughForwardInf(this)) @@ -70,57 +77,53 @@ else if (rui.getPosCount() != max) reply = new Report(rui.getSubstitutions(), replySupport, reportSign, rui.getType()); + reportsToBeSent.add(reply); response.setReport(reply); Set forwardChannels = getOutgoingChannelsForReport(reply); response.addAllChannels(forwardChannels); - res.add(response); + responseList.add(response); } else if(this.getTerm() instanceof Open) { + // knownInstances contain instances found for the rule node itself + if(knownInstances.isEmpty()) + return null; + VarNodeSet freeVars = ((Open) this.getTerm()).getFreeVariables(); + boolean allBound; Report ruiReport = new Report(rui.getSubstitutions(), replySupport, reportSign, rui.getType()); for(Report r : knownInstances) { - reply = ruiReport.combine(r); - if(reply != null) { - response.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - response.addAllChannels(forwardChannels); - res.add(response); - response.clear(); + // Only positive reports should be considered + if(r.getSign() == false) + continue; + + allBound = true; + // Check that each free variable in this rule node is bound in the + // current report + for(VariableNode var : freeVars) { + if(!r.getSubstitutions().isBound(var)) { + allBound = false; + break; + } + } + + // All free variables of this rule node are bound in the current report + if(allBound) { + reply = ruiReport.combine(r); + if(reply != null) { + response.clear(); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } } } } - return res;*/ - - PropositionSet replySupport = new PropositionSet(); - for(FlagNode fn : rui.getFlagNodeSet()) - try { - replySupport.union(fn.getSupport()); - } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - e.printStackTrace(); - } - - // TODO - // Add rule node to replySupport - // If Closed, just return this - // Else, from knownInstances which contains instances found for the rule node itself - // Check for report type, get support to union with rui, and get subs to union with rui - - // If node is Closed, need to check if it was asserted thru forward inference, - // by calling method isAssertedthruForwardInference(), to get type of replyReport - - consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); - - Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, - rui.getType()); - reportsToBeSent.add(reply); - - RuleResponse r = new RuleResponse(); - r.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - r.addAllChannels(forwardChannels); + if(responseList.isEmpty()) + return null; - return r; + return responseList; } @Override diff --git a/src/sneps/snip/rules/DoIfNode.java b/src/sneps/snip/rules/DoIfNode.java index 22d32961..1f8563e5 100644 --- a/src/sneps/snip/rules/DoIfNode.java +++ b/src/sneps/snip/rules/DoIfNode.java @@ -1,5 +1,6 @@ package sneps.snip.rules; +import java.util.ArrayList; import java.util.Set; import sneps.network.RuleNode; @@ -31,7 +32,7 @@ protected byte getSIndexType() { } @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { + protected ArrayList applyRuleOnRui(RuleUseInfo tRui) { // TODO Auto-generated method stub return null; } diff --git a/src/sneps/snip/rules/NumericalEntailment.java b/src/sneps/snip/rules/NumericalEntailment.java index f9900846..91ae37bf 100644 --- a/src/sneps/snip/rules/NumericalEntailment.java +++ b/src/sneps/snip/rules/NumericalEntailment.java @@ -4,16 +4,23 @@ import java.util.Set; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; +import sneps.network.VariableNode; import sneps.network.classes.setClasses.FlagNodeSet; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; +import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; +import sneps.snip.InferenceTypes; import sneps.snip.Report; +import sneps.snip.Runner; import sneps.snip.channels.Channel; import sneps.snip.classes.FlagNode; import sneps.snip.classes.RuisHandler; @@ -57,14 +64,11 @@ public NumericalEntailment(Molecular syn) { */ @Override public ArrayList applyRuleHandler(Report report, Node signature) { - //processNodes(antecedents); - System.out.println("---------------"); - if(report.isNegative()) return null; ArrayList responseList = new ArrayList(); - RuleResponse response = new RuleResponse(); + ArrayList response = new ArrayList(); PropositionSet propSet = report.getSupport(); FlagNodeSet fns = new FlagNodeSet(); @@ -77,27 +81,26 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if (ruisHandler == null) { response = applyRuleOnRui(constantRUI); if(response != null) - responseList.add(response); + responseList.addAll(response); } else { RuleUseInfoSet combined = ruisHandler.combineConstantRUI(constantRUI); for (RuleUseInfo tRui : combined) { response = applyRuleOnRui(tRui); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } else { // Inserting the RuleUseInfo into the RuleNode's RuisHandler: - // SIndex in case there are shared variables between the antecedents, or + // SIndex in case there are shared variables between all the antecedents, or // RUISet in case there are no shared variables if(ruisHandler == null) ruisHandler = addRuiHandler(); // The RUI created for the given report is inserted to the RuisHandler RuleUseInfoSet res = ruisHandler.insertRUI(rui); - System.out.println(res); if(constantRUI != null) { RuleUseInfo combined; @@ -106,7 +109,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { if(combined != null) { response = applyRuleOnRui(combined); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } @@ -114,7 +117,7 @@ public ArrayList applyRuleHandler(Report report, Node signature) { for (RuleUseInfo tRui : res) { response = applyRuleOnRui(tRui); if(response != null) - responseList.add(response); + responseList.addAll(response); } } } @@ -125,32 +128,85 @@ public ArrayList applyRuleHandler(Report report, Node signature) { return responseList; } - protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + protected ArrayList applyRuleOnRui(RuleUseInfo rui) { if(rui.getPosCount() < i) return null; + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + Report reply; + PropositionSet replySupport = new PropositionSet(); + PropositionSet ruleSupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { - replySupport.union(fn.getSupport()); + replySupport = replySupport.union(fn.getSupport()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { e.printStackTrace(); } - - // TODO - // Add rule node to replySupport - Report reply = new Report(rui.getSubstitutions(), replySupport, true, - rui.getType()); - System.out.println(reply); - reportsToBeSent.add(reply); + if(this.getTerm() instanceof Closed) { + try { + ruleSupport = ruleSupport.add(this.getId()); + replySupport = replySupport.union(ruleSupport); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException | + DuplicatePropositionException e) { + } + + if(Runner.isNodeAssertedThroughForwardInf(this)) + reply = new Report(rui.getSubstitutions(), replySupport, true, + InferenceTypes.FORWARD); + else + reply = new Report(rui.getSubstitutions(), replySupport, true, + rui.getType()); + + reportsToBeSent.add(reply); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + else if(this.getTerm() instanceof Open) { + // knownInstances contain instances found for the rule node itself + if(knownInstances.isEmpty()) + return null; + VarNodeSet freeVars = ((Open) this.getTerm()).getFreeVariables(); + boolean allBound; + Report ruiReport = new Report(rui.getSubstitutions(), replySupport, + true, rui.getType()); + for(Report r : knownInstances) { + // Only positive reports should be considered + if(r.getSign() == false) + continue; + + allBound = true; + // Check that each free variable in this rule node is bound in the + // current report + for(VariableNode var : freeVars) { + if(!r.getSubstitutions().isBound(var)) { + allBound = false; + break; + } + } + + // All free variables of this rule node are bound in the current report + if(allBound) { + reply = ruiReport.combine(r); + if(reply != null) { + response.clear(); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + } + } + } - RuleResponse r = new RuleResponse(); - r.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - r.addAllChannels(forwardChannels); + if(responseList.isEmpty()) + return null; - return r; + return responseList; } /** diff --git a/src/sneps/snip/rules/OrEntailment.java b/src/sneps/snip/rules/OrEntailment.java index 2c4ea272..19a9b9a0 100644 --- a/src/sneps/snip/rules/OrEntailment.java +++ b/src/sneps/snip/rules/OrEntailment.java @@ -3,14 +3,21 @@ import java.util.ArrayList; import java.util.Set; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; import sneps.network.RuleNode; +import sneps.network.VariableNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; +import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; +import sneps.snip.InferenceTypes; import sneps.snip.Report; +import sneps.snip.Runner; import sneps.snip.channels.Channel; import sneps.snip.classes.RuisHandler; import sneps.snip.classes.RuleResponse; @@ -31,26 +38,75 @@ public ArrayList applyRuleHandler(Report report, Node signature) { ArrayList responseList = new ArrayList(); RuleResponse response = new RuleResponse(); - PropositionSet replySupport = new PropositionSet(); - try { - replySupport.union(report.getSupport()); - } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { - e.printStackTrace(); + Report reply; + + PropositionSet replySupport = report.getSupport(); + PropositionSet ruleSupport = new PropositionSet(); + + if(this.getTerm() instanceof Closed) { + try { + ruleSupport = ruleSupport.add(this.getId()); + replySupport = replySupport.union(ruleSupport); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException | + DuplicatePropositionException e) { + } + + if(Runner.isNodeAssertedThroughForwardInf(this)) + reply = new Report(report.getSubstitutions(), replySupport, + true, InferenceTypes.FORWARD); + else + reply = new Report(report.getSubstitutions(), replySupport, + true, report.getInferenceType()); + + reportsToBeSent.add(reply); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + else if(this.getTerm() instanceof Open) { + // knownInstances contain instances found for the rule node itself + if(knownInstances.isEmpty()) + return null; + VarNodeSet freeVars = ((Open) this.getTerm()).getFreeVariables(); + boolean allBound; + for(Report r : knownInstances) { + // Only positive reports for this rule node should be considered + if(r.getSign() == false) + continue; + + allBound = true; + // Check that each free variable in this rule node is bound in the + // current report + for(VariableNode var : freeVars) { + if(!r.getSubstitutions().isBound(var)) { + allBound = false; + break; + } + } + + // All free variables of this rule node are bound in the current report + if(allBound) { + reply = report.combine(r); + if(reply != null) { + response.clear(); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + } + } } - // TODO - // Add rule node to replySupport - Report reply = new Report(report.getSubstitutions(), replySupport, - true, report.getInferenceType()); - reportsToBeSent.add(reply); - response.setReport(reply); - Set outgoingChannels = getOutgoingChannelsForReport(reply); - response.addAllChannels(outgoingChannels); - responseList.add(response); + + if(responseList.isEmpty()) + return null; + return responseList; } @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { + protected ArrayList applyRuleOnRui(RuleUseInfo tRui) { return null; } diff --git a/src/sneps/snip/rules/ThreshEntailment.java b/src/sneps/snip/rules/ThreshEntailment.java index d4a2f676..7d86f8c2 100644 --- a/src/sneps/snip/rules/ThreshEntailment.java +++ b/src/sneps/snip/rules/ThreshEntailment.java @@ -1,15 +1,23 @@ package sneps.snip.rules; +import java.util.ArrayList; import java.util.Set; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.RuleNode; +import sneps.network.VariableNode; import sneps.network.classes.setClasses.NodeSet; import sneps.network.classes.setClasses.PropositionSet; import sneps.network.classes.setClasses.RuleUseInfoSet; +import sneps.network.classes.setClasses.VarNodeSet; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Molecular; +import sneps.network.classes.term.Open; +import sneps.snip.InferenceTypes; import sneps.snip.Report; +import sneps.snip.Runner; import sneps.snip.channels.Channel; import sneps.snip.classes.RuleUseInfo; import sneps.snip.classes.SIndex; @@ -34,7 +42,7 @@ public ThreshEntailment(Molecular syn) { } @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo rui) { + protected ArrayList applyRuleOnRui(RuleUseInfo rui) { boolean reportSign = false; if (rui.getPosCount() == min && rui.getNegCount() == getAntSize() - max - 1) @@ -43,31 +51,82 @@ else if (rui.getPosCount() != min - 1 || rui.getNegCount() != getAntSize() - max) return null; + consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); + ArrayList responseList = new ArrayList(); + RuleResponse response = new RuleResponse(); + Report reply; + PropositionSet replySupport = new PropositionSet(); + PropositionSet ruleSupport = new PropositionSet(); for(FlagNode fn : rui.getFlagNodeSet()) try { - replySupport.union(fn.getSupport()); + replySupport = replySupport.union(fn.getSupport()); } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException e) { e.printStackTrace(); } - - // TODO - // Add rule node to replySupport - - consequents = antecedents.difference(rui.getFlagNodeSet().getAllNodes()); - System.out.println(consequents); - Report reply = new Report(rui.getSubstitutions(), replySupport, reportSign, - rui.getType()); - reportsToBeSent.add(reply); - System.out.println(reply); + if(this.getTerm() instanceof Closed) { + try { + ruleSupport = ruleSupport.add(this.getId()); + replySupport = replySupport.union(ruleSupport); + } catch (NotAPropositionNodeException | NodeNotFoundInNetworkException | + DuplicatePropositionException e) { + } + + if(Runner.isNodeAssertedThroughForwardInf(this)) + reply = new Report(rui.getSubstitutions(), replySupport, reportSign, + InferenceTypes.FORWARD); + else + reply = new Report(rui.getSubstitutions(), replySupport, reportSign, + rui.getType()); + + reportsToBeSent.add(reply); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + else if(this.getTerm() instanceof Open) { + // knownInstances contain instances found for the rule node itself + if(knownInstances.isEmpty()) + return null; + VarNodeSet freeVars = ((Open) this.getTerm()).getFreeVariables(); + boolean allBound; + Report ruiReport = new Report(rui.getSubstitutions(), replySupport, + reportSign, rui.getType()); + for(Report r : knownInstances) { + // Only positive reports should be considered + if(r.getSign() == false) + continue; + + allBound = true; + // Check that each free variable in this rule node is bound in the + // current report + for(VariableNode var : freeVars) { + if(!r.getSubstitutions().isBound(var)) { + allBound = false; + break; + } + } + + // All free variables of this rule node are bound in the current report + if(allBound) { + reply = ruiReport.combine(r); + if(reply != null) { + response.clear(); + response.setReport(reply); + Set forwardChannels = getOutgoingChannelsForReport(reply); + response.addAllChannels(forwardChannels); + responseList.add(response); + } + } + } + } - RuleResponse r = new RuleResponse(); - r.setReport(reply); - Set forwardChannels = getOutgoingChannelsForReport(reply); - r.addAllChannels(forwardChannels); + if(responseList.isEmpty()) + return null; - return r; + return responseList; } @Override diff --git a/src/sneps/snip/rules/WhenDoNode.java b/src/sneps/snip/rules/WhenDoNode.java index ebba9e01..d20da483 100644 --- a/src/sneps/snip/rules/WhenDoNode.java +++ b/src/sneps/snip/rules/WhenDoNode.java @@ -1,5 +1,6 @@ package sneps.snip.rules; +import java.util.ArrayList; import java.util.Set; import sneps.network.RuleNode; @@ -31,7 +32,7 @@ protected byte getSIndexType() { } @Override - protected RuleResponse applyRuleOnRui(RuleUseInfo tRui) { + protected ArrayList applyRuleOnRui(RuleUseInfo tRui) { // TODO Auto-generated method stub return null; } diff --git a/tests/AndEntailmentTests.java b/tests/AndEntailmentTests.java index e838bf18..5f9eee38 100644 --- a/tests/AndEntailmentTests.java +++ b/tests/AndEntailmentTests.java @@ -25,6 +25,7 @@ import sneps.network.classes.Semantic; import sneps.network.classes.Wire; import sneps.network.classes.term.Base; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.FlagNodeSet; @@ -36,8 +37,6 @@ import sneps.snip.Report; import sneps.snip.classes.FlagNode; import sneps.snip.classes.PTree; -import sneps.snip.classes.RuisHandler; -import sneps.snip.classes.RuleUseInfo; import sneps.snip.matching.Binding; import sneps.snip.matching.LinearSubstitutions; import sneps.snip.rules.AndEntailment; @@ -205,7 +204,7 @@ public void setUp() { //------------------------ AND ---------------------------// - and = new AndEntailment(new Open("Open", dcs)); + and = new AndEntailment(new Closed("Closed", dcs)); sub.putIn(new Binding((VariableNode) var1, john)); support = new PropositionSet(); diff --git a/tests/AndOrTest.java b/tests/AndOrTest.java index 24dffa98..665327e6 100644 --- a/tests/AndOrTest.java +++ b/tests/AndOrTest.java @@ -21,6 +21,7 @@ import sneps.network.classes.Relation; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.NodeSet; @@ -169,7 +170,7 @@ public static void setUp() throws Exception { //---------------------------- ANDOR -----------------------------------// - andor = new AndOrEntailment(new Open("Open", dcss)); + andor = new AndOrEntailment(new Closed("Open", dcss)); andor.setMax(2); andor.setMin(1); @@ -242,7 +243,7 @@ public void test2() { | NodeNotFoundInNetworkException e) { e.printStackTrace(); }*/ - report = new Report(sub, support, false, InferenceTypes.FORWARD); + report = new Report(sub, support, false, InferenceTypes.BACKWARD); support = new PropositionSet(); /*try { @@ -321,10 +322,12 @@ public void test4() { e.printStackTrace(); }*/ sub.putIn(new Binding((VariableNode) var, fido)); - report = new Report(sub, support, true, InferenceTypes.BACKWARD); + report = new Report(sub, support, false, InferenceTypes.BACKWARD); andor.applyRuleHandler(report, prop5); - assertEquals(0, andor.getReplies().size()); + assertEquals(1, andor.getReplies().size()); + assertEquals(false, andor.getReplies().get(0).getSign()); + assertEquals(3, andor.getConsequents().size()); } public void tearDown() { diff --git a/tests/NumericalEntailmentTests.java b/tests/NumericalEntailmentTests.java index 83fd780b..b14f95db 100644 --- a/tests/NumericalEntailmentTests.java +++ b/tests/NumericalEntailmentTests.java @@ -39,9 +39,9 @@ public class NumericalEntailmentTests extends TestCase { private static Context context; private static String contextName = "TempContext"; private static NumericalEntailment numerical; - private static Node fido, var, dog, barks; - private static Node prop1, prop2, prop3, prop4; - private static Report report, report1; + private static Node fido, leo, var, var1, dog, barks, walks; + private static Node prop1, prop2, prop3, prop4, prop5; + private static Report report, report1, report2, report3; public void setUp() { try { @@ -71,25 +71,34 @@ public void setUp() { rels.clear(); rels.add(antsRel); rels.add(consRel); CaseFrame caseFrameAC = Network.defineCaseFrame("AntsCons", rels); Wire wire1 = null, wire2 = null, wire3 = null, wire4 = null; + Wire wire5 = null, wire6 = null, wire7 = null; rels.clear(); //----------------------------- fido, dog, barks, X -----------------------------// try { var = Network.buildVariableNode("X"); + var1 = Network.buildVariableNode("Y"); fido = Network.buildBaseNode("Fido", new Semantic("Base")); + leo = Network.buildBaseNode("Leo", new Semantic("Base")); dog = Network.buildBaseNode("Dog", new Semantic("Base")); barks = Network.buildBaseNode("Barks", new Semantic("Base")); + walks = Network.buildBaseNode("Walks", new Semantic("Base")); wire1 = new Wire(memberRel, fido); wire2 = new Wire(classRel, dog); wire3 = new Wire(doesRel, barks); wire4 = new Wire(memberRel, var); + wire5 = new Wire(memberRel, var1); + wire6 = new Wire(memberRel, leo); + wire7 = new Wire(doesRel, walks); } catch (IllegalIdentifierException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); } var = new VariableNode(new Variable("X")); + var1 = new VariableNode(new Variable("Y")); + //--------------------------- prop1, prop2, prop3, prop4 -----------------------------// @@ -105,6 +114,9 @@ public void setUp() { wires.clear(); wires.add(wire1); wires.add(wire3); prop4 = Network.buildMolecularNode(wires, caseFrameMD); + + wires.clear(); wires.add(wire4); wires.add(wire2); + prop5 = Network.buildMolecularNode(wires, caseFrameMC); } catch (CannotBuildNodeException | EquivalentNodeException | NotAPropositionNodeException | NodeNotFoundInNetworkException e1) { assertNotNull(e1.getMessage(), e1); @@ -119,6 +131,8 @@ public void setUp() { NodeSet nodeSet6 = new NodeSet(); NodeSet nodeSet7 = new NodeSet(); NodeSet nodeSet8 = new NodeSet(); + NodeSet nodeSet9 = new NodeSet(); + NodeSet nodeSet10 = new NodeSet(); DownCable dc1; DownCableSet dcs; nodeSet1.addNode(fido); @@ -149,7 +163,6 @@ public void setUp() { dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop3 = new PropositionNode(new Open("Prop3", dcs)); - //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// nodeSet7.addNode(fido); @@ -161,13 +174,24 @@ public void setUp() { dcs = new DownCableSet(dcList, caseFrameMD); prop4 = new PropositionNode(new Closed("Prop4", dcs)); dcList.clear(); - //------------------------------------------------------------// + //------------------------------------------------------------// + nodeSet9.addNode(var); + dc1 = new DownCable(memberRel, nodeSet9); + dcList.add(dc1); + nodeSet10.addNode(walks); + dc1 = new DownCable(doesRel, nodeSet10); + dcList.add(dc1); + dcs = new DownCableSet(dcList, caseFrameMC); + prop5 = new PropositionNode(new Open("Prop5", dcs)); + dcList.clear(); + //------------------------------------------------------------// //------------------------- Numerical Setup ------------------------------------// nodeSet.addNode(prop1); nodeSet.addNode(prop2); nodeSet.addNode(prop3); + nodeSet.addNode(prop5); dc.add(new DownCable(antsRel, nodeSet)); nodeSett.addNode(prop4); @@ -177,7 +201,7 @@ public void setUp() { //---------------------------- NUMERICAL -----------------------------------// - numerical = new NumericalEntailment(new Open("Open", dcss)); + numerical = new NumericalEntailment(new Closed("Closed", dcss)); numerical.setI(1); sub = new LinearSubstitutions(); @@ -199,14 +223,33 @@ public void setUp() { }*/ sub1.putIn(new Binding((VariableNode) var, fido)); report1 = new Report(sub1, support1, true, InferenceTypes.BACKWARD); - + + LinearSubstitutions sub2 = new LinearSubstitutions(); + PropositionSet support2 = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub2.putIn(new Binding((VariableNode) var, leo)); + report2 = new Report(sub2, support2, true, InferenceTypes.BACKWARD); + + LinearSubstitutions sub3 = new LinearSubstitutions(); + PropositionSet support3 = new PropositionSet(); + /*try { + support.add(prop5.getId()); + } catch (DuplicatePropositionException | NotAPropositionNodeException | NodeNotFoundInNetworkException e) { + e.printStackTrace(); + }*/ + sub3.putIn(new Binding((VariableNode) var, fido)); + report3 = new Report(sub3, support3, true, InferenceTypes.BACKWARD); } @Test public void testAntsandConsq() { assertEquals(1, numerical.getConsequents().size()); - assertEquals(3, numerical.getAntecedents().size()); - assertEquals(1, numerical.getAntsWithVars().size()); + assertEquals(4, numerical.getAntecedents().size()); + assertEquals(2, numerical.getAntsWithVars().size()); assertEquals(2, numerical.getAntsWithoutVars().size()); } @@ -220,21 +263,19 @@ public void testApplyRuleHandler() { @Test public void testApplyRuleHandler2() { - numerical.setI(2); numerical.clear(); + numerical.setI(2); numerical.applyRuleHandler(report, prop1); - assertEquals(0, numerical.getReplies().size()); } @Test public void testApplyRuleHandler3() { - numerical.setI(2); numerical.clear(); + numerical.setI(2); numerical.applyRuleHandler(report, prop1); - assertEquals(0, numerical.getReplies().size()); numerical.applyRuleHandler(report1, prop3); @@ -245,6 +286,21 @@ public void testApplyRuleHandler3() { assertEquals(SIndex.SINGLETON, ((SIndex) (numerical.getRuisHandler())).getRuiHandlerType()); assertEquals(1, numerical.getReplies().size()); } + + @Test + public void testApplyRuleHandler4() { + numerical.clear(); + numerical.setI(2); + + numerical.applyRuleHandler(report1, prop3); + assertEquals(0, numerical.getReplies().size()); + + numerical.applyRuleHandler(report2, prop5); + assertEquals(0, numerical.getReplies().size()); + + numerical.applyRuleHandler(report3, prop5); + assertEquals(1, numerical.getReplies().size()); + } @Test public void testGetDownNodeSet() { @@ -254,7 +310,7 @@ public void testGetDownNodeSet() { assertFalse("NumericalEntailment: getDownAntNodeSet retuns an empty NodeSet", downAntNodeSet.isEmpty()); assertEquals(1, numerical.getDownConsqNodeSet().size()); - assertEquals(3, numerical.getDownAntNodeSet().size()); + assertEquals(4, numerical.getDownAntNodeSet().size()); } public void tearDown() { diff --git a/tests/OrTests.java b/tests/OrTests.java index a16774ea..27722a64 100644 --- a/tests/OrTests.java +++ b/tests/OrTests.java @@ -1,5 +1,6 @@ import static org.junit.Assert.*; + import java.util.ArrayList; import java.util.LinkedList; @@ -103,44 +104,51 @@ public static void SetUp() { LinkedList dcList = new LinkedList(); NodeSet nodeSet1 = new NodeSet(); + NodeSet nodeSet2 = new NodeSet(); + NodeSet nodeSet3 = new NodeSet(); + NodeSet nodeSet4 = new NodeSet(); + NodeSet nodeSet5 = new NodeSet(); + NodeSet nodeSet6 = new NodeSet(); + NodeSet nodeSet7 = new NodeSet(); + NodeSet nodeSet8 = new NodeSet(); DownCable dc1; DownCableSet dcs; nodeSet1.addNode(fido); dc1 = new DownCable(memberRel, nodeSet1); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet2.addNode(dog); + dc1 = new DownCable(classRel, nodeSet2); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop1 = new PropositionNode(new Closed("Prop1", dcs)); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet3.addNode(dog); + dc1 = new DownCable(classRel, nodeSet3); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); + nodeSet4.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet4); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameCD); prop2 = new PropositionNode(new Closed("Prop2", dcs)); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(var); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet5.addNode(var); + dc1 = new DownCable(memberRel, nodeSet5); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(dog); - dc1 = new DownCable(classRel, nodeSet1); + nodeSet6.addNode(dog); + dc1 = new DownCable(classRel, nodeSet6); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMC); prop3 = new PropositionNode(new Open("Prop3", dcs)); - ((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); + //((Open) (prop3.getTerm())).getFreeVariables().addVarNode((VariableNode) var); dcList.clear(); //------------------------------------------------------------// - nodeSet1.clear(); nodeSet1.addNode(fido); - dc1 = new DownCable(memberRel, nodeSet1); + nodeSet7.addNode(fido); + dc1 = new DownCable(memberRel, nodeSet7); dcList.add(dc1); - nodeSet1.clear(); nodeSet1.addNode(barks); - dc1 = new DownCable(doesRel, nodeSet1); + nodeSet8.addNode(barks); + dc1 = new DownCable(doesRel, nodeSet8); dcList.add(dc1); dcs = new DownCableSet(dcList, caseFrameMD); prop4 = new PropositionNode(new Closed("Prop4", dcs)); @@ -148,13 +156,7 @@ public static void SetUp() { nodeSet.addNode(prop1); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); nodeSet.addNode(prop2); - dc.add(new DownCable(antsRel, nodeSet)); - - nodeSet.clear(); nodeSet.addNode(prop3); dc.add(new DownCable(antsRel, nodeSet)); @@ -164,11 +166,10 @@ public static void SetUp() { DownCableSet dcss = new DownCableSet(dc, caseFrameAC); - /** * Or-Entailment */ - or = new OrEntailment(new Open("Open", dcss)); + or = new OrEntailment(new Closed("Closed", dcss)); sub.putIn(new Binding((VariableNode) var, fido)); LinearSubstitutions s = new LinearSubstitutions(); diff --git a/tests/SIndexTest.java b/tests/SIndexTest.java index 6172bbd2..e55fbf5c 100644 --- a/tests/SIndexTest.java +++ b/tests/SIndexTest.java @@ -8,7 +8,6 @@ import sneps.exceptions.NodeNotFoundInNetworkException; import sneps.exceptions.NotAPropositionNodeException; import sneps.network.Node; -import sneps.network.PropositionNode; import sneps.network.VariableNode; import sneps.network.classes.term.Base; import sneps.network.classes.term.Variable; @@ -39,24 +38,14 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo Node n = new Node(ba); Node n1 = new Node(ba1); Node n2 = new Node(ba2); - Node n3 = new Node(); - Node n4 = new Node(); - Node n5 = new Node(); - Node n6 = new Node(); - Node n7 = new Node(); - + LinearSubstitutions ls = new LinearSubstitutions(); LinearSubstitutions ls1 = new LinearSubstitutions(); LinearSubstitutions ls2 = new LinearSubstitutions(); - LinearSubstitutions ls3 = new LinearSubstitutions(); LinearSubstitutions ls4 = new LinearSubstitutions(); LinearSubstitutions ls5 = new LinearSubstitutions(); - PropositionNode pn = new PropositionNode(); - PropositionNode pn2 = new PropositionNode(); - PropositionNode pn3 = new PropositionNode(); - PropositionSet ps = new PropositionSet(); Variable v = new Variable("x"); @@ -105,11 +94,6 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo Binding b = new Binding(vn, n); Binding b1 = new Binding(vn1, n1); Binding b2 = new Binding(vn2, n1); - Binding b3 = new Binding(vn3, n); - Binding b4 = new Binding(vn4, n); - Binding b5 = new Binding(vn5, n1); - Binding b6 = new Binding(vn6, n); - Binding b7 = new Binding(vn7, n); Binding b8 = new Binding(vn, n1); Binding b9 = new Binding(vn, n2); @@ -121,12 +105,6 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo FlagNode fn = new FlagNode(n, ps, 1); FlagNode fn1 = new FlagNode(n1, ps, 1); - FlagNode fn2 = new FlagNode(n2, ps, 1); - FlagNode fn3 = new FlagNode(n3, ps, 1); - FlagNode fn4 = new FlagNode(n4, ps, 1); - FlagNode fn5 = new FlagNode(n5, ps, 1); - FlagNode fn6 = new FlagNode(n6, ps, 1); - FlagNode fn7 = new FlagNode(n7, ps, 1); FlagNodeSet fns = new FlagNodeSet(); FlagNodeSet fns1 = new FlagNodeSet(); @@ -157,18 +135,6 @@ public void newSIndex() throws NotAPropositionNodeException, NodeNotFoundInNetwo index1.insertRUI(rui); - RuleUseInfo rui1 = new RuleUseInfo(ls1, 1, 0, fns1, InferenceTypes.BACKWARD); - - - RuleUseInfo rui2 = new RuleUseInfo(ls1, 1, 0, fns2, InferenceTypes.BACKWARD); - - - RuleUseInfo rui3 = new RuleUseInfo(ls1, 1, 0, fns2, InferenceTypes.BACKWARD); - - - RuleUseInfo rui4 = new RuleUseInfo(ls3, 0, 1, fns3, InferenceTypes.BACKWARD); - - RuleUseInfo rui5 = new RuleUseInfo(ls2, 2, 0, fns4, InferenceTypes.BACKWARD); index.insertRUI(rui5); index1.insertRUI(rui5); diff --git a/tests/ThreshTest.java b/tests/ThreshTest.java index 175f3f91..f621224f 100644 --- a/tests/ThreshTest.java +++ b/tests/ThreshTest.java @@ -8,6 +8,7 @@ import sneps.exceptions.CannotBuildNodeException; import sneps.exceptions.DuplicateContextNameException; +import sneps.exceptions.DuplicatePropositionException; import sneps.exceptions.EquivalentNodeException; import sneps.exceptions.IllegalIdentifierException; import sneps.exceptions.NodeNotFoundInNetworkException; @@ -21,6 +22,7 @@ import sneps.network.classes.Relation; import sneps.network.classes.Semantic; import sneps.network.classes.Wire; +import sneps.network.classes.term.Closed; import sneps.network.classes.term.Open; import sneps.network.classes.term.Variable; import sneps.network.classes.setClasses.NodeSet; @@ -173,7 +175,7 @@ public static void setUp() throws Exception { //---------------------------- THRESH -----------------------------------// - thresh = new ThreshEntailment(new Open("Open", dcss)); + thresh = new ThreshEntailment(new Closed("Open", dcss)); thresh.setMax(3); thresh.setMin(2); @@ -246,6 +248,7 @@ public void test2() { report = new Report(sub, support, true, InferenceTypes.BACKWARD); thresh.applyRuleHandler(report, prop5); + assertEquals(0, thresh.getReplies().size()); sub = new LinearSubstitutions(); support = new PropositionSet(); From 4166267cf9e0317f5309f10a86ecef2afe102e79 Mon Sep 17 00:00:00 2001 From: Youssef Walid Date: Mon, 29 Jun 2020 18:48:18 +0200 Subject: [PATCH 22/22] Update .project --- out/production/Java-SNePS-3.0/.project | 6 ------ 1 file changed, 6 deletions(-) diff --git a/out/production/Java-SNePS-3.0/.project b/out/production/Java-SNePS-3.0/.project index d756504d..ff1494d7 100644 --- a/out/production/Java-SNePS-3.0/.project +++ b/out/production/Java-SNePS-3.0/.project @@ -5,13 +5,7 @@ - - org.eclipse.jdt.core.javabuilder - - - - org.eclipse.jdt.core.javanature