Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CspListener is now a function and can be implemented with lambda expr…
…essions.
- Loading branch information
1 parent
39ba97c
commit 7323532
Showing
11 changed files
with
108 additions
and
179 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
49 changes: 42 additions & 7 deletions
49
aima-core/src/main/java/aima/core/search/csp/CspListener.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,15 +1,50 @@ | |||
package aima.core.search.csp; | package aima.core.search.csp; | ||
|
|
||
import aima.core.search.framework.Metrics; | |||
|
|||
/** | /** | ||
* Interface which allows interested clients to register at a solution strategy | * Interface which allows interested clients to register at a CSP solver | ||
* and follow their progress step by step. | * and follow its progress step by step. | ||
* | * | ||
* @author Ruediger Lunde | * @author Ruediger Lunde | ||
*/ | */ | ||
public interface CspListener<VAR extends Variable, VAL> { | public interface CspListener<VAR extends Variable, VAL> { | ||
/** Informs about changed assignments. */ | /** | ||
void stateChanged(Assignment<VAR, VAL> assignment, CSP<VAR, VAL> csp); | * Informs about changed assignments. | ||
* | |||
* @param csp a CSP, possibly changed by an inference step. | |||
* @param assignment a new assignment or null if the last processing step was an inference step. | |||
*/ | |||
void stateChanged(CSP<VAR, VAL> csp, Assignment<VAR, VAL> assignment); | |||
|
|||
/** | |||
* A simple CSP listener implementation which counts assignment changes and changes caused by | |||
* inference steps and provides some metrics. | |||
* @author Ruediger Lunde | |||
*/ | |||
class StepCounter<VAR extends Variable, VAL> implements CspListener<VAR, VAL> { | |||
private int assignmentCount = 0; | |||
private int inferenceCount = 0; | |||
|
|||
@Override | |||
public void stateChanged(CSP<VAR, VAL> csp, Assignment<VAR, VAL> assignment) { | |||
if (assignment != null) | |||
++assignmentCount; | |||
else | |||
++inferenceCount; | |||
} | |||
|
|||
public void reset() { | |||
assignmentCount = 0; | |||
inferenceCount = 0; | |||
} | |||
|
|
||
/** Informs about changed domains (inferences). */ | public Metrics getResults() { | ||
void stateChanged(CSP<VAR, VAL> csp); | Metrics result = new Metrics(); | ||
result.set("assignmentCount", assignmentCount); | |||
if (inferenceCount != 0) | |||
result.set("inferenceCount", inferenceCount); | |||
return result; | |||
} | |||
} | |||
} | } |
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
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
Oops, something went wrong.