-
Notifications
You must be signed in to change notification settings - Fork 18
/
DataModel.scala
329 lines (286 loc) · 12.1 KB
/
DataModel.scala
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/** This software is released under the University of Illinois/Research and Academic Use License. See
* the LICENSE file in the root folder for details. Copyright (c) 2016
*
* Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign
* http://cogcomp.cs.illinois.edu/
*/
package edu.illinois.cs.cogcomp.saul.datamodel
import edu.illinois.cs.cogcomp.core.datastructures.vectors.{ ExceptionlessInputStream, ExceptionlessOutputStream }
import edu.illinois.cs.cogcomp.saul.datamodel.edge.{ AsymmetricEdge, Edge, Link, SymmetricEdge }
import edu.illinois.cs.cogcomp.saul.datamodel.node.{ JoinNode, Node, NodeProperty }
import edu.illinois.cs.cogcomp.saul.datamodel.property.features.discrete._
import edu.illinois.cs.cogcomp.saul.datamodel.property.features.real._
import edu.illinois.cs.cogcomp.saul.datamodel.property.{ EvaluatedProperty, Property }
import edu.illinois.cs.cogcomp.saul.util.Logging
import scala.collection.mutable.ListBuffer
import scala.reflect.ClassTag
/** Represents the data model that stores the data object graph. Extend this trait to define nodes and edges for
* representing data for a learning problem.
*/
trait DataModel extends Logging {
val PID = 'PID
final val nodes = new ListBuffer[Node[_]]
final val properties = new ListBuffer[NodeProperty[_]]
final val edges = new ListBuffer[Edge[_, _]]
// TODO: Implement this function.
def select[T <: AnyRef](node: Node[T], conditions: EvaluatedProperty[T, _]*): List[T] = {
val conds = conditions.toList
node.getAllInstances.filter({
t =>
conds.exists({
cond => cond.property.sensor(t).equals(cond.value)
})
}).toList
}
def clearInstances(): Unit = {
nodes.foreach(_.clear())
edges.foreach(_.clear())
}
def addFromModel[T <: DataModel](dataModel: T): Unit = {
assert(this.nodes.size == dataModel.nodes.size)
for ((n1, n2) <- nodes.zip(dataModel.nodes)) {
n1.populateFrom(n2)
}
assert(this.edges.size == dataModel.edges.size)
for ((e1, e2) <- edges.zip(dataModel.edges)) {
e1.populateFrom(e2)
}
}
@deprecated("Use node.properties to get the properties for a specific node")
def getPropertiesForType[T <: AnyRef](implicit tag: ClassTag[T]): List[Property[T]] = {
this.properties.filter(a => a.tag.equals(tag)).map(_.asInstanceOf[Property[T]]).toList
}
@deprecated("Use node.populate() instead.")
def populate[T <: AnyRef](node: Node[T], coll: Seq[T]) = {
node.populate(coll)
}
@deprecated
def getNodeWithType[T <: AnyRef](implicit tag: ClassTag[T]): Node[T] = {
this.nodes.filter {
e: Node[_] => tag.equals(e.tag)
}.head.asInstanceOf[Node[T]]
}
@deprecated
def getFromRelation[FROM <: AnyRef, NEED <: AnyRef](t: FROM)(implicit tag: ClassTag[FROM], headTag: ClassTag[NEED]): Iterable[NEED] = {
val dm = this
if (tag.equals(headTag)) {
Set(t.asInstanceOf[NEED])
} else {
val r = this.edges.filter {
r => r.from.tag.toString.equals(tag.toString) && r.to.tag.toString.equals(headTag.toString)
}
if (r.isEmpty) {
// reverse search
val r = this.edges.filter {
r => r.to.tag.toString.equals(tag.toString) && r.from.tag.toString.equals(headTag.toString)
}
if (r.isEmpty) {
throw new Exception(s"Failed to found relations between $tag to $headTag")
} else r flatMap (_.asInstanceOf[Edge[NEED, FROM]].backward.neighborsOf(t)) distinct
} else r flatMap (_.asInstanceOf[Edge[FROM, NEED]].forward.neighborsOf(t)) distinct
}
}
// TODO: comment this function
@deprecated
def getFromRelation[T <: AnyRef, HEAD <: AnyRef](name: Symbol, t: T)(implicit tag: ClassTag[T], headTag: ClassTag[HEAD]): Iterable[HEAD] = {
if (tag.equals(headTag)) {
List(t.asInstanceOf[HEAD])
} else {
val r = this.edges.filter {
r =>
r.from.tag.equals(tag) && r.to.tag.equals(headTag) && r.forward.name.isDefined && name.equals(r.forward.name.get)
}
// there must be only one such relation
if (r.isEmpty) {
throw new Exception(s"Failed to find any relation between $tag to $headTag")
} else if (r.size > 1) {
throw new Exception(s"Found too many relations between $tag to $headTag,\nPlease specify a name")
} else {
r.head.forward.asInstanceOf[Link[T, HEAD]].neighborsOf(t)
}
}
}
@deprecated
def getRelatedFieldsBetween[T <: AnyRef, U <: AnyRef](implicit fromTag: ClassTag[T], toTag: ClassTag[U]): Iterable[Link[T, U]] = {
this.edges.filter(r => r.from.tag.equals(fromTag) && r.to.tag.equals(toTag)).map(_.forward.asInstanceOf[Link[T, U]]) ++
this.edges.filter(r => r.to.tag.equals(fromTag) && r.from.tag.equals(toTag)).map(_.backward.asInstanceOf[Link[T, U]])
}
/** node definitions */
def node[T <: AnyRef](implicit tag: ClassTag[T]): Node[T] = node((x: T) => x)
def node[T <: AnyRef](keyFunc: T => Any)(implicit tag: ClassTag[T]): Node[T] = {
val n = new Node[T](keyFunc, tag)
nodes += n
n
}
def join[A <: AnyRef, B <: AnyRef](a: Node[A], b: Node[B])(matcher: (A, B) => Boolean)(implicit tag: ClassTag[(A, B)]): Node[(A, B)] = {
val n = new JoinNode(a, b, matcher, tag)
a.joinNodes += n
b.joinNodes += n
nodes += n
n
}
/** edges */
def edge[A <: AnyRef, B <: AnyRef](a: Node[A], b: Node[B], name: Symbol = 'default): Edge[A, B] = {
val e = AsymmetricEdge(new Link(a, b, Some(name)), new Link(b, a, Some(Symbol("-" + name.name))))
a.outgoing += e
b.incoming += e
edges += e
e
}
def symmEdge[A <: AnyRef](a: Node[A], b: Node[A], name: Symbol = 'default): Edge[A, A] = {
val e = SymmetricEdge(new Link(a, b, Some(name)))
a.incoming += e
a.outgoing += e
b.incoming += e
b.outgoing += e
edges += e
e
}
class PropertyApply[T <: AnyRef] private[DataModel] (val node: Node[T], name: String, cache: Boolean, ordered: Boolean) {
papply =>
// TODO(danielk): make the hashmaps immutable
lazy val propertyCacheMap = {
val map = collection.mutable.HashMap[T, Any]()
node.propertyCacheList += map
map
}
def getOrUpdate(input: T, f: T => Any): Any = propertyCacheMap.getOrElseUpdate(input, f(input))
def apply(f: T => Boolean)(implicit tag: ClassTag[T]): BooleanProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[Boolean] } else f
val a = new BooleanProperty[T](name, cachedF) with NodeProperty[T] { override def node: Node[T] = papply.node }
papply.node.properties += a
properties += a
a
}
def apply(f: T => List[Int])(implicit tag: ClassTag[T], d: DummyImplicit): RealPropertyCollection[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[List[Int]] } else f
val newf: T => List[Double] = { t => cachedF(t).map(_.toDouble) }
val a = if (ordered) {
new RealArrayProperty[T](name, newf) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
} else {
new RealGenProperty[T](name, newf) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
}
papply.node.properties += a
properties += a
a
}
/** Discrete sensor feature with range, same as real name in lbjava */
def apply(f: T => Int)(implicit tag: ClassTag[T], d1: DummyImplicit, d2: DummyImplicit): RealProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[Int] } else f
val newf: T => Double = { t => cachedF(t).toDouble }
val a = new RealProperty[T](name, newf) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
papply.node.properties += a
properties += a
a
}
/** Discrete sensor feature with range, same as real% and real[] in lbjava */
def apply(f: T => List[Double])(implicit tag: ClassTag[T], d1: DummyImplicit, d2: DummyImplicit,
d3: DummyImplicit): RealCollectionProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[List[Double]] } else f
val a = new RealCollectionProperty[T](name, cachedF, ordered) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
papply.node.properties += a
properties += a
a
}
/** Discrete sensor feature with range, same as real name in lbjava */
def apply(f: T => Double)(implicit tag: ClassTag[T], d1: DummyImplicit, d2: DummyImplicit, d3: DummyImplicit,
d4: DummyImplicit): RealProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[Double] } else f
val a = new RealProperty[T](name, cachedF) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
papply.node.properties += a
properties += a
a
}
/** Discrete feature without range, same as discrete SpamLabel in lbjava */
def apply(f: T => String)(implicit tag: ClassTag[T], d1: DummyImplicit, d2: DummyImplicit, d3: DummyImplicit,
d4: DummyImplicit, d5: DummyImplicit): DiscreteProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[String] } else f
val a = new DiscreteProperty[T](name, cachedF, None) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
papply.node.properties += a
properties += a
a
}
/** Discrete array feature with range, same as discrete[] and discrete% in lbjava */
def apply(f: T => List[String])(implicit tag: ClassTag[T], d1: DummyImplicit, d2: DummyImplicit, d3: DummyImplicit,
d4: DummyImplicit, d5: DummyImplicit, d6: DummyImplicit): DiscreteCollectionProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[List[String]] } else f
val a = new DiscreteCollectionProperty[T](name, cachedF, !ordered) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
papply.node.properties += a
properties += a
a
}
/** Discrete feature with range, same as discrete{"spam", "ham"} SpamLabel in lbjava */
def apply(range: String*)(f: T => String)(implicit tag: ClassTag[T], d1: DummyImplicit, d2: DummyImplicit, d3: DummyImplicit,
d4: DummyImplicit, d5: DummyImplicit, d6: DummyImplicit,
d7: DummyImplicit): DiscreteProperty[T] = {
def cachedF = if (cache) { x: T => getOrUpdate(x, f).asInstanceOf[String] } else f
val r = range.toList
val a = new DiscreteProperty[T](name, cachedF, Some(r)) with NodeProperty[T] {
override def node: Node[T] = papply.node
}
papply.node.properties += a
properties += a
a
}
}
def property[T <: AnyRef](node: Node[T], name: String = "prop" + properties.size, cache: Boolean = false, ordered: Boolean = false) =
new PropertyApply[T](node, name, cache, ordered)
/** Methods for caching Data Model */
var hasDerivedInstances = false
def deriveInstances() = {
nodes.foreach { node =>
val relatedProperties = properties.filter(property => property.tag.equals(node.tag)).toList
node.deriveInstances(relatedProperties)
}
edges.foreach { edge =>
edge.deriveIndexWithIds()
}
hasDerivedInstances = true
}
val defaultDIFilePath = "models/" + getClass.getCanonicalName + ".di"
def write(filePath: String = defaultDIFilePath) = {
val out = ExceptionlessOutputStream.openCompressedStream(filePath)
out.writeInt(nodes.size)
nodes.zipWithIndex.foreach {
case (node, nodeId) =>
out.writeInt(nodeId)
node.writeDerivedInstances(out)
}
out.writeInt(edges.size)
edges.zipWithIndex.foreach {
case (edge, edgeId) =>
out.writeInt(edgeId)
edge.writeIndexWithIds(out)
}
out.close()
}
def load(filePath: String = defaultDIFilePath) = {
val in = ExceptionlessInputStream.openCompressedStream(filePath)
val nodesSize = in.readInt()
(0 until nodesSize).foreach { _ =>
val nodeId = in.readInt()
nodes(nodeId).loadDerivedInstances(in)
}
val edgesSize = in.readInt()
(0 until edgesSize).foreach { _ =>
val edgeId = in.readInt()
edges(edgeId).loadIndexWithIds(in)
}
in.close()
hasDerivedInstances = true
}
}