下面列出了org.eclipse.jface.viewers.CheckboxCellEditor#org.apache.uima.cas.FeatureStructure 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Convert an SFArray to a string.
*
* @param <S> the generic type
* @param array the array
* @param clazz the clazz
* @param toString the to string
* @param separator the separator
* @return the string
*/
// NOTE This is checked by the filter
@SuppressWarnings("unchecked")
protected <S> String asString(
FSArray array, Class<S> clazz, Function<S, String> toString, String separator) {
if (array == null) {
return "";
}
final FeatureStructure[] fses = array.toArray();
if (fses == null) {
return "";
}
return Arrays.stream(fses)
.filter(Objects::nonNull)
.filter(fs -> clazz.isAssignableFrom(fs.getClass()))
.map(fs -> toString.apply((S) fs))
.collect(Collectors.joining(separator));
}
/**
* Get the feature value of this {@code Feature} on this annotation
*/
private static Object getFeatureValue(FeatureStructure aFS, Feature aFeature)
{
switch (aFeature.getRange().getName()) {
case CAS.TYPE_NAME_STRING:
return aFS.getFeatureValueAsString(aFeature);
case CAS.TYPE_NAME_BOOLEAN:
return aFS.getBooleanValue(aFeature);
case CAS.TYPE_NAME_FLOAT:
return aFS.getFloatValue(aFeature);
case CAS.TYPE_NAME_INTEGER:
return aFS.getIntValue(aFeature);
case CAS.TYPE_NAME_BYTE:
return aFS.getByteValue(aFeature);
case CAS.TYPE_NAME_DOUBLE:
return aFS.getDoubleValue(aFeature);
case CAS.TYPE_NAME_LONG:
aFS.getLongValue(aFeature);
case CAS.TYPE_NAME_SHORT:
aFS.getShortValue(aFeature);
default:
return null;
// return aFS.getFeatureValue(aFeature);
}
}
public void checkDeltaWithAllMods() {
makeRandomFss(casSrc, mSrc, Akof1, 7);
TTypeSystem m = getTT(EqTwoTypes);
remoteCas = setupCas(m);
loadCas(casSrc, mSrc);
ReuseInfo ri[] = serializeDeserialize(casSrc, remoteCas, null, null);
MarkerImpl marker = (MarkerImpl) remoteCas.createMarker();
lfs = getIndexedFSs(remoteCas, m);
makeRandomFss(remoteCas, m, Akof1, 8);
int i = 0;
for (FeatureStructure fs : lfs) {
if (((i++) % 2) == 0) {
maybeSetFeature(fs, m, lfs.get(random.nextInt(lfs.size())));
}
}
makeRandomUpdatesBelowMark(remoteCas, m, Akof1);
verifyDelta(marker, ri);
}
public void testCas() {
CAS cas = this.jcas.getCas();
TypeSystem ts = cas.getTypeSystem();
Type annotType = ts.getType(annotationTypeName);
FeatureStructure fs = cas.createFS(annotType);
Feature stringSetFeat = ts.getFeatureByFullName(annotationTypeName
+ TypeSystem.FEATURE_SEPARATOR + stringSetFeatureName);
fs.setStringValue(stringSetFeat, definedValue1);
fs.setStringValue(stringSetFeat, definedValue2);
fs.setStringValue(stringSetFeat, definedValue3);
// next should be ok https://issues.apache.org/jira/browse/UIMA-1839
fs.setStringValue(stringSetFeat, null);
boolean exCaught = false;
try {
fs.setStringValue(stringSetFeat, undefinedValue);
} catch (CASRuntimeException e) {
exCaught = true;
}
assertTrue(exCaught);
}
CASArtifact(
@Nullable LabelAdapters labelAdapters,
CAS cas,
String artifactID
) {
this.labelAdapters = labelAdapters;
this.cas = cas;
TypeSystem typeSystem = cas.getTypeSystem();
metadataType = typeSystem.getType("ArtifactMetadata");
keyFeature = metadataType.getFeatureByBaseName("key");
valueFeature = metadataType.getFeatureByBaseName("value");
metadataCas = cas.createView("metadata");
metadataCas.setDocumentText("");
Type idType = typeSystem.getType("ArtifactID");
Feature idFeat = idType.getFeatureByBaseName("artifactID");
this.artifactID = artifactID;
FeatureStructure documentIdFs = metadataCas.createFS(idType);
documentIdFs.setStringValue(idFeat, artifactID);
metadataCas.addFsToIndexes(documentIdFs);
metadataIndex = metadataCas.getIndexRepository().getIndex("metadata", metadataType);
casMetadata = new CASMetadata();
}
/**
* Convenience constructor which only needs the callback for encountered feature structures.
*
* @param featureStructureEncounteredCallback callback for encountered feature structures
*/
FsCopiers(UnaryOperator<FeatureStructure> featureStructureEncounteredCallback,
FeatureCopiers featureCopiers) {
this.featureCopiers = featureCopiers;
this.featureStructureEncounteredCallback = featureStructureEncounteredCallback;
fsCopiers = new HashMap<>();
fsCopiers.put(CAS.TYPE_NAME_BOOLEAN_ARRAY, copyArray(BooleanArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_BYTE_ARRAY, copyArray(ByteArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_DOUBLE_ARRAY, copyArray(DoubleArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_FLOAT_ARRAY, copyArray(FloatArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_FS_ARRAY, this::copyFsArray);
fsCopiers.put(CAS.TYPE_NAME_LONG_ARRAY, copyArray(LongArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_INTEGER_ARRAY, copyArray(IntArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_SHORT_ARRAY, copyArray(ShortArrayFS.class));
fsCopiers.put(CAS.TYPE_NAME_STRING_ARRAY, copyArray(StringArrayFS.class));
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (NamedEntityCandidate_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = NamedEntityCandidate_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new NamedEntityCandidate(addr, NamedEntityCandidate_Type.this);
NamedEntityCandidate_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new NamedEntityCandidate(addr, NamedEntityCandidate_Type.this);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (EventMention_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = EventMention_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new EventMention(addr, EventMention_Type.this);
EventMention_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new EventMention(addr, EventMention_Type.this);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (DocumentTextHolder_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = DocumentTextHolder_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new DocumentTextHolder(addr, DocumentTextHolder_Type.this);
DocumentTextHolder_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new DocumentTextHolder(addr, DocumentTextHolder_Type.this);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (Zone_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = Zone_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new Zone(addr, Zone_Type.this);
Zone_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new Zone(addr, Zone_Type.this);
}
/**
* Fetch all annotations of the given type or its sub-types from the given FS array.
*
* @param aArray
* the FS array
* @param aType
* the CAS type.
* @return a new collection of all feature structures of the given type.
*/
public static <T extends FeatureStructure> List<T> create(ArrayFS<T> aArray, Type aType) {
TypeSystem ts = aArray.getCAS().getTypeSystem();
List<FeatureStructure> data = new ArrayList<FeatureStructure>(aArray.size());
for (int i = 0; i < aArray.size(); i++) {
FeatureStructure value = aArray.get(i);
if (value != null && (aType == null || ts.subsumes(aType, value.getType()))) {
data.add(value);
}
}
return (List<T>) asList(data.toArray(new FeatureStructure[data.size()]));
}
public boolean match(FeatureStructure fs) {
final FeatureStructureImplC fsi = (FeatureStructureImplC) fs;
compile(fsi.getCAS().getTypeSystem());
final int typeCode = fsi._getTypeCode();
TypeSystemImpl tsi = (TypeSystemImpl) this.ts;
for (int i = 0; i < typeSet.size(); i++) {
if (tsi.subsumes(typeSet.get(i), typeCode)) {
return true;
}
}
return false;
}
/**
* If two relations share any end point (source or target), they are considered to be
* <b>overlapping</b>.
*/
public static boolean overlapping(FeatureStructure aRel1Src, FeatureStructure aRel1Tgt,
FeatureStructure aRel2Src, FeatureStructure aRel2Tgt)
{
return isSame(aRel1Src, aRel2Src) || isSame(aRel1Src, aRel2Tgt)
|| isSame(aRel1Tgt, aRel2Src) || isSame(aRel1Tgt, aRel2Tgt);
}
public void testDynamicLists() throws Exception {
setupTypeSystem("allTypes.xml");
FeatureStructure[] fss = new FeatureStructure[20];
fss[0] = emptyIntList();
fss[1] = intList(33, fss[0]); // value 33, linked to 0
fss[2] = intList(22, fss[1]);
fss[3] = intList(11, fss[2]);
fss[4] = intList(110, fss[2]); // joins at 2
jcas.addFsToIndexes(fss[3]);;
jcas.addFsToIndexes(fss[4]);
jcs.setPrettyPrint(true);
serializeAndCompare("twoListMerge.txt");
jcs.setStaticEmbedding();
serializeAndCompare("twoListMergeStatic.txt");
cas.reset();
jcs = new JsonCasSerializer().setOmit0Values(true);
jcs.setPrettyPrint(true);
fss[0] = emptyIntList();
fss[1] = intList(33, fss[0]); // value 33, linked to 0
fss[2] = intList(22, fss[1]);
fss[3] = intList(11, fss[2]);
cas.addFsToIndexes(fss[3]);
serializeAndCompare("indexedSingleList.txt");
jcs.setStaticEmbedding();
serializeAndCompare("indexedSingleListStatic.txt");
}
/**
* @param fs -
* @return true if seen before
*/
boolean addReference(FeatureStructure fs) {
String v = tree.get(fs);
if (null == v) {
tree.put(fs, "seen once");
return false;
}
if (v.equals("seen once")) {
tree.put(fs, refNamePrefix + Integer.toString(this.count++));
}
return true;
}
public static void setLinkFeatureValue(FeatureStructure aFS, Feature aFeature,
List<FeatureStructure> linkFSes)
{
// Create a new array if size differs otherwise re-use existing one
ArrayFS array = (ArrayFS) WebAnnoCasUtil.getFeatureFS(aFS, aFeature.getShortName());
if (array == null || (array.size() != linkFSes.size())) {
array = aFS.getCAS().createArrayFS(linkFSes.size());
}
// Fill in links
array.copyFromArray(linkFSes.toArray(new FeatureStructure[linkFSes.size()]), 0, 0,
linkFSes.size());
aFS.setFeatureValue(aFeature, array);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (Chemical_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = Chemical_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new Chemical(addr, Chemical_Type.this);
Chemical_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new Chemical(addr, Chemical_Type.this);
}
public static void makeLinkHostFS(JCas aCas, int aBegin, int aEnd, FeatureStructure... aLinks)
{
Type hostType = aCas.getTypeSystem().getType(HOST_TYPE);
AnnotationFS hostA1 = aCas.getCas().createAnnotation(hostType, aBegin, aEnd);
hostA1.setFeatureValue(hostType.getFeatureByBaseName("links"),
FSCollectionFactory.createFSArray(aCas, asList(aLinks)));
aCas.getCas().addFsToIndexes(hostA1);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (Unknown_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = Unknown_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new Unknown(addr, Unknown_Type.this);
Unknown_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new Unknown(addr, Unknown_Type.this);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (MoleculeDictTerm_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = MoleculeDictTerm_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new MoleculeDictTerm(addr, MoleculeDictTerm_Type.this);
MoleculeDictTerm_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new MoleculeDictTerm(addr, MoleculeDictTerm_Type.this);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (Name_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = Name_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new Name(addr, Name_Type.this);
Name_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new Name(addr, Name_Type.this);
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (WordNetDictionaryMatch_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = WordNetDictionaryMatch_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new WordNetDictionaryMatch(addr, WordNetDictionaryMatch_Type.this);
WordNetDictionaryMatch_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new WordNetDictionaryMatch(addr, WordNetDictionaryMatch_Type.this);
}
@Test
public void testSimpleSlotFeature() throws Exception
{
JCas jcas = makeJCasOneSentence();
CAS cas = jcas.getCas();
List<Token> tokens = new ArrayList<>(select(jcas, Token.class));
Token t1 = tokens.get(0);
Token t2 = tokens.get(1);
Token t3 = tokens.get(2);
Type type = cas.getTypeSystem().getType("webanno.custom.SimpleSpan");
AnnotationFS s2 = cas.createAnnotation(type, t2.getBegin(), t2.getEnd());
cas.addFsToIndexes(s2);
AnnotationFS s3 = cas.createAnnotation(type, t3.getBegin(), t3.getEnd());
cas.addFsToIndexes(s3);
FeatureStructure link1 = makeLinkFS(jcas, "p1", s2);
FeatureStructure link2 = makeLinkFS(jcas, "p2", s3);
makeLinkHostFS(jcas, t1.getBegin(), t1.getEnd(), link1, link2);
writeAndAssertEquals(jcas,
WebannoTsv3Writer.PARAM_SLOT_FEATS, asList("webanno.custom.SimpleLinkHost:links"),
WebannoTsv3Writer.PARAM_SPAN_LAYERS, asList("webanno.custom.SimpleSpan",
"webanno.custom.SimpleLinkHost"),
WebannoTsv3Writer.PARAM_LINK_TYPES, asList("webanno.custom.LinkType"),
WebannoTsv3Writer.PARAM_SLOT_TARGETS, asList("webanno.custom.SimpleSpan"));
}
@Test
public void testOpsNeedingAnnotation() {
Type type = cas.getTypeSystem().getType("x.y.z.SentenceNoJCas");
FeatureStructure s = cas.createAnnotation(type, 0, 4);
cas.indexRepository.addFS(s);
boolean b = cas.<Annotation>select(type).covering(1, 2).map(f ->f.getBegin()).findFirst().isPresent();
assertTrue(b);
}
public void process(CAS aCAS) throws AnalysisEngineProcessException {
// get handle to CAS view containing XML document
CAS xmlCas = aCAS.getView("xmlDocument");
InputStream xmlStream = xmlCas.getSofa().getSofaDataStream();
// parse with detag handler
DetagHandler handler = new DetagHandler();
try {
SAXParser parser = parserFactory.newSAXParser();
parser.parse(xmlStream, handler);
} catch (Exception e) {
throw new AnalysisEngineProcessException(e);
}
// create the plain text view and set its document text
CAS plainTextView = aCAS.createView("plainTextDocument");
plainTextView.setDocumentText(handler.getDetaggedText());
plainTextView.setDocumentLanguage(aCAS.getView("_InitialView").getDocumentLanguage());
// Index the SourceDocumentInformation object, if there is one, in the new sofa.
// This is needed by the SemanticSearchCasIndexer
Iterator iter = xmlCas.getAnnotationIndex(sourceDocInfoType).iterator();
if (iter.hasNext()) {
FeatureStructure sourceDocInfoFs = (FeatureStructure) iter.next();
plainTextView.getIndexRepository().addFS(sourceDocInfoFs);
}
}
/**
* Get the chain link before the given link within the given chain. The given link must be part
* of the given chain.
*
* @param aChain
* a chain head feature structure.
* @param aLink
* a link.
* @return the link before the given link or null if the given link is the first link of the
* chain.
*/
private AnnotationFS getPrevLink(FeatureStructure aChain, AnnotationFS aLink)
{
AnnotationFS prevLink = null;
AnnotationFS curLink = getFirstLink(aChain);
while (curLink != null) {
if (WebAnnoCasUtil.isSame(curLink, aLink)) {
break;
}
prevLink = curLink;
curLink = getNextLink(curLink);
}
return prevLink;
}
/**
* Retrieves the primitive value.
*
* @param structure the structure
* @param feature the feature
* @return the primitive value as object
*/
public static Object getPrimitive(FeatureStructure structure, Feature feature) {
TypeSystem ts = structure.getCAS().getTypeSystem();
Class<?> primitiveClass = getPrimitiveClass(ts, feature);
Object result;
if (Boolean.class.equals(primitiveClass)) {
result = structure.getBooleanValue(feature);
} else if (Byte.class.equals(primitiveClass)) {
result = structure.getByteValue(feature);
} else if (Short.class.equals(primitiveClass)) {
result = structure.getShortValue(feature);
} else if (Integer.class.equals(primitiveClass)) {
result = structure.getIntValue(feature);
} else if (Long.class.equals(primitiveClass)) {
result = structure.getLongValue(feature);
} else if (Float.class.equals(primitiveClass)) {
result = structure.getFloatValue(feature);
} else if (Double.class.equals(primitiveClass)) {
result = structure.getDoubleValue(feature);
} else if (String.class.equals(primitiveClass)) {
result = structure.getStringValue(feature);
if (result == null)
result = "";
} else {
throw new IllegalStateException("Unexpected type: "
+ feature.getRange().getName());
}
return result;
}
private static Object extractValueForAgreement(FeatureStructure aFs, String aFeature,
int aLinkIndex, LinkCompareBehavior aLCB)
{
boolean isPrimitiveFeature = aFs.getType().getFeatureByBaseName(aFeature).getRange()
.isPrimitive();
// If the feature on a position is set, then it is a subposition
boolean isSubPosition = aLinkIndex != -1;
// BEGIN PARANOIA
assert aFs.getType().getFeatureByBaseName(aFeature).getRange()
.isPrimitive() == isPrimitiveFeature;
// primitive implies not subposition - if this is primitive and subposition, we
// should never have gotten here in the first place.
assert !isPrimitiveFeature || !isSubPosition;
// END PARANOIA
if (isPrimitiveFeature && !isSubPosition) {
// Primitive feature / primary position
return getFeature(aFs, aFeature);
}
else if (!isPrimitiveFeature && isSubPosition) {
// Link feature / sub-position
return extractLinkFeatureValueForAgreement(aFs, aFeature, aLinkIndex, aLCB);
}
else {
throw new IllegalStateException("Should never get here: primitive: "
+ aFs.getType().getFeatureByBaseName(aFeature).getRange()
.isPrimitive() + "; subpos: " + isSubPosition);
}
}
public FeatureStructure createFS(int addr, CASImpl cas) {
if (DocumentAnnotation_Type.this.useExistingInstance) {
// Return eq fs instance if already created
FeatureStructure fs = DocumentAnnotation_Type.this.jcas.getJfsFromCaddr(addr);
if (null == fs) {
fs = new DocumentAnnotation(addr, DocumentAnnotation_Type.this);
DocumentAnnotation_Type.this.jcas.putJfsFromCaddr(addr, fs);
return fs;
}
return fs;
} else return new DocumentAnnotation(addr, DocumentAnnotation_Type.this);
}
private static void makeChainHead(Type aType, AnnotationFS first)
{
CAS cas = first.getCAS();
FeatureStructure h = cas.createFS(aType);
FSUtil.setFeature(h, "first", first);
cas.addFsToIndexes(h);
}