Skip to content

Commit

Permalink
Merge pull request #4462 from inception-project/feature/4461-Show-cla…
Browse files Browse the repository at this point in the history
…ss-in-class-tree-when-selecting-a-class

#4461 - Show class in class tree when selecting a class
  • Loading branch information
reckart authored Jan 27, 2024
2 parents 1e1c232 + 6e727e0 commit 551237f
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,10 @@ private CandidateEntity initCandidate(CandidateEntity candidate, String aQuery,
public List<KBHandle> rankCandidates(String aQuery, String aMention, Set<KBHandle> aCandidates,
CAS aCas, int aBegin)
{
long startTime = currentTimeMillis();
var startTime = currentTimeMillis();

// Set the feature values
List<CandidateEntity> candidates = aCandidates.stream() //
var candidates = aCandidates.stream() //
.map(CandidateEntity::new) //
.map(candidate -> initCandidate(candidate, aQuery, aMention, aCas, aBegin))
.map(candidate -> {
Expand All @@ -452,7 +452,7 @@ public List<KBHandle> rankCandidates(String aQuery, String aMention, Set<KBHandl
// Sort candidates by multiple keys.
candidates.sort(BaselineRankingStrategy.getInstance());

List<KBHandle> results = candidates.stream() //
var results = candidates.stream() //
.map(candidate -> {
KBHandle handle = candidate.getHandle();
handle.setDebugInfo(String.valueOf(candidate.getFeatures()));
Expand All @@ -463,13 +463,13 @@ public List<KBHandle> rankCandidates(String aQuery, String aMention, Set<KBHandl
}) //
.collect(Collectors.toList());

int rank = 1;
for (KBHandle handle : results) {
var rank = 1;
for (var handle : results) {
handle.setRank(rank);
rank++;
}

long duration = currentTimeMillis() - startTime;
var duration = currentTimeMillis() - startTime;
log.debug("Ranked [{}] candidates for mention [{}] and query [{}] in [{}] ms",
results.size(), aMention, aQuery, duration);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public void setScore(double aScore)

public static KBHandle of(KBObject aObject)
{
return new KBHandle(aObject.getIdentifier(), aObject.getUiLabel());
return aObject.toKBHandle();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ default KBHandle toKBHandle()
handle.setName(getName());
handle.setLanguage(getLanguage());
handle.setDescription(getDescription());
handle.setKB(getKB());
return handle;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package de.tudarmstadt.ukp.inception.ui.kb;

import java.lang.invoke.MethodHandles;
import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -36,6 +37,9 @@
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wicketstuff.event.annotation.OnEvent;

import de.tudarmstadt.ukp.inception.kb.IriConstants;
import de.tudarmstadt.ukp.inception.kb.KnowledgeBaseService;
Expand All @@ -51,11 +55,14 @@ public class ConceptTreePanel
{
private static final long serialVersionUID = -4032884234215283745L;

private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private @SpringBean KnowledgeBaseService kbService;

private IModel<KBObject> selectedConcept;
private IModel<KnowledgeBase> kbModel;
private IModel<ConceptTreeProviderOptions> options;
private AbstractTree<KBObject> tree;

public ConceptTreePanel(String aId, IModel<KnowledgeBase> aKbModel,
IModel<KBObject> selectedConceptModel)
Expand All @@ -68,7 +75,25 @@ public ConceptTreePanel(String aId, IModel<KnowledgeBase> aKbModel,
kbModel = aKbModel;
options = Model.of(new ConceptTreeProviderOptions());

AbstractTree<KBObject> tree = new DefaultNestedTree<KBObject>("tree",
tree = createConceptTree();
add(tree);

var addLink = new LambdaAjaxLink("add",
_target -> send(getPage(), Broadcast.BREADTH, new AjaxNewConceptEvent(_target)));
addLink.add(new Label("label", new ResourceModel("concept.list.add")));
addLink.add(new WriteProtectionBehavior(kbModel));
add(addLink);

var form = new Form<ConceptTreeProviderOptions>("form", CompoundPropertyModel.of(options));
form.add(new CheckBox("showAllConcepts").setOutputMarkupId(true) //
.add(new LambdaAjaxFormSubmittingBehavior("change",
this::actionPreferenceChanged)));
add(form);
}

private DefaultNestedTree<KBObject> createConceptTree()
{
return new DefaultNestedTree<KBObject>("tree",
new ConceptTreeProvider(kbService, kbModel, options), Model.ofSet(new HashSet<>()))
{
private static final long serialVersionUID = -270550186750480253L;
Expand Down Expand Up @@ -112,27 +137,29 @@ protected boolean isSelected()
};
}
};
add(tree);

LambdaAjaxLink addLink = new LambdaAjaxLink("add",
target -> send(getPage(), Broadcast.BREADTH, new AjaxNewConceptEvent(target)));
addLink.add(new Label("label", new ResourceModel("concept.list.add")));
addLink.add(new WriteProtectionBehavior(kbModel));
add(addLink);
}

Form<ConceptTreeProviderOptions> form = new Form<>("form",
CompoundPropertyModel.of(options));
form.add(new CheckBox("showAllConcepts").setOutputMarkupId(true) //
.add(new LambdaAjaxFormSubmittingBehavior("change",
this::actionPreferenceChanged)));
add(form);
@OnEvent
public void onConceptSelectionEvent(AjaxConceptSelectionEvent aEvent)
{
// Try expanding the path to the selected concept
if (selectedConcept.isPresent().getObject()) {
var c = (KBObject) selectedConcept.getObject();
var parents = kbService.getParentConceptList(c.getKB(), c.getIdentifier(), false);
LOG.debug("Trying to expand {}", parents);
for (var h : parents) {
tree.expand(h);
}
aEvent.getTarget().add(this);
aEvent.getTarget().appendJavaScript("document.querySelector('#" + getMarkupId()
+ " .selected')?.scrollIntoView({block: 'center'});");
}
}

private void actionSelectionChanged(AjaxRequestTarget aTarget)
{
// if the selection changes, publish an event denoting the change
AjaxConceptSelectionEvent e = new AjaxConceptSelectionEvent(aTarget,
selectedConcept.getObject().toKBHandle());
var e = new AjaxConceptSelectionEvent(aTarget, selectedConcept.getObject().toKBHandle());
send(getPage(), Broadcast.BREADTH, e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean hasChildren(KBObject aNode)
// To avoid having to send a query to the KB for every child node, just assume
// that there might be child nodes and show the expander until we have actually
// loaded the children, cached them and can show the true information.
List<KBHandle> children = childrensCache.get(aNode);
var children = childrensCache.get(aNode);
if (children == null) {
return true;
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public Iterator<? extends KBObject> getChildren(KBObject aNode)
try {
// If the KB is read-only, then we cache the values and re-use the cached values.
if (kbModel.getObject().isReadOnly()) {
List<KBHandle> children = childrensCache.get(aNode);
var children = childrensCache.get(aNode);
if (children == null) {
children = kbService.listChildConcepts(kbModel.getObject(),
aNode.getIdentifier(), options.getObject().isShowAllConcepts());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
Expand Down Expand Up @@ -151,16 +150,16 @@ public KnowledgeBasePanel(String id, IModel<Project> aProjectModel,
private KnowledgeBaseItemAutoCompleteField createSearchField(String aId,
IModel<KBHandle> aHandleModel, IModel<Project> aProjectModel)
{
KnowledgeBaseItemAutoCompleteField field = new KnowledgeBaseItemAutoCompleteField(aId,
aHandleModel, _query -> listSearchResults(aProjectModel.getObject(), _query))
var field = new KnowledgeBaseItemAutoCompleteField(aId, aHandleModel,
_query -> listSearchResults(aProjectModel.getObject(), _query))
{
private static final long serialVersionUID = 3188821013226116770L;

@Override
protected void onSelected(AjaxRequestTarget aTarget)
{
KBHandle selectedResource = this.getModelObject();
Optional<KBObject> optKbObject = kbService.readItem(kbModel.getObject(),
var selectedResource = this.getModelObject();
var optKbObject = kbService.readItem(kbModel.getObject(),
selectedResource.getIdentifier());

if (!optKbObject.isPresent()) {
Expand All @@ -186,12 +185,10 @@ protected void onSelected(AjaxRequestTarget aTarget)
*/
private List<KBHandle> listSearchResults(Project aProject, String aTypedString)
{
List<KBHandle> results;
KnowledgeBase kb = kbModel.getObject();
results = conceptLinkingService.searchItems(kb, aTypedString).stream()
.limit(entityLinkingProperties.getCandidateDisplayLimit())
.collect(Collectors.toList());
return results;
var kb = kbModel.getObject();
return conceptLinkingService.searchItems(kb, aTypedString).stream() //
.limit(entityLinkingProperties.getCandidateDisplayLimit()) //
.toList();
}

/**
Expand All @@ -204,8 +201,8 @@ private void sendSelectionChangedEvents(AjaxRequestTarget aTarget, KBObject aKbO
new AjaxConceptSelectionEvent(aTarget, KBHandle.of(aKbObject), true));
}
else if (aKbObject instanceof KBInstance) {
List<KBHandle> conceptsForInstance = kbService
.getConceptForInstance(kbModel.getObject(), aKbObject.getIdentifier(), true);
var conceptsForInstance = kbService.getConceptForInstance(kbModel.getObject(),
aKbObject.getIdentifier(), true);

if (!conceptsForInstance.isEmpty()) {
send(getPage(), Broadcast.BREADTH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@
import static java.util.Collections.emptyMap;
import static org.apache.commons.lang3.exception.ExceptionUtils.getRootCauseMessage;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.feedback.IFeedback;
import org.apache.wicket.markup.html.form.Form;
Expand Down Expand Up @@ -147,7 +144,7 @@ private void actionSave(AjaxRequestTarget aTarget, Form<KnowledgeBaseWrapper> aF
aTarget.add(this);

try {
KnowledgeBaseWrapper kbw = kbwModel.getObject();
var kbw = kbwModel.getObject();

// if dealing with a remote repository and a non-empty URL, get a new
// RepositoryImplConfig for the new URL; otherwise keep using the existing config
Expand All @@ -167,8 +164,8 @@ private void actionSave(AjaxRequestTarget aTarget, Form<KnowledgeBaseWrapper> aF

if (kb.getType() == LOCAL) {
kbService.defineBaseProperties(kb);
for (Pair<String, File> f : kbw.getFiles()) {
try (InputStream is = new FileInputStream(f.getValue())) {
for (var f : kbw.getFiles()) {
try (var is = new FileInputStream(f.getValue())) {
kbService.importData(kb, f.getValue().getName(), is);
success("Imported: " + f.getKey());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ kb.wizard.steps.accessSpecific.description=Description
kb.wizard.steps.accessSpecific.hostInstitutionName=Host Institution
kb.wizard.steps.accessSpecific.authorName=Author(s)
kb.wizard.steps.accessSpecific.homepage=Homepage
kb.wizard.steps.accessSpecific.license=License
kb.wizard.title=New Knowledge Base

kb.settings.general = General Settings
Expand Down Expand Up @@ -102,7 +103,7 @@ kb.export.nt=N-Triples
RepositoryType.LOCAL=Local
RepositoryType.REMOTE=Remote (SPARQL)

kb.reification = Reification
kb.reification = Reification

SchemaProfile.RDFSCHEMA=RDF
SchemaProfile.OWLSCHEMA=OWL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,25 +389,25 @@ private void actionDownloadKbAndSetIRIs(AjaxRequestTarget aTarget)
{
try {
if (selectedKnowledgeBaseProfile != null) {
String accessUrl = selectedKnowledgeBaseProfile.getAccess().getAccessUrl();
var accessUrl = selectedKnowledgeBaseProfile.getAccess().getAccessUrl();

FileUploadDownloadHelper fileUploadDownloadHelper = new FileUploadDownloadHelper(
getApplication());
var fileUploadDownloadHelper = new FileUploadDownloadHelper(getApplication());

if (!accessUrl.startsWith(CLASSPATH_PREFIX)) {
File tmpFile = fileUploadDownloadHelper
.writeFileDownloadToTemporaryFile(accessUrl, getModel());
getModel().getObject().putFile(selectedKnowledgeBaseProfile.getName(), tmpFile);
}
else {
if (accessUrl.startsWith(CLASSPATH_PREFIX)) {
// import from classpath
File kbFile = fileUploadDownloadHelper
var kbFile = fileUploadDownloadHelper
.writeClasspathResourceToTemporaryFile(accessUrl, getModel());
getModel().getObject().putFile(selectedKnowledgeBaseProfile.getName(), kbFile);
}
else if (accessUrl != null) {
var tmpFile = fileUploadDownloadHelper
.writeFileDownloadToTemporaryFile(accessUrl, getModel());
getModel().getObject().putFile(selectedKnowledgeBaseProfile.getName(), tmpFile);
}

KnowledgeBase kb = getModel().getObject().getKb();
var kb = getModel().getObject().getKb();
kb.applyRootConcepts(selectedKnowledgeBaseProfile);
kb.applyAdditionalMatchingProperties(selectedKnowledgeBaseProfile);
kb.applyMapping(selectedKnowledgeBaseProfile.getMapping());
kb.setFullTextSearchIri(
selectedKnowledgeBaseProfile.getAccess().getFullTextSearchIri());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import de.tudarmstadt.ukp.inception.kb.model.KnowledgeBase;
import de.tudarmstadt.ukp.inception.kb.yaml.KnowledgeBaseInfo;
import de.tudarmstadt.ukp.inception.kb.yaml.KnowledgeBaseProfile;
import de.tudarmstadt.ukp.inception.security.client.auth.AuthenticationTraitsEditor;
Expand Down Expand Up @@ -219,11 +218,11 @@ protected void populateItem(ListItem<KnowledgeBaseProfile> item)
private void actionPopulate(AjaxRequestTarget aTarget, KnowledgeBaseProfile aProfile)
{
// set all the fields according to the chosen profile
KnowledgeBaseWrapper kbw = getModel().getObject();
var kbw = getModel().getObject();
kbw.setUrl(aProfile.getAccess().getAccessUrl());
// sets root concepts list - if null then an empty list otherwise change the
// values to IRI and populate the list
KnowledgeBase kb = kbw.getKb();
var kb = kbw.getKb();
kb.applyRootConcepts(aProfile);
kb.applyAdditionalMatchingProperties(aProfile);
kb.applyMapping(aProfile.getMapping());
Expand Down

0 comments on commit 551237f

Please sign in to comment.