-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evolog Modules: aggregate terms into list using aggregate literal
- Loading branch information
1 parent
d1f349e
commit 3540762
Showing
7 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...a/at/ac/tuwien/kr/alpha/core/programs/transformation/aggregates/encoders/ListEncoder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.encoders; | ||
|
||
import at.ac.tuwien.kr.alpha.api.programs.InputProgram; | ||
import at.ac.tuwien.kr.alpha.api.programs.Predicate; | ||
import at.ac.tuwien.kr.alpha.api.programs.ProgramParser; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.AggregateAtom; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.BasicAtom; | ||
import at.ac.tuwien.kr.alpha.api.programs.terms.Term; | ||
import at.ac.tuwien.kr.alpha.commons.Predicates; | ||
import at.ac.tuwien.kr.alpha.commons.comparisons.ComparisonOperators; | ||
import at.ac.tuwien.kr.alpha.commons.programs.atoms.Atoms; | ||
import at.ac.tuwien.kr.alpha.commons.util.Util; | ||
import at.ac.tuwien.kr.alpha.core.programs.transformation.aggregates.AggregateRewritingContext; | ||
import org.stringtemplate.v4.ST; | ||
|
||
import java.util.Set; | ||
|
||
public class ListEncoder extends AbstractAggregateEncoder { | ||
|
||
private static final ST LIST_AGGREGATION = Util.aspStringTemplate( | ||
// First, establish ordering of elements (which we need to establish the order within the list) | ||
"$id$_element_greater(ARGS, N, K) :- $id$_element(ARGS, N), $id$_element(ARGS, K), N > K. " + | ||
"$id$_element_not_successor(ARGS, N, K) :- $id$_element_greater(ARGS, N, I), $id$_element_greater(ARGS, I, K). " + | ||
"$id$_element_successor(ARGS, N, K) :- $id$_element_greater(ARGS, N, K), not $id$_element_not_successor(ARGS, N, K). " + | ||
"$id$_element_has_successor(ARGS, N) :- $id$_element_successor(ARGS, _, N). " + | ||
// Now build the list as a recursively nested function term | ||
"$id$_lst_element(ARGS, IDX, lst(N, lst_empty)) :- $id$_element(ARGS, N), not $id$_element_has_successor(ARGS, N), IDX = 0. " + | ||
"$id$_lst_element(ARGS, IDX, lst(N, lst(K, TAIL))) :- $id$_element(ARGS, N), $id$_element_successor(ARGS, K, N), $id$_lst_element(ARGS, PREV_IDX, lst(K, TAIL)), IDX = PREV_IDX + 1. " + | ||
"has_next_$id$_element(ARGS, IDX) :- $id$_lst_element(ARGS, IDX, _), NEXT_IDX = IDX + 1, $id$_lst_element(ARGS, NEXT_IDX, _). " + | ||
"$aggregate_result$(ARGS, LIST) :- $id$_lst_element(ARGS, IDX, LIST), not has_next_$id$_element(ARGS, IDX)."); | ||
|
||
private final ProgramParser parser; | ||
|
||
protected ListEncoder(ProgramParser parser) { | ||
super(AggregateAtom.AggregateFunctionSymbol.LIST, Set.of(ComparisonOperators.EQ)); | ||
this.parser = parser; | ||
} | ||
|
||
@Override | ||
protected InputProgram encodeAggregateResult(AggregateRewritingContext.AggregateInfo aggregateToEncode) { | ||
ST encodingTemplate = new ST(LIST_AGGREGATION); | ||
encodingTemplate.add("id", aggregateToEncode.getId()); | ||
encodingTemplate.add("aggregate_result", aggregateToEncode.getOutputAtom().getPredicate().getName()); | ||
return parser.parse(encodingTemplate.render()); | ||
} | ||
|
||
@Override | ||
protected BasicAtom buildElementRuleHead(String aggregateId, AggregateAtom.AggregateElement element, Term aggregateArguments) { | ||
Predicate headPredicate = Predicates.getPredicate(this.getElementTuplePredicateSymbol(aggregateId), 2); | ||
if (element.getElementTerms().size() != 1) { | ||
throw new IllegalArgumentException("List elements may only consist of one term."); | ||
} | ||
Term value = element.getElementTerms().get(0); | ||
return Atoms.newBasicAtom(headPredicate, aggregateArguments, value); | ||
} | ||
|
||
@Override | ||
protected String getElementTuplePredicateSymbol(String aggregateId) { | ||
return aggregateId + "_element"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
alpha-solver/src/test/resources/e2e-tests/neighboring-vertices-list.evl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
% Graph is undirected | ||
edge(Y, X) :- edge(X, Y). | ||
|
||
%% Generate list of neighboring vertices for each vertex in the graph | ||
neighbor(V, N) :- vertex(V), vertex(N), edge(V, N). | ||
neighbors(V, LST) :- vertex(V), LST = #list{ N : neighbor(V, N)}. | ||
|
||
#test lineGraph(expect: 1) { | ||
given { | ||
vertex(1). vertex(2). edge(1, 2). | ||
} | ||
assertForAll { | ||
:- not neighbors(1, lst(2, lst_empty)). | ||
:- not neighbors(2, lst(1, lst_empty)). | ||
} | ||
} | ||
|
||
#test network(expect: 1) { | ||
given { | ||
vertex(1..8). | ||
|
||
edge(1, 2). | ||
edge(1, 3). | ||
edge(1, 4). | ||
edge(2, 5). | ||
edge(1, 3). | ||
edge(1, 4). | ||
edge(2, 5). | ||
edge(3, 6). | ||
edge(4, 7). | ||
edge(5, 8). | ||
edge(6, 8). | ||
edge(7, 8). | ||
} | ||
assertForAll { | ||
:- not neighbors(1, lst(2, lst(3, lst(4, lst_empty)))). | ||
:- not neighbors(2, lst(1, lst(5, lst_empty))). | ||
:- not neighbors(3, lst(1, lst(6, lst_empty))). | ||
:- not neighbors(4, lst(1, lst(7, lst_empty))). | ||
:- not neighbors(5, lst(2, lst(8, lst_empty))). | ||
:- not neighbors(6, lst(3, lst(8, lst_empty))). | ||
:- not neighbors(7, lst(4, lst(8, lst_empty))). | ||
:- not neighbors(8, lst(5, lst(6, lst(7, lst_empty)))). | ||
} | ||
} |