Skip to content

Commit 29734f7

Browse files
committed
addl choice nogoods (work in progress)
1 parent 1fbbbf0 commit 29734f7

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2020, the Alpha Team.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1) Redistributions of source code must retain the above copyright notice, this
9+
* list of conditions and the following disclaimer.
10+
*
11+
* 2) Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package at.ac.tuwien.kr.alpha.common;
27+
28+
public class NegativeChoicePredicate extends Predicate {
29+
30+
private final static String PREDICATE_NEGATION_PREFIX = "_n";
31+
32+
private final Predicate originalChoicePredicate;
33+
34+
protected NegativeChoicePredicate(Predicate originalChoicePredicate) {
35+
super(PREDICATE_NEGATION_PREFIX + originalChoicePredicate.getName(), originalChoicePredicate.getArity() + 1, true, false);
36+
this.originalChoicePredicate = originalChoicePredicate;
37+
}
38+
39+
public static NegativeChoicePredicate getInstance(Predicate choicePredicate) {
40+
return new NegativeChoicePredicate(choicePredicate);
41+
}
42+
43+
public Predicate getOriginalChoicePredicate() {
44+
return originalChoicePredicate;
45+
}
46+
}

src/main/java/at/ac/tuwien/kr/alpha/grounder/NoGoodGenerator.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/**
2-
* Copyright (c) 2017-2018, the Alpha Team.
1+
/*
2+
* Copyright (c) 2017-2020, the Alpha Team.
33
* All rights reserved.
44
*
55
* Additional changes made by Siemens.
@@ -72,7 +72,7 @@ public class NoGoodGenerator {
7272

7373
/**
7474
* Generates all NoGoods resulting from a non-ground rule and a variable substitution.
75-
*
75+
*
7676
* @param nonGroundRule
7777
* the non-ground rule.
7878
* @param substitution
@@ -97,7 +97,7 @@ List<NoGood> generateNoGoodsFromGroundSubstitution(final InternalRule nonGroundR
9797

9898
final Atom groundHeadAtom = nonGroundRule.getHeadAtom().substitute(substitution);
9999
final int headId = atomStore.putIfAbsent(groundHeadAtom);
100-
100+
101101
// Prepare atom representing the rule body.
102102
final RuleAtom bodyAtom = new RuleAtom(nonGroundRule, substitution);
103103

@@ -113,7 +113,7 @@ List<NoGood> generateNoGoodsFromGroundSubstitution(final InternalRule nonGroundR
113113
final int headLiteral = atomToLiteral(atomStore.putIfAbsent(nonGroundRule.getHeadAtom().substitute(substitution)));
114114

115115
choiceRecorder.addHeadToBody(headId, atomOf(bodyRepresentingLiteral));
116-
116+
117117
// Create a nogood for the head.
118118
result.add(NoGood.headFirst(negateLiteral(headLiteral), bodyRepresentingLiteral));
119119

@@ -135,6 +135,16 @@ List<NoGood> generateNoGoodsFromGroundSubstitution(final InternalRule nonGroundR
135135
result.addAll(choiceRecorder.generateChoiceNoGoods(posLiterals, negLiterals, bodyRepresentingLiteral));
136136
}
137137

138+
// If this is a choice rule, add nogood that prevents both positive and negative head to be true at the same time.
139+
if (groundHeadAtom.getPredicate() instanceof NegativeChoicePredicate) {
140+
// TODO: clean up
141+
// TODO: should we find the original atom instead of constructing a new one?
142+
final List<Term> terms = groundHeadAtom.getTerms().subList(1, groundHeadAtom.getTerms().size());
143+
BasicAtom complement = new BasicAtom(((NegativeChoicePredicate) groundHeadAtom.getPredicate()).getOriginalChoicePredicate(), terms);
144+
final int complementId = atomStore.putIfAbsent(complement);
145+
result.add(new NoGood(atomToLiteral(complementId), headLiteral));
146+
}
147+
138148
return result;
139149
}
140150

src/main/java/at/ac/tuwien/kr/alpha/grounder/transformation/ChoiceHeadToNormal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
package at.ac.tuwien.kr.alpha.grounder.transformation;
2929

30+
import at.ac.tuwien.kr.alpha.common.NegativeChoicePredicate;
3031
import at.ac.tuwien.kr.alpha.common.Predicate;
3132
import at.ac.tuwien.kr.alpha.common.atoms.Atom;
3233
import at.ac.tuwien.kr.alpha.common.atoms.BasicAtom;
@@ -48,7 +49,6 @@
4849
* Copyright (c) 2017-2020, the Alpha Team.
4950
*/
5051
public class ChoiceHeadToNormal extends ProgramTransformation<InputProgram, InputProgram> {
51-
private final static String PREDICATE_NEGATION_PREFIX = "_n";
5252

5353
@Override
5454
public InputProgram apply(InputProgram inputProgram) {
@@ -91,7 +91,7 @@ public InputProgram apply(InputProgram inputProgram) {
9191
// Construct head atom for the choice.
9292
Predicate headPredicate = head.getPredicate();
9393

94-
Predicate negPredicate = Predicate.getInstance(PREDICATE_NEGATION_PREFIX + headPredicate.getName(), headPredicate.getArity() + 1, true);
94+
Predicate negPredicate = NegativeChoicePredicate.getInstance(headPredicate);
9595
List<Term> headTerms = new ArrayList<>(head.getTerms());
9696
headTerms.add(0, ConstantTerm.getInstance("1")); // FIXME: when introducing classical negation, this is 1 for classical positive atoms and 0 for
9797
// classical negative atoms.

0 commit comments

Comments
 (0)