-
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: Revert to single-predicate module input
- Loading branch information
1 parent
761331c
commit 60b819f
Showing
7 changed files
with
103 additions
and
69 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
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
73 changes: 73 additions & 0 deletions
73
...a-core/src/main/java/at/ac/tuwien/kr/alpha/core/programs/transformation/ModuleLinker.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,73 @@ | ||
package at.ac.tuwien.kr.alpha.core.programs.transformation; | ||
|
||
import at.ac.tuwien.kr.alpha.api.Alpha; | ||
import at.ac.tuwien.kr.alpha.api.programs.NormalProgram; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom; | ||
import at.ac.tuwien.kr.alpha.api.programs.atoms.ModuleAtom; | ||
import at.ac.tuwien.kr.alpha.api.programs.literals.Literal; | ||
import at.ac.tuwien.kr.alpha.api.programs.literals.ModuleLiteral; | ||
import at.ac.tuwien.kr.alpha.api.programs.modules.Module; | ||
import at.ac.tuwien.kr.alpha.api.programs.rules.NormalRule; | ||
import at.ac.tuwien.kr.alpha.api.programs.rules.heads.NormalHead; | ||
import at.ac.tuwien.kr.alpha.commons.programs.rules.Rules; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Program transformation that translates {@link at.ac.tuwien.kr.alpha.api.programs.literals.ModuleLiteral}s into | ||
* {@link at.ac.tuwien.kr.alpha.api.programs.literals.ExternalLiteral}s by constructing {@link at.ac.tuwien.kr.alpha.api.programs.atoms.ExternalAtom}s | ||
* which solve the ASP implementation of the module with the given inputs. | ||
*/ | ||
public class ModuleLinker extends ProgramTransformation<NormalProgram, NormalProgram> { | ||
|
||
// Note: References to a standard library of modules that are always available for linking should be member variables of a linker. | ||
|
||
private final Alpha moduleRunner; | ||
|
||
public ModuleLinker(Alpha moduleRunner) { | ||
this.moduleRunner = moduleRunner; | ||
} | ||
|
||
|
||
@Override | ||
public NormalProgram apply(NormalProgram inputProgram) { | ||
Map<String, Module> moduleTable = inputProgram.getModules().stream().collect(Collectors.toMap(Module::getName, Function.identity())); | ||
List<NormalRule> transformedRules = inputProgram.getRules().stream() | ||
.map(rule -> containsModuleAtom(rule) ? linkModuleAtoms(rule, moduleTable) : rule) | ||
.collect(Collectors.toList()); | ||
return null; | ||
} | ||
|
||
private NormalRule linkModuleAtoms(NormalRule rule, Map<String, Module> moduleTable) { | ||
NormalHead newHead = rule.getHead(); | ||
Set<Literal> newBody = rule.getBody().stream() | ||
.map(literal -> { | ||
if (literal instanceof ModuleLiteral) { | ||
ModuleLiteral moduleLiteral = (ModuleLiteral) literal; | ||
return translateModuleAtom(moduleLiteral.getAtom(), moduleTable).toLiteral(!moduleLiteral.isNegated()); | ||
} else { | ||
return literal; | ||
} | ||
}) | ||
.collect(Collectors.toSet()); | ||
return Rules.newNormalRule(newHead, newBody); | ||
} | ||
|
||
private ExternalAtom translateModuleAtom(ModuleAtom moduleAtom, Map<String, Module> moduleTable) { | ||
if (!moduleTable.containsKey(moduleAtom.getModuleName())) { | ||
throw new IllegalArgumentException("Module " + moduleAtom.getModuleName() + " not found in module table."); | ||
} | ||
Module implementationModule = moduleTable.get(moduleAtom.getModuleName()); | ||
//implementationModule. | ||
return null; | ||
} | ||
|
||
private static boolean containsModuleAtom(NormalRule rule) { | ||
return rule.getBody().stream().anyMatch(literal -> literal instanceof ModuleLiteral); | ||
} | ||
|
||
} |
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