private void deleteEntities(Collection<String> ids) throws YardException { FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); Collection<String> toDelete = new HashSet<String>(ids); for(String id : ids){ if(id != null && !id.isEmpty()){ fieldQuery.setConstraint(RdfResourceEnum.aboutRepresentation.getUri(), new ReferenceConstraint(id)); for(Iterator<String> it = entityhubYard.findReferences(fieldQuery).iterator();it.hasNext();){ toDelete.add(it.next()); } } } if(!toDelete.isEmpty()){ entityhubYard.remove(toDelete); } }
private void deleteEntities(Collection<String> ids) throws YardException { FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); Collection<String> toDelete = new HashSet<String>(ids); for(String id : ids){ if(id != null && !id.isEmpty()){ fieldQuery.setConstraint(RdfResourceEnum.aboutRepresentation.getUri(), new ReferenceConstraint(id)); for(Iterator<String> it = entityhubYard.findReferences(fieldQuery).iterator();it.hasNext();){ toDelete.add(it.next()); } } } if(!toDelete.isEmpty()){ entityhubYard.remove(toDelete); } }
/** * @param rQuery * @param query */ private void addNameConstraint(ReconcileQuery rQuery, FieldQuery query) { //maybe an other column was also mapped to the NAME_FIELD property Collection<ReconcileValue> additionalValues = rQuery.removeProperty(NAME_FIELD); List<String> values; if(additionalValues == null){ values = Collections.singletonList(rQuery.getQuery()); } else { values = new ArrayList<String>(additionalValues.size()+1); values.add(rQuery.getQuery()); for(ReconcileValue value : additionalValues){ if(value != null && value.getValue() instanceof String){ values.add((String)value.getValue()); } } } query.setConstraint(NAME_FIELD, new TextConstraint(values)); } /**
@Override public Collection<Entity> getMappingsByTarget(String targetId) throws YardException{ if(targetId == null){ log.warn("NULL parsed as Reference -> call to getMappingsBySymbol ignored (return null)"); return null; } FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(targetId)); QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery); Collection<Entity> mappings = new HashSet<Entity>(); for(Representation rep : resultList){ mappings.add(loadEntity(rep)); } return mappings; }
@Override public Collection<Entity> getMappingsByTarget(String targetId) throws YardException{ if(targetId == null){ log.warn("NULL parsed as Reference -> call to getMappingsBySymbol ignored (return null)"); return null; } FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(targetId)); QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery); Collection<Entity> mappings = new HashSet<Entity>(); for(Representation rep : resultList){ mappings.add(loadEntity(rep)); } return mappings; }
/** * Uses the public API to clone the state of this instance to the instance provided as parameter. * * @param <C> * An implementation of the FieldQuery interface * @param copyTo * An instance to copy the state of this on. * @return The parsed instance */ public <C extends FieldQuery> C copyTo(C copyTo) { copyTo.removeAllConstraints(); copyTo.removeAllSelectedFields(); for (Entry<String,Constraint> entry : queryConstraint.entrySet()) { // we need not to copy keys or values, because everything is // immutable copyTo.setConstraint(entry.getKey(), entry.getValue()); } copyTo.addSelectedFields(selected); copyTo.setLimit(limit); copyTo.setOffset(offset); return copyTo; }
/** * Uses the public API to clone the state of this instance to the instance provided as parameter. * * @param <C> * An implementation of the FieldQuery interface * @param copyTo * An instance to copy the state of this on. * @return The parsed instance */ public <C extends FieldQuery> C copyTo(C copyTo) { copyTo.removeAllConstraints(); copyTo.removeAllSelectedFields(); for (Entry<String,Constraint> entry : queryConstraint.entrySet()) { // we need not to copy keys or values, because everything is // immutable copyTo.setConstraint(entry.getKey(), entry.getValue()); } copyTo.addSelectedFields(selected); copyTo.setLimit(limit); copyTo.setOffset(offset); return copyTo; }
fieldQuery.setConstraint(RdfResourceEnum.mappingSource.getUri(), new ReferenceConstraint(reference)); QueryResultList<Representation> resultList = entityhubYard.findRepresentation(fieldQuery); if(!resultList.isEmpty()){
protected QueryResultList<Entity> query(Site dbpediaSite, String savedEntityLabel, String language, String extractionContext) throws SiteException { FieldQuery query = dbpediaSite.getQueryFactory().createFieldQuery(); if (savedEntityLabel != null && !savedEntityLabel.isEmpty()) { Constraint labelConstraint; if (language != null) { labelConstraint = new TextConstraint(savedEntityLabel, false, language, null); } else { labelConstraint = new TextConstraint(savedEntityLabel, false); } // TODO: what happens if a recommendation was not based on rdfs:label? query.setConstraint(RDFS_LABEL.getUnicodeString(), labelConstraint); } else { log.warn("parsed label {} was empty or NULL. Will use Similarity constraint only!", savedEntityLabel); } query.setConstraint(SpecialFieldEnum.fullText.getUri(), new SimilarityConstraint(extractionContext)); query.setLimit(25); return dbpediaSite.findEntities(query); }
private void deleteMappingsbyTarget(String id) throws YardException { if(id != null && !id.isEmpty()){ FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(id)); deleteEntities(ModelUtils.asCollection( entityhubYard.findReferences(fieldQuery).iterator())); } }
private void deleteMappingsbyTarget(String id) throws YardException { if(id != null && !id.isEmpty()){ FieldQuery fieldQuery = getQueryFactory().createFieldQuery(); fieldQuery.setConstraint(RdfResourceEnum.mappingTarget.getUri(), new ReferenceConstraint(id)); deleteEntities(ModelUtils.asCollection( entityhubYard.findReferences(fieldQuery).iterator())); } }
@Test public void testFindTextWildcards(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //prefix search with * FieldQuery query = getYard().getQueryFactory().createFieldQuery(); String wildcard = data.textValue1.getText(); wildcard = wildcard.substring(0, wildcard.length()-1) + "*"; query.setConstraint(data.textField, new TextConstraint(wildcard,PatternType.wildcard,false, "en")); query.addSelectedField(data.refField); query.addSelectedField(data.textField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1en.getId(), data.r2en.getId()), Arrays.asList(data.refField, data.textField)); //wildcard with ? query = getYard().getQueryFactory().createFieldQuery(); //selects r1en and r2en wildcard = data.textValue1.getText(); wildcard = wildcard.substring(0, wildcard.length()-1) + "?"; query.setConstraint(data.textField, new TextConstraint(wildcard,PatternType.wildcard,false, "de")); query.addSelectedField(data.refField); query.addSelectedField(data.textField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1de.getId(), data.r2de.getId()), Arrays.asList(data.refField, data.textField)); }
/** * Same as {@link #testFindText()} but using * {@link Yard#findRepresentation(FieldQuery)} to execute the queries */ @Test public void testFindRepresentationText(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //query for all languages and value1 FieldQuery query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText())); validateQueryResults(query, getYard().findRepresentation(query), Arrays.asList( data.r1.getId(), data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.textField, data.refField, data.intField)); //same for value2 query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText())); validateQueryResults(query, getYard().findRepresentation(query), Arrays.asList( data.r2.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.textField, data.refField, data.intField)); } /**
/** * Tests simple {@link RangeConstraint} */ @Test public void testFindRange(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //query for all languages and value1 FieldQuery query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.intField, new RangeConstraint(data.intValue2,data.intValue5,true)); query.addSelectedField(data.intField); query.addSelectedField(data.refField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId(), data.r5.getId()), Arrays.asList(data.intField, data.refField)); //same for value2 query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.intField, new RangeConstraint(data.intValue2,data.intValue10,false)); query.addSelectedField(data.intField); query.addSelectedField(data.textField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r5.getId()), Arrays.asList(data.intField, data.textField)); } /**
query.setConstraint(data.textField, new TextConstraint( Arrays.asList(data.textValue1.getText(), data.textValue2.getText()), "en")); query.setConstraint(data.textField, new TextConstraint( Arrays.asList(data.textValue1.getText(), data.textValue2.getText()), "en", "de"));
/** * Tests simple {@link ValueConstraint}s */ @Test public void testFindValues(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //query for all languages and value1 FieldQuery query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.intField, new ValueConstraint(data.intValue1)); query.addSelectedField(data.intField); query.addSelectedField(data.textField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.intField, data.textField)); //same for value2 query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.intField, new ValueConstraint(data.intValue2)); query.addSelectedField(data.intField); query.addSelectedField(data.textField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.intField, data.textField)); } /**
/** * Test a simple {@link TextConstraint} for any language */ @Test public void testFindText(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //query for all languages and value1 FieldQuery query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText())); query.addSelectedField(data.textField); query.addSelectedField(data.refField); validateQueryResults(query, getYard().find(query), Arrays.asList( data.r1.getId(), data.r1en.getId(), data.r1de.getId()), Arrays.asList(data.textField, data.refField)); //same for value2 query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText())); query.addSelectedField(data.textField); query.addSelectedField(data.refField); validateQueryResults(query, getYard().find(query), Arrays.asList( data.r2.getId(), data.r2en.getId(), data.r2de.getId()), Arrays.asList(data.textField, data.refField)); } /**
/** * Same as {@link #testFindText()} but using * {@link Yard#findReferences(FieldQuery)} to execute the queries */ @Test public void testFindReferencesText(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //query for all languages and value1 FieldQuery query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.textField, new TextConstraint(data.textValue1.getText())); validateQueryResults(query, getYard().findReferences(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId())); //same for value2 query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.textField, new TextConstraint(data.textValue2.getText())); validateQueryResults(query, getYard().findReferences(query), Arrays.asList( data.r2.getId(), data.r2en.getId(), data.r2de.getId())); }
/** * Gets an Entity from the configured {@link Site} based on the NER text and type. * * @param ner * @param language * @return * @throws EngineException */ private Entity lookupEntity(Span ner, String language) throws EngineException { Site site = getReferencedSite(); FieldQueryFactory queryFactory = site == null ? entityHub.getQueryFactory() : site.getQueryFactory(); FieldQuery query = queryFactory.createFieldQuery(); Constraint labelConstraint; String namedEntityLabel = ner.getSpan(); labelConstraint = new TextConstraint(namedEntityLabel, false, language, null); query.setConstraint(RDFS_LABEL.getUnicodeString(), labelConstraint); query.setConstraint(RDF_TYPE.getUnicodeString(), new ReferenceConstraint(ner.getAnnotation(NlpAnnotations.NER_ANNOTATION).value().getType() .getUnicodeString())); query.setLimit(1); QueryResultList<Entity> results = site == null ? // if site is NULL entityHub.findEntities(query) : // use the Entityhub site.findEntities(query); // else the referenced site if (results.isEmpty()) return null; // We set the limit to 1 so if it found anything it should contain just 1 entry return results.iterator().next(); }
/** * Test a {@link ReferenceConstraint} */ @Test public void testFindReference(){ //init the test data FieldQueryTestData data = getFieldQueryTestData(); //query for all languages and value1 FieldQuery query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.refField, new ReferenceConstraint(data.refValue1.getReference())); query.addSelectedField(data.intField); query.addSelectedField(data.refField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r1.getId(), data.r1en.getId(), data.r1de.getId(), data.r5.getId()), Arrays.asList(data.intField, data.refField)); //same for value2 query = getYard().getQueryFactory().createFieldQuery(); query.setConstraint(data.refField, new ReferenceConstraint(data.refValue2.getReference())); query.addSelectedField(data.intField); query.addSelectedField(data.refField); validateQueryResults(query, getYard().find(query), Arrays.asList(data.r2.getId(), data.r2en.getId(), data.r2de.getId(), data.r10.getId()), Arrays.asList(data.intField, data.refField)); }