* @throws IOException -
* error while reading corpus
* @throws CollectionException
*/
private Object[] readNext(int fetchSize) throws IOException, CollectionException {
ProcessTrace localTrace = new ProcessTrace_impl(cpm.getPerformanceTuningSettings());
boolean success = false;
Object[] casObjects = null;
threadState = 1000; // Entering hasNext()
// Checks if the CollectionReader has any documents left
long start = 0;
if (timer != null) {
start = timer.getTimeInMillis();
}
boolean eventStarted = false;
// CasObject based CollectionReader does not support returning more than
// one CAS at a time. So
// fake support for this by calling its getNext() until the casList is
// filled to max capacity.
// The capacity of casList is equal to the CollectionReader fetchSize,
// defined in CR descriptor.
if (collectionReader instanceof CollectionReader) {
casList = new CAS[fetchSize];
for (int i = 0; i < fetchSize; i++) {
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST, this.getClass().getName(),
"process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE, "UIMA_CPM_cr_fetch_new_cas__FINEST",
new Object[] { Thread.currentThread().getName() });
}
threadState = 1001; // Waiting for CAS
// Get the cas from the pool.
while (cpm.isRunning() && (casList[i] = casPool.getCas(0)) == null)
; // intentionally empty while loop
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(
Level.FINEST,
this.getClass().getName(),
"process",
CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
"UIMA_CPM_cr_check_cas_for_null__FINEST",
new Object[] { Thread.currentThread().getName(),
String.valueOf((casList[i] == null)) });
}
if (cpm.isRunning() == false) {
// CPM is in shutdown stage. No need to enqueue additional
// documents/CAS'es. Just release
// those that have been aquired so far back to the pool and
// return null, indicating
// end of processing.
if (timer != null) {
totalFetchTime += (timer.getTimeInMillis() - start);
}
for (int listCounter = 0; casList != null && casList[i] != null
&& listCounter < casList.length; listCounter++) {
casPool.releaseCas(casList[listCounter]);
// synchronized (casPool) { // redundant - releaseCas call does this
// casPool.notifyAll();
// }
}
if (cpmStatTable != null) {
Progress[] progress = collectionReader.getProgress();
cpmStatTable.put("COLLECTION_READER_PROGRESS", progress);
cpmStatTable.put("COLLECTION_READER_TIME", Long.valueOf(totalFetchTime));
}
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST, this.getClass().getName(),
"process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
"UIMA_CPM_in_shutdown_state__FINEST",
new Object[] { Thread.currentThread().getName() });
}
return null;
}
if (casList[i] == null) {
return null;
}
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST, this.getClass().getName(),
"process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE, "UIMA_CPM_got_new_cas__FINEST",
new Object[] { Thread.currentThread().getName() });
}
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST, this.getClass().getName(),
"process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE, "UIMA_CPM_call_cas_reset__FINEST",
new Object[] { Thread.currentThread().getName() });
}
casList[i].reset();
// If Collection Reader and CAS Initilaizer do not declare any
// output SofAs, must be passed the default view (meaning whatever's
//mapped to _InitialView) for backward compatiblity
Capability[] capabilities;
CasInitializer casIni = ((CollectionReader) collectionReader).getCasInitializer();
if (casIni != null)
capabilities = casIni.getProcessingResourceMetaData().getCapabilities();
else
capabilities = ((CollectionReader) collectionReader).getProcessingResourceMetaData()
.getCapabilities();
boolean sofaUnaware = true;
for (int j = 0; j < capabilities.length; j++) {
if (capabilities[j].getOutputSofas().length > 0) {
sofaUnaware = false;
break;
}
}
threadState = 1003; // Entering
// set the current component info of the CAS, so that it knows
// the sofa
// mappings for the component that's about to process it
UimaContextAdmin context = ((CollectionReader) collectionReader).getUimaContextAdmin();
casList[i].setCurrentComponentInfo(context.getComponentInfo());
try {
if (sofaUnaware) {
// sofa-unaware CR, give it whatever is mapped to the
// initial view (creating that view first if it's not the default)
String absSofaName = context.getComponentInfo().mapToSofaID(CAS.NAME_DEFAULT_SOFA);
if (!CAS.NAME_DEFAULT_SOFA.equals(absSofaName)) {
casList[i].createView(CAS.NAME_DEFAULT_SOFA);
}
CAS view = casList[i].getView(CAS.NAME_DEFAULT_SOFA);
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST,
this.getClass().getName(), "process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
"UIMA_CPM_call_cr_next__FINEST",
new Object[] { Thread.currentThread().getName(), "CAS" });
}
localTrace.startEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "");
eventStarted = true;
((CollectionReader) collectionReader).getNext(view);
localTrace.endEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "success");
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST,
this.getClass().getName(), "process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
"UIMA_CPM_call_cr_next_finished__FINEST",
new Object[] { Thread.currentThread().getName(), "CAS" });
}
} else
// sofa-aware CR, give it the base CAS
{
CAS baseCas = ((CASImpl) casList[i]).getBaseCAS();
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST,
this.getClass().getName(), "process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
"UIMA_CPM_call_cr_next__FINEST",
new Object[] { Thread.currentThread().getName(), "CAS" });
}
localTrace.startEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "");
eventStarted = true;
((CollectionReader) collectionReader).getNext(baseCas);
localTrace.endEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "success");
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST,
this.getClass().getName(), "process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE,
"UIMA_CPM_call_cr_next_finished__FINEST",
new Object[] { Thread.currentThread().getName(), "CAS" });
}
}
success = true;
} finally {
// be sure to unset the component info in the CAS, since the
// CAS is no longer
// being processed by the CollectionReader
casList[i].setCurrentComponentInfo(null);
if (eventStarted) // use this to make sure we dont end event that has not been explicitely
// started
{
if (!success) {
localTrace.endEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "failure");
}
synchronized (globalSharedProcessTrace) {
globalSharedProcessTrace.aggregate(localTrace);
}
}
}
}
casObjects = casList;
if (casObjects != null && casObjects.length > 0) {
try {
if (((CASImpl) casList[0]).isBackwardCompatibleCas()) {
CAS view = casList[0].getView(CAS.NAME_DEFAULT_SOFA);
lastDocId[0] = ConsumerCasUtils.getStringFeatValue(view, Constants.METADATA_KEY,
Constants.DOC_ID);
} else {
lastDocId[0] = "";
}
} catch (Exception e) {
lastDocId[0] = "";
}
}
} else {
// Retrieve next set of CAS'es. fetchSize is hint to the
// CollectionReader how many to return
// Some CollectionReaders return a batch with size different than
// recommended in
// fetchSize. Most notably, Large WF Store decides itself how many
// entities to return for
// each fetch.
if (UIMAFramework.getLogger().isLoggable(Level.FINEST)) {
UIMAFramework.getLogger(this.getClass()).logrb(Level.FINEST, this.getClass().getName(),
"process", CPMUtils.CPM_LOG_RESOURCE_BUNDLE, "UIMA_CPM_call_cr_next__FINEST",
new Object[] { Thread.currentThread().getName(), "CasData" });
}
localTrace.startEvent(collectionReader.getProcessingResourceMetaData().getName(), "Process",
"");
try {
casObjects = ((CasDataCollectionReader) collectionReader).getNext(fetchSize);
success = true;
} finally {
if (!success) {
localTrace.endEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "failure");
} else {
localTrace.endEvent(collectionReader.getProcessingResourceMetaData().getName(),
"Process", "success");
}
synchronized (globalSharedProcessTrace) {
globalSharedProcessTrace.aggregate(localTrace);