-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnifier.java
More file actions
158 lines (118 loc) · 4.47 KB
/
Copy pathUnifier.java
File metadata and controls
158 lines (118 loc) · 4.47 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import java.util.ArrayList;
/**
* unifier
*/
public class Unifier {
public Unifier() {
}
/**
* implementation of unifyVar algorithm
* theta is the unifier so far
* @param b the variable
* @param j the expression(term)
*/
private ArrayList<Subst> unifyVar(Term b, Term j, ArrayList<Subst> theta) {
if(!b.isVar()) {
System.out.println("Can't unify a non variable.");
return null;
}
//if {b/value} E theta then return unify(value, j, theta)
for(int i = 0; i < theta.size(); i++) {
if(theta.get(i).getVar().equals(b)) {
return unify(theta.get(i).getTerm(), j, theta);
}
//else if {j/value} E theta then return unify(b, value, theta)
if(theta.get(i).getVar().equals(j)) {
return unify(b, theta.get(i).getTerm(), theta);
}
}
// if occur-check(j,b) return null(failure)
if(j.occurCheck(b)) {
return null;
}
// add {b/j} to theta
theta.add(new Subst(b, j));
return theta;
}
public ArrayList<Subst> unify(Object z, Object j, ArrayList<Subst> theta) {
//failure
if(theta == null)
return null;
//if z = j then return theta
if(z.equals(j))
return theta;
//if variable(z) then return unify-var(z, j, theta)
if(z instanceof Term && ((Term) z).isVar()) {
return unifyVar((Term)z,(Term)j, theta);
}
//if variable(j) then return unify-var(j, z, theta)
if(j instanceof Term && ((Term) j).isVar()) {
return unifyVar((Term)j,(Term)z, theta);
}
//if compound(z) and compound?(j)then return unify(args(z), args(j), unify(op(z), op(j), theta))
// j and z are each either atomic statement or a functional symbol
if(z instanceof Atomic || (z instanceof Term && ((Term)z).isFuncSymb())) {
if(j instanceof Atomic || (j instanceof Term && ((Term)j).isFuncSymb())) {
if(z instanceof Atomic) {
if(j instanceof Atomic) {
// both atomic
// if z and j have different names they can't be unified
if(!((Atomic)z).getName().equals(((Atomic)j).getName()))
return null;
//return unify(args(z), args(j), unify(op(z), op(j), theta))
return unify(((Atomic)z).args(), ((Atomic)j).args(), unify(((Atomic)z).ops(), ((Atomic)j).ops(), theta));
}
else {
// z atomic and j term
// if z and j have different names they can't be unified
if(!((Atomic)z).getName().equals(((Term)j).getName()))
return null;
//return unify(args(z), args(j), unify(op(z), op(j), theta))
return unify(((Atomic)z).args(), ((Term)j).args(), unify(((Atomic)z).ops(), ((Term)j).ops(), theta));
}
}
else {
if(j instanceof Atomic) {
// z term and j atomic
// if z and j have different names they can't be unified
if(!((Term)z).getName().equals(((Atomic)j).getName()))
return null;
//return unify(args(z), args(j), unify(op(z), op(j), theta))
return unify(((Term)z).args(), ((Atomic)j).args(), unify(((Term)z).ops(), ((Atomic)j).ops(), theta));
}
else {
// both term
// if z and j have different names they can't be unified
if(!((Term)z).getName().equals(((Term)j).getName()))
return null;
//return unify(args(z), args(j), unify(op(z), op(j), theta))
return unify(((Term)z).args(), ((Term)j).args(), unify(((Term)z).ops(), ((Term)j).ops(), theta));
}
}
} // end if j
} // end if z
//if list(z) and list(j) the return unify(rest(z), rest(j), unify(first(z), first(j), theta))
if(z instanceof ArrayList<?> && j instanceof ArrayList<?>) {
// create copies of lists
ArrayList<?> listz = new ArrayList<>((ArrayList<?>)z);
ArrayList<?> listj = new ArrayList<>((ArrayList<?>)j);
//if the lists are of different size can't be unified
// return failure
if(listz.size() != listj.size())
return null;
if(!((ArrayList<?>)z).isEmpty() && !((ArrayList<?>)j).isEmpty()) {
//Object firstz = ((ArrayList<?>)z).remove(0);
Object firstz = listz.remove(0);
//Object firstj = ((ArrayList<?>)j).remove(0);
Object firstj = listj.remove(0);
// System.out.println("Unifiy ---");
// System.out.println(firstz);
// System.out.println(firstj);
// System.out.println("Unifiy ---");
return unify(listz, listj, unify(firstz, firstj, theta));
}
}
//return failure
return null;
}
}