1 | package tuffy.mln; |
2 | import java.util.*; |
3 | |
4 | import tuffy.util.StringMan; |
5 | |
6 | |
7 | /** |
8 | * An atomic formula. It's designed as a light-weight construct, hence |
9 | * all fields are transparent (public). |
10 | */ |
11 | public class Atom { |
12 | |
13 | /** |
14 | * Enumerated type of Atoms. 1) NONE; 2) EVIDENCE: atom in evidence; |
15 | * 3) QUERY: atom in query; 4) QUEVID: atom in query as evidence. |
16 | */ |
17 | public static enum AtomType {EVIDENCE, QUERY, NONE, QUEVID}; |
18 | |
19 | /** |
20 | * The predicate of this Atom. |
21 | */ |
22 | |
23 | public Predicate pred; |
24 | /** |
25 | * The argument list represented as a tuple of integers: |
26 | * constant as positive number and variable as negative number. |
27 | */ |
28 | public Tuple args = null; |
29 | |
30 | public ArrayList<String> sargs = null; |
31 | |
32 | /** |
33 | * Truth value of this atom. |
34 | */ |
35 | public Boolean truth = null; |
36 | |
37 | /** |
38 | * Probability of "soft evidence". |
39 | */ |
40 | public Double prior = null; |
41 | |
42 | /** |
43 | * Type of this atom. Values are enumerated by {@link AtomType}. |
44 | */ |
45 | public AtomType type = AtomType.NONE; |
46 | |
47 | /** |
48 | * Map the {@link AtomType} value of this atom into an integer, |
49 | * which is used internally by the DB. |
50 | */ |
51 | public int club() { |
52 | switch(type) { |
53 | case NONE: return 0; |
54 | case QUERY: return 1; |
55 | case EVIDENCE: return 2; |
56 | case QUEVID: return 3; // evidence in query |
57 | default: return 0; |
58 | } |
59 | } |
60 | |
61 | /** |
62 | * Test if this atom is soft evidence. |
63 | */ |
64 | public boolean isSoftEvidence(){ |
65 | return prior != null; |
66 | } |
67 | |
68 | /** |
69 | * Return the number of grounded atoms when grounding this atom. |
70 | * This number equals to the multiplication of the domain size |
71 | * of each distinct variable appearing in this atom. That is, |
72 | * we assume cross product is used. |
73 | */ |
74 | public long groundSize(){ |
75 | long size = 1; |
76 | |
77 | // ASSUMPTION OF DATA STRUCTURE: $\forall$ i<j, args.get(i) > args.get(j) if |
78 | // args.get(i), args.get(j) < 0. (i.e., naming new variables |
79 | // sequentially from left to right. |
80 | int lastVar = 0; |
81 | for(int i=0; i<pred.arity(); i++){ |
82 | // if this is a new variable not seen before |
83 | if(args.get(i) < lastVar){ |
84 | Type t = pred.getTypeAt(i); |
85 | size *= t.size(); |
86 | lastVar = args.get(i); |
87 | } |
88 | } |
89 | return size; |
90 | } |
91 | |
92 | /** |
93 | * Create an evidence atom. |
94 | * @param p the predicate |
95 | * @param as the arguments |
96 | * @param t the truth value |
97 | */ |
98 | public Atom(Predicate p, ArrayList<Integer> as, boolean t){ |
99 | pred = p; |
100 | args = new Tuple(as); |
101 | |
102 | truth = t; |
103 | type = AtomType.EVIDENCE; |
104 | } |
105 | |
106 | /** |
107 | * Create a soft evidence atom. |
108 | * Note that the type of 'soft evidence' atoms is NONE, |
109 | * as opposed to EVIDENCE. |
110 | * @param p the predicate |
111 | * @param as the arguments |
112 | * @param prior the prior probability |
113 | */ |
114 | public Atom(Predicate p, ArrayList<Integer> as, double prior){ |
115 | pred = p; |
116 | args = new Tuple(as); |
117 | truth = null; |
118 | this.prior = prior; |
119 | type = AtomType.NONE; |
120 | } |
121 | |
122 | |
123 | public Atom(ArrayList<String> as, double prior){ |
124 | pred = null; |
125 | sargs = as; |
126 | truth = null; |
127 | this.prior = prior; |
128 | type = AtomType.NONE; |
129 | } |
130 | |
131 | public Atom(ArrayList<String> as, boolean t){ |
132 | pred = null; |
133 | sargs = as; |
134 | truth = t; |
135 | type = AtomType.EVIDENCE; |
136 | } |
137 | |
138 | |
139 | |
140 | /** |
141 | * Create an atom of type NONE. |
142 | * The default truth value of unknown is null. |
143 | * |
144 | * @param p the predicate |
145 | * @param at the arguments in the form of a tuple |
146 | * |
147 | * @see tuffy.ground.KBMC#run() |
148 | */ |
149 | public Atom(Predicate p, Tuple at){ |
150 | assert(at.list.length == p.arity()); |
151 | pred = p; |
152 | args = at; |
153 | truth = null; |
154 | type = AtomType.NONE; |
155 | } |
156 | |
157 | /** |
158 | * Returns this atom's human-friendly string representation. |
159 | */ |
160 | public String toString() { |
161 | ArrayList<String> as = new ArrayList<String>(); |
162 | for(int v : args.list) { |
163 | if(v >= 0){ |
164 | as.add("C" + v); |
165 | }else{ |
166 | as.add("v" + (-v)); |
167 | } |
168 | } |
169 | return pred.getName() + StringMan.commaListParen(as); |
170 | } |
171 | |
172 | } |