-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCnf.java
More file actions
94 lines (91 loc) · 2.35 KB
/
Cnf.java
File metadata and controls
94 lines (91 loc) · 2.35 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
class Literal {
private AtomicFormula atom;
private boolean neg;
Literal(AtomicFormula atom) {
this.atom = atom;
this.neg = false;
}
Literal(AtomicFormula atom, boolean neg) {
this.atom = atom;
this.neg = neg;
}
public AtomicFormula atom() {
return atom;
}
public boolean neg() {
return neg;
}
public static Literal Lit(AtomicFormula atom) {
return new Literal(atom);
}
public static Literal Not(AtomicFormula atom) {
return new Literal(atom, true);
}
public static Literal Not(Literal lit) {
return new Literal(lit.atom(), !lit.neg());
}
public boolean isTrue(Structure m) {
return neg ^ atom.isTrue(m);
}
public String toString() {
return (neg() ? "-" : "") + atom();
}
}
class Clause extends ArrayList<Literal> {
Clause(Literal... lits) {
super(Arrays.asList(lits));
}
Clause(Collection<? extends Literal> lits) {
super(lits);
}
public boolean isTrue(Structure m) {
for (Literal lit : this)
if (lit.isTrue(m))
return true;
return false;
}
public Set<AtomicFormula> atoms() {
Set<AtomicFormula> vs = new HashSet<AtomicFormula>();
for (Literal lit : this)
vs.add(lit.atom());
return vs;
}
public String toString() {
return stream()
.map( l -> l.toString() )
.collect(Collectors.joining(" "))
;
}
}
class Cnf extends ArrayList<Clause> {
Cnf(Clause... cls) {
super(Arrays.asList(cls));
}
Cnf(Collection<? extends Clause> cls) {
super(cls);
}
public boolean isTrue(Structure m) {
for(Clause c : this)
if (!c.isTrue(m))
return false;
return true;
}
public Set<AtomicFormula> atoms() {
Set<AtomicFormula> vs = new HashSet<AtomicFormula>();
for (Clause cls : this)
vs.addAll(cls.atoms());
return vs;
}
public String toString() {
return stream()
.map( cls -> cls.toString() + "\n" )
.collect(Collectors.joining(""))
;
}
}