private EntityGraphFactory createfactory(EntityGraphOptions options) { UimaMonitor monitor = new UimaMonitor("test", EntityGraphFactoryTest.class); return new EntityGraphFactory(monitor, options); }
@Override protected void doProcess(JCas jCas) throws AnalysisEngineProcessException { String documentSourceName = SourceUtils.getDocumentSourceBaseName(jCas); Graph entityGraph = factory.create(jCas); processGraph(documentSourceName, entityGraph); }
private void copyProperties(Features features, Element from, Element to) { addProperty( features, to, MENTIONS_PROPERTY, Collections.singletonList(aggregateProperties(from))); List<String> aggregateProperties = options.getAggregateProperties(); ElementHelper.propertyValueMap(from) .entrySet() .stream() .filter(e -> !aggregateProperties.contains(e.getKey())) .forEach( property -> { Object value = property.getValue(); if (property instanceof List) { addProperty(features, to, property.getKey(), (List<?>) value); } else { setProperty(to, property.getKey(), value); } }); }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @return the document graph * @throws AnalysisEngineProcessException */ public Graph create(JCas jCas) throws AnalysisEngineProcessException { Graph graph = createTransformGraph(options.isMultiValueProperties()); load(jCas, graph, graph.features()); return graph; }
/** * Load the data form the jCas into the given graph. * * @param jCas to load the data from * @param graph to load the data into * @throws AnalysisEngineProcessException */ public void load(JCas jCas, Graph graph, Features features) throws AnalysisEngineProcessException { try (Graph documentGraph = factory.create(jCas)) { GraphTraversalSource fromTraversal = documentGraph.traversal(); GraphTraversalSource destTraversal = graph.traversal(); mapEntities(features, fromTraversal, destTraversal); mapEvents(features, fromTraversal, destTraversal); mapRelations(features, fromTraversal, destTraversal); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } }
.V(v.id()) .in(MENTION_OF) .sideEffect(vt -> mentions.add(aggregateProperties(vt.get()))) .properties() .group() vt -> { Vertex entityVert = vt.get(); addProperty(features, entityVert, MENTIONS_PROPERTY, mentions); setProperty(entityVert, LINKING, v.property(LINKING).value()); .filter(e -> !aggregateProperties.contains(e.getKey())) .forEach( e -> addProperty(features, entityVert, e.getKey(), e.getValue())); addSingleProperty( entityVert, LONGEST_VALUE, values, LONGEST_VALUE_STRATEGY); addSingleProperty( entityVert, MOST_COMMON_VALUE, values, MOST_COMMON_VALUE_STRATEGY); })
@SuppressWarnings("rawtypes") protected void addProperty(Features features, Element v, String key, List<?> values) { if (v instanceof Vertex && !Cardinality.single.equals(features.vertex().getCardinality(key))) { values.stream().filter(not(isNull())).forEach(value -> setProperty(v, key, value)); } else { ValueStrategy valueStrategy = options.getValueStrategyProvider().get(key); addSingleProperty(v, key, values, valueStrategy); } }
private void mapEvents( Features features, GraphTraversalSource fromTraversal, GraphTraversalSource destTraversal) { fromTraversal .V() .hasLabel(DocumentGraphFactory.EVENT) .sideEffect( tv -> { Vertex origEvent = tv.get(); Vertex transEvent = destTraversal.addV(EVENT).property(id, origEvent.id()).next(); copyProperties(features, origEvent, transEvent); origEvent .edges(Direction.BOTH) .forEachRemaining( origEdge -> destTraversal .V(origEdge.inVertex().id()) .addE(PARTICIPANT_IN) .to(transEvent) .iterate()); }) .iterate(); }
.V(v.id()) .in(MENTION_OF) .sideEffect(vt -> mentions.add(aggregateProperties(vt.get()))) .properties() .group() vt -> { Vertex entityVert = vt.get(); addProperty(features, entityVert, MENTIONS_PROPERTY, mentions); setProperty(entityVert, LINKING, v.property(LINKING).value()); .filter(e -> !aggregateProperties.contains(e.getKey())) .forEach( e -> addProperty(features, entityVert, e.getKey(), e.getValue())); addSingleProperty( entityVert, LONGEST_VALUE, values, LONGEST_VALUE_STRATEGY); addSingleProperty( entityVert, MOST_COMMON_VALUE, values, MOST_COMMON_VALUE_STRATEGY); })
/** * Load the data form the jCas into the given graph. * * @param jCas to load the data from * @param graph to load the data into * @throws AnalysisEngineProcessException */ public void load(JCas jCas, Graph graph, Features features) throws AnalysisEngineProcessException { try (Graph documentGraph = factory.create(jCas)) { GraphTraversalSource fromTraversal = documentGraph.traversal(); GraphTraversalSource destTraversal = graph.traversal(); mapEntities(features, fromTraversal, destTraversal); mapEvents(features, fromTraversal, destTraversal); mapRelations(features, fromTraversal, destTraversal); } catch (Exception e) { throw new AnalysisEngineProcessException(e); } }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @return the document graph * @throws AnalysisEngineProcessException */ public Graph create(JCas jCas) throws AnalysisEngineProcessException { Graph graph = createTransformGraph(options.isMultiValueProperties()); load(jCas, graph, graph.features()); return graph; }
@SuppressWarnings("rawtypes") protected void addProperty(Features features, Element v, String key, List<?> values) { if (v instanceof Vertex && !Cardinality.single.equals(features.vertex().getCardinality(key))) { values.stream().filter(not(isNull())).forEach(value -> setProperty(v, key, value)); } else { ValueStrategy valueStrategy = options.getValueStrategyProvider().get(key); addSingleProperty(v, key, values, valueStrategy); } }
private void mapEvents( Features features, GraphTraversalSource fromTraversal, GraphTraversalSource destTraversal) { fromTraversal .V() .hasLabel(DocumentGraphFactory.EVENT) .sideEffect( tv -> { Vertex origEvent = tv.get(); Vertex transEvent = destTraversal.addV(EVENT).property(id, origEvent.id()).next(); copyProperties(features, origEvent, transEvent); origEvent .edges(Direction.BOTH) .forEachRemaining( origEdge -> destTraversal .V(origEdge.inVertex().id()) .addE(PARTICIPANT_IN) .to(transEvent) .iterate()); }) .iterate(); }
@Override protected void doProcess(JCas jCas) throws AnalysisEngineProcessException { String documentSourceName = SourceUtils.getDocumentSourceBaseName(jCas); Graph entityGraph = factory.create(jCas); processGraph(documentSourceName, entityGraph); }
private void copyProperties(Features features, Element from, Element to) { addProperty( features, to, MENTIONS_PROPERTY, Collections.singletonList(aggregateProperties(from))); List<String> aggregateProperties = options.getAggregateProperties(); ElementHelper.propertyValueMap(from) .entrySet() .stream() .filter(e -> !aggregateProperties.contains(e.getKey())) .forEach( property -> { Object value = property.getValue(); if (property instanceof List) { addProperty(features, to, property.getKey(), (List<?>) value); } else { setProperty(to, property.getKey(), value); } }); }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @param features to use for the graph * @return the document graph * @throws AnalysisEngineProcessException */ public Graph create(JCas jCas, Features features) throws AnalysisEngineProcessException { Graph graph = createTransformGraph(features.vertex().supportsMultiProperties()); load(jCas, graph, features); return graph; }
@Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { super.doInitialize(aContext); try { Set<Class<? extends Entity>> typeClasses = TypeUtils.getTypeClasses(Entity.class, typeNames); EntityGraphOptions.Builder builder = EntityGraphOptions.builder() .withContentHashAsId(contentHashAsId) .withEvents(outputEvents) .withStopFeatures(filterFeatures) .withAggregateProperties(aggregate) .withTypeClasses(typeClasses) .withMultiValueProperties(multiValueProperties) .withDefaultValueStrategy(createValueStrategy(defaultValueStrategyType)) .withValueCoercer(valueCoercer); if (valueStrategyTypes != null) { for (int i = 0; i < valueStrategyTypes.length; i += 2) { builder.withValueStrategy( valueStrategyTypes[i], createValueStrategy(valueStrategyTypes[i + 1])); } } addOptions(builder); factory = new EntityGraphFactory(getMonitor(), builder.build()); } catch (Exception e) { throw new ResourceInitializationException(e); } }
private void mapRelations( Features features, GraphTraversalSource fromTraversal, GraphTraversalSource destTraversal) { fromTraversal .V() .in(MENTION_OF) .outE(RELATION) .inV() .out(MENTION_OF) .path() .sideEffect( tp -> { Path path = tp.get(); Vertex source = path.get(0); Edge relation = path.get(2); Vertex target = path.get(4); GraphTraversal<Vertex, Vertex> sourceTraversal = destTraversal.V(source.id()); GraphTraversal<Vertex, Vertex> targetTraversal = destTraversal.V(target.id()); if (sourceTraversal.hasNext() && targetTraversal.hasNext()) { Vertex destSource = sourceTraversal.next(); Vertex destTarget = targetTraversal.next(); Edge destRelation = destSource.addEdge(RELATION, destTarget, id, relation.id()); copyProperties(features, relation, destRelation); } }) .iterate(); }
@Test public void testValueCoerser() throws UIMAException { EntityGraphOptions options = EntityGraphOptions.builder().withValueCoercer((v) -> 1).build(); EntityGraphFactory factory = createfactory(options); JCas jCas = JCasFactory.createJCas(); JCasTestGraphUtil.populateJcas(jCas); Graph graph = factory.create(jCas); final GraphTraversalSource traversal = graph.traversal(); traversal .V() .forEachRemaining( v -> { Set<Object> values = ElementHelper.propertyValueMap(v).values().stream().collect(Collectors.toSet()); assertAllValuesCoerceTo1(v, values); }); traversal .E() .forEachRemaining( v -> { Set<Object> values = ElementHelper.propertyValueMap(v).values().stream().collect(Collectors.toSet()); assertTrue(values.contains(1) && values.size() == 1 || values.size() == 0); }); }
/** * Create a document graph from the given jCas. * * @param jCas to create the document from * @param features to use for the graph * @return the document graph * @throws AnalysisEngineProcessException */ public Graph create(JCas jCas, Features features) throws AnalysisEngineProcessException { Graph graph = createTransformGraph(features.vertex().supportsMultiProperties()); load(jCas, graph, features); return graph; }