-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUimaStatusCallbackListener.scala
More file actions
136 lines (116 loc) · 4.58 KB
/
Copy pathUimaStatusCallbackListener.scala
File metadata and controls
136 lines (116 loc) · 4.58 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
package uimaAS
import org.apache.uima.aae.client.UimaAsBaseCallbackListener
import org.apache.uima.aae.client.UimaASProcessStatus
import org.apache.uima.aae.client.UimaAsynchronousEngine
import org.apache.uima.collection.EntityProcessStatus
import org.apache.uima.cas.CAS
import org.apache.uima.jcas.JCas
import scala.collection.concurrent
import scala.concurrent.{ Future, Promise }
import scala.concurrent.ExecutionContext.Implicits.global
import java.util.concurrent.ConcurrentLinkedQueue
import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import scala.concurrent.Await
import scala.concurrent.duration.Duration
class UimaStatusCallbackListener[T](
val engine: UimaAsynchronousEngine,
val block: Util.Block[T] = Util.noOp,
val maybeOutputDir: Option[String] = None,
val collectionTotal: Option[Int] = None,
val logCas: Boolean = true) extends UimaAsBaseCallbackListener {
import collection.JavaConversions._
val startTime = System.nanoTime() / 1000000
val casMap: concurrent.Map[String, Long] = concurrent.TrieMap()
val queue = new ConcurrentLinkedQueue[Util.TaggedAnalysis[T]]()
val promisedCompletion = Promise[Unit]
val promisedResults = Promise[Util.Results[T]]
val logger = Logger(LoggerFactory.getLogger(""))
var entityCount: Int = 0
var size: Long = 0
val outputDir: Util.OutputDir =
maybeOutputDir.fold(Util.TmpDir.create())(Util.UserSpecifiedDir)
def stopOnErr(status: EntityProcessStatus, msg: String, ignoreErrors: Boolean = false)(block: => Unit): Unit = {
if (status != null && status.isException()) {
logger.error(msg)
val exceptions = status.getExceptions()
for (e <- exceptions) {
logger.error(e.toString)
}
if (!ignoreErrors) {
logger.error("Terminating Client...")
engine.stop()
} else {
block
}
} else {
block
}
}
override def initializationComplete(status: EntityProcessStatus): Unit =
stopOnErr(status, "Error on getMeta call to remote service:") {}
override def collectionProcessComplete(status: EntityProcessStatus): Unit =
stopOnErr(status, "Error on collection process complete call to remote service:") {
if (collectionTotal.nonEmpty) {
Await.ready(promisedCompletion.future, Duration.Inf)
}
promisedResults.success(queue.toMap)
// promisedIterator.success(queue.iterator().map { path =>
// val cas = engine.getCAS()
// Util.deserializeCasBinary(cas, path)
// })
logger.info(s"Completed $entityCount documents")
if (size > 0) {
logger.info(s"; $size characters")
}
val elapsedTime = System.nanoTime() / 1000000 - startTime
logger.info("Time Elapsed : " + elapsedTime + " ms ");
val perfReport = engine.getPerformanceReport()
if (perfReport != null) {
logger.info("\n\n ------------------ PERFORMANCE REPORT ------------------\n");
logger.info(perfReport);
}
}
override def entityProcessComplete(cas: CAS, status: EntityProcessStatus): Unit =
stopOnErr(status, "Error on process CAS call to remote service:", true) {
if (logCas) {
var ip: Option[String] = None
val events = status.getProcessTrace.getEventsByComponentName("UimaEE", false)
for (event <- events) {
if (event.getDescription().equals("Service IP")) {
ip = Some(event.getResultMessage())
}
}
val casId = status.asInstanceOf[UimaASProcessStatus].getCasReferenceId
if (casId != null) {
val current = System.nanoTime() / 1000000 - startTime
casMap.get(casId).foreach { start =>
logger.debug(s"$ip \t $start \t ${current - start}")
}
} else {
logger.debug(".");
if (0 == (entityCount + 1) % 50) {
logger.info((entityCount + 1) + " processed\n");
}
}
}
// val serialized = Util.serializeCasBinary(cas, outputDir.path, Some(entityCount))
entityCount = entityCount + 1
val docText = cas.getDocumentText
if (docText != null) {
size += docText.length
}
queue.add(block(cas.getJCas))
collectionTotal.foreach { total =>
if (entityCount == total) {
promisedCompletion.success(Unit)
}
}
}
override def onBeforeMessageSend(status: UimaASProcessStatus): Unit = {
val current = System.nanoTime() / 1000000 - startTime
casMap.put(status.getCasReferenceId, current)
}
override def onBeforeProcessCAS(status: UimaASProcessStatus, nodeIP: String, pid: String): Unit = {
}
}