下面列出了org.eclipse.jface.viewers.CheckboxCellEditor#org.apache.uima.cas.CAS 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static TypeSystemDescription createMultiLinkWithRoleTestTypeSytem(String... aFeatures)
throws Exception
{
List<TypeSystemDescription> typeSystems = new ArrayList<>();
TypeSystemDescription tsd = new TypeSystemDescription_impl();
// Link type
TypeDescription linkTD = tsd.addType(LINK_TYPE, "", CAS.TYPE_NAME_TOP);
linkTD.addFeature("role", "", CAS.TYPE_NAME_STRING);
linkTD.addFeature("target", "", Token.class.getName());
// Link host
TypeDescription hostTD = tsd.addType(HOST_TYPE, "", CAS.TYPE_NAME_ANNOTATION);
hostTD.addFeature("links", "", CAS.TYPE_NAME_FS_ARRAY, linkTD.getName(), false);
for (String feature : aFeatures) {
hostTD.addFeature(feature, "", CAS.TYPE_NAME_STRING);
}
typeSystems.add(tsd);
typeSystems.add(TypeSystemDescriptionFactory.createTypeSystemDescription());
return CasCreationUtils.mergeTypeSystems(typeSystems);
}
@Test
public void singleEmptyCasTest()
throws Exception
{
String text = "";
CAS user1Cas = JCasFactory.createJCas().getCas();
user1Cas.setDocumentText(text);
Map<String, List<CAS>> casByUser = new LinkedHashMap<>();
casByUser.put("user1", asList(user1Cas));
List<SpanDiffAdapter> diffAdapters = asList(new SpanDiffAdapter(Token.class.getName()));
DiffResult result = doDiff(diffAdapters, LINK_TARGET_AS_LABEL, casByUser).toResult();
// result.print(System.out);
assertEquals(0, result.size());
assertEquals(0, result.getDifferingConfigurationSets().size());
}
public static void renderTokens(CAS aCas, GetDocumentResponse aResponse, AnnotatorState aState)
{
int winBegin = aState.getWindowBeginOffset();
int winEnd = aState.getWindowEndOffset();
Type tokenType = CasUtil.getType(aCas, Token.class);
List<AnnotationFS> tokens = selectCovered(aCas, tokenType, winBegin, winEnd);
for (AnnotationFS fs : tokens) {
// attach type such as POS adds non-existing token element for ellipsis annotation
if (fs.getBegin() == fs.getEnd()) {
continue;
}
split(aResponse.getSentenceOffsets(), fs.getCoveredText(), fs.getBegin() - winBegin,
fs.getEnd() - winBegin)
.forEach(range -> {
aResponse.addToken(range.getBegin(), range.getEnd());
if (DEBUG) {
aResponse.addEntity(new Entity(new VID(fs), "Token",
new Offsets(range.getBegin(), range.getEnd()),
fs.getCoveredText(), "#d9d9d9",
"[" + fs.getBegin() + "-" + fs.getEnd() + "]"));
}
});
}
}
/**
* Register the given CAS for a special purpose into the session.
*
* @param aSpecialPurpose
* the unique purpose identifier - unique with respect to the current session.
* @param aMode
* the access mode.
* @param aCas
* the CAS itself.
* @return the managed CAS state.
*/
public SessionManagedCas add(String aSpecialPurpose, CasAccessMode aMode, CAS aCas)
{
Validate.notNull(aSpecialPurpose, "The purpose cannot be null");
Validate.notNull(aMode, "The access mode cannot be null");
Validate.notNull(aCas, "The CAS cannot be null");
SessionManagedCas managedCas = new SessionManagedCas(SPECIAL_PURPOSE, aSpecialPurpose,
aMode, aCas);
Map<String, SessionManagedCas> casByUser = managedCases
.computeIfAbsent(SPECIAL_PURPOSE, key -> new LinkedHashMap<>());
casByUser.put(aSpecialPurpose, managedCas);
LOGGER.trace("CAS storage session [{}]: added {}", hashCode(), managedCas);
return managedCas;
}
public static CAS readWebAnnoTSV(String aPath, TypeSystemDescription aType)
throws UIMAException, IOException
{
CollectionReader reader = createReader(WebannoTsv2Reader.class,
WebannoTsv2Reader.PARAM_SOURCE_LOCATION, "src/test/resources/" + aPath);
CAS cas;
if (aType != null) {
TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory
.createTypeSystemDescription();
List<TypeSystemDescription> allTypes = new ArrayList<>();
allTypes.add(builtInTypes);
allTypes.add(aType);
cas = JCasFactory.createJCas(CasCreationUtils.mergeTypeSystems(allTypes)).getCas();
}
else {
cas = JCasFactory.createJCas().getCas();
}
reader.getNext(cas);
return cas;
}
@Test
public void thatIncrementalPosEvaluationWorks() throws Exception
{
IncrementalSplitter splitStrategy = new IncrementalSplitter(0.8, 250, 10);
OpenNlpPosRecommender sut = new OpenNlpPosRecommender(recommender, traits);
List<CAS> casList = loadAllData();
int i = 0;
while (splitStrategy.hasNext() && i < 3) {
splitStrategy.next();
double score = sut.evaluate(casList, splitStrategy).computeF1Score();
assertThat(score).isStrictlyBetween(0.0, 1.0);
i++;
}
}
/**
* @see junit.framework.TestCase#setUp()
*/
public void setUp() throws Exception {
super.setUp();
casMgr = initCAS();
cas = (CASImpl)casMgr;
TypeSystem ts = cas.getTypeSystem();
wordType = ts.getType(WORD_TYPE);
// assert(wordType != null);
separatorType = ts.getType(SEP_TYPE);
eosType = ts.getType(EOS_TYPE);
tokenType = ts.getType(TOKEN_TYPE);
tokenTypeFeature = ts.getFeatureByFullName(TOKEN_TYPE_FEAT_Q);
startFeature = ts.getFeatureByFullName(CAS.FEATURE_FULL_NAME_BEGIN);
endFeature = ts.getFeatureByFullName(CAS.FEATURE_FULL_NAME_END);
sentenceType = ts.getType(SENT_TYPE);
}
@Override
public CAS getEditorCas()
throws IOException
{
AnnotatorState state = CurationPage.this.getModelObject();
if (state.getDocument() == null) {
throw new IllegalStateException("Please open a document first!");
}
// If we have a timestamp, then use it to detect if there was a concurrent access
verifyAndUpdateDocumentTimestamp(state, curationDocumentService
.getCurationCasTimestamp(state.getDocument()));
return curationDocumentService.readCurationCas(state.getDocument());
}
private void reinitTypeSystem(TypeSystemImpl tsa) {
annotationType = tsa.getType(CAS.TYPE_NAME_ANNOTATION);
// new primitive types
exampleType = tsa.refreshType(exampleType);
floatFeature = tsa.refreshFeature(floatFeature);
stringFeature = tsa.refreshFeature(stringFeature);
booleanFeature = tsa.refreshFeature(booleanFeature);
byteFeature = tsa.refreshFeature(byteFeature);
shortFeature = tsa.refreshFeature(shortFeature);
longFeature = tsa.refreshFeature(longFeature);
doubleFeature = tsa.refreshFeature(doubleFeature);
intArrayFeature = tsa.refreshFeature(intArrayFeature);
floatArrayFeature = tsa.refreshFeature(floatArrayFeature);
stringArrayFeature = tsa.refreshFeature(stringArrayFeature);
booleanArrayFeature = tsa.refreshFeature(booleanArrayFeature);
byteArrayFeature = tsa.refreshFeature(byteArrayFeature);
shortArrayFeature = tsa.refreshFeature(shortArrayFeature);
longArrayFeature = tsa.refreshFeature(longArrayFeature);
doubleArrayFeature = tsa.refreshFeature(doubleArrayFeature);
}
private void actionAnnotate(AjaxRequestTarget aTarget)
{
try {
// When updating an annotation in the sidebar, we must not force a
// re-focus after rendering
getRequestCycle().setMetaData(IsSidebarAction.INSTANCE, true);
// Load the boiler-plate
CAS cas = jcasProvider.get();
FeatureStructure fs = selectFsByAddr(cas, getModelObject().getId());
AnnotationLayer layer = annotationService.findLayer(project.getObject(), fs);
TypeAdapter adapter = annotationService.getAdapter(layer);
// Update the features of the selected annotation from the values presently in
// the feature editors
writeFeatureEditorModelsToCas(adapter, cas);
// persist changes
annotationPage.writeEditorCas(cas);
}
catch (Exception e) {
handleException(DocumentMetadataAnnotationDetailPanel.this, aTarget, e);
}
}
private String actionGetDocument(CAS aCas)
{
StopWatch timer = new StopWatch();
timer.start();
GetDocumentResponse response = new GetDocumentResponse();
String json;
if (getModelObject().getProject() != null) {
render(response, aCas);
json = toJson(response);
lastRenderedJson = json;
}
else {
json = toJson(response);
}
timer.stop();
metrics.renderComplete(RenderType.FULL, timer.getTime(), json, null);
return json;
}
private void createChunkLayer(Project aProject)
throws IOException
{
AnnotationLayer chunkLayer = new AnnotationLayer(Chunk.class.getName(), "Chunk", SPAN_TYPE,
aProject, true, TOKENS, NO_OVERLAP);
annotationSchemaService.createLayer(chunkLayer);
AnnotationFeature chunkValueFeature = new AnnotationFeature();
chunkValueFeature.setDescription("Chunk tag");
chunkValueFeature.setName("chunkValue");
chunkValueFeature.setType(CAS.TYPE_NAME_STRING);
chunkValueFeature.setProject(aProject);
chunkValueFeature.setUiName("Tag");
chunkValueFeature.setLayer(chunkLayer);
annotationSchemaService.createFeature(chunkValueFeature);
}
public static void writeSerializedCas(CAS aCas, File aFile)
throws IOException
{
FileUtils.forceMkdir(aFile.getParentFile());
CASCompleteSerializer serializer = null;
CAS realCas = getRealCas(aCas);
// UIMA-6162 Workaround: synchronize CAS during de/serialization
synchronized (((CASImpl) realCas).getBaseCAS()) {
try {
serializer = serializeCASComplete((CASImpl) getRealCas(aCas));
// BEGIN SAFEGUARD --------------
// Safeguard that we do NOT write a CAS which can afterwards not be read and thus
// would render the document broken within the project
// Reason we do this: https://issues.apache.org/jira/browse/UIMA-6162
CAS dummy = WebAnnoCasUtil.createCas();
deserializeCASComplete(serializer, (CASImpl) getRealCas(dummy));
// END SAFEGUARD --------------
}
catch (Exception e) {
if (LOG.isDebugEnabled()) {
preserveForDebugging(aFile, aCas, serializer);
}
throw new IOException(e);
}
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(aFile))) {
os.writeObject(serializer);
}
}
}
@Override public void getNext(CAS aCAS) throws IOException, CollectionException {
Resource res = nextFile();
initCas(aCAS, res);
Path path = Paths.get(res.getResolvedUri());
json = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
try {
convert(aCAS);
} catch (AnalysisEngineProcessException | CASException e) {
throw new RuntimeException(e);
}
}
private AnnotationFS createNEAnno(CAS aCas, String aValue, int aBegin, int aEnd)
{
Type type = aCas.getTypeSystem().getType(NamedEntity.class.getTypeName());
AnnotationFS clickedFs = aCas.createAnnotation(type, aBegin, aEnd);
Feature value = type.getFeatureByBaseName("value");
clickedFs.setStringValue(value, aValue);
aCas.addFsToIndexes(clickedFs);
return clickedFs;
}
@Override
public List<Pair<LogMessage, AnnotationFS>> validate(CAS aCas)
{
List<Pair<LogMessage, AnnotationFS>> messages = new ArrayList<>();
for (SpanLayerBehavior behavior : behaviors) {
long startTime = currentTimeMillis();
messages.addAll(behavior.onValidate(this, aCas));
log.trace("Validation for [{}] on [{}] took {}ms", behavior.getClass().getSimpleName(),
getLayer().getUiName(), currentTimeMillis() - startTime);
}
return messages;
}
@Test
public void testOK()
throws Exception
{
TypeSystemDescription tsd = UIMAFramework.getResourceSpecifierFactory()
.createTypeSystemDescription();
String refTypeName = "RefType";
TypeDescription refTypeDesc = tsd.addType(refTypeName, null, CAS.TYPE_NAME_ANNOTATION);
refTypeDesc.addFeature("ref", null, CAS.TYPE_NAME_ANNOTATION);
CAS cas = CasCreationUtils.createCas(tsd, null, null);
Type refType = cas.getTypeSystem().getType(refTypeName);
// A regular index annotation
AnnotationFS anno1 = cas.createAnnotation(cas.getAnnotationType(), 0, 1);
cas.addFsToIndexes(anno1);
// An indexed annotation but reachable through an indexe one (below)
AnnotationFS anno2 = cas.createAnnotation(cas.getAnnotationType(), 0, 1);
cas.addFsToIndexes(anno2);
// An indexed annotation that references the non-indexed annotation above
AnnotationFS anno3 = cas.createAnnotation(refType, 0, 1);
anno3.setFeatureValue(refType.getFeatureByBaseName("ref"), anno2);
cas.addFsToIndexes(anno3);
List<LogMessage> messages = new ArrayList<>();
CasDoctor cd = new CasDoctor(AllFeatureStructuresIndexedCheck.class);
// A project is not required for this check
boolean result = cd.analyze(null, cas, messages);
messages.forEach(System.out::println);
assertTrue(result);
}
@Override
public boolean accepts(AnnotationLayer aLayer, AnnotationFeature aFeature)
{
if (aLayer == null || aFeature == null) {
return false;
}
return (asList(SINGLE_TOKEN, TOKENS).contains(aLayer.getAnchoringMode()))
&& !aLayer.isCrossSentence() && SPAN_TYPE.equals(aLayer.getType())
&& (CAS.TYPE_NAME_STRING.equals(aFeature.getType()) || aFeature.isVirtualFeature());
}
@Test
public void testChain() throws Exception
{
TypeSystemDescription tsd = createTypeSystemDescription(
"tsd/chain");
TypeSystemAnalysis analysis = TypeSystemAnalysis.of(tsd);
AnnotationLayer chainLayer = new AnnotationLayer();
chainLayer.setName("webanno.custom.Chain");
chainLayer.setUiName("Chain");
chainLayer.setType(WebAnnoConst.CHAIN_TYPE);
chainLayer.setAnchoringMode(AnchoringMode.CHARACTERS);
chainLayer.setOverlapMode(OverlapMode.ANY_OVERLAP);
chainLayer.setCrossSentence(true);
AnnotationFeature referenceRelationFeature = new AnnotationFeature(
"referenceRelation", CAS.TYPE_NAME_STRING);
AnnotationFeature referenceTypeFeature = new AnnotationFeature(
"referenceType", CAS.TYPE_NAME_STRING);
SoftAssertions softly = new SoftAssertions();
softly.assertThat(analysis.getLayers())
.containsExactlyInAnyOrder(chainLayer)
.usingFieldByFieldElementComparator();
softly.assertThat(analysis.getFeatures(chainLayer.getName()))
.containsExactlyInAnyOrder(referenceRelationFeature, referenceTypeFeature)
.usingFieldByFieldElementComparator();
softly.assertAll();
}
@Override
public void fireAction(AnnotationActionHandler aActionHandler, AnnotatorState aModelObject,
AjaxRequestTarget aTarget, CAS aCas, VID aParamId, String aAction)
throws IOException, AnnotationException
{
for (AnnotationEditorExtension ext : getExtensions()) {
if (!ext.getBeanName().equals(aParamId.getExtensionId())) {
continue;
}
ext.handleAction(aActionHandler, aModelObject, aTarget, aCas, aParamId, aAction);
}
}
private void createNewSpanAnnotation(AjaxRequestTarget aTarget, SpanAdapter aAdapter,
CAS aCas)
throws IOException, AnnotationException
{
AnnotatorState state = getModelObject();
Selection selection = state.getSelection();
AnnotationFS annoFs = aAdapter.add(state.getDocument(), state.getUser().getUsername(),
aCas, selection.getBegin(), selection.getEnd());
selection.selectSpan(annoFs);
}
@Override
public Panel createResultsPanel(String aId,
IModel<PairwiseAnnotationResult<UnitizingAgreementResult>> aResults,
SerializableSupplier<Map<String, List<CAS>>> aCasMapSupplier)
{
return new PairwiseUnitizingAgreementTable(aId, aResults);
}
/**
* @see org.apache.uima.analysis_engine.annotator.TextAnnotator#process(org.apache.uima.cas.text.CAS,
* org.apache.uima.analysis_engine.ResultSpecification)
*/
public void process(CAS tcas, ResultSpecification resultSpec)
throws AnnotatorProcessException {
if (true) {
try {
// use standard output file
File fp = new File(this.testBaseDir, "SequencerTest.txt");
if (!fp.exists()) {
fp.createNewFile();
}
if (fp.canWrite()) {
// write result specification to the output file
OutputStreamWriter writer = new OutputStreamWriter(
new FileOutputStream(fp, true), StandardCharsets.UTF_8);
writer.write("\nResultSpec for annotator " + this.name + ":\n");
TypeOrFeature[] tofs = resultSpec.getResultTypesAndFeatures();
// sort by name to ensure consistent output for testing purposes
Arrays.sort(tofs, new Comparator<TypeOrFeature>() {
public int compare(TypeOrFeature o1, TypeOrFeature o2) {
return o1.getName().compareTo(o2.getName());
}
});
for (int i = 0; i < tofs.length; i++) {
writer.write(tofs[i].getName() + "\n");
}
writer.flush();
writer.close();
} else {
throw new IOException("Cannot write to " + fp.getAbsolutePath());
}
} catch (IOException e) {
// If an error occurs, throw new annotator exception
throw new AnnotatorProcessException(e);
}
}
}
public void testOutOfTypeSystemDataComplexCas() throws Exception {
// deserialize a complex XCAS
CAS originalCas = CasCreationUtils.createCas(typeSystem, null, indexes);
InputStream serCasStream = new FileInputStream(JUnitExtension.getFile("ExampleCas/cas.xml"));
XCASDeserializer.deserialize(serCasStream, originalCas);
serCasStream.close();
//serialize to XMI
String xmiStr = serialize(originalCas, null);
//deserialize into a CAS with no type system
CAS casWithNoTs = CasCreationUtils.createCas(new TypeSystemDescription_impl(),
new TypePriorities_impl(), new FsIndexDescription[0]);
XmiSerializationSharedData sharedData = new XmiSerializationSharedData();
deserialize(xmiStr, casWithNoTs, sharedData, true, -1);
// now reserialize including OutOfTypeSystem data
String xmiStr2 = serialize(casWithNoTs, sharedData);
//deserialize into a new CAS that has the full type system
CAS newCas = CasCreationUtils.createCas(typeSystem, null, indexes);
deserialize(xmiStr2, newCas, null, false, -1);
//compare
CasComparer.assertEquals(originalCas, newCas);
//Test a partial type system with a missing some missing features and
//missing "Organization" type
File partialTypeSystemFile = JUnitExtension.getFile("ExampleCas/partialTestTypeSystem.xml");
TypeSystemDescription partialTypeSystem = UIMAFramework.getXMLParser().parseTypeSystemDescription(
new XMLInputSource(partialTypeSystemFile));
CAS partialTsCas = CasCreationUtils.createCas(partialTypeSystem, null, indexes);
XmiSerializationSharedData sharedData2 = new XmiSerializationSharedData();
deserialize(xmiStr, partialTsCas, sharedData2, true, -1);
String xmiStr3 = serialize(partialTsCas, sharedData2);
newCas.reset();
deserialize(xmiStr3, newCas, null, false, -1);
CasComparer.assertEquals(originalCas, newCas);
}
public static void writeSerializedCas(CAS aCas, File aFile)
throws IOException
{
FileUtils.forceMkdir(aFile.getParentFile());
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(aFile))) {
CASCompleteSerializer serializer = serializeCASComplete((CASImpl) aCas);
os.writeObject(serializer);
}
}
public static Map<String, List<CAS>> load(String... aPaths)
throws UIMAException, IOException
{
Map<String, List<CAS>> casByUser = new LinkedHashMap<>();
int n = 1;
for (String path : aPaths) {
CAS cas = read(path);
casByUser.put("user" + n, asList(cas));
n++;
}
return casByUser;
}
/**
* @see org.apache.uima.collection.CollectionReader#getNext(org.apache.uima.cas.CAS)
*/
public void getNext(CAS aCAS) throws IOException, CollectionException {
File currentFile = (File) mFiles.get(mCurrentIndex++);
try (InputStream inputStream = new FileInputStream(currentFile)) {
XmiCasDeserializer.deserialize(inputStream, aCAS, !mFailOnUnknownType);
} catch (SAXException e) {
throw new CollectionException(e);
}
}
/**
* To test non-normal case, change Eclipse run config by adding the jvm arg:
* -Duima.allow_duplicate_add_to_indexes
* @throws CASException
*/
public void testDupFsIndex() throws CASException {
cas.setSofaDataString("something", "text"); // otherwise triggers failure in addFsToIndex - no sofa ref
JCas jcas = cas.getJCas();
Annotation a = new Annotation(jcas, 0, 4);
cas.addFsToIndexes(a);
cas.addFsToIndexes(a);
cas.addFsToIndexes(a);
int expected = /*FSIndexRepositoryImpl.IS_ALLOW_DUP_ADD_2_INDEXES ? 4 :*/ 2;
assertEquals(expected, cas.getIndexRepository().getIndex(CASTestSetup.ANNOT_SORT_INDEX).size());
assertEquals(expected, cas.getIndexRepository().getIndex(CASTestSetup.ANNOT_BAG_INDEX).size());
assertEquals(expected, cas.getIndexRepository().getIndex(CAS.STD_ANNOTATION_INDEX).size());
}
private static void assertCorrectlyLoaded(CAS cas, boolean leniently) throws Exception {
// Check if all the annotations are there (mind the file contains FSes that are NOT annotations!)
Assert.assertEquals(
leniently ? SIMPLE_CAS_DEFAULT_INDEX_SIZE_LENIENT : SIMPLE_CAS_DEFAULT_INDEX_SIZE,
cas.getAnnotationIndex().size());
// Count ALL FSes now, including the ones that are not annotations!
List<String> expectedTypes = new ArrayList<>(asList(
"org.apache.uima.testTypeSystem.Entity",
"org.apache.uima.testTypeSystem.Organization",
"org.apache.uima.testTypeSystem.Owner",
"org.apache.uima.testTypeSystem.Person",
"uima.tcas.DocumentAnnotation"));
if (leniently) {
// This type was renamed to "org.apache.uima.testTypeSystem.OwnerRenamed"
expectedTypes.remove("org.apache.uima.testTypeSystem.Owner");
}
List<String> fsTypes = new ArrayList<>();
// FSIterator<FeatureStructure> fsi = cas.getIndexRepository()
// .getAllIndexedFS(cas.getTypeSystem().getTopType());
Collection<TOP> s = cas.getIndexedFSs();
Iterator<TOP> fsi = s.iterator();
int fsCount = 0;
while (fsi.hasNext()) {
TOP fs = (TOP) fsi.next();
String typeName = fs.getType().getName();
if (!fsTypes.contains(typeName)) {
fsTypes.add(typeName);
}
fsCount++;
}
Collections.sort(fsTypes);
Assert.assertEquals(
leniently ? SIMPLE_CAS_ALL_INDEXED_SIZE_LENIENT : SIMPLE_CAS_ALL_INDEXED_SIZE, fsCount);
Assert.assertEquals(expectedTypes, fsTypes);
}
/**
* Retrieves annotation editor adapters.
*
* @param adapter the adapter
* @return an adapter or null
*/
@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
if (IContentOutlinePage.class.equals(adapter) && getDocument() != null) {
return mOutlinePage;
}
else if (CAS.class.equals(adapter) && getDocument() != null) {
return getDocument().getCAS();
}
else {
return super.getAdapter(adapter);
}
}