|
1 | | -package aima.gui.swing.demo.search.csp; |
2 | | - |
3 | | -import aima.core.agent.Action; |
4 | | -import aima.core.agent.Agent; |
5 | | -import aima.core.agent.Percept; |
6 | | -import aima.core.agent.impl.AbstractEnvironment; |
7 | | -import aima.core.search.csp.Assignment; |
8 | | -import aima.core.search.csp.CSP; |
9 | | - |
10 | | -/** |
11 | | - * Simple environment which maintains a CSP and an assignment. The state |
12 | | - * is modified by executing {@link StateChangeAction}s. |
13 | | - * @author Ruediger Lunde |
14 | | - */ |
15 | | -public class CSPEnvironment extends AbstractEnvironment { |
16 | | - CSP csp; |
17 | | - Assignment assignment; |
18 | | - |
19 | | - public void init(CSP csp) { |
20 | | - this.csp = csp; |
21 | | - assignment = null; |
22 | | - } |
23 | | - |
24 | | - public CSP getCSP() { |
25 | | - return csp; |
26 | | - } |
27 | | - |
28 | | - public Assignment getAssignment() { |
29 | | - return assignment; |
30 | | - } |
31 | | - |
32 | | - /** Executes the provided action and returns null. */ |
33 | | - @Override |
34 | | - public void executeAction(Agent agent, Action action) { |
35 | | - if (action instanceof StateChangeAction) { |
36 | | - StateChangeAction a = (StateChangeAction) action; |
37 | | - if (a.updateCSP()) |
38 | | - csp = a.getCSP(); |
39 | | - if (a.updateAssignment()) |
40 | | - assignment = a.getAssignment(); |
41 | | - if (agent == null) |
42 | | - updateEnvironmentViewsAgentActed(agent, action); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - /** Returns null. */ |
47 | | - @Override |
48 | | - public Percept getPerceptSeenBy(Agent anAgent) { |
49 | | - return null; |
50 | | - } |
51 | | - |
52 | | - /** Action to modify the CSP environment state. */ |
53 | | - public static class StateChangeAction implements Action { |
54 | | - private CSP csp; |
55 | | - private Assignment assignment; |
56 | | - |
57 | | - /** Update the domains of the CSP. */ |
58 | | - public StateChangeAction(CSP csp) { |
59 | | - this.csp = csp; |
60 | | - } |
61 | | - |
62 | | - /** Update the current assignment. */ |
63 | | - public StateChangeAction(Assignment assignment, CSP csp) { |
64 | | - this.csp = csp; |
65 | | - this.assignment = assignment; |
66 | | - } |
67 | | - |
68 | | - public boolean updateCSP() { |
69 | | - return csp != null; |
70 | | - } |
71 | | - |
72 | | - public CSP getCSP() { |
73 | | - return csp; |
74 | | - } |
75 | | - |
76 | | - public boolean updateAssignment() { |
77 | | - return assignment != null; |
78 | | - } |
79 | | - |
80 | | - public Assignment getAssignment() { |
81 | | - return assignment; |
82 | | - } |
83 | | - |
84 | | - @Override |
85 | | - public boolean isNoOp() { |
86 | | - return false; |
87 | | - } |
88 | | - |
89 | | - public String toString() { |
90 | | - return "State Change " |
91 | | - + (updateAssignment() ? assignment : "(Domain Reduction)"); |
92 | | - } |
93 | | - } |
94 | | -} |
| 1 | +package aima.gui.swing.demo.search.csp; |
| 2 | + |
| 3 | +import aima.core.agent.Action; |
| 4 | +import aima.core.agent.Agent; |
| 5 | +import aima.core.agent.Percept; |
| 6 | +import aima.core.agent.impl.AbstractEnvironment; |
| 7 | +import aima.core.search.csp.Assignment; |
| 8 | +import aima.core.search.csp.CSP; |
| 9 | + |
| 10 | +/** |
| 11 | + * Simple environment which maintains a CSP and an assignment. The state |
| 12 | + * is modified by executing {@link StateChangeAction}s. |
| 13 | + * @author Ruediger Lunde |
| 14 | + */ |
| 15 | +public class CSPEnvironment extends AbstractEnvironment { |
| 16 | + CSP csp; |
| 17 | + Assignment assignment; |
| 18 | + |
| 19 | + public void init(CSP csp) { |
| 20 | + this.csp = csp; |
| 21 | + assignment = null; |
| 22 | + } |
| 23 | + |
| 24 | + public CSP getCSP() { |
| 25 | + return csp; |
| 26 | + } |
| 27 | + |
| 28 | + public Assignment getAssignment() { |
| 29 | + return assignment; |
| 30 | + } |
| 31 | + |
| 32 | + /** Executes the provided action and returns null. */ |
| 33 | + @Override |
| 34 | + public void executeAction(Agent agent, Action action) { |
| 35 | + if (action instanceof StateChangeAction) { |
| 36 | + StateChangeAction a = (StateChangeAction) action; |
| 37 | + if (a.updateCSP()) |
| 38 | + csp = a.getCSP(); |
| 39 | + if (a.updateAssignment()) |
| 40 | + assignment = a.getAssignment(); |
| 41 | + if (agent == null) |
| 42 | + updateEnvironmentViewsAgentActed(agent, action); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** Returns null. */ |
| 47 | + @Override |
| 48 | + public Percept getPerceptSeenBy(Agent anAgent) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + /** Action to modify the CSP environment state. */ |
| 53 | + public static class StateChangeAction implements Action { |
| 54 | + private CSP csp; |
| 55 | + private Assignment assignment; |
| 56 | + |
| 57 | + /** Update the domains of the CSP. */ |
| 58 | + public StateChangeAction(CSP csp) { |
| 59 | + this.csp = csp; |
| 60 | + } |
| 61 | + |
| 62 | + /** Update the current assignment. */ |
| 63 | + public StateChangeAction(Assignment assignment, CSP csp) { |
| 64 | + this.csp = csp; |
| 65 | + this.assignment = assignment; |
| 66 | + } |
| 67 | + |
| 68 | + public boolean updateCSP() { |
| 69 | + return csp != null; |
| 70 | + } |
| 71 | + |
| 72 | + public CSP getCSP() { |
| 73 | + return csp; |
| 74 | + } |
| 75 | + |
| 76 | + public boolean updateAssignment() { |
| 77 | + return assignment != null; |
| 78 | + } |
| 79 | + |
| 80 | + public Assignment getAssignment() { |
| 81 | + return assignment; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean isNoOp() { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + public String toString() { |
| 90 | + return "State Change " |
| 91 | + + (updateAssignment() ? assignment : "(Domain Reduction)"); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments