NoMultipleIncomingRelationsCheck implements Check { @Override public boolean check(Project aProject, CAS aCas, List<LogMessage> aMessages) { boolean ok = true; if (annotationService == null) { return ok; } List<AnnotationLayer> allAnnoLayers = annotationService.listAnnotationLayer(aProject); if (allAnnoLayers != null) { for (AnnotationLayer layer : allAnnoLayers) { if (!WebAnnoConst.RELATION_TYPE.equals(layer.getType())) { continue; } if (!Dependency.class.getName().equals(layer.getName())) { continue; } Type type; try { type = getType(aCas, layer.getName()); } catch (IllegalArgumentException e) { continue; } Map<AnnotationFS, AnnotationFS> incoming = new HashMap<>(); for (AnnotationFS rel : select(aCas, type)) { AnnotationFS source = getFeature(rel, FEAT_REL_SOURCE, AnnotationFS.class); AnnotationFS target = getFeature(rel, FEAT_REL_TARGET, AnnotationFS.class); AnnotationFS existingSource = incoming.get(target); if (existingSource != null) { Optional<Integer> sentenceNumber = Optional.empty(); try { sentenceNumber = Optional.of(WebAnnoCasUtil .getSentenceNumber(target.getCAS(), target.getBegin())); } catch (IndexOutOfBoundsException e) { sentenceNumber = Optional.empty(); } if (sentenceNumber.isPresent()) { aMessages.add(new LogMessage(this, LogLevel.WARN, "Sentence %d: Relation [%s] -> [%s] points to span that already has an " + "incoming relation [%s] -> [%s].", sentenceNumber.get(), source.getCoveredText(), target.getCoveredText(), existingSource.getCoveredText(), target.getCoveredText())); } else { aMessages.add(new LogMessage(this, LogLevel.WARN, "Relation [%s] -> [%s] points to span that already has an " + "incoming relation [%s] -> [%s].", source.getCoveredText(), target.getCoveredText(), existingSource.getCoveredText(), target.getCoveredText())); } } else { incoming.put(target, source); } } } } return ok; }  @Override boolean check(Project aProject, CAS aCas, List<LogMessage> aMessages);  }
@Test public void testFail() throws Exception { AnnotationLayer relationLayer = new AnnotationLayer(); relationLayer.setName(Dependency.class.getName()); relationLayer.setType(WebAnnoConst.RELATION_TYPE); Mockito.when(annotationService.listAnnotationLayer(Mockito.isNull())) .thenReturn(Arrays.asList(relationLayer)); JCas jcas = JCasFactory.createJCas(); jcas.setDocumentText("This is a test."); Token spanThis = new Token(jcas, 0, 4); spanThis.addToIndexes(); Token spanIs = new Token(jcas, 5, 7); spanIs.addToIndexes(); Token spanA = new Token(jcas, 8, 9); spanA.addToIndexes(); Dependency dep1 = new Dependency(jcas, 0, 7); dep1.setGovernor(spanThis); dep1.setDependent(spanIs); dep1.addToIndexes(); Dependency dep2 = new Dependency(jcas, 0, 9); dep2.setGovernor(spanA); dep2.setDependent(spanIs); dep2.addToIndexes(); List<LogMessage> messages = new ArrayList<>(); boolean result = check.check(null, jcas.getCas(), messages); messages.forEach(System.out::println); assertTrue(result); assertEquals(1, messages.size()); assertEquals( "[NoMultipleIncomingRelationsCheck] Relation [This] -> [is] points to span that already has an incoming relation [a] -> [is].", messages.get(0).toString()); }
@Test public void testOK() throws Exception { AnnotationLayer relationLayer = new AnnotationLayer(); relationLayer.setName(Dependency.class.getName()); relationLayer.setType(WebAnnoConst.RELATION_TYPE); Mockito.when(annotationService.listAnnotationLayer(Mockito.isNull())) .thenReturn(Arrays.asList(relationLayer)); JCas jcas = JCasFactory.createJCas(); jcas.setDocumentText("This is a test."); Token spanThis = new Token(jcas, 0, 4); spanThis.addToIndexes(); Token spanIs = new Token(jcas, 6, 8); spanIs.addToIndexes(); Token spanA = new Token(jcas, 9, 10); spanA.addToIndexes(); Dependency dep1 = new Dependency(jcas, 0, 8); dep1.setGovernor(spanThis); dep1.setDependent(spanIs); dep1.addToIndexes(); Dependency dep2 = new Dependency(jcas, 6, 10); dep2.setGovernor(spanIs); dep2.setDependent(spanA); dep2.addToIndexes(); List<LogMessage> messages = new ArrayList<>(); boolean result = check.check(null, jcas.getCas(), messages); messages.forEach(System.out::println); assertTrue(result); }
@Test public void testOkBecauseCoref() throws Exception { AnnotationLayer relationLayer = new AnnotationLayer(); relationLayer.setName(CoreferenceChain.class.getName()); relationLayer.setType(WebAnnoConst.CHAIN_TYPE); Mockito.when(annotationService.listAnnotationLayer(Mockito.isNull())) .thenReturn(Arrays.asList(relationLayer)); JCas jcas = JCasFactory.createJCas(); jcas.setDocumentText("This is a test."); Token spanThis = new Token(jcas, 0, 4); spanThis.addToIndexes(); Token spanIs = new Token(jcas, 6, 8); spanIs.addToIndexes(); Token spanA = new Token(jcas, 9, 10); spanA.addToIndexes(); Dependency dep1 = new Dependency(jcas, 0, 8); dep1.setGovernor(spanThis); dep1.setDependent(spanIs); dep1.addToIndexes(); Dependency dep2 = new Dependency(jcas, 0, 10); dep2.setGovernor(spanA); dep2.setDependent(spanIs); dep2.addToIndexes(); List<LogMessage> messages = new ArrayList<>(); boolean result = check.check(null, jcas.getCas(), messages); messages.forEach(System.out::println); assertTrue(result); }