Java Code Examples for org.apache.uima.cas.impl.Serialization#deserializeCASComplete()

The following examples show how to use org.apache.uima.cas.impl.Serialization#deserializeCASComplete() . 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: 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 2
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 3
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 4
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 5
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 6
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());
  }
}