private void addMentionProperties(Map<String, Object> mention, Traverser<Vertex> vt) { Vertex vertex = vt.get(); mention .entrySet() .stream() .filter(property -> property.getValue() != null) .filter(property -> property.getKey() != fields.getExternalId()) .forEach(property -> vertex.property(property.getKey(), coerce(property.getValue()))); }
/** Add history to the map */ public Map<String, Object> convert() { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(new LinkedHashMap<>()); List<Object> list = createEventsList(); map.put(fields.getHistory(), list); return map; }
private Map<String, Object> createEventMap(HistoryEvent historyEvent) { Map<String, Object> linkedHistoryMap = new IgnoreEmptyKeyMapDecorator<>(new LinkedHashMap<>()); if (historyEvent.getRecordable().getInternalId() != entityInternalId) { // Only save the internal id as a reference to entities which aren't this one. linkedHistoryMap.put( fields.getHistoryRecordable(), historyEvent.getRecordable().getInternalId()); } linkedHistoryMap.put(fields.getHistoryAction(), historyEvent.getAction()); linkedHistoryMap.put(fields.getHistoryType(), historyEvent.getEventType()); linkedHistoryMap.put(fields.getHistoryParameters(), historyEvent.getParameters()); linkedHistoryMap.put(fields.getHistoryReferrer(), historyEvent.getReferrer()); linkedHistoryMap.put(fields.getHistoryTimestamp(), historyEvent.getTimestamp()); return linkedHistoryMap; }
.stream() .map(converter::convertEntity) .peek(mention -> types.add(mention.get(fields.getType()).toString())) .filter( mention -> !traversal.V(coerce(mention.get(fields.getExternalId()))).hasNext()) .map( mention -> traversal .addV(MENTION) .property(T.id, coerce(mention.get(fields.getExternalId()))) .sideEffect(vt -> addMentionProperties(mention, vt)) .next())
private void convertFeatures(Map<String, Object> map, Base base) { for (Feature f : base.getType().getFeatures()) { processFeature(map, base, f); } map.put(fields.getType(), base.getType().getShortName()); if (map.get(FIELD_VALUE) == null || Strings.isNullOrEmpty(map.get(FIELD_VALUE).toString())) { map.put(FIELD_VALUE, base.getCoveredText()); } }
@Test public void testMergedHistoryEventIsConverted() { Map<String, Object> mergedHistoryMap = (Map<String, Object>) historyList.get(1); assertEquals( "Merged history event should have a type of " + HistoryEvents.MERGED_TYPE, HistoryEvents.MERGED_TYPE, mergedHistoryMap.get(fields.getHistoryType())); } }
private void putGeoJson(Map<String, Object> map, String geojson) { try { if (!Strings.isNullOrEmpty(geojson)) { map.put(fields.getGeoJSON(), mapper.readValue(geojson, mapLikeType)); } } catch (IOException e) { getMonitor().warn("Unable to persist geoJson", e); } }
.stream() .map(converter::convertEntity) .peek(mention -> types.add(mention.get(fields.getType()).toString())) .filter( mention -> !traversal.V(coerce(mention.get(fields.getExternalId()))).hasNext()) .map( mention -> traversal .addV(MENTION) .property(T.id, coerce(mention.get(fields.getExternalId()))) .sideEffect(vt -> addMentionProperties(mention, vt)) .next())
@Test public void testHistoryEventIsConvertedToMap() { Map<String, Object> nonMergedHistoryMap = (Map<String, Object>) historyList.get(0); Map<String, String> nonMergedHistoryParameters = (Map<String, String>) nonMergedHistoryMap.get(fields.getHistoryParameters()); assertEquals( "Linked history map should have an action", ACTION, nonMergedHistoryMap.get(fields.getHistoryAction())); assertEquals( "Linked history map should have an event type", EVENT_TYPE, nonMergedHistoryMap.get(fields.getHistoryType())); assertEquals( "History parameters should have kwy1-value1", "value1", nonMergedHistoryParameters.get("key1")); assertEquals( "Linked history map should have referrer", REFERRER, nonMergedHistoryMap.get(fields.getHistoryReferrer())); assertEquals( "Linked history map should have timestamp", currentTimeMillis, nonMergedHistoryMap.get(fields.getHistoryTimestamp())); }
private void addRelationProperties( Map<String, Object> relation, Traverser<? extends Element> relationElement) { Element rel = relationElement.get(); relation .entrySet() .stream() .filter(property -> property.getValue() != null) .filter(property -> property.getKey() != fields.getExternalId()) .forEach(property -> setProperty(rel, property.getKey(), property.getValue())); }
@SuppressWarnings("unchecked") @Test public void testHistory() throws AnalysisEngineProcessException { jCas.setDocumentText("Bill went to London. William came back."); Person p = Annotations.createPerson(jCas, 0, 4, "Bill"); Person q = Annotations.createPerson(jCas, 21, 28, NAME_2); DocumentHistory documentHistory = history.getHistory("unknown:" + getDocumentAnnotation(jCas).getHash()); documentHistory.add(HistoryEvents.createAdded(p, "test")); documentHistory.add(HistoryEvents.createAdded(q, "test")); documentHistory.add(HistoryEvents.createMerged(p, "test", q.getInternalId())); documentHistory.add(HistoryEvents.createMerged(p, "fakeId merge", 500)); documentHistory.add(HistoryEvents.createRemoved(q, "test")); ae.process(jCas); Collection<HistoryEvent> pHistory = documentHistory.getHistory(p.getInternalId()); Collection<HistoryEvent> qHistory = documentHistory.getHistory(q.getInternalId()); assertEquals(1, documents.count()); assertEquals(2, entities.count()); Document a = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, "Bill")).first(); List<Document> pH = (List<Document>) ((List<Document>) a.get(Mongo.FIELD_ENTITIES)).get(0).get(fields.getHistory()); assertEquals(pHistory.size() + qHistory.size(), pH.size()); Document b = entities.find(new Document(Mongo.FIELD_ENTITIES + "." + VALUE, NAME_2)).first(); List<Document> qH = (List<Document>) ((List<Document>) b.get(Mongo.FIELD_ENTITIES)).get(0).get(fields.getHistory()); assertEquals(qHistory.size(), qH.size()); }
private void addRelationProperties( Map<String, Object> relation, Traverser<? extends Element> relationElement) { Element rel = relationElement.get(); relation .entrySet() .stream() .filter(property -> property.getValue() != null) .filter(property -> property.getKey() != fields.getExternalId()) .forEach(property -> setProperty(rel, property.getKey(), property.getValue())); }
private void addMentionProperties(Map<String, Object> mention, Traverser<Vertex> vt) { Vertex vertex = vt.get(); mention .entrySet() .stream() .filter(property -> property.getValue() != null) .filter(property -> property.getKey() != fields.getExternalId()) .forEach(property -> vertex.property(property.getKey(), coerce(property.getValue()))); }
/** Get the mongo db, collection and create some indexes */ @Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { MongoDatabase db = mongoResource.getDB(); entitiesCollection = db.getCollection(entitiesCollectionName); relationsCollection = db.getCollection(relationsCollectionName); documentsCollection = db.getCollection(documentsCollectionName); documentsCollection.createIndex(new Document(fields.getExternalId(), 1)); entitiesCollection.createIndex(new Document(fields.getExternalId(), 1)); relationsCollection.createIndex(new Document(fields.getExternalId(), 1)); relationsCollection.createIndex(new Document(FIELD_DOCUMENT_ID, 1)); entitiesCollection.createIndex(new Document(FIELD_DOCUMENT_ID, 1)); stopFeatures = new HashSet<>(); stopFeatures.add("uima.cas.AnnotationBase:sofa"); stopFeatures.add("uk.gov.dstl.baleen.types.BaleenAnnotation:internalId"); }
private void deleteAnyExistingContent(String documentId) { entitiesCollection.deleteMany(new Document(FIELD_DOCUMENT_ID, documentId)); relationsCollection.deleteMany(new Document(FIELD_DOCUMENT_ID, documentId)); documentsCollection.deleteMany(new Document(fields.getExternalId(), documentId)); }
/** Get the mongo db, collection and create some indexes */ @Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { MongoDatabase db = mongoResource.getDB(); relationsCollection = db.getCollection(relationsCollectionName); relationsCollection.createIndex(new Document(fields.getExternalId(), 1)); relationsCollection.createIndex(new Document(FIELD_SOURCE_VALUE, 1)); relationsCollection.createIndex(new Document(FIELD_VALUE, 1)); relationsCollection.createIndex(new Document(FIELD_TARGET_VALUE, 1)); relationsCollection.createIndex( new Document(ImmutableMap.of(FIELD_SOURCE_VALUE, 1, FIELD_TARGET_VALUE, 1))); relationsCollection.createIndex(new Document(FIELD_DOCUMENT_ID, 1)); }
/** Get the mongo db, collection and create some indexes */ @Override public void doInitialize(UimaContext aContext) throws ResourceInitializationException { MongoDatabase db = mongoResource.getDB(); eventsCollection = db.getCollection(eventsCollectionName); eventsCollection.createIndex(new Document(fields.getExternalId(), 1)); eventsCollection.createIndex(new Document(FIELD_DOCUMENT_ID, 1)); eventsCollection.createIndex(new Document(FIELD_TYPE, 1)); eventsCollection.createIndex(new Document(FIELD_TYPES, 1)); eventsCollection.createIndex(new Document(FIELD_TOKENS, 1)); Set<String> stopFeatures = new HashSet<>(); stopFeatures.add("uima.cas.AnnotationBase:sofa"); stopFeatures.add("uk.gov.dstl.baleen.types.BaleenAnnotation:internalId"); textClass = getTextClass(); }
/** * Convert from a relation to a map. * * @param relation the relation to convert * @return a map containing the relation's fields (and history is required) */ public Map<String, Object> convertRelation(Relation relation) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, relation); if (outputHistory && documentHistory != null) { convertHistory(map, relation); } map.put(fields.getExternalId(), relation.getExternalId()); return map; }
/** * Convert from an entity to a map. * * @param entity the entity to convert * @return a map containing the entity's fields (and history is required) */ public Map<String, Object> convertEntity(Entity entity) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, entity); if (outputHistory && documentHistory != null) { convertHistory(map, entity); } map.put(fields.getExternalId(), entity.getExternalId()); return map; }
/** * Convert from an event to a map. * * @param event the relation to convert * @return a map containing the relation's fields (and history is required) */ public Map<String, Object> convertEvent(Event event) { Map<String, Object> map = new IgnoreEmptyKeyMapDecorator<>(Maps.newHashMap()); convertFeatures(map, event); if (outputHistory && documentHistory != null) { convertHistory(map, event); } map.put(fields.getExternalId(), event.getExternalId()); return map; }