org.apache.uima.cas.impl.Serialization Java Examples

The following examples show how to use org.apache.uima.cas.impl.Serialization. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testJavaSerialization() throws Exception {

    // create FS
    createExampleFS(cas);

    // serialize
    CASSerializer cs = Serialization.serializeNoMetaData(cas);

    // reset
    cas.reset();

    // deserialize
    cas = Serialization.createCAS(casMgr, cs);

    // check values
    validateFSData(cas);
  }
 
Example #2
Source File: NewPrimitiveTypesTest.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void testBlobSerialization() throws Exception {

    // create FS
    createExampleFS(cas);

    // serialize
    ByteArrayOutputStream fos = new ByteArrayOutputStream();
    Serialization.serializeCAS(cas, fos);

    // reset
    cas.reset();

    // deserialize
    ByteArrayInputStream fis = new ByteArrayInputStream(fos.toByteArray());
    Serialization.deserializeCAS(cas, fis);

    // check values
    validateFSData(cas);
  }
 
Example #3
Source File: SCAS.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public void readFields(DataInput dataInput) throws IOException {
    int size = dataInput.readInt();
    byte[] bytes = new byte[size];
    dataInput.readFully(bytes);
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    Serialization.deserializeCAS(this.cas, bais);
}
 
Example #4
Source File: TypeSystemReinitTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testReinitCASCompleteSerializerWithArrays() throws Exception {
  try {
    AnalysisEngineDescription aed = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(JUnitExtension
                    .getFile("ExampleTae/arrayTypeSerialization.xml")));
   
    CAS cas1 = CasCreationUtils.createCas(aed);
    cas1.setDocumentText("foo");
    CASCompleteSerializer ser = Serialization.serializeCASComplete((CASMgr) cas1);

    CAS tcas2 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    CASImpl cas2 = ((CASImpl) tcas2).getBaseCAS();
    tcas2.setDocumentText("bar");

    // reinit
    //  This uses cas2 which only has a base type system to start, 
    //    and loads it from a complete serialization which has other new types
    cas2.getBinaryCasSerDes().reinit(ser);
    CAS tcas3 = cas2.getCurrentView();

    assertTrue(tcas2 == tcas3);
    assertNotNull(cas1.getTypeSystem().getType("Test.ArrayType"));
    assertNotNull(tcas3.getTypeSystem().getType("Test.ArrayType"));
    
    TypeSystemImpl ts = (TypeSystemImpl)cas2.getTypeSystem();
    Type arrayType = ts.getType("Test.ArrayType");
    Feature arrayFeat = arrayType.getFeatureByBaseName("arrayFeature");
    TypeImpl featRange = (TypeImpl)(arrayFeat.getRange());
   
    assertTrue(ts.ll_isArrayType(featRange.getCode()));
    assertFalse(arrayFeat.isMultipleReferencesAllowed());
    
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example #5
Source File: TypeSystemReinitTest.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public void testReinitCASCompleteSerializer() throws Exception {
  try {
    AnalysisEngineDescription aed = UIMAFramework.getXMLParser().parseAnalysisEngineDescription(
            new XMLInputSource(JUnitExtension
                    .getFile("TextAnalysisEngineImplTest/TestPrimitiveTae1.xml")));
    TypeSystemDescription tsd = UIMAFramework.getXMLParser().parseTypeSystemDescription(
            new XMLInputSource(getClass().getResource("/org/apache/uima/examples/SourceDocumentInformation.xml")));

    List<MetaDataObject> l = new ArrayList<>();
    l.add(aed);
    l.add(tsd);
    CAS cas1 = CasCreationUtils.createCas(l);
    cas1.setDocumentText("foo");
    CASCompleteSerializer ser = Serialization.serializeCASComplete((CASMgr) cas1);

    CAS tcas2 = CasCreationUtils.createCas(new TypeSystemDescription_impl(), null, null);
    CASImpl cas2 = ((CASImpl) tcas2).getBaseCAS();
    tcas2.setDocumentText("bar");

    // reinit
    //  This uses cas2 which only has a base type system to start, 
    //    and loads it from a complete serialization which has other new types
    cas2.getBinaryCasSerDes().reinit(ser);
    CAS tcas3 = cas2.getCurrentView();

    assertTrue(tcas2 == tcas3);
    assertNotNull(cas1.getTypeSystem().getType("NamedEntity"));
    assertNotNull(tcas3.getTypeSystem().getType("NamedEntity"));

    FeatureStructure fs = tcas3.createFS(tcas3.getTypeSystem().getType("NamedEntity"));
    tcas3.getIndexRepository().addFS(fs);
  } catch (Exception e) {
    JUnitExtension.handleException(e);
  }
}
 
Example #6
Source File: UimacppEngine.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * next
 * @param segment tbd 
 * @throws UimacppException wraps any exception
 */
public void next(CAS segment) throws UimacppException {

  try {

    // get the CAS data of CAS produce by segmenter component.
    if (hasNext) {

      nextSegmentJNI();
      CASSerializer casSerializerOut = new CASSerializer();
      // per document data
      serializeSegmentJNI(true);
      casSerializerOut.heapMetaData = null;
      casSerializerOut.heapArray = (int[]) getSerializedSegmentDataJNI(FSHEAP);
      casSerializerOut.fsIndex = (int[]) getSerializedSegmentDataJNI(INDEXEDFSS);
      casSerializerOut.stringTable = (String[]) getSerializedSegmentDataJNI(STRINGSYMBOL);

      casSerializerOut.byteHeapArray = (byte[]) getSerializedSegmentDataJNI(BYTEHEAP);
      casSerializerOut.shortHeapArray = (short[]) getSerializedSegmentDataJNI(SHORTHEAP);
      casSerializerOut.longHeapArray = (long[]) getSerializedSegmentDataJNI(LONGHEAP);
      releaseSegmentJNI();

      CASMgr casMgr = (CASMgr) segment;
      CAS newCAS = Serialization.createCAS(casMgr, casSerializerOut);
      if (newCAS != casMgr) {
        throw new RuntimeException("CASMgr and CAS should be identical");
      }
    } else {
      throw new RuntimeException("This analysis component has no CASs to return.");
    }
  } catch (Exception exc) {
    throwJTafException(exc);
  }

}
 
Example #7
Source File: UimacppEngine.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * hasNext
 * @return true if there's a next element
 * @throws UimacppException wraps any exception
 */
public boolean hasNext() throws UimacppException {

  try {
    hasNext = hasNextSegmentJNI();
    // get the CAS data of the original input CAS.
    if (!hasNext) {
      CASSerializer casSerializerOut = new CASSerializer();
      // per document data
      serializeCASJNI(true);
      casSerializerOut.heapMetaData = null;
      casSerializerOut.heapArray = (int[]) getSerializedDataJNI(FSHEAP);
      casSerializerOut.fsIndex = (int[]) getSerializedDataJNI(INDEXEDFSS);
      casSerializerOut.stringTable = (String[]) getSerializedDataJNI(STRINGSYMBOL);

      casSerializerOut.byteHeapArray = (byte[]) getSerializedDataJNI(BYTEHEAP);
      casSerializerOut.shortHeapArray = (short[]) getSerializedDataJNI(SHORTHEAP);
      casSerializerOut.longHeapArray = (long[]) getSerializedDataJNI(LONGHEAP);

      CASMgr casMgr = (CASMgr) cas;
      CAS newCAS = Serialization.createCAS(casMgr, casSerializerOut);
      if (newCAS != casMgr) {
        throw new RuntimeException("CASMgr and CAS should be identical");
      }
    }
  } catch (Exception exc) {
    throwJTafException(exc);
  }
  return hasNext;
}
 
Example #8
Source File: VinciBinaryAnalysisEngineServiceStub.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Call process.
 *
 * @param aCAS the a CAS
 * @throws ResourceServiceException the resource service exception
 * @see AnalysisEngineServiceStub#callProcess(CAS)
 */
public void callProcess(CAS aCAS) throws ResourceServiceException {
  try {
    AFrame requestFrame = new AFrame();
    requestFrame.fset(Constants.VINCI_COMMAND, Constants.ANNOTATE);
    // serialize CAS (including type system)
    CASMgr cas = (CASMgr) aCAS;
    CASCompleteSerializer serializer = Serialization.serializeCASComplete(cas);

    requestFrame.fsetTrueBinary("BinaryCAS", SerializationUtils.serialize(serializer));

    AFrame responseFrame = (AFrame) mVinciClient.sendAndReceive(requestFrame, mTimeout);

    // deserialize CAS from response frame
    byte[] responseCasBytes = responseFrame.fgetTrueBinary("BinaryCAS");
    CASSerializer responseSerializer = (CASSerializer) SerializationUtils
            .deserialize(responseCasBytes);
    ((CASImpl) cas).getBinaryCasSerDes().reinit(responseSerializer);

    // also read annotation time and enter into AnalysisEngineManagementMBean
    int annotationTime = responseFrame.fgetInt(Constants.ANNOTATION_TIME);
    if (annotationTime > 0) {
      AnalysisEngineManagementImpl mbean = (AnalysisEngineManagementImpl) mOwner
              .getManagementInterface();
      mbean.reportAnalysisTime(annotationTime);
    }
  } catch (Exception e) {
    throw new ResourceServiceException(e);
  }
}
 
Example #9
Source File: SCAS.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public void write(DataOutput dataOutput) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Serialization.serializeWithCompression(this.cas, baos);

    dataOutput.writeInt(baos.size());
    dataOutput.write(baos.toByteArray());
}
 
Example #10
Source File: SCAS.java    From ambiverse-nlu with Apache License 2.0 5 votes vote down vote up
public SCAS copy() throws UIMAException, IOException, ClassNotFoundException {
    JCas jcas = JCasFactory.createJCas();
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    ObjectOutputStream docOS = new ObjectOutputStream(buffer);
    CASCompleteSerializer serializerOut = Serialization.serializeCASComplete(this.getJCas().getCasImpl());
    docOS.writeObject(serializerOut);
    docOS.close();
    ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    CASCompleteSerializer serializerIn = (CASCompleteSerializer)is.readObject();
    Serialization.deserializeCASComplete(serializerIn, jcas.getCasImpl());
    return new SCAS(jcas.getCas());
}
 
Example #11
Source File: RabbitWriter.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static byte[] serialize(CAS cas) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    Serialization.serializeWithCompression(cas, out);
    out.close();
    return bos.toByteArray();
}
 
Example #12
Source File: RabbitWriter.java    From bluima with Apache License 2.0 5 votes vote down vote up
public static void deserialize(CAS cas, byte[] byteArray)
        throws IOException, ClassNotFoundException {
    ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
    ObjectInputStream in = new ObjectInputStream(bis);

    Serialization.deserializeCAS(cas, in);
    in.close();
}
 
Example #13
Source File: VinciBinaryAnalysisEngineService_impl.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Analyzes a given document by a CasObjectProcessor. When completed this method returns a
 * VinciFrame containing XCAS translated into a set of Vinci subFrames. Each subframe containing
 * one annotation with all its attributes.
 *
 * @param aRequestFrame          request frame
 * @return VinciFrame containing XCAS translated into a set of Vinci subframes.
 * @throws ServiceException the service exception
 */
private Transportable analyze(AFrame aRequestFrame) throws ServiceException {
  CAS cas = null;
  try {
    // get CAS object from pool
    cas = mCasPool.getCas(0);

    // deserialize into CAS object
    byte[] casBytes = aRequestFrame.fgetTrueBinary("BinaryCAS");
    CASCompleteSerializer serializer = (CASCompleteSerializer) SerializationUtils
            .deserialize(casBytes);
    Serialization.deserializeCASComplete(serializer, (CASMgr) cas);

    long annotStartTime = System.currentTimeMillis();
    // invoke Analysis Engine
    mAE.processCas(cas);
    int annotationTime = (int) (System.currentTimeMillis() - annotStartTime);
    if (debug) {
      System.out.println("Annotation took: " + annotationTime + "ms");
    }

    // serialize CAS
    AFrame responseFrame = new AFrame();
    CASSerializer responseSerializer = Serialization.serializeCAS(cas);
    byte[] responseCasBytes = SerializationUtils.serialize(responseSerializer);
    responseFrame.fsetTrueBinary("BinaryCAS", responseCasBytes);
    // also add annotation time
    responseFrame.fset(Constants.ANNOTATION_TIME, annotationTime);

    // UIMAFramework.getLogger().log("CAS ACount::" +
    // cas.getAnnotationIndex().size());
    int totalAnnots = 0;
    SofaFS sofa;
    FSIterator sItr = cas.getSofaIterator();
    while (sItr.isValid()) {
      sofa = (SofaFS) sItr.get();
      totalAnnots += cas.getView(sofa).getAnnotationIndex().size();
      sItr.moveToNext();
    }
    UIMAFramework.getLogger().log(Level.FINE, "CAS Annotation Count::" + totalAnnots);

    return responseFrame;
  } catch (Throwable ex) {
    UIMAFramework.getLogger().log(Level.SEVERE, "", ex);
    throw new ServiceException("Unexpected exception in analyze(): " + ex);
  } finally {
    // release CAS back to pool
    if (cas != null) {
      mCasPool.releaseCas(cas);
    }
  }
}
 
Example #14
Source File: SerializationNoMDTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Test driver.
 */
public void testMain() throws Exception {

  // Read the document into a String. I'm sure there are better ways to
  // do this.
  File textFile = JUnitExtension.getFile("data/moby.txt");
  String moby = file2String(textFile);
  // String moby = file2String(System.getProperty("cas.data.test") + "moby.txt");
  String line;
  BufferedReader br = new BufferedReader(new StringReader(moby));
  StringBuffer buf = new StringBuffer();
  List<String> docs = new ArrayList<>();
  while ((line = br.readLine()) != null) {
    if (line.startsWith(".. <p")) {
      docs.add(buf.toString());
      buf = new StringBuffer();
    } else {
      buf.append(line + "\n");
    }
  }
  docs.add(buf.toString());
  buf = null;

  final int numDocs = docs.size();
  final int max = 30;
  int docCount = 0;
  long overallTime = System.currentTimeMillis();
  int numTok, numSent;
  CASSerializer cs;
  while (docCount < max) {
    for (int i = 0; i < numDocs && docCount < max; i++) {
      // System.out.println("Processing document: " + i);
      // Set document text in first CAS.
      cas.setDocumentText(docs.get(i));

      tokenize();
      numTok = cas.getAnnotationIndex(tokenType).size();
      assertTrue(numTok > 0);
      // System.out.println(" Number of tokens: " + numTok);

      // System.out.println("Serializing...");
      cs = Serialization.serializeNoMetaData(cas);
      cas = Serialization.createCAS(casMgr, cs);

      assertTrue(numTok == cas.getAnnotationIndex(tokenType).size());

      createSentences();
      numSent = cas.getAnnotationIndex(sentenceType).size();
      assertTrue(numSent > 0);
      // System.out.println(" Number of sentences: " + numSent);

      // System.out.println("Serializing...");
      cs = Serialization.serializeNoMetaData(cas);
      cas = Serialization.createCAS(casMgr, cs);

      assertTrue(numTok == cas.getAnnotationIndex(tokenType).size());
      assertTrue(numSent == cas.getAnnotationIndex(sentenceType).size());
      // System.out.println(" Number of tokens: " + numTok);
      checkSentences();

      // System.out.println("Serializing...");
      cs = Serialization.serializeNoMetaData(cas);
      cas = Serialization.createCAS(casMgr, cs);

      assertTrue(numTok == cas.getAnnotationIndex(tokenType).size());
      assertTrue(numSent == cas.getAnnotationIndex(sentenceType).size());
      // System.out.println(" Verify: " + numTok + " tokens, " + numSent + " sentences.");

      casMgr.reset();

      ++docCount;
    }
    // System.out.println("Number of documents processed: " + docCount);
  }
  overallTime = System.currentTimeMillis() - overallTime;
  // System.out.println("Time taken over all: " + new TimeSpan(overallTime));

}
 
Example #15
Source File: IndexSerializationTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Test driver.
 */
public void testMain() throws Exception {

  for (int i = 0; i < 10; i++) {
    cas.getIndexRepository().addFS(cas.createAnnotation(annotationType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(sentenceType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
  }
  for (int i = 19; i >= 10; i--) {
    cas.getIndexRepository().addFS(cas.createAnnotation(annotationType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(sentenceType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
    cas.getIndexRepository().addFS(cas.createAnnotation(tokenType, i * 2, (i * 2) + 1));
  }

  AnnotationFS searchAnnot = cas.createAnnotation(annotationType, 0, 1);
  assertTrue(cas.getAnnotationIndex().find(searchAnnot) != null);
  assertTrue(cas.getIndexRepository().getIndex(ANNOT_SET_INDEX).find(searchAnnot) != null);
  // find() does not produce useful results on bag indexes, since the comparator
  // is not defined.
  // assertTrue(cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX).find(searchAnnot) != null);

  searchAnnot.setIntValue(endFeature, 4);
  assertTrue(cas.getAnnotationIndex().find(searchAnnot) == null);
  assertTrue(cas.getIndexRepository().getIndex(ANNOT_SET_INDEX).find(searchAnnot) == null);
  assertTrue(cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX).find(searchAnnot) == null);

  final int ordSize = cas.getAnnotationIndex().size();
  final int setSize = cas.getIndexRepository().getIndex(ANNOT_SET_INDEX).size();
  final int bagSize = cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX).size();
  // System.out.println("Before serialization\n");
  // System.out.println("Size of ordered index: " + ordSize);
  // System.out.println("Size of set index: " + setSize);
  // System.out.println("Size of bag index: " + bagSize);

  CASCompleteSerializer cs;
  cs = Serialization.serializeCASComplete(casMgr);
  // casMgr = CASFactory.createCAS();
  CASMgr realCasMgr = CASFactory.createCAS();  // creates base view, but no ts, so no ir
  ((CASImpl) realCasMgr).commitTypeSystem();   // also makes index repo (which will be replaced), but doesn't init the built-in indexes
  Serialization.deserializeCASComplete(cs, realCasMgr);
  cas = ((CASImpl) realCasMgr).getCurrentView();
  casMgr = (CASMgr) cas;

  // System.out.println("After serialization\n");
  FSIndex<? extends FeatureStructure> index = cas.getAnnotationIndex();
  assertTrue(index != null);
  assertTrue(index.getIndexingStrategy() == FSIndex.SORTED_INDEX);
  // System.out.println("Size of ordered index: " + index.size());
  assertTrue(index.size() == ordSize);

  index = cas.getIndexRepository().getIndex(ANNOT_BAG_INDEX);
  assertTrue(index != null);
  assertTrue(index.getIndexingStrategy() == FSIndex.BAG_INDEX);
  // System.out.println("Size of bag index: " + index.size());
  assertTrue(index.size() == bagSize);

  index = cas.getIndexRepository().getIndex(ANNOT_SET_INDEX);
  assertTrue(index != null);
  assertTrue(index.getIndexingStrategy() == FSIndex.SET_INDEX);
  // System.out.println("Size of set index: " + index.size());
  // System.out.println("Should be: " + setSize);
  assertTrue(index.size() == setSize);

}
 
Example #16
Source File: SCAS.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public byte[] serialize() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Serialization.serializeWithCompression(this.cas, baos);

    return baos.toByteArray();
}
 
Example #17
Source File: SCAS.java    From ambiverse-nlu with Apache License 2.0 4 votes vote down vote up
public void deserialize(byte[] bytes) throws UIMAException {
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    Serialization.deserializeCAS(this.cas, bais);
}
 
Example #18
Source File: SerializationReinitTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testDeltaBinaryShortLongArrayMods() throws Exception {
  CASImpl cas2 = (CASImpl) initCAS();
  CASImpl cas3 = (CASImpl) initCAS();

  // create short array and long array
  FeatureStructure newFS1 = cas.createFS(theTypeType); 
  ByteArrayFS newBA1 = cas.createByteArrayFS(1); 
  ShortArrayFS newSA1 = cas.createShortArrayFS(1); 
  LongArrayFS newLA1 = cas.createLongArrayFS(1);
  newBA1.set(0, (byte)1);
  newSA1.set(0, (short)2);
  newLA1.set(0, (long)4);
  newFS1.setFeatureValue(theByteArrayFeature, newBA1);
  newFS1.setFeatureValue(theShortArrayFeature, newSA1);
  newFS1.setFeatureValue(theLongArrayFeature, newLA1);
  cas.getIndexRepository().addFS(newFS1);
      
  //serialize binary, non compressed, not delta
  ByteArrayOutputStream fos = new ByteArrayOutputStream();
  Serialization.serializeCAS(cas, fos);

  //deserialize into cas2
  ByteArrayInputStream fis = new ByteArrayInputStream(fos.toByteArray());
  Serialization.deserializeCAS(cas2, fis);
  CasComparer.assertEquals(cas, cas2);

  //=======================================================================
  //create Marker, add/modify fs and serialize in delta xmi format.
  Marker marker = cas2.createMarker();

  // modify a value in the int arrays
  Iterator<AnnotationFS> typeIterator = cas2.getAnnotationIndex(theTypeType).iterator();
  assertTrue(typeIterator.hasNext());
  FeatureStructure fsWithArrays = typeIterator.next();
  
  ((ByteArrayFS)fsWithArrays.getFeatureValue(theByteArrayFeature)).set(0, (byte) 11);
  ((ShortArrayFS)fsWithArrays.getFeatureValue(theShortArrayFeature)).set(0, (short) 22);
  ((LongArrayFS)fsWithArrays.getFeatureValue(theLongArrayFeature)).set(0, (long) 44);

  // serialize cas2 in delta format 
  ByteArrayOutputStream fosDelta = new ByteArrayOutputStream();
  Serialization.serializeCAS(cas2, fosDelta, marker);
  
  //======================================================================
  //deserialize delta binary into cas1
  ByteArrayInputStream fisDelta = new ByteArrayInputStream(fosDelta.toByteArray());
  Serialization.deserializeCAS(cas, fisDelta);
  
  //======================================================================
  //serialize complete cas and deserialize into cas3 and compare with cas1.
  ByteArrayOutputStream fosFull = new ByteArrayOutputStream();
  Serialization.serializeCAS(cas2, fosFull);
  ByteArrayInputStream fisFull = new ByteArrayInputStream(fosFull.toByteArray());
  Serialization.deserializeCAS(cas3, fisFull);
  CasComparer.assertEquals(cas, cas3); 

}
 
Example #19
Source File: SerializationReinitTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/** Test basic blob serialization
 */
public void testBlob() throws Exception {

  /*
   * Test that FS, indexes and strings work after repeated blob serialization
   * For each iteration, add two new FS, serialize and test all created so
   * The first FS sets the string feature using standard API => goes into stringlist
   * The second FS sets the string feature using lowlevel API => goes into stringheap 
   * 
   * Throw in tests of the byte, short and long heaps as well
   * 
   */
String testString = "testString";
cas.reset();
LowLevelCAS ll_cas = cas.getLowLevelCAS();
FSIndexRepository ir = cas.getIndexRepository();
int ll_strfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theStringFeature);
int ll_bytefeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theByteFeature);
int ll_shortfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theShortFeature);
int ll_bytearrayfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theByteArrayFeature);
int ll_shortarrayfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theShortArrayFeature);
int ll_longfeatcode = ll_cas.ll_getTypeSystem().ll_getCodeForFeature(theLongFeature);

for (int cycle = 0; cycle < 10; cycle +=2 ) {
  FeatureStructure newFS1 = cas.createFS(theTypeType); 
  newFS1.setIntValue(startFeature, cycle);
  newFS1.setIntValue(endFeature, cycle+1);
  // set string using normal string feature create
  newFS1.setStringValue(theStringFeature, testString);
  newFS1.setByteValue(theByteFeature, (byte)cycle);
  newFS1.setShortValue(theShortFeature, (short)cycle);
  newFS1.setLongValue(theLongFeature, (long)cycle);
  ByteArrayFS newBA1 = cas.createByteArrayFS(1); 
  ShortArrayFS newSA1 = cas.createShortArrayFS(1); 
  newBA1.set(0, (byte)cycle);
  newSA1.set(0, (short)cycle);
  newFS1.setFeatureValue(theByteArrayFeature, newBA1);
  newFS1.setFeatureValue(theShortArrayFeature, newSA1);
  ir.addFS(newFS1);

  FeatureStructure newFS2 = cas.createFS(theTypeType);
  ByteArrayFS newBA2 = cas.createByteArrayFS(1);
  ShortArrayFS newSA2 = cas.createShortArrayFS(1); 
  newFS2.setIntValue(startFeature, cycle+1);
  newFS2.setIntValue(endFeature, cycle+2);
  ir.addFS(newFS2);
  CASImpl ci = (CASImpl) cas;
  ci.setId2FSsMaybeUnconditionally(newFS2, newBA2, newSA2);
  // set string using lowlevel string create API
  final int llfs2 = ll_cas.ll_getFSRef(newFS2);
  final int llba2 = ll_cas.ll_getFSRef(newBA2);
  final int llsa2 = ll_cas.ll_getFSRef(newSA2);
  
  ll_cas.ll_setCharBufferValue(llfs2, ll_strfeatcode,
          testString.toCharArray(), 0, testString.length());
  ll_cas.ll_setByteValue(llfs2, ll_bytefeatcode, (byte)(cycle+1));
  ll_cas.ll_setShortValue(llfs2, ll_shortfeatcode, (short)(cycle+1));
  ll_cas.ll_setLongValue(llfs2, ll_longfeatcode, (long)(cycle+1));
  ll_cas.ll_setByteArrayValue(llba2, 0, (byte)(cycle+1));
  ll_cas.ll_setShortArrayValue(llsa2, 0, (short)(cycle+1));
  newFS2.setFeatureValue(theByteArrayFeature, newBA2);
  newFS2.setFeatureValue(theShortArrayFeature, newSA2);
  ir.addFS(newFS2);

  ByteArrayOutputStream fos = new ByteArrayOutputStream();
  Serialization.serializeCAS(cas, fos);
    cas.reset();
  ByteArrayInputStream fis = new ByteArrayInputStream(fos.toByteArray());
  Serialization.deserializeCAS(cas, fis);

  FSIndex<AnnotationFS> idx = cas.getAnnotationIndex(theTypeType);
  FSIterator<AnnotationFS> iter = idx.iterator();
  for (int tc = 0; tc < cycle + 1; tc++) {
    FeatureStructure testFS = iter.get();
    iter.moveToNext();
    assertTrue(tc == testFS.getIntValue(startFeature));
    assertTrue(testString.equals(testFS.getStringValue(theStringFeature)));
    assertTrue(tc == testFS.getByteValue(theByteFeature));
    assertTrue(tc == testFS.getShortValue(theShortFeature));
    assertTrue(tc == testFS.getLongValue(theLongFeature));
    ByteArrayFS ba = (ByteArrayFS)testFS.getFeatureValue(theByteArrayFeature);
    assertTrue(tc == ba.get(0));
    ShortArrayFS sa = (ShortArrayFS)testFS.getFeatureValue(theShortArrayFeature);
    assertTrue(tc == sa.get(0));
  }
  }  
}
 
Example #20
Source File: SerializationReinitTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
   * Test driver.
   */
  public void testMain() throws Exception {

    // System.out.println("Setting up CAS.");
    // Create the initial CAS.
    long time = System.currentTimeMillis();
    time = System.currentTimeMillis() - time;
    // System.out.println("CAS set up: " + new TimeSpan(time));

    time = System.currentTimeMillis();
    // Read the document into a String. I'm sure there are better ways to
    File textFile = JUnitExtension.getFile("data/moby.txt");
    String moby = FileUtils.file2String(textFile);
    // String moby = file2String(System.getProperty("cas.data.test") + "moby.txt");
    String line;
//    BufferedReader br = new BufferedReader(new StringReader(moby));
    StringBuffer buf = new StringBuffer(10000);
    List<String> docs = new ArrayList<>();
    Matcher m = nlPattern.matcher(moby);
    while (m.find()) {
      line = m.group();
      if (line.startsWith(".. <p")) {
        docs.add(buf.toString());
        buf.setLength(0);
      } else {
        buf.append(line + "\n");
      }
    }
//    while ((line = br.readLine()) != null) {
//      if (line.startsWith(".. <p")) {
//        docs.add(buf.toString());
//        buf = new StringBuffer();
//      } else {
//        buf.append(line + "\n");
//      }
//    }
    m.appendTail(buf);
    docs.add(buf.toString());
    buf = null;

    final int numDocs = docs.size();
    final int max = 30;
    int docCount = 0;
    long overallTime = System.currentTimeMillis();
    int numTok, numSent;
    CASSerializer cs;
    while (docCount < max) {
      for (int i = 0; i < numDocs && docCount < max; i++) {
        // System.out.println("Processing document: " + i);
        // Set document text in first CAS.
        cas.setDocumentText(docs.get(i));

        tokenize();
        numTok = cas.getAnnotationIndex(tokenType).size();
        assertTrue(numTok > 0);
        // System.out.println(" Number of tokens: " + numTok);

        // System.out.println("Serializing...");
        cs = Serialization.serializeCAS(cas);
        cas = Serialization.createCAS(casMgr, cs);

        assertTrue(numTok == cas.getAnnotationIndex(tokenType).size());

        createSentences();
        numSent = cas.getAnnotationIndex(sentenceType).size();
        assertTrue(numSent > 0);
        // System.out.println(" Number of sentences: " + numSent);

        // System.out.println("Serializing...");
        cs = Serialization.serializeCAS(cas);
        cas = Serialization.createCAS(casMgr, cs);

        assertTrue(numTok == cas.getAnnotationIndex(tokenType).size());
        assertTrue(numSent == cas.getAnnotationIndex(sentenceType).size());

        // System.out.println("Serializing...");
        cs = Serialization.serializeCAS(cas);
        cas = Serialization.createCAS(casMgr, cs);

        assertTrue(numTok == cas.getAnnotationIndex(tokenType).size());
        assertTrue(numSent == cas.getAnnotationIndex(sentenceType).size());
        // System.out.println(" Verify: " + numTok + " tokens, " + numSent + " sentences.");

        casMgr.reset();

        ++docCount;
      }
      // System.out.println("Number of documents processed: " + docCount);
    }
    overallTime = System.currentTimeMillis() - overallTime;
    // System.out.println("Time taken over all: " + new TimeSpan(overallTime));

  }
 
Example #21
Source File: CasIOUtils.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public static void writeTypeSystem(CAS aCas, OutputStream aOS, boolean includeIndexDefs) throws IOException {
  writeJavaObject(includeIndexDefs 
                      ? Serialization.serializeCASMgr((CASImpl) aCas)
                      : Serialization.serializeCASMgrTypeSystemOnly((CASImpl) aCas)
                    , aOS);
}
 
Example #22
Source File: CppUimajEngine.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public int process(String doc, int[] heapArray, int[] fsIndex, String[] stringTable,
        int[] resultSpecTypes, int[] resultSpecFeatures, int sofaNum, byte[] aByteHeapArray,
        short[] aShortHeapArray, long[] aLongHeapArray) {
  int result = 0;
  try {
    // System.err.println("CppUimajEngine.process() called");

    casImpl.reset();

    // 1. deserialize CAS

    CASSerializer serializer = new CASSerializer();
    // set serialization data
    serializer.heapArray = heapArray;
    serializer.fsIndex = fsIndex;
    serializer.stringTable = stringTable;

    serializer.byteHeapArray = aByteHeapArray;
    serializer.shortHeapArray = aShortHeapArray;
    serializer.longHeapArray = aLongHeapArray;

    casImpl.getBinaryCasSerDes().reinit(serializer);

    // 2. create result spec
    if (ae != null) {

      ResultSpecification rs = ae.createResultSpecification(casImpl.getTypeSystem());
      for (int i = 0; i < resultSpecTypes.length; ++i) {
        // allAnnotatorFeatures is not considere here! (TODO)
        rs
                .addResultType(casImpl.getTypeSystemImpl().ll_getTypeForCode(resultSpecTypes[i]).getName(),
                        false);
      }
      for (int i = 0; i < resultSpecFeatures.length; ++i) {
        rs.addResultFeature(casImpl.getTypeSystemImpl().ll_getFeatureForCode(resultSpecFeatures[i])
                .getName());
      }
      // 3. call process with cas
      ae.process(casImpl, rs);

    } else if (cc != null) {
      // 3. call process with tcas or cas
      if (requiresTCas && sofaNum == 0) {
        result = 1;
        exceptionString = "This CasConsumer expects a View, but the Sofa from which to construct one is not specified.";
      } else if (sofaNum > 0) {
        CAS view = casImpl.getView(sofaNum);
        cc.processCas(view);
      } else {
        cc.processCas(casImpl);
      }
    }
    // 4. deserialize CAS again
    CASSerializer deSerializer = Serialization.serializeCAS(casImpl);

    saveSerializedCAS(deSerializer);

  } catch (Exception exc) {
    result = 1;
    logException(exc);
  }
  return result;
}
 
Example #23
Source File: CppUimajEngine.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public int initialize(String config, String dataPath, int[] typeInheritance,
        int[] typePriorities, int[] featureDefs, int[] featureOffset, String[] typeNames,
        String[] featureNames, int[] stringSubTypes, String[] stringSubTypeValues,
        int[] stringSubTypeValuePos, String[] indexIDs, int[] indexKinds, int[] compStarts,
        int[] compDefs) {
  int result = 0;
  try {
    // System.out.println("CppUimajEngine::initialize()");
    CASMgrSerializer serializer = new CASMgrSerializer();
    serializer.typeOrder = typePriorities;
    serializer.indexNames = indexIDs;

    // trivalliy construct the name to index map
    serializer.nameToIndexMap = new int[indexIDs.length];
    for (int i = 0; i < serializer.nameToIndexMap.length; ++i) {
      serializer.nameToIndexMap[i] = i;
    }

    serializer.indexingStrategy = indexKinds;
    serializer.comparatorIndex = compStarts;
    serializer.comparators = compDefs;

    serializer.typeNames = typeNames;
    serializer.featureNames = featureNames;
    serializer.typeInheritance = typeInheritance;
    serializer.featDecls = featureDefs;
    serializer.topTypeCode = 1;
    serializer.featureOffsets = featureOffset;
    serializer.stringSubtypes = stringSubTypes;
    serializer.stringSubtypeValues = stringSubTypeValues;
    serializer.stringSubtypeValuePos = stringSubTypeValuePos;

    byte[] bar = config.getBytes(StandardCharsets.UTF_16);
    ByteArrayInputStream bais = new ByteArrayInputStream(bar);
    XMLInputSource in = new XMLInputSource(bais, null);
    ResourceSpecifier specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
    bais.close();

    ResourceManager resMgr = UIMAFramework.newDefaultResourceManager();
    resMgr.setDataPath(dataPath);

    if (specifier instanceof CasConsumerDescription) {
      cc = UIMAFramework.produceCasConsumer(specifier);
      CasConsumerDescription ccdesc = (CasConsumerDescription) specifier;
      Capability[] capabilities = ccdesc.getCasConsumerMetaData().getCapabilities();
      for (int i = 0; i < capabilities.length; i++) {
        String[] inputsofas = capabilities[i].getInputSofas();
        if (inputsofas.length > 0)
          requiresTCas = false;
      }
    } else {
      ae = UIMAFramework.produceAnalysisEngine(specifier, resMgr, null);
    }

    casImpl = (CASImpl) CASFactory.createCAS();
    casImpl.commitTypeSystem();

    // Create the Base indexes in order to deserialize
    casImpl.initCASIndexes();
    casImpl.getIndexRepositoryMgr().commit();

    // deserialize into this CAS
    CASCompleteSerializer completeSerializer = new CASCompleteSerializer();
    completeSerializer.setCasMgrSerializer(serializer);
    completeSerializer.setCasSerializer(Serialization.serializeCAS(casImpl));

    casImpl.getBinaryCasSerDes().reinit(completeSerializer);

    // System.out.println(cc.getProcessingResourceMetaData().getName());
  } catch (Exception exc) {
    result = 1;
    logException(exc);
  }

  return result;
}
 
Example #24
Source File: UimacppEngine.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * process the document.
 * @param rs the result specification
 * @param aCas the CAS
 * @param casIsEmpty tbd
 * @throws UimacppException wraps any exception
 */
public void process(ResultSpecification rs, CAS aCas, boolean casIsEmpty) throws UimacppException {

  int isTCas=0;
  String sofaName=aCas.getViewName();
  if (sofaName != null) {
    isTCas=1;
  }

  cas = aCas.getCurrentView();

  try {
    resetJNI();

    if (!casIsEmpty) {
      CASSerializer casSerializerIn = Serialization.serializeCAS(cas);
      /**
       * fillCASJNI(casSerializerIn.heapArray, casSerializerIn.fsIndex,
       * casSerializerIn.stringTable);
       */

      fillCASJNI(casSerializerIn.heapArray, casSerializerIn.fsIndex, casSerializerIn.stringTable,
              casSerializerIn.byteHeapArray, casSerializerIn.shortHeapArray,
              casSerializerIn.longHeapArray);
    }

    IntVector resultSpecTypes = new IntVector();
    IntVector resultSpecFeatures = new IntVector();
    if (rs != null) {

      serializeResultSpecification(rs, (CASImpl) cas, resultSpecTypes, resultSpecFeatures);
    }

    processJNI(isTCas, sofaName, resultSpecTypes.toArray(), resultSpecFeatures.toArray());

    // call hasNext() to see if this returns segments
    // if there are no segments this will get the
    // CAS data.
    if (hasNext()) {
      return;
    }

  } catch (Exception exc) {
    throwJTafException(exc);
  }
}
 
Example #25
Source File: CasAnnotationViewerApplet.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Called when the applet is initialized.
 */
@Override
public void init() {
  try {
    // get applet parameter - URL from which to get the CAS
    String casURL = getParameter("CasUrl");

    // open URL connection to get the serialized CAS
    URLConnection con = new URL(casURL).openConnection();

    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setDefaultUseCaches(false);
    con.setRequestProperty("Content-Type", "application/octet-stream");
    // con.connect();

    InputStream in = con.getInputStream();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    byte[] buf = new byte[2048];
    int bytesRead = in.read(buf);
    while (bytesRead > 0) {
      byteStream.write(buf, 0, bytesRead);
      bytesRead = in.read(buf);
    }
    byte[] bytes = byteStream.toByteArray();
    in.close();
    byteStream.close();
    System.out.println("Got " + bytes.length + " bytes.");

    // deserialize CAS
    CASMgr casMgr = CASFactory.createCAS();
    CASCompleteSerializer serializer = (CASCompleteSerializer) SerializationUtils
            .deserialize(bytes);
    Serialization.deserializeCASComplete(serializer, casMgr);

    // get 2nd applet parameter - right-to-left text orientation
    boolean rightToLeft = false;
    String rightToLeftParam = getParameter("RightToLeftTextOrientation");
    if (rightToLeftParam != null && rightToLeftParam.equalsIgnoreCase("true")) {
      rightToLeft = true;
    }

    // create viewer component and add to this applet
    mViewer = new CasAnnotationViewer();
    // NOTE: it seems to be important to add the viewer to the frame
    // before calling setCAS. If we do it the other way around
    // we seem to frequently cause the browser to hang.
    getContentPane().add(mViewer);

    mViewer.setCAS(casMgr.getCAS().getView(CAS.NAME_DEFAULT_SOFA));
    mViewer.setRightToLeftTextOrientation(rightToLeft);

    // add a listener that detects resize events
    addComponentListener(new MyComponentListener());

    // set initial size of tree viewer panel
    resizeTreeViewer();

  } catch (Exception e) {
    e.printStackTrace();
  }

}
 
Example #26
Source File: CasTreeViewerApplet.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * Called when the applet is initialized.
 */
@Override
public void init() {
  try {
    // get applet parameter - URL from which to get the CAS
    String casURL = getParameter("CasUrl");

    // open URL connection to get the serialized CAS
    URLConnection con = new URL(casURL).openConnection();

    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setDefaultUseCaches(false);
    con.setRequestProperty("Content-Type", "application/octet-stream");
    // con.connect();

    InputStream in = con.getInputStream();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    byte[] buf = new byte[2048];
    int bytesRead = in.read(buf);
    while (bytesRead > 0) {
      byteStream.write(buf, 0, bytesRead);
      bytesRead = in.read(buf);
    }
    byte[] bytes = byteStream.toByteArray();
    in.close();
    byteStream.close();
    System.out.println("Got " + bytes.length + " bytes.");

    // deserialize CAS
    CASMgr casMgr = CASFactory.createCAS();
    CASCompleteSerializer serializer = (CASCompleteSerializer) SerializationUtils
            .deserialize(bytes);
    Serialization.deserializeCASComplete(serializer, casMgr);

    // create tree viewer component and add to this applet
    mTreeViewer = new CasTreeViewer(casMgr.getCAS().getView(CAS.NAME_DEFAULT_SOFA));
    getContentPane().add(mTreeViewer);

    // add a listener that detects resize events
    addComponentListener(new MyComponentListener());

    // set initial size of tree viewer panel
    resizeTreeViewer();

  } catch (Exception e) {
    e.printStackTrace();
  }

}
 
Example #27
Source File: ServiceDataCargo.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Unmarshalls the CAS data in this <code>ServiceDataCargo</code> into an existing
 * <code>CAS</code> instance. The data in the exsiting CAS will be replaced by the CAS data in
 * this object.
 * 
 * @param aCas the CAS to unmarshal into
 * @param aReplaceCasTypeSystem if true, assumes serialized data contains the type system
 * @throws CASException passthru
 */
 public void unmarshalCas(CAS aCas, boolean aReplaceCasTypeSystem) throws CASException {
  CASMgr casMgr = (CASMgr) aCas;
  if (aReplaceCasTypeSystem) {
    Serialization.deserializeCASComplete(mCasSer, casMgr);
  } else {
    Serialization.createCAS(casMgr, mCasSer.getCASSerializer());
  }
}
 
Example #28
Source File: ServiceDataCargo.java    From uima-uimaj with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new <code>SerializableAnalysisProcessData</code> that contains the given
 * <code>CAS</code> and <code>ProcessTrace</code>.
 * 
 * @param aCAS
 *          the CAS whose state will be extracted into this object
 * @param aProcessTrace
 *          the process trace object. This may be null, if no process trace is available. (For
 *          example, ProcessTrace data may often be returned from a service but not passed to the
 *          service.)
 */
public ServiceDataCargo(CAS aCAS, ProcessTrace aProcessTrace) {
  mCasSer = Serialization.serializeCASComplete((CASMgr) aCAS);
  mProcessTrace = aProcessTrace;
}
 
Example #29
Source File: ServiceDataCargo.java    From uima-uimaj with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new <code>SerializableAnalysisProcessData</code> that contains information
 * extracted from the specified <code>AnalysisProcessData</code>.
 * 
 * @param aData
 *          the AnalysisProcessData to extract from
 */
public ServiceDataCargo(AnalysisProcessData aData) {
  mCasSer = Serialization.serializeCASComplete((CASMgr) aData.getCAS());
  mProcessTrace = aData.getProcessTrace();
}