private String getDocumentId(JCas jCas) { return ConsumerUtils.getExternalId( UimaSupport.getDocumentAnnotation(jCas), options.isContentHashAsId()); }
/** @return the build {@link DocumentGraphOptions} */ public DocumentGraphOptions build() { if (typeClasses == null) { typeClasses = TypeUtils.getAnnotationClasses(Entity.class); } return new DocumentGraphOptions( contentHashAsId, outputMeta, outputContent, outputDocument, outputReferents, outputRelations, outputRelationsAsLinks, outputEvents, ImmutableSet.copyOf(typeClasses), ImmutableSet.copyOf(stopFeatures), valueCoercer); } }
private Optional<Vertex> traversal(JCas jCas, GraphTraversalSource traversal) { EntityRelationConverter converter = new EntityRelationConverter(monitor, options.getStopFeatures(), fields, false); loadMentions(jCas, traversal, converter); if (options.isOutputRelations()) { loadRelations(jCas, traversal, converter); } else { monitor.debug("DocumentGraph relations ommitted"); } if (options.isOutputEvents()) { loadEvents(jCas, traversal, converter); } else { monitor.debug("DocumentGraph events ommitted"); } return loadDocument(jCas, traversal); }
/** * Load the data form the jCas into the given graph. * * @param jCas to load the data from * @param graph to load the data into */ public void load(JCas jCas, Graph graph) { GraphTraversalSource traversal = graph.traversal(); Optional<Vertex> document = traversal(jCas, traversal); if (options.isOutputContent()) { loadDocumentContent(jCas, graph, document); } if (options.isOutputMeta()) { loadGraphMetadata(jCas, graph, document); } else { monitor.debug("DocumentGraph metadata skiped"); } }
@Test public void testDocumentGraphMetadataHasContentFromJCas() throws UIMAException { DocumentGraphOptions options = DocumentGraphOptions.builder().withContent(true).build(); DocumentGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); Variables variables = graph.variables(); assertEquals(JCasTestGraphUtil.CONTENT, variables.get(FIELD_CONTENT).orElse("")); }
@Test public void testDocumentGraphHasCorrectPropertiesFromJCas() throws UIMAException { DocumentGraphOptions options = DocumentGraphOptions.builder().build(); DocumentGraphFactory factory = createfactory(options); UimaSupport.getDocumentAnnotation(jCas), options.isContentHashAsId());
TypeUtils.filterAnnotations(JCasUtil.select(jCas, Entity.class), options.getTypeClasses()); .collect(Collectors.toList()); if (options.isOutputReferenceTargets()) { String refId = ConsumerUtils.getExternalId(e.getValue()); if (!traversal.V(coerce(refId)).hasNext()) {
private Object coerce(Object value) { return options.getValueCoercer().coerce(value); }
private Optional<Vertex> loadDocument(JCas jCas, GraphTraversalSource traversal) { String documentId = getDocumentId(jCas); if (options.isOutputDocument()) { Vertex documentVert = traversal .addV(DOCUMENT) .property(T.id, coerce(documentId)) .sideEffect( tv -> { Vertex documentVertex = tv.get(); getGraphMetadata(jCas) .entrySet() .stream() .forEach(e -> setProperty(documentVertex, e.getKey(), e.getValue())); }) .next(); traversal .V() .filter(v -> !v.get().equals(documentVert)) .addE(MENTION_IN) .to(documentVert) .iterate(); return Optional.of(documentVert); } else { traversal.V().property(FIELD_DOCUMENT_ID, coerce(documentId)).iterate(); traversal.E().property(FIELD_DOCUMENT_ID, coerce(documentId)).iterate(); return Optional.empty(); } }
@Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); Set<Class<? extends Entity>> typeClasses = TypeUtils.getTypeClasses(Entity.class, typeNames); DocumentGraphOptions.Builder builder = DocumentGraphOptions.builder() .withContentHashAsId(contentHashAsId) .withContent(outputContent) .withMeta(outputMeta) .withReferenceTargets(outputReferents) .withRelations(outputRelations) .withRelationsAsLinks(outputRelationsAsLinks) .withEvents(outputEvents) .withDocument(outputDocument) .withStopFeatures(filterFeatures) .withValueCoercer(valueCoercer) .withTypeClasses(typeClasses); addOptions(builder); factory = new DocumentGraphFactory(getMonitor(), builder.build()); }
/** * Load the data form the jCas into the given graph. * * @param jCas to load the data from * @param graph to load the data into */ public void load(JCas jCas, Graph graph) { GraphTraversalSource traversal = graph.traversal(); Optional<Vertex> document = traversal(jCas, traversal); if (options.isOutputContent()) { loadDocumentContent(jCas, graph, document); } if (options.isOutputMeta()) { loadGraphMetadata(jCas, graph, document); } else { monitor.debug("DocumentGraph metadata skiped"); } }
TypeUtils.filterAnnotations(JCasUtil.select(jCas, Entity.class), options.getTypeClasses()); .collect(Collectors.toList()); if (options.isOutputReferenceTargets()) { String refId = ConsumerUtils.getExternalId(e.getValue()); if (!traversal.V(coerce(refId)).hasNext()) {
private Object coerce(Object value) { return options.getValueCoercer().coerce(value); }
private Optional<Vertex> loadDocument(JCas jCas, GraphTraversalSource traversal) { String documentId = getDocumentId(jCas); if (options.isOutputDocument()) { Vertex documentVert = traversal .addV(DOCUMENT) .property(T.id, coerce(documentId)) .sideEffect( tv -> { Vertex documentVertex = tv.get(); getGraphMetadata(jCas) .entrySet() .stream() .forEach(e -> setProperty(documentVertex, e.getKey(), e.getValue())); }) .next(); traversal .V() .filter(v -> !v.get().equals(documentVert)) .addE(MENTION_IN) .to(documentVert) .iterate(); return Optional.of(documentVert); } else { traversal.V().property(FIELD_DOCUMENT_ID, coerce(documentId)).iterate(); traversal.E().property(FIELD_DOCUMENT_ID, coerce(documentId)).iterate(); return Optional.empty(); } }
@Test public void testDocumentGraphCreatesMetadataFromJCas() throws UIMAException { DocumentGraphOptions options = DocumentGraphOptions.builder().withMeta(true).build(); DocumentGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); assertMetadata(jCas, graph.variables().asMap()); }
private Optional<Vertex> traversal(JCas jCas, GraphTraversalSource traversal) { EntityRelationConverter converter = new EntityRelationConverter(monitor, options.getStopFeatures(), fields, false); loadMentions(jCas, traversal, converter); if (options.isOutputRelations()) { loadRelations(jCas, traversal, converter); } else { monitor.debug("DocumentGraph relations ommitted"); } if (options.isOutputEvents()) { loadEvents(jCas, traversal, converter); } else { monitor.debug("DocumentGraph events ommitted"); } return loadDocument(jCas, traversal); }
private String getDocumentId(JCas jCas) { return ConsumerUtils.getExternalId( UimaSupport.getDocumentAnnotation(jCas), options.isContentHashAsId()); }
/** @return the build {@link DocumentGraphOptions} */ public DocumentGraphOptions build() { if (typeClasses == null) { typeClasses = TypeUtils.getAnnotationClasses(Entity.class); } return new DocumentGraphOptions( contentHashAsId, outputMeta, outputContent, outputDocument, outputReferents, outputRelations, outputRelationsAsLinks, outputEvents, ImmutableSet.copyOf(typeClasses), ImmutableSet.copyOf(stopFeatures), valueCoercer); } }
@Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); Set<Class<? extends Entity>> typeClasses = TypeUtils.getTypeClasses(Entity.class, typeNames); DocumentGraphOptions.Builder builder = DocumentGraphOptions.builder() .withContentHashAsId(contentHashAsId) .withContent(outputContent) .withMeta(outputMeta) .withReferenceTargets(outputReferents) .withRelations(outputRelations) .withRelationsAsLinks(outputRelationsAsLinks) .withEvents(outputEvents) .withDocument(outputDocument) .withStopFeatures(filterFeatures) .withValueCoercer(valueCoercer) .withTypeClasses(typeClasses); addOptions(builder); factory = new DocumentGraphFactory(getMonitor(), builder.build()); }
@Test public void testDocumentGraphWithTypeFiltering() throws UIMAException { Set<Class<? extends Entity>> typeClasses = TypeUtils.getTypeClasses(Entity.class, Person.class.getSimpleName()); DocumentGraphOptions options = DocumentGraphOptions.builder().withTypeClasses(typeClasses).build(); DocumentGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); assertEquals(2, graph.traversal().V().hasLabel(REFERENCE_TARGET).count().next().intValue()); assertEquals(1, graph.traversal().V().hasLabel(EVENT).count().next().intValue()); assertEquals(3, graph.traversal().V().hasLabel(MENTION).count().next().intValue()); assertEquals(1, graph.traversal().V().hasLabel(RELATION).count().next().intValue()); assertEquals(3, graph.traversal().E().hasLabel(MENTION_OF).count().next().intValue()); assertEquals(2, graph.traversal().E().hasLabel(PARTICIPANT_IN).count().next().intValue()); assertNoDocumentNode(graph); assertNoRelationEdges(graph); assertEquals(7, IteratorUtils.count(graph.vertices())); assertEquals(7, IteratorUtils.count(graph.edges())); }