-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacci.java
More file actions
140 lines (119 loc) · 5.2 KB
/
Fibonacci.java
File metadata and controls
140 lines (119 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package javachr.examples;
import javachr.constraints.Constraint;
import javachr.RuleApplicator;
import javachr.SimpleRuleApplicator;
import javachr.rules.Propagation;
import javachr.rules.Rule;
import javachr.rules.Simpagation;
import java.util.List;
import static javachr.examples.ExampleFactory.printDurationAndResult;
import static javachr.rules.head.Head.ofType;
public class Fibonacci {
public static class Fib {
final int a;
final long b;
Fib(int a, long b){
this.a = a;
this.b = b;
}
@Override
public String toString(){
return "fib(" + a + ", " + b + ")";
}
@Override
public boolean equals(Object obj){
return obj instanceof Fib && ((Fib) obj).a == this.a && ((Fib) obj).b == this.b;
}
}
public static void main(String[] args) {
RuleApplicator fibHandler = new SimpleRuleApplicator(getRules());
// fibHandler.setTracer(new CommandLineTracer());
System.out.println("Fibonacci 42:");
long start = System.nanoTime();
List<Constraint<?>> result = fibHandler.execute(42);
long end = System.nanoTime();
printDurationAndResult(start, end, result);
}
static Rule[] getRules(){
// MAX => fib(0, 1), fib(1, 1).
Rule r1 = new Propagation(ofType(Integer.class))
// .guard(x -> {}) // Not necessary
.body((head, newConstraints) -> {
newConstraints.add(new Fib(0, 0));
newConstraints.add(new Fib(1, 1));
});
// MAX, fib(N1, M1), fib(N2, M2) => N1 == N2 - 1 | fib(N2 + 1, M1 + M2).
Rule r2 = new Propagation(ofType(Integer.class), ofType(Fib.class), ofType(Fib.class))
.guard(head ->
((Fib) head[1].get()).a == ((Fib) head[2].get()).a - 1
&& ((Fib) head[2].get()).a < (int) head[0].get()
)
.body((head, newConstraints) -> {
int N2 = ((Fib) head[2].get()).a;
long M1 = ((Fib) head[1].get()).b;
long M2 = ((Fib) head[2].get()).b;
newConstraints.add(new Fib(N2+1, M1 + M2));
});
// fib(N1, M1), MAX <=> N1 == MAX | fib(N1, M1).
Rule r3 = new Simpagation(1, ofType(Fib.class), ofType(Integer.class))
.guard((h1, h2) -> h2[0].get().equals(((Fib) h1[0].get()).a));
return new Rule[]{r1, r2, r3};
}
public static Rule[] getRules2(){
// MAX => fib(0, 1), fib(1, 1).
Rule r1 = new Propagation(Integer.class)
// .guard(head -> {}) // Not necessary
.body((head, newConstraints) -> {
newConstraints.add(new Fib(0, 0));
newConstraints.add(new Fib(1, 1));
});
// MAX, fib(N1, M1), fib(N2, M2) => N1 == N2 - 1 | fib(N2 + 1, M1 + M2).
Rule r2 = new Propagation(Integer.class, Fib.class, Fib.class)
.guard(head ->
((Fib) head[1].get()).a == ((Fib) head[2].get()).a - 1
&& ((Fib) head[2].get()).a < (int) head[0].get()
)
.body((head, newConstraints) -> {
int N2 = ((Fib) head[2].get()).a;
long M1 = ((Fib) head[1].get()).b;
long M2 = ((Fib) head[2].get()).b;
newConstraints.add(new Fib(N2+1, M1 + M2));
});
Rule r3 = new Simpagation(1, Fib.class, Integer.class)
.guard((h1, h2) ->
h2[0].get().equals(((Fib) h1[0].get()).a)
);
return new Rule[]{r1, r2, r3};
}
public static Rule[] getRules3(){
// MAX => fib(0, 1), fib(1, 1).
Rule r1 = new Propagation(1)
.guard(head -> head[0].get() instanceof Integer)
.body((head, newConstraints) -> {
newConstraints.add(new Fib(0, 0));
newConstraints.add(new Fib(1, 1));
});
// MAX, fib(N1, M1), fib(N2, M2) => N1 == N2 - 1 | fib(N2 + 1, M1 + M2).
Rule r2 = new Propagation(3)
.guard(head ->
head[0].isOfType(Integer.class)
&& head[1].isOfType(Fib.class)
&& head[2].isOfType(Fib.class)
&& ((Fib) head[1].get()).a == ((Fib) head[2].get()).a-1
&& ((Fib) head[2].get()).a < (int) head[0].get()
)
.body((head, newConstraints) -> {
int N2 = ((Fib) head[2].get()).a;
long M1 = ((Fib) head[1].get()).b;
long M2 = ((Fib) head[2].get()).b;
newConstraints.add(new Fib(N2+1, M1 + M2));
});
Rule r3 = new Simpagation(1, 1)
.guard((h1, h2) ->
h1[0].isOfType(Fib.class)
&& h2[0].isOfType(Integer.class)
&& h2[0].get().equals(((Fib) h1[0].get()).a)
);
return new Rule[]{r1, r2, r3};
}
}