org.apache.uima.jcas.cas.Sofa Java Examples

The following examples show how to use org.apache.uima.jcas.cas.Sofa. 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: AeroRemoteApiController.java    From webanno with Apache License 2.0 6 votes vote down vote up
private static void forceOverwriteSofa(CAS aCas, String aValue)
{
    try {
        Sofa sofa = (Sofa) aCas.getSofa();
        MethodHandle _FH_sofaString = (MethodHandle) FieldUtils.readField(sofa,
                "_FH_sofaString", true);
        Method method = MethodUtils.getMatchingMethod(Sofa.class, "wrapGetIntCatchException",
                MethodHandle.class);
        int adjOffset;
        try {
            method.setAccessible(true);
            adjOffset = (int) method.invoke(null, _FH_sofaString);
        }
        finally {
            method.setAccessible(false);
        }
        sofa._setStringValueNcWj(adjOffset, aValue);
    }
    catch (Exception e) {
        throw new IllegalStateException("Cannot force-update SofA string", e);
    }
}
 
Example #2
Source File: XCASSerializer.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Push the indexed FSs onto the queue.
 */
private void enqueueIndexed() {
  Collection<Sofa> sofaCollection = cas.getBaseIndexRepositoryImpl().<Sofa>getIndexedFSs(Sofa.class);
  int sofaCount = sofaCollection.size();
  if (sofaCount > 0) {
    Sofa[] allSofas = sofaCollection.toArray(new Sofa[sofaCount]);
    
    // XCAS requires sofas in order of id
    Arrays.sort(allSofas, (fs1, fs2) -> Integer.compare(fs1._id, fs2._id) );
    enqueueArray(allSofas, 0);
  }

  // Get indexes for each SofaFS in the CAS
  for (int sofaNum = 1, numViews = cas.getViewCount(); sofaNum <= numViews; sofaNum++) {
    FSIndexRepositoryImpl viewIR = (FSIndexRepositoryImpl) cas.getBaseCAS().getSofaIndexRepository(sofaNum);
    if (viewIR != null) {
      Collection<TOP> fssInView = viewIR.getIndexedFSs();
      if (! fssInView.isEmpty()) {
        enqueueCollection(fssInView, sofaNum);
      }
    }
  }
}
 
Example #3
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
public void assertEqualsInner(CAS c1, CAS c2) {
  alreadyCompared.clear();
  
  // this code handles initial views with no SofaFS
  CAS initialView1 = c1.getView(CAS.NAME_DEFAULT_SOFA);
  CAS initialView2 = c2.getView(CAS.NAME_DEFAULT_SOFA);
  assertEqualViewsInner(initialView1, initialView2);
  // this code skips the initial view, if it doesn't have a sofa FS
  FSIterator<Sofa> sofaIter = c1.getSofaIterator();
  int c1Sofas = 0;
  while (sofaIter.hasNext()) {
    SofaFS sofa = sofaIter.next();
    CAS tcas1 = c1.getView(sofa);
    CAS tcas2 = c2.getView(tcas1.getViewName());
    assertEqualViewsInner(tcas1, tcas2);
    c1Sofas++;
  }
  sofaIter = c2.getSofaIterator();
  int c2Sofas = 0;
  while (sofaIter.hasNext()) {
    c2Sofas++;
    sofaIter.moveToNext();
  }
  Assert.assertTrue(c1Sofas == c2Sofas);
}
 
Example #4
Source File: BinaryCasSerDes.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
private Sofa makeSofaFromHeap(int sofaAddr, StringHeap stringHeap2, CommonSerDesSequential csds, boolean isUnordered) {
  TOP sofa = csds.addr2fs.get(sofaAddr);
  if (sofa != null) {
    return (Sofa) sofa;
  }
  // create sofa
  int sofaNum = heapFeat(sofaAddr, tsi.sofaNum);
  String sofaName = stringHeap2.getStringForCode(heapFeat(sofaAddr, tsi.sofaId));
  sofa = baseCas.createSofa(sofaNum, sofaName, null);
  if (isUnordered) {
    csds.addFSunordered(sofa, sofaAddr);
  } else {
    csds.addFS(sofa, sofaAddr);
  }
  return (Sofa) sofa;
}
 
Example #5
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<CAS> getViewIterator(String localViewNamePrefix) {
  // do sofa mapping for current component
  String absolutePrefix = null;
  if (getCurrentComponentInfo() != null) {
    absolutePrefix = getCurrentComponentInfo().mapToSofaID(localViewNamePrefix);
  }
  if (absolutePrefix == null) {
    absolutePrefix = localViewNamePrefix;
  }

  // find Sofas with this prefix
  List<CAS> viewList = new ArrayList<>();
  FSIterator<Sofa> sofaIter = getSofaIterator();
  while (sofaIter.hasNext()) {
    SofaFS sofa = sofaIter.next();
    String sofaId = sofa.getSofaID();
    if (sofaId.startsWith(absolutePrefix)) {
      if ((sofaId.length() == absolutePrefix.length())
          || (sofaId.charAt(absolutePrefix.length()) == '.')) {
        viewList.add(getView(sofa));
      }
    }
  }
  return viewList.iterator();
}
 
Example #6
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
@Override
public CAS createView(String aSofaID) {
  // do sofa mapping for current component
  String absoluteSofaName = null;
  if (getCurrentComponentInfo() != null) {
    absoluteSofaName = getCurrentComponentInfo().mapToSofaID(aSofaID);
  }
  if (absoluteSofaName == null) {
    absoluteSofaName = aSofaID;
  }

  // Can't use name of Initial View
  if (CAS.NAME_DEFAULT_SOFA.equals(absoluteSofaName)) {
    throw new CASRuntimeException(CASRuntimeException.SOFANAME_ALREADY_EXISTS, aSofaID);
  }
  Sofa newSofa = createSofa(absoluteSofaName, null);
  CAS newView = getView(newSofa);
  ((CASImpl) newView).registerView(newSofa);
  return newView;
}
 
Example #7
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
Sofa createSofa(int sofaNum, String sofaName, String mimeType) {
    if (this.svd.sofaNameSet.contains(sofaName)) {
      throw new CASRuntimeException(CASRuntimeException.SOFANAME_ALREADY_EXISTS, sofaName);
    }
    final boolean viewAlreadyExists = sofaNum == this.svd.viewCount;
    if (!viewAlreadyExists) {
      if (sofaNum == 1) {      // skip the test for sofaNum == 1 - this can be set "later"
        if (this.svd.viewCount == 0) {
          this.svd.viewCount = 1;
        } // else it is == or higher, so don't reset it down
      } else { // sofa is not initial sofa - is guaranteed to be set when view created
//        if (sofaNum != this.svd.viewCount + 1) {
//          System.out.println("debug");
//        }
        assert (sofaNum == this.svd.viewCount + 1);
        this.svd.viewCount = sofaNum;        
      }
    }
    
    Sofa sofa = new Sofa(
        getTypeSystemImpl().sofaType, 
        this.getBaseCAS(),  // view for a sofa is the base cas to correspond to where it gets indexed
        sofaNum,
        sofaName,
        mimeType);
    
    this.getBaseIndexRepository().addFS(sofa);
    this.svd.sofaNameSet.add(sofaName);
    if (!viewAlreadyExists) {
      getView(sofa);  // create the view that goes with this Sofa
    }
    return sofa;
  }
 
Example #8
Source File: JsonCasSerializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeView(Sofa sofa, Collection<TOP> added, Collection<TOP> deleted, Collection<TOP> reindexed) throws IOException {
  jch.writeNlJustBeforeNext();
  jg.writeFieldName(cds.getXmiId(sofa));
  jg.writeStartObject();
  writeViewForDeltas(ADDED_MEMBERS_NAME, added);
  writeViewForDeltas(DELETED_MEMBERS_NAME, deleted);
  writeViewForDeltas(REINDEXED_MEMBERS_NAME, reindexed);      
  jg.writeEndObject();
}
 
Example #9
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
Sofa createInitialSofa(String mimeType) { 
  Sofa sofa = createSofa(1, CAS.NAME_DEFAULT_SOFA, mimeType);

  registerInitialSofa();
  this.mySofaRef = sofa;
  return sofa;
}
 
Example #10
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private SofaFS getSofa(String sofaName) {
  FSIterator<Sofa> iterator = this.svd.baseCAS.getSofaIterator();
  while (iterator.hasNext()) {
    SofaFS sofa = iterator.next();
    if (sofaName.equals(sofa.getSofaID())) {
      return sofa;
    }
  }
  throw new CASRuntimeException(CASRuntimeException.SOFANAME_NOT_FOUND, sofaName);
}
 
Example #11
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public Sofa getSofaRef() {
  if (this.mySofaRef == null) {
    // create the SofaFS for _InitialView ...
    // ... and reset mySofaRef to point to it
    this.mySofaRef = this.createInitialSofa(null);
  }
  return this.mySofaRef;
}
 
Example #12
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Internal use, for cases where deserializing - special case setting sofString to skip updating the document annotation
 * @param fs -
 * @param feat -
 * @param s -
 */
public static void setFeatureValueFromStringNoDocAnnotUpdate(FeatureStructureImplC fs, FeatureImpl feat, String s) {
  if (fs instanceof Sofa && 
      feat.getCode() == sofaStringFeatCode) {
    ((Sofa)fs).setLocalSofaDataNoDocAnnotUpdate(s);
  } else {
    setFeatureValueFromString(fs, feat, s);
  }
}
 
Example #13
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
  public CASImpl getView(SofaFS aSofa) {
    Sofa sofa = (Sofa) aSofa;
    final int sofaNbr = sofa.getSofaRef();
//    final Integer sofaNbrInteger = Integer.valueOf(sofaNbr);

    CASImpl aView = svd.getViewFromSofaNbr(sofaNbr);
    if (null == aView) {
      // This is the deserializer case, or the case where an older API created a
      // sofa,
      // which is now creating the associated view

      // create a new CAS view
      aView = new CASImpl(this.svd.baseCAS, sofa);
      svd.setViewForSofaNbr(sofaNbr, aView);
      verifySofaNameUniqueIfDeserializedViewAdded(sofaNbr, sofa);
      return aView;
    }

    // for deserialization - might be reusing a view, and need to tie new Sofa
    // to old View
    if (null == aView.mySofaRef) {
      aView.mySofaRef = sofa;
    }

    verifySofaNameUniqueIfDeserializedViewAdded(sofaNbr, aSofa);
    return aView;
  }
 
Example #14
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private void setSofaThingsMime(Consumer<Sofa> c, String msg) {
  if (this == this.svd.baseCAS) {
    throw new CASRuntimeException(CASRuntimeException.INVALID_BASE_CAS_METHOD, msg);
  }   
  Sofa sofa = getSofaRef();
  c.accept(sofa);
}
 
Example #15
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends CAS> Iterator<T> getViewIterator() {
  return new Iterator<T>() {
    
    final CASImpl initialView = getInitialView();  // creates one if not existing, w/o sofa  

    boolean isInitialView_but_noSofa = !initialView.mySofaIsValid(); // true if has no Sofa in initial view
                                                         //     but is reset to false once iterator moves
                                                         //     off of initial view.
    
                                                         // if initial view has a sofa, we just use the 
                                                         // sofa iterator instead.

    final FSIterator<Sofa> sofaIter = getSofaIterator(); 

    @Override
    public boolean hasNext() {
      if (isInitialView_but_noSofa) {
        return true;
      }
      return sofaIter.hasNext(); 
    }

    @Override
    public T next() {
      if (isInitialView_but_noSofa) {
        isInitialView_but_noSofa = false;  // no incr of sofa iterator because it was missing initial view
        return (T) initialView;
      }
      return (T) getView(sofaIter.next());
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
    
  };
}
 
Example #16
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * excludes initial view if its sofa is not valid
 * 
 * @return iterator over all views except the base view
 */
public Iterator<CASImpl> getViewImplIterator() {
  return new Iterator<CASImpl>() {
    
    final CASImpl initialView = getInitialView();  // creates one if not existing, w/o sofa  

    boolean isInitialView_but_noSofa = !initialView.mySofaIsValid(); // true if has no Sofa in initial view
                                                         //     but is reset to false once iterator moves
                                                         //     off of initial view.
    
                                                         // if initial view has a sofa, we just use the 
                                                         // sofa iterator instead.

    final FSIterator<Sofa> sofaIter = getSofaIterator(); 

    @Override
    public boolean hasNext() {
      if (isInitialView_but_noSofa) { // set to false once iterator moves off of first value
        return true;
      }
      return sofaIter.hasNext(); 
    }

    @Override
    public CASImpl next() {
      if (isInitialView_but_noSofa) {
        isInitialView_but_noSofa = false;  // no incr of sofa iterator because it was missing initial view
        return initialView;
      }
      return getView(sofaIter.next());
    }

    @Override
    public void remove() {
      throw new UnsupportedOperationException();
    }
    
  };
}
 
Example #17
Source File: BinaryCasSerDes.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private Sofa getSofaFromAnnotBase(int annotBaseAddr, StringHeap stringHeap2, Int2ObjHashMap<TOP, TOP> addr2fs,
                                  CommonSerDesSequential csds) {
  int sofaAddr = heapFeat(annotBaseAddr, tsi.annotBaseSofaFeat);
  if (0 == sofaAddr) {
    return null;
  }
  // get existing sofa or create sofa
  return makeSofaFromHeap(sofaAddr, stringHeap2, csds, SOFA_AHEAD_OF_NORMAL_ORDER);
}
 
Example #18
Source File: BinaryCasSerDes.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param fs
 * @param feat
 * @param s
 * @param fixups4forwardFsRefs
 * @return true if caller needs to do an appropriate fs._setStringValue...
 */
private boolean updateStringFeature(TOP fs, FeatureImpl feat, String s, List<Runnable> fixups4forwardFsRefs) {
  if (null == s) {
    return false;  // null is the default value, no need to set it
  }
  if (fs instanceof Sofa) {
    if (feat == tsi.sofaId) {
      return false;  // do nothing, this value was already used
    }
    Sofa sofa = (Sofa)fs;
    if (feat == tsi.sofaMime) {
      sofa.setMimeType(s);
      return false;
    }
    if (feat == tsi.sofaUri) {
      sofa.setRemoteSofaURI(s);
      return false;
    }
    if (feat == tsi.sofaString) {
      if (fixups4forwardFsRefs != null) {
        // has to be deferred because it updates docAnnot which might not be deser yet.
        //   TODO no longer needed, calls the version which doesn't update docAnnot 9/2017
        Sofa capturedSofa = sofa;
        String capturedString = s;
        fixups4forwardFsRefs.add(() -> capturedSofa.setLocalSofaDataNoDocAnnotUpdate(capturedString));
      } else {
        sofa.setLocalSofaData(s);
      }
      return false;
    }
  }
  return true; //    fs._setStringValueNcNj(feat, s);
}
 
Example #19
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
private static List<TOP> populate(Collection<TOP> items, Set<TOP> visited) {
  List<TOP> s = new ArrayList<>();
  for (TOP fs : items) {
    if (!(fs instanceof Sofa) && !visited.contains(fs)) {
      s.add(fs);
    }
  }
  return s;
}
 
Example #20
Source File: JsonCasSerializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeView(Sofa sofa, Collection<TOP> members) throws IOException {
  jch.writeNlJustBeforeNext();
  String sofaXmiId = (null == sofa) ? "0" : cds.getXmiId(sofa);
  jg.writeArrayFieldStart(sofaXmiId);
  writeViewMembers(members);
  //check for out-of-typesystem members
  if (cds.sharedData != null) {
    List<String> ootsMembers = cds.sharedData.getOutOfTypeSystemViewMembers(sofaXmiId);
    jch.writeNlJustBeforeNext();
    writeViewMembers(ootsMembers);
  }

  jg.writeEndArray();
}
 
Example #21
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
public boolean isBackwardCompatibleCas() {
  // check that there is exactly one sofa
  if (this.svd.viewCount != 1) {
    return false;
  }

  if (!this.svd.initialSofaCreated) {
    return false;
  }
  
  Sofa sofa = this.getInitialView().getSofa();

  // check for mime type exactly equal to "text"
  String sofaMime = sofa.getMimeType();
  if (!"text".equals(sofaMime)) {
    return false;
  }
  // check that sofaURI and sofaArray are not set
  String sofaUri = sofa.getSofaURI();
  if (sofaUri != null) {
    return false;
  }
  TOP sofaArray = sofa.getSofaArray();
  if (sofaArray != null) {
    return false;
  }
  // check that name is NAME_DEFAULT_SOFA
  String sofaname = sofa.getSofaID();
  return NAME_DEFAULT_SOFA.equals(sofaname);
}
 
Example #22
Source File: CasSerializerSupport.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param sofaNum - starts at 1
 * @return the sofa FS, or null
 */
public Sofa getSofa(int sofaNum) {  
  if (sofaNum != 1 || cas.isInitialSofaCreated()) { //skip if initial view && no Sofa yet
                                                    // all non-initial-views must have a sofa
    return ((CASImpl)cas.getView(sofaNum)).getSofaRef();
  }
  return null;
}
 
Example #23
Source File: XCASDeserializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Common code run at finalize time, to set ref values and handle out-of-typesystem data
 * 
 * @param extId the external ID identifying either a deserialized FS or an out-of-typesystem instance
 * @param fs Feature Structure whose fi reference feature is to be set with a value derived from extId and FSinfo
 * @param fi the featureImpl
*/
private void finalizeRefValue(int extId, TOP fs, FeatureImpl fi) {
  FSInfo fsInfo = fsTree.get(extId);
  if (fsInfo == null) {

    // this feature may be a ref to an out-of-typesystem FS.
    // add it to the Out-of-typesystem features list (APL)
    if (extId != 0 && outOfTypeSystemData != null) {
      List<Pair<String, Object>> ootsAttrs = outOfTypeSystemData.extraFeatureValues.computeIfAbsent(fs, k -> new ArrayList<>());
      String featFullName = fi.getName();
      int separatorOffset = featFullName.indexOf(TypeSystem.FEATURE_SEPARATOR);
      String featName = "_ref_" + featFullName.substring(separatorOffset + 1);
      ootsAttrs.add(new Pair(featName, Integer.toString(extId)));
    }
    CASImpl.setFeatureValueMaybeSofa(fs, fi, null);
  } else {
    // the sofa ref in annotationBase is set when the fs is created, not here
    if (fi.getCode() != TypeSystemConstants.annotBaseSofaFeatCode) { 
      if (fs instanceof Sofa) {
        // special setters for sofa values
        Sofa sofa = (Sofa) fs;
        switch (fi.getRangeImpl().getCode()) {
        case TypeSystemConstants.sofaArrayFeatCode: sofa.setLocalSofaData(fsInfo.fs); break;
        default: throw new CASRuntimeException(UIMARuntimeException.INTERNAL_ERROR);
        }
        return;
      }
      
      // handle case where feature is xyz[] (an array ref, not primitive) but the value of fs is FSArray
      ts.fixupFSArrayTypes(fi.getRangeImpl(), fsInfo.fs);
      CASImpl.setFeatureValueMaybeSofa(fs, fi, fsInfo.fs);
    }
  }
}
 
Example #24
Source File: XmiCasDeserializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @param sofaXmiIdAsString xmiId
 * @param sofaNum 1 if sofa null, or the sofa Xmi Id
 * @return the FS Repository associated with the sofa xmiId
 * @throws XCASParsingException
 */
private FSIndexRepositoryImpl getIndexRepo(String sofaXmiIdAsString, int sofaXmiId) throws XCASParsingException {
  // a view with no Sofa will be added to the 1st, _InitialView, index

  if (sofaXmiIdAsString == null) {
    return (FSIndexRepositoryImpl) indexRepositories.get(1);
  }
  // translate sofa's xmi:id into its sofanum
  Sofa sofa = (Sofa) maybeGetFsForXmiId(sofaXmiId);
  if (null == sofa) {
    if (sofaXmiId == 0) report0xmiId();  //debug
    throw createException(XCASParsingException.UNKNOWN_ID, Integer.toString(sofaXmiId));
  }
  return (FSIndexRepositoryImpl) indexRepositories.get(sofa.getSofaNum());
}
 
Example #25
Source File: XmiCasDeserializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * Deserialize one feature 
 * called from readFS 751
 * called from processDeferred, to handle features specified as child elements
 * @param type -
 * @param fs the FS
 * @param featName the feature name
 * @param featVal the value of the feature
 * @param isNewFS  true if this is a new FS
 * @return feature code or -1 if no feature in this type system
 * @throws SAXException if Type doesn't have the feature, and we're not in lenient mode
 */
private int handleFeatureFromName(final TypeImpl type, TOP fs, String featName, String featVal, final boolean isNewFS) throws SAXException {
  final FeatureImpl feat = (FeatureImpl) type.getFeatureByBaseName(featName);
  if (feat == null) {
    if (!this.lenient) {
      throw createException(XCASParsingException.UNKNOWN_FEATURE, featName);
    }
    else {
      // this logic mimics the way version 2 did this.
      if (isDoingDeferredChildElements) {
        ArrayList<String> featValAsArrayList = new ArrayList<>(1);
        featValAsArrayList.add(featVal);
        sharedData.addOutOfTypeSystemChildElements(fs, featName, featValAsArrayList);
      } else {
        sharedData.addOutOfTypeSystemAttribute(fs, featName, featVal);
      }
    }
    return -1;
  }
  
  //Sofa FS  
  //only update Sofa data features and mime type feature. skip other features.
  //skip Sofa data features if Sofa data is already set.
  //these features may not be modified.
  if ((fs instanceof Sofa) && !isNewFS) {
  	if (featName.equals(CAS.FEATURE_BASE_NAME_SOFAID) || 
  		  featName.equals(CAS.FEATURE_BASE_NAME_SOFANUM))   { 
        return feat.getCode();
  	} else if (((Sofa)fs).isSofaDataSet()) {
  	  return feat.getCode();
  	}
  }

  handleFeatSingleValue(fs, feat, featVal);
  return feat.getCode();
}
 
Example #26
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
CASImpl(CASImpl cas, SofaFS aSofa) {
  
  // these next fields are final and must be set in the constructor
  this.svd = cas.svd;

  this.mySofaRef = (Sofa) aSofa;

  // get the indexRepository for this Sofa
  this.indexRepository = (this.mySofaRef == null) ? 
      (FSIndexRepositoryImpl) cas.getSofaIndexRepository(1) : 
      (FSIndexRepositoryImpl) cas.getSofaIndexRepository(aSofa);
  if (null == this.indexRepository) {
    // create the indexRepository for this CAS
    // use the baseIR to create a lightweight IR copy
    FSIndexRepositoryImpl baseIndexRepo = (FSIndexRepositoryImpl) cas.getBaseIndexRepository();
    this.indexRepository = new FSIndexRepositoryImpl(this, baseIndexRepo);
    // the index creation depends on "indexRepository" already being set
    baseIndexRepo.name2indexMap.keySet().stream().forEach(key -> this.indexRepository.createIndex(baseIndexRepo, key));
    this.indexRepository.commit();
    // save new sofa index
    if (this.mySofaRef == null) {
      cas.setSofaIndexRepository(1, this.indexRepository);
    } else {
      cas.setSofaIndexRepository(aSofa, this.indexRepository);
    }
  }
}
 
Example #27
Source File: XmiCasSerializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
protected void writeView(Sofa sofa, Collection<TOP> added, Collection<TOP> deleted, Collection<TOP> reindexed) throws SAXException {
  String sofaXmiId = cds.getXmiId(sofa);
  workAttrs.clear();
  if (sofaXmiId != null && sofaXmiId.length() > 0) {
    addAttribute(workAttrs, "sofa", sofaXmiId);
  }
  writeViewForDeltas("added_members", added);
  writeViewForDeltas("deleted_members", deleted);
  writeViewForDeltas("reindexed_members", reindexed);
        
  XmlElementName elemName = uimaTypeName2XmiElementName("uima.cas.View");
  startElement(elemName, workAttrs, 0);
  endElement(elemName);
}
 
Example #28
Source File: XmiCasSerializer.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
@Override
    protected void writeView(Sofa sofa, Collection<TOP> members) throws Exception {
      workAttrs.clear();
      // this call should never generate a new XmiId, it should just retrieve the existing one for the sofa
      String sofaXmiId = (sofa == null) ? null : cds.getXmiId(sofa);   
      if (sofaXmiId != null && sofaXmiId.length() > 0) {
        addAttribute(workAttrs, "sofa", sofaXmiId);
      }
      StringBuilder membersString = new StringBuilder();
      boolean isPastFirstElement = writeViewMembers(membersString, members);
      //check for out-of-typesystem members
      if (cds.sharedData != null) {
        List<String> ootsMembers = cds.sharedData.getOutOfTypeSystemViewMembers(sofaXmiId);
        writeViewMembers(membersString, ootsMembers, isPastFirstElement);
      }
      if (membersString.length() > 0) {
        workAttrs.addAttribute(
            "", 
            "members",
            "members",
            CDATA_TYPE, 
            membersString.toString());

//      addAttribute(workAttrs, "members", membersString.substring(0, membersString.length() - 1));
        if (membersString.length() > 0) {
          XmlElementName elemName = uimaTypeName2XmiElementName("uima.cas.View");
          startElement(elemName, workAttrs, 0);
          endElement(elemName);
        }
      }
    }
 
Example #29
Source File: CASImpl.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
void refreshView(CAS cas, SofaFS aSofa) {

    if (aSofa != null) {
      // save address of SofaFS
      this.mySofaRef = (Sofa) aSofa;
    } else {
      // this is the InitialView
      this.mySofaRef = null;
    }

    // toss the JCas, if it exists
    this.jcas = null;

    // create the indexRepository for this Sofa
    final FSIndexRepositoryImpl baseIndexRepo = (FSIndexRepositoryImpl) ((CASImpl) cas).getBaseIndexRepository();
    this.indexRepository = new FSIndexRepositoryImpl(this,baseIndexRepo);
    // the index creation depends on "indexRepository" already being set
    baseIndexRepo.name2indexMap.keySet().stream().forEach(key -> this.indexRepository.createIndex(baseIndexRepo, key));

    this.indexRepository.commit();
    // save new sofa index
    if (this.mySofaRef == null) {
      ((CASImpl) cas).setSofaIndexRepository(1, this.indexRepository);
    } else {
      ((CASImpl) cas).setSofaIndexRepository(aSofa, this.indexRepository);
    }
  }
 
Example #30
Source File: CasComparer.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param fs1
 * @param fs2
 * @param visited when called for sorting FSs, is sortCompareSeen(cleared); 
 *                when called for comparing for equality, holds FSs already compared in other views
 * @return
 */
private int compare1(TOP fs1, TOP fs2, Set<TOP> visited) {
  if (!isSortUse) {  // only do null check for non- sort use
    if (fs1 == null && fs2 == null) {
      return 0;
    }
    if (fs1 == null) return chkEqual(-1, "fs1 was null and fs2 was not");
    if (fs2 == null) return chkEqual(1,  "fs2 was null and fs1 was not");
  }
  
  boolean wasPresent1 = !visited.add(fs1);
  boolean wasPresent2 = !visited.add(fs2);
  
  if (wasPresent1 && wasPresent2) {
    return 0;  // already checked and found equal
  }
  
  if (!wasPresent1 && !wasPresent2) {
    int r;
    TypeImpl t1, t2;
    if (0 != (r = compStr((t1 = (TypeImpl) fs1.getType()).getName(), (t2 = (TypeImpl) fs2.getType()).getName()))) {
      return chkEqual(r, "Types of FSs are different: Type1 = %s, Type2 = %s", t1, t2);
    }
    // types are the same
    
    if (CAS.TYPE_NAME_SOFA.equals(t1.getName())) {
      if (isSortUse) {
        return Integer.compare(((Sofa)fs1).getSofaNum(), ((Sofa)fs2).getSofaNum());
      }
      return 0;  // skip comparing sofa so this routine can be used for cas copier testing
    }

    if (t1.isArray()) {
      final int len1 = ((CommonArrayFS)fs1).size();
      if (0 != (r = Integer.compare(len1, ((CommonArrayFS)fs2).size()))) {
        return r;
      }
      
      SlotKind kind = t1.getComponentSlotKind();
      
      switch(kind) {
      case Slot_BooleanRef: return compareAllArrayElements(len1, i -> Boolean.compare(((BooleanArray  )fs1).get(i), ((BooleanArray)fs2).get(i)), "Miscompare Boolean Arrays %n%s%n%s", fs1, fs2);
      case Slot_ByteRef:    return compareAllArrayElements(len1, i -> Byte   .compare(((ByteArray     )fs1).get(i), ((ByteArray   )fs2).get(i)), "Miscompare Byte Arrays %n%s%n%s"   , fs1, fs2);
      case Slot_ShortRef:   return compareAllArrayElements(len1, i -> Short  .compare(((ShortArray    )fs1).get(i), ((ShortArray  )fs2).get(i)), "Miscompare Short Arrays %n%s%n%s"  , fs1, fs2);
      case Slot_Int:     return compareAllArrayElements   (len1, i -> Integer.compare(((IntegerArray  )fs1).get(i), ((IntegerArray)fs2).get(i)), "Miscompare Integer Arrays %n%s%n%s", fs1, fs2);
      case Slot_LongRef:    return compareAllArrayElements(len1, i -> Long   .compare(((LongArray     )fs1).get(i), ((LongArray   )fs2).get(i)), "Miscompare Long Arrays %n%s%n%s", fs1, fs2);
      case Slot_Float:   return compareAllArrayElements   (len1, i -> Integer.compare(Float.floatToRawIntBits   (((FloatArray )fs1).get(i)), 
                                                                                      Float.floatToRawIntBits   (((FloatArray )fs2).get(i))),    "Miscompare Float Arrays %n%s%n%s", fs1, fs2);
      case Slot_DoubleRef:  return compareAllArrayElements(len1, i -> Long   .compare(Double.doubleToRawLongBits(((DoubleArray)fs1).get(i)), 
                                                                                      Double.doubleToRawLongBits(((DoubleArray)fs2).get(i))),    "Miscompare Double Arrays %n%s%n%s", fs1, fs2);
      case Slot_HeapRef: return    compareAllArrayElements(len1, i ->        compare1((TOP)((FSArray<?>       )fs1).get(i), (TOP)((FSArray<?> )fs2).get(i), visited), "Miscompare FS Arrays %n%s%n%s", fs1, fs2);
      case Slot_StrRef:  return    compareAllArrayElements(len1, i -> Misc.compareStrings(((StringArray   )fs1).get(i), ((StringArray )fs2).get(i)), "Miscompare String Arrays %n%s%n%s", fs1, fs2);
      default: 
        Misc.internalError(); return 0;  // only to avoid a compile error
      }        
    }
    
    ts = fs1.getCAS().getTypeSystem();
    return compareFeatures(fs1, fs2, t1.getFeatureImpls(), t2.getFeatureImpls(), visited);           
  }
  
  // getting here: one was already traversed, the other not.  Possible use case:
  //   fs1 is a list with a loop; fs2 is a list without a loop
  //   arbitrarily return the one with a loop first
  
  if (fs1 instanceof EmptyList) { 
    return 0;  // allow different or shared EmptyList instances to compare equal
    // because some deserializers or user code can create them as shared or not
  }
  if (wasPresent1) {
    return chkEqual(-1, "First element had a ref loop %s%n, second didn't so far %s", fs1, fs2);
  }
  return chkEqual(-1, "Second element had a ref loop %s%n, first didn't so far %s", fs2, fs1);
  
}