-
Notifications
You must be signed in to change notification settings - Fork 0
/
TalkingRuleOrConstraint.java
684 lines (623 loc) · 29.6 KB
/
TalkingRuleOrConstraint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/*
* Copyright 2018–2022 University of Tübingen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.tuebingen.sfs.psl.talk.rule;
import de.tuebingen.sfs.psl.engine.PslProblem;
import de.tuebingen.sfs.psl.engine.RuleAtomGraph;
import de.tuebingen.sfs.psl.talk.BeliefScale;
import de.tuebingen.sfs.psl.talk.ConstantRenderer;
import de.tuebingen.sfs.psl.talk.PrintableAtom;
import de.tuebingen.sfs.psl.talk.pred.NotEqualPred;
import de.tuebingen.sfs.psl.talk.pred.TalkingPredicate;
import de.tuebingen.sfs.psl.util.data.StringUtils;
import de.tuebingen.sfs.psl.util.data.Tuple;
import org.linqs.psl.database.DataStore;
import org.linqs.psl.model.atom.Atom;
import org.linqs.psl.model.formula.FormulaAnalysis;
import org.linqs.psl.model.predicate.Predicate;
import org.linqs.psl.model.rule.Rule;
import org.linqs.psl.model.rule.arithmetic.AbstractArithmeticRule;
import org.linqs.psl.model.rule.arithmetic.expression.SummationAtom;
import org.linqs.psl.model.rule.arithmetic.expression.SummationAtomOrAtom;
import org.linqs.psl.model.rule.logical.AbstractLogicalRule;
import org.linqs.psl.parser.ModelLoader;
import org.linqs.psl.parser.RulePartial;
import org.linqs.psl.reasoner.function.FunctionComparator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static de.tuebingen.sfs.psl.talk.rule.SentenceHelper.*;
// To use a custom class for properly rendering the PSL constants (atom arguments), create the following classes:
// - `class YourConstantRenderer`, a class that assigns human-understandable strings to PSL constants
// - `interface YourTalkingRule`
// - `class YourTalkingLogicalRule extends TalkingLogicalRule implements YourTalkingRule`
// - `class YourTalkingArithmeticRule extends TalkingArithmeticRule implements YourTalkingRule`
// - `class YourTalkingPredicate extends TalkingPredicate`
// `YourTalkingPredicate` and `YourTalkingRule` can extend their explanation/verbalization
// methods by an additional argument: YourConstantRenderer.
// An example implementation of this will be available at
// https://github.com/jdellert/etinen-shared/tree/master/src/main/java/de/tuebingen/sfs/eie/talk
public abstract class TalkingRuleOrConstraint {
// private static final Pattern ATOM_PATTERN = Pattern.compile("(?<=\\w\\()[^\\(]+(?=\\))");
private static final Pattern ATOM_PATTERN = Pattern.compile("\\w{4,}\\([^\\(]+\\)");
// Rule name
private String name;
// String representation of rule
private String ruleString;
// The rule itself
private Rule rule;
// A VERBALIZATION of the rule that can be presented to the user.
private String verbalization;
// Arguments of the rule as entered in the rule string
// (e.g. "Prec(Lang1, Lang2, Proto)")
private String[] args;
private PslProblem pslProblem = null;
private Map<String, TalkingPredicate> talkingPreds = null;
TalkingRuleOrConstraint(String name, String ruleString, Rule rule, PslProblem pslProblem, String verbalization) {
this.name = name;
setVerbalization(verbalization);
setRule(ruleString, rule);
this.pslProblem = pslProblem;
}
// For serialization.
TalkingRuleOrConstraint(String name, String ruleString) {
this(name, ruleString, null);
}
// For serialization.
TalkingRuleOrConstraint(String name, String ruleString, String verbalization) {
this.pslProblem = null;
this.rule = null;
setName(name);
setVerbalization(verbalization);
setRuleString(ruleString);
}
public static TalkingRuleOrConstraint createTalkingRuleOrConstraint(String name, String ruleString, Rule rule,
PslProblem pslProblem) {
return createTalkingRuleOrConstraint(name, ruleString, rule, pslProblem, null);
}
public static TalkingRuleOrConstraint createTalkingRuleOrConstraint(String name, String ruleString, Rule rule,
PslProblem pslProblem, String verbalization) {
if (TalkingRule.isWeightedRule(ruleString)) {
double weight = 1.0;
if (ruleString.contains(":")) {
try {
String[] fields = StringUtils.split(ruleString, ':');
weight = Double.parseDouble(fields[0].strip());
ruleString = fields[1].strip();
} catch (Exception e) {
weight = 1.0;
}
}
if (rule instanceof AbstractLogicalRule)
return new TalkingLogicalRule(name, weight, ruleString, rule, pslProblem, verbalization);
if (rule instanceof AbstractArithmeticRule)
return new TalkingArithmeticRule(name, weight, ruleString, rule, pslProblem, verbalization);
}
if (rule instanceof AbstractLogicalRule)
return new TalkingLogicalConstraint(name, ruleString, rule, pslProblem, verbalization);
if (rule instanceof AbstractArithmeticRule)
return new TalkingArithmeticConstraint(name, ruleString, rule, pslProblem, verbalization);
return null;
}
public static Rule createRule(DataStore dataStore, String ruleString) {
Rule rule = null;
try {
RulePartial partial = ModelLoader.loadRulePartial(dataStore, ruleString);
rule = partial.toRule();
} catch (Exception e) {
e.printStackTrace();
}
return rule;
}
protected void setRule(String ruleString, Rule rule) {
this.rule = rule;
this.ruleString = ruleString;
if (rule != null) {
List<SummationAtomOrAtom> atoms;
if (rule instanceof AbstractLogicalRule) {
AbstractLogicalRule logRule = (AbstractLogicalRule) rule;
FormulaAnalysis.DNFClause dnf = logRule.getNegatedDNF();
atoms = new ArrayList<>(dnf.getPosLiterals());
atoms.addAll(dnf.getNegLiterals());
} else if (rule instanceof AbstractArithmeticRule) {
AbstractArithmeticRule ariRule = (AbstractArithmeticRule) rule;
FunctionComparator comparator = ariRule.getExpression().getComparator();
atoms = ariRule.getExpression().getAtoms();
} else {
atoms = new ArrayList<>();
}
args = new String[atoms.size()];
for (int i = 0; i < atoms.size(); i++) {
SummationAtomOrAtom atom = atoms.get(i);
Predicate pred = (atom instanceof SummationAtom) ? ((SummationAtom) atom).getPredicate() :
((Atom) atom).getPredicate();
args[i] = atom.toString();
}
return;
}
List<String> args = new ArrayList<>();
Matcher matcher = ATOM_PATTERN.matcher(ruleString);
while (matcher.find()) {
String argSection = matcher.group();
for (String arg : argSection.split(",")) {
args.add(arg.trim());
}
}
this.args = new String[args.size()];
for (int i = 0; i < args.size(); i++) {
this.args[i] = args.get(i);
}
}
public String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
public String getRuleString() {
return ruleString;
}
protected void setRuleString(String ruleString) {
setRule(ruleString, null);
}
public Rule getRule() {
return rule;
}
public String getVerbalization() {
return verbalization;
}
protected void setVerbalization(String verbalization) {
this.verbalization = (verbalization != null) ? verbalization : name;
}
protected String[] getArgs() {
return args;
}
protected PslProblem getPslProblem() {
return pslProblem;
}
protected Map<String, TalkingPredicate> getTalkingPredicates() {
if (pslProblem != null) return pslProblem.getTalkingPredicates();
return talkingPreds;
}
public void setTalkingPredicates(Map<String, TalkingPredicate> talkingPreds) {
this.talkingPreds = talkingPreds;
}
public String generateExplanation(ConstantRenderer renderer, String groundingName, String contextAtom,
RuleAtomGraph rag, boolean whyExplanation) {
return getDefaultExplanation(renderer, groundingName, contextAtom, rag, whyExplanation);
}
public abstract String getDefaultExplanation(ConstantRenderer renderer, String groundingName, String contextAtom,
RuleAtomGraph rag, boolean whyExplanation);
/**
* @return An introductory sentence summarizing the reasoning pattern expressed by the rule.
*/
public String getIntroductorySentence() {
if (verbalization.isEmpty()) {
return name + ".";
}
return verbalization.endsWith(".") ? verbalization : verbalization + ".";
}
/**
* Returns the explanation for a rule with a context atom that's on the PSL problem's ignore list.
*
* @param contextAtom Atom displayed in the fact window
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and not on the ignore list)
* @param renderer Renderer for the atom arguments. Can be null.
* @return the explanation
*/
public String getContextlessExplanation(PrintableAtom contextAtom, List<PrintableAtom> printableAtoms,
ConstantRenderer renderer) {
StringBuilder sb = new StringBuilder();
sb.append(getIntroductorySentence()).append(" This rule ");
if (printableAtoms.size() > 0) {
sb.append("links ");
addNpWithoutUrl(contextAtom, sb, renderer);
sb.append(" to ");
appendAnd(printableAtoms, sb, renderer); // method adds atom URLs
} else {
sb.append("includes ");
addNpWithoutUrl(contextAtom, sb, renderer);
}
sb.append(" with unknown effects.");
return sb.toString();
}
/**
* Returns the explanation for a rule with a context atom that cannot be pushed further in the direction the rule
* exerts pressure.
*
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and not on the ignore list)
* @param renderer Renderer for the atom arguments. Can be null.
* @return the explanation
*/
public String getExplanationForPolarAtom(List<PrintableAtom> printableAtoms, ConstantRenderer renderer) {
// If the context atom is 1.0/0.0 and can't be higher/lower:
// Intro, then, in parentheses, list the atom links/values.
return getMinimalExplanation(printableAtoms, renderer);
}
/**
* Returns a very minimal explanation consisting of an introductory sentence followed by a list of links to
* connected atoms (when possible, verbalized) and their belief values.
*
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and not on the ignore list)
* @param renderer Renderer for the atom arguments. Can be null.
* @return explanation
*/
public String getMinimalExplanation(List<PrintableAtom> printableAtoms, ConstantRenderer renderer) {
// Intro, then, in parentheses, list the atom links/values.
StringBuilder sb = new StringBuilder();
sb.append(getIntroductorySentence()).append(" (");
int numArgs = printableAtoms.size();
for (int i = 0; i < numArgs; i++) {
addSentenceWithUrl(printableAtoms, i, sb, renderer);
if (i == numArgs - 2) {
sb.append("and ");
} else if (i != numArgs - 1) {
sb.append(", ");
}
}
sb.append(".)");
return sb.toString();
}
/**
* Returns an explanation for the consequent of a logical rule (A & B -> *C*) or
* for the (single) atom that opposes the other atoms in an arithmetic rule expressing an inequality (A + B <= *C*).
*
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and not on the ignore list)
* @param renderer Renderer for the atom arguments. Can be null.
* @param negatedConsequent Whether the consequent of the rule is negated in the ruleString
* @param consequent The atom in the consequent of the rule.
* @return the verbalization
*/
public String getExplanationForConsequent(List<PrintableAtom> printableAtoms, ConstantRenderer renderer,
boolean negatedConsequent, PrintableAtom consequent) {
StringBuilder sb = new StringBuilder();
sb.append(getIntroductorySentence()).append(" ");
sb.append("Since ");
int numArgs = printableAtoms.size();
for (int i = 0; i < numArgs; i++) {
addSentenceWithUrl(printableAtoms, i, sb, renderer);
if (i == numArgs - 1) {
sb.append(" and ");
} else {
sb.append(", ");
}
}
sb.append("the value of ");
addNpWithoutUrl(consequent, sb, renderer);
sb.append(" has a");
if (negatedConsequent) {
sb.append("n upper");
} else {
sb.append(" lower");
}
sb.append(" limit.");
// TODO Tie this to the RuleAtomGraph's computeBodyScore method
// and the minimum/maximum methods in the BeliefScale class instead.
// -> "The atom needs to be at least X."
return sb.toString();
}
/**
* Returns the explanation for a context atom in a rule that is trivially satisfied by the consequent's value.
* If the context atom is in the consequent of the rule, use the method {@link #getExplanationForConsequent(
*List, ConstantRenderer, boolean, PrintableAtom)} instead.
*
* @param contextAtom Atom displayed in the fact window.
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and the consequent, and not on the ignore list)
* @param renderer Renderer for the atom arguments. Can be null.
* @param negatedConsequent Whether the consequent of the rule is negated in the ruleString
* @param consequent The atom in the consequent of the rule. NOT identical to the context atom!
* @return the verbalization
*/
public String getExplanationForTriviallySatisfiedRule(PrintableAtom contextAtom, List<PrintableAtom> printableAtoms,
ConstantRenderer renderer, boolean negatedConsequent,
PrintableAtom consequent) {
StringBuilder sb = new StringBuilder();
sb.append(getIntroductorySentence()).append(" ");
sb.append("In this case, ");
addNpWithoutUrl(contextAtom, sb, renderer);
int numArgs = printableAtoms.size();
if (numArgs == 1) {
sb.append(" and ");
} else if (numArgs > 1) {
sb.append(", ");
}
for (int i = 0; i < numArgs; i++) {
addNpWithUrl(printableAtoms, i, sb, renderer);
if (i == numArgs - 2) {
sb.append(" and");
} else if (i < numArgs - 1) {
sb.append(",");
}
sb.append(" ");
}
sb.append("determine");
if (numArgs == 0) {
sb.append("s");
}
sb.append(" a ");
if (negatedConsequent) {
sb.append("maximum");
} else {
sb.append("minimum");
}
sb.append(" value for ");
addNpWithUrl(consequent, sb, renderer);
sb.append(". However, since it already has a value of ");
if (negatedConsequent) {
sb.append("0 %");
} else {
sb.append("100 %");
}
sb.append(", changing the value of any of the other atoms would not violate this rule.");
return sb.toString();
}
/**
* Only lists the atoms and their values.
* Used when we don't have enough information to generate a more useful explanation (i.e. information on the consequent).
*
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and not on the ignore list)
* @param whyNotLower If true: explanation appears in the WHY block, otherwise: in
* the WHY NOT block.
* @param renderer Renderer for the atom arguments. Can be null.
* @return the explanation
*/
public String getBarebonesExplanation(List<PrintableAtom> printableAtoms, boolean whyNotLower,
ConstantRenderer renderer) {
StringBuilder sb = new StringBuilder();
sb.append(getIntroductorySentence()).append(" ");
sb.append("This is ").append(whyNotLower ? "supported" : "limited");
if (printableAtoms.size() == 0)
return sb.append(" by the rule '").append(getVerbalization()).append("'").toString();
sb.append(" by ");
List<String> components = new ArrayList<>();
for (int i = 0; i < printableAtoms.size(); i++) {
StringBuilder sbComponent = new StringBuilder();
addNpWithUrl(printableAtoms, i, sbComponent, renderer);
sbComponent.append(" being ");
if (printableAtoms.get(i).canTalk()) {
if (printableAtoms.get(i).getPred().verbalizeOnHighLowScale) {
sbComponent.append(BeliefScale.verbalizeBeliefAsAdjectiveHigh(printableAtoms.get(i).getBelief()));
} else {
sbComponent.append(BeliefScale.verbalizeBeliefAsAdjective(printableAtoms.get(i).getBelief()));
}
} else {
sbComponent.append(String.format("%.2f", printableAtoms.get(i).getBelief()));
}
components.add(sbComponent.toString());
}
appendAnd(components, sb, false);
sb.append(".");
return sb.toString();
}
/**
* Generates the explanation for an antecedent (body argument) in a logical rule.
*
* @param contextAtom Atom displayed in the fact window
* @param printableAtoms All arguments of the ground rule that are eligible for
* printing (i.e. unequal to the context atom and the consequent, and not on the ignore list)
* @param renderer Renderer for the atom arguments. Can be null.
* @param negatedConsequent Whether the consequent of the rule is negated in the ruleString
* @param consequent The consequent of the rule
* @return the explanation
*/
public String getExplanationForAntecedent(PrintableAtom contextAtom, List<PrintableAtom> printableAtoms,
ConstantRenderer renderer, boolean negatedConsequent,
PrintableAtom consequent) {
// Intro, then:
// [This antecedent] and [antecedent atom w/ value] determine a minimum/maximum value for [consequent].
// Since [consequent] is only/already [score], [antecedent] cannot be higher than [max_score].
StringBuilder sb = new StringBuilder();
sb.append(getIntroductorySentence()).append(" ");
addNpWithoutUrl(contextAtom, sb, renderer);
int numArgs = printableAtoms.size();
if (numArgs > 0) {
sb.append(" and ");
}
for (int i = 0; i < numArgs; i++) {
addNpWithUrl(printableAtoms, i, sb, renderer);
if (i == numArgs - 2) {
sb.append(" and");
} else if (i < numArgs - 1) {
sb.append(",");
}
sb.append(" ");
}
sb.append("determine");
if (numArgs == 0) {
sb.append("s");
}
sb.append(" a ");
if (negatedConsequent) {
sb.append("maximum");
} else {
sb.append("minimum");
}
sb.append(" value for ");
addNpWithUrl(consequent, sb, renderer);
sb.append(". Since ");
addNpWithoutUrl(consequent, sb, renderer);
if (negatedConsequent) {
sb.append(" already reaches a ");
if (consequent.getPred().verbalizeOnHighLowScale) {
sb.append("similarity");
} else {
sb.append("confidence");
}
sb.append(" level of \\textit{");
if (consequent.canTalk() && consequent.getPred().verbalizeOnHighLowScale) {
sb.append(BeliefScale.verbalizeBeliefAsAdjectiveHigh(consequent.getBelief()));
} else {
sb.append(BeliefScale.verbalizeBeliefAsAdjective(consequent.getBelief()));
}
sb.append("}");
} else {
if (consequent.canTalk() && consequent.getPred().verbalizeOnHighLowScale) {
sb.append("is ");
sb.append(BeliefScale.verbalizeBeliefAsSimilarityWithOnly(consequent.getBelief()));
} else {
BeliefScale.verbalizeBeliefAsPredicateWithOnly(consequent.getBelief());
}
}
sb.append(", the possible value for ");
addNpWithoutUrl(contextAtom, sb, renderer);
sb.append(" is limited.");
return sb.toString();
}
/**
* Generates an explanation for logical rules and for non-equative arithmetic rules.
*
* @param contextAtom Atom displayed in the fact window
* @param contextFound Was the context found amongst the arguments of the ground
* rule? If not, its predicate is closed or it is on the RAG's
* renderer's ignore list.
* @param printableAtoms Arguments of the ground rule, except for the context atom and the consequent
* @param whyNotLower If true: explanation appears in the WHY block, otherwise: in
* the WHY NOT block.
* @param renderer Renderer for the atom arguments. Can be null.
* @param negatedConsequent True if consequent is negated. Only matters if consequent != null.
* @param consequent The (singular) consequent/head of a logical rule. Can be null. Can be identical to the contextAtom.
* @return Unequative explanation
*/
String getUnequativeExplanation(PrintableAtom contextAtom, boolean contextFound, List<PrintableAtom> printableAtoms,
boolean whyNotLower, ConstantRenderer renderer, boolean negatedConsequent,
PrintableAtom consequent) {
if (!contextFound) {
// Context atom is not among given ground atoms (e.g. because it's on the problem's ignore list)
if (consequent != null) {
printableAtoms.add(consequent);
}
return getContextlessExplanation(contextAtom, printableAtoms, renderer);
}
if ((contextAtom.getBelief() > 1 - RuleAtomGraph.DISSATISFACTION_PRECISION && !whyNotLower) ||
(contextAtom.getBelief() < RuleAtomGraph.DISSATISFACTION_PRECISION && whyNotLower)) {
// "Why is this atom with value 1.0 not higher?" or
// "Why is this atom with value 0.0 not lower?"
if (consequent != null) {
printableAtoms.add(consequent);
}
return getExplanationForPolarAtom(printableAtoms, renderer);
}
if (contextAtom.equals(consequent)) {
return getExplanationForConsequent(printableAtoms, renderer, negatedConsequent, consequent);
}
if (consequent == null) {
return getBarebonesExplanation(printableAtoms, whyNotLower, renderer);
}
if ((negatedConsequent && consequent.getBelief() < RuleAtomGraph.DISSATISFACTION_PRECISION) ||
(!negatedConsequent && consequent.getBelief() > 1 - RuleAtomGraph.DISSATISFACTION_PRECISION)) {
// The rule is trivially satisfied.
return getExplanationForTriviallySatisfiedRule(contextAtom, printableAtoms, renderer, negatedConsequent,
consequent);
}
return getExplanationForAntecedent(contextAtom, printableAtoms, renderer, negatedConsequent, consequent);
}
@SuppressWarnings("rawtypes")
protected Map[] getComponents(String groundingName, RuleAtomGraph rag) {
String[] args = getArgs();
// Get arguments of ground rule
List<Tuple> atomToStatus = rag.getLinkedAtomsForGroundingWithLinkStatusAsList(groundingName);
HashMap<String, String> atoms = new HashMap<>();
HashMap<String, Double> beliefValues = new HashMap<>();
HashMap<String, String[]> arguments = new HashMap<>();
for (int i = 0; i < args.length; i++) {
String groundAtom = atomToStatus.get(i).get(0);
String[] predDetails = groundAtom.split("\\(");
String predName = predDetails[0];
atoms.put(predName, groundAtom);
beliefValues.put(predName, rag.getValue(groundAtom));
arguments.put(predName, predDetails[1].substring(0, predDetails[1].length() - 1).split(","));
}
return new HashMap[]{atoms, beliefValues, arguments};
}
@SuppressWarnings("rawtypes")
protected Map[] getDetailedComponentsInclClosed(String groundingName, RuleAtomGraph rag) {
String[] args = getArgs();
// Get arguments of ground rule
List<Tuple> atomToStatus = rag.getLinkedAtomsForGroundingWithLinkStatusAsList(groundingName);
HashMap<String, String> atomsLite = new HashMap<>();
HashMap<String, Double> beliefValues = new HashMap<>();
HashMap<String, Double> beliefValuesLite = new HashMap<>();
HashMap<String, String[]> argumentsLite = new HashMap<>();
for (int i = 0; i < args.length; i++) {
String groundAtom = atomToStatus.get(i).get(0);
beliefValues.put(groundAtom, rag.getValue(groundAtom));
String[] predDetails = groundAtom.split("\\(");
String predName = predDetails[0];
atomsLite.put(predName, groundAtom);
beliefValuesLite.put(predName, rag.getValue(groundAtom));
argumentsLite.put(predName, predDetails[1].substring(0, predDetails[1].length() - 1).split(","));
}
return new HashMap[]{atomsLite, beliefValues, beliefValuesLite, argumentsLite};
}
/**
* This should not contain '\t'!
*/
public abstract String getSerializedParameters();
public static String escapeForURL(String s) {
s = s.replaceAll("(?!\\\\)\\[", "\\\\[");
s = s.replaceAll("(?!\\\\)\\]", "\\\\]");
s = s.replaceAll("(?!\\\\)\\{", "\\\\{");
s = s.replaceAll("(?!\\\\)\\}", "\\\\}");
return s;
}
/**
* Fills the provided list of printable atoms and updates the context atom.
*
* @param atomToStatus The ground atoms and their statuses in the RAG
* @param rag The rule-atom graph
* @param nameToTalkingPredicate The map of predicates to their TalkingPredicate classes
* @param contextAtom The ground atom in the focus of the FactWindow
* @param printableAtoms The to-be-filled list of ground atoms.
* @param statuses The to-be-filled list of atom statuses.
* @return the PrintableAtom instance of the contextAtom
*/
PrintableAtom extractAtoms(List<Tuple> atomToStatus, RuleAtomGraph rag,
Map<String, TalkingPredicate> nameToTalkingPredicate, String contextAtom,
List<PrintableAtom> printableAtoms, List<String> statuses) {
PrintableAtom printableContextAtom = null;
for (Tuple tuple : atomToStatus) {
String atom = tuple.get(0);
if (atom.startsWith(NotEqualPred.SYMBOL)) {
continue;
}
if (atom.contains("=")) {
// Skip (non-)identity checks like (X != Y)
continue;
}
String[] predDetails = StringUtils.split(atom, '(');
if (rag.getIgnoredPredicates().contains(predDetails[0])) {
continue;
}
PrintableAtom talkingAtom = new PrintableAtom(atom, nameToTalkingPredicate.get(predDetails[0]),
predDetails[1].substring(0, predDetails[1].length() - 1).split(",\\s?"), rag.getValue(atom));
if (atom.equals(contextAtom)) {
printableContextAtom = talkingAtom;
continue;
}
printableAtoms.add(talkingAtom);
statuses.add(tuple.get(1));
}
return printableContextAtom;
}
}