use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class ComplexTypeTest method testProfType.
@Test
public void testProfType() throws Exception {
TypeSystemDescription tsd = TypeSystemDescriptionFactory.createTypeSystemDescription("desc.types.TestTypeSystemDescriptor");
CAS cas = CasCreationUtils.createCas(tsd, null, null);
cas.setDocumentText("I listen to lectures by Prof. Gurevych sometimes.");
TypeSystem ts = cas.getTypeSystem();
Type profType = ts.getType("de.tud.Prof");
Feature profNameFeature = profType.getFeatureByBaseName("fullName");
Feature profBossFeature = profType.getFeatureByBaseName("boss");
AnnotationFS proemel = cas.createAnnotation(profType, 0, 0);
proemel.setStringValue(profNameFeature, "Hans Juergen Proeml");
cas.addFsToIndexes(proemel);
AnnotationFS gurevych = cas.createAnnotation(profType, 24, 38);
gurevych.setStringValue(profNameFeature, "Iryna Gurevych");
gurevych.setFeatureValue(profBossFeature, proemel);
cas.addFsToIndexes(gurevych);
/*
* for (String feature : Arrays.asList("fullName", "boss")) { Feature someFeature =
* gurevych.getType().getFeatureByBaseName(feature); if
* (someFeature.getRange().isPrimitive()) { String value =
* gurevych.getFeatureValueAsString(someFeature); System.out.println(value); } else {
* FeatureStructure value = gurevych.getFeatureValue(someFeature);
* System.out.printf("%s (%s)%n", value.getFeatureValueAsString(profNameFeature),
* value.getType()); } }
*/
ConstraintsGrammar parser = new ConstraintsGrammar(new FileInputStream("src/test/resources/rules/prof.rules"));
Parse p = parser.Parse();
ParsedConstraints constraints = p.accept(new ParserVisitor());
Evaluator constraintsEvaluator = new ValuesGenerator();
List<PossibleValue> possibleValues = constraintsEvaluator.generatePossibleValues(gurevych, "professorName", constraints);
List<PossibleValue> exValues = new LinkedList<>();
exValues.add(new PossibleValue("Iryna Gurevych", false));
assertEquals(possibleValues, exValues);
}
use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class AnnotationSchemaServiceImpl method getProjectTypes.
@Override
public TypeSystemDescription getProjectTypes(Project aProject) {
// Create a new type system from scratch
TypeSystemDescription tsd = new TypeSystemDescription_impl();
for (AnnotationLayer type : listAnnotationLayer(aProject)) {
if (type.getType().equals(SPAN_TYPE) && !type.isBuiltIn()) {
TypeDescription td = tsd.addType(type.getName(), "", CAS.TYPE_NAME_ANNOTATION);
generateFeatures(tsd, td, type);
} else if (type.getType().equals(RELATION_TYPE) && !type.isBuiltIn()) {
TypeDescription td = tsd.addType(type.getName(), "", CAS.TYPE_NAME_ANNOTATION);
AnnotationLayer attachType = type.getAttachType();
td.addFeature(WebAnnoConst.FEAT_REL_TARGET, "", attachType.getName());
td.addFeature(WebAnnoConst.FEAT_REL_SOURCE, "", attachType.getName());
generateFeatures(tsd, td, type);
} else if (type.getType().equals(CHAIN_TYPE) && !type.isBuiltIn()) {
TypeDescription tdChains = tsd.addType(type.getName() + "Chain", "", CAS.TYPE_NAME_ANNOTATION_BASE);
tdChains.addFeature("first", "", type.getName() + "Link");
// Custom features on chain layers are currently not supported
// generateFeatures(tsd, tdChains, type);
TypeDescription tdLink = tsd.addType(type.getName() + "Link", "", CAS.TYPE_NAME_ANNOTATION);
tdLink.addFeature("next", "", type.getName() + "Link");
tdLink.addFeature("referenceType", "", CAS.TYPE_NAME_STRING);
tdLink.addFeature("referenceRelation", "", CAS.TYPE_NAME_STRING);
}
}
return tsd;
}
use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class AnnotationSchemaServiceImpl method upgradeCas.
@Override
public void upgradeCas(CAS aCas, SourceDocument aSourceDocument, String aUser) throws UIMAException, IOException {
TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription projectTypes = getProjectTypes(aSourceDocument.getProject());
TypeSystemDescription allTypes = CasCreationUtils.mergeTypeSystems(asList(projectTypes, builtInTypes));
// Prepare template for new CAS
CAS newCas = JCasFactory.createJCas(allTypes).getCas();
CASCompleteSerializer serializer = Serialization.serializeCASComplete((CASImpl) newCas);
// Save old type system
TypeSystem oldTypeSystem = aCas.getTypeSystem();
// Save old CAS contents
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
Serialization.serializeWithCompression(aCas, os2, oldTypeSystem);
// Prepare CAS with new type system
Serialization.deserializeCASComplete(serializer, (CASImpl) aCas);
// Restore CAS data to new type system
Serialization.deserializeCAS(aCas, new ByteArrayInputStream(os2.toByteArray()), oldTypeSystem, null);
// Make sure JCas is properly initialized too
aCas.getJCas();
try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aSourceDocument.getProject().getId()))) {
Project project = aSourceDocument.getProject();
log.info("Upgraded CAS of user [{}] for " + "document [{}]({}) in project [{}]({})", aUser, aSourceDocument.getName(), aSourceDocument.getId(), project.getName(), project.getId());
}
}
use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class WebAnnoTsv3WriterTestBase method testTwoSentencesWithNoSpaceInBetween.
@Test
public void testTwoSentencesWithNoSpaceInBetween() throws Exception {
TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("src/test/resources/desc/type/webannoTestTypes.xml");
TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
JCas jcas = JCasFactory.createJCas(merged);
DocumentMetaData.create(jcas).setDocumentId("doc");
jcas.setDocumentText("onetwo");
new Token(jcas, 0, 3).addToIndexes();
new Sentence(jcas, 0, 3).addToIndexes();
new Token(jcas, 3, 6).addToIndexes();
new Sentence(jcas, 3, 6).addToIndexes();
writeAndAssertEquals(jcas);
}
use of org.apache.uima.resource.metadata.TypeSystemDescription in project webanno by webanno.
the class WebAnnoTsv3XReaderWriterRoundTripTest method runTest.
@Test
public void runTest() throws Exception {
TypeSystemDescription global = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription local;
if (new File(referenceFolder, "typesystem.xml").exists()) {
local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath(new File(referenceFolder, "typesystem.xml").toString());
} else {
local = TypeSystemDescriptionFactory.createTypeSystemDescriptionFromPath("src/test/resources/desc/type/webannoTestTypes.xml");
}
TypeSystemDescription merged = CasCreationUtils.mergeTypeSystems(asList(global, local));
String targetFolder = "target/test-output/WebAnnoTsv3XReaderWriterRoundTripTest/" + referenceFolder.getName();
CollectionReaderDescription reader = createReaderDescription(WebannoTsv3XReader.class, merged, WebannoTsv3XReader.PARAM_SOURCE_LOCATION, referenceFolder, WebannoTsv3XReader.PARAM_PATTERNS, "reference.tsv");
AnalysisEngineDescription checker = createEngineDescription(DKProCoreConventionsChecker.class);
AnalysisEngineDescription tsvWriter = createEngineDescription(WebannoTsv3XWriter.class, merged, WebannoTsv3XWriter.PARAM_TARGET_LOCATION, targetFolder, WebannoTsv3XWriter.PARAM_STRIP_EXTENSION, true);
AnalysisEngineDescription xmiWriter = createEngineDescription(XmiWriter.class, merged, XmiWriter.PARAM_TARGET_LOCATION, targetFolder, XmiWriter.PARAM_STRIP_EXTENSION, true);
SimplePipeline.runPipeline(reader, checker, tsvWriter, xmiWriter);
String referenceTsv = FileUtils.readFileToString(new File(referenceFolder, "reference.tsv"), "UTF-8");
String actualTsv = FileUtils.readFileToString(new File(targetFolder, "reference.tsv"), "UTF-8");
//
// The XMI files here are not compared semantically but using their serialization which
// is subject to minor variations depending e.g. on the order in which annotation are
// created in the CAS. Thus, this code is commented out and should only be used on a
// case-by-case base to compare XMIs during development.
//
// String referenceXmi = FileUtils.readFileToString(new File(referenceFolder,
// "reference.xmi"),
// "UTF-8");
//
// String actualXmi = FileUtils.readFileToString(new File(targetFolder, "reference.xmi"),
// "UTF-8");
assertEquals(referenceTsv, actualTsv);
// assertEquals(referenceXmi, actualXmi);
}
Aggregations