Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve missing imports resolution fixes #676 #677

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.*;

Expand Down Expand Up @@ -175,6 +177,12 @@ public List<XMLCatalog> getAllCatalogs() {
public XMLCatalog getActiveCatalog() {
return activeCatalog;
}

public void changeActiveCatalog(@Nonnull File newCatalogFile) throws IOException {
File newCatalogDir = newCatalogFile.getParentFile();
localCatalogs.put(newCatalogDir, CatalogUtilities.parseDocument(newCatalogFile.toURI().toURL()));
activeCatalog = localCatalogs.get(newCatalogDir);
}

public File getActiveCatalogFolder() {
return activeCatalogFolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ public File chooseOWLFile(String title) {
return UIUtil.openFile(f, title, "OWL File", OWL_EXTENSIONS);
}

public File chooseOWLOrCatalogFile(String title) {
JFrame f = (JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, getParent());
if (f == null) {
f = new JFrame();
}
return UIUtil.openFile(f, title, "OWL or Catalog File", OWL_EXTENSIONS);
}


public File saveOWLFile(String title) {
return UIUtil.saveFile((JFrame) SwingUtilities.getAncestorOfClass(JFrame.class, getParent()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.protege.editor.owl.ui.library;

import org.protege.editor.core.ProtegeManager;
import org.protege.editor.core.ui.util.UIUtil;
import org.protege.editor.core.ui.workspace.WorkspaceFrame;
import org.protege.editor.owl.model.library.OntologyCatalogManager;
import org.protege.editor.owl.ui.action.ProtegeOWLAction;
import org.protege.editor.owl.ui.ontology.imports.AddImportsStrategy;
import org.protege.editor.owl.ui.ontology.imports.wizard.GetImportsVisitor;
import org.protege.editor.owl.ui.ontology.imports.wizard.ImportInfo;
import org.protege.xmlcatalog.XMLCatalog;
import org.protege.xmlcatalog.entry.Entry;
import org.semanticweb.owlapi.model.OWLOntology;
import org.slf4j.LoggerFactory;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Created by vblagodarov on 24-07-17.
*/
public class ChangeActiveOntologyLibraryAction extends ProtegeOWLAction {
public void actionPerformed(ActionEvent e) {
try {
WorkspaceFrame frame = ProtegeManager.getInstance().getFrame(getOWLWorkspace());
File catalogFile = UIUtil.openFile(
frame,
"Choose catalog file containing ontology repository information",
"Choose XML Catalog",
Collections.singleton("xml"));
if (catalogFile != null) {
OntologyCatalogManager catalogManager = getOWLModelManager().getOntologyCatalogManager();
catalogManager.changeActiveCatalog(catalogFile);
XMLCatalog activeCatalog = catalogManager.getActiveCatalog();
GetImportsVisitor getter = new GetImportsVisitor();
for (Entry entry : activeCatalog.getEntries()) {
entry.accept(getter);
}
OWLOntology activeOntology = getOWLModelManager().getActiveOntology();
XMLCatalogManager xmlCatalogManager = new XMLCatalogManager(activeCatalog);
Set<ImportInfo> importsToImport = xmlCatalogManager.getImports().stream().filter(
importInfo -> activeOntology.getDirectImportsDocuments().contains(importInfo.getImportLocation())).collect(Collectors.toSet());

AddImportsStrategy ais = new AddImportsStrategy(getOWLEditorKit(), activeOntology, importsToImport);
ais.addImports();
}
} catch (Exception ex) {
LoggerFactory.getLogger(EditActiveOntologyLibraryAction.class)
.error("An error occurred whilst attempting to change ontology catalog: {}", e);
}
}

@Override
public void initialise() throws Exception {

}

@Override
public void dispose() throws Exception {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
package org.protege.editor.owl.ui.library;

import com.google.common.base.Optional;
import org.protege.editor.owl.model.repository.MasterOntologyIDExtractor;
import org.protege.editor.owl.ui.ontology.imports.wizard.GetImportsVisitor;
import org.protege.editor.owl.ui.ontology.imports.wizard.ImportInfo;
import org.protege.xmlcatalog.EntryVisitor;
import org.protege.xmlcatalog.XMLCatalog;
import org.protege.xmlcatalog.entry.*;
import org.protege.xmlcatalog.owlapi.XMLCatalogIRIMapper;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLOntologyID;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.net.URI;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
* Created by vblagodarov on 01-08-17.
*/
public class XMLCatalogManager {

XMLCatalogIRIMapper iriMapper;
XMLCatalogEntriesVisitor entriesVisitor;
MasterOntologyIDExtractor extractor = new MasterOntologyIDExtractor();
XMLCatalog catalog;

public XMLCatalogManager(XMLCatalog xmlCatalog) {
catalog = xmlCatalog;
iriMapper = new XMLCatalogIRIMapper(xmlCatalog);
entriesVisitor = new XMLCatalogEntriesVisitor();
for (Entry entry : xmlCatalog.getEntries()) {
entry.accept(entriesVisitor);
}
}

public XMLCatalog getCatalog() {
return catalog;
}

public Collection<ImportInfo> getImports() {
Set<ImportInfo> imports = new HashSet<>();
for (XMLCatalogUriEntry entry : getAllUriEntries()) {
ImportInfo myImport = new ImportInfo();
myImport.setImportLocation(IRI.create(entry.getEntry().getName()));
myImport.setPhysicalLocation(entry.getPhysicalLocation());

Optional<OWLOntologyID> id = extractor.getOntologyId(entry.getPhysicalLocation());
//If ID is not set, the import will be ignored
if (id.isPresent()) {
myImport.setOntologyID(id.get());
}
imports.add(myImport);
}
return imports;
}

public boolean containsUri(URI physicalLocation) {
if (physicalLocation == null) {
return false;
}
return getAllUriEntries().stream().filter(
entry -> entry.getPhysicalLocation().equals(physicalLocation)).findAny().isPresent();
}

public Collection<XMLCatalogUriEntry> getAllUriEntries() {
Set<XMLCatalogUriEntry> entries = new HashSet<>();
for (UriEntry uriEntry : entriesVisitor.getAllUriEntries()) {
IRI ontologyIRI = IRI.create(uriEntry.getName());
URI redirect = iriMapper.getDocumentIRI(ontologyIRI).toURI();
entries.add(redirect == null ? new XMLCatalogUriEntry(ontologyIRI, uriEntry) :
new XMLCatalogUriEntry(ontologyIRI, uriEntry, redirect));
}

return entries;
}

public class XMLCatalogUriEntry {
private UriEntry entry;
private URI location;
private IRI ontologyIRI;

public XMLCatalogUriEntry(@Nonnull IRI ontologyIRI, @Nonnull UriEntry uriEntry) {
this(ontologyIRI, uriEntry, uriEntry.getAbsoluteURI());
}

public XMLCatalogUriEntry(@Nonnull IRI ontologyIRI, @Nonnull UriEntry uriEntry, @Nonnull URI physicalLocation) {
this.ontologyIRI = ontologyIRI;
entry = uriEntry;
location = physicalLocation;
}

public UriEntry getEntry() {
return entry;
}

public IRI getOntologyIRI() {
return ontologyIRI;
}

public URI getPhysicalLocation() {
return location;
}
}

private class XMLCatalogEntriesVisitor implements EntryVisitor {

private Set<UriEntry> uriEntries = new HashSet<>();

public Set<UriEntry> getAllUriEntries() {
return uriEntries;
}

@Override
public void visit(UriEntry entry) {
uriEntries.add(entry);
}

@Override
public void visit(GroupEntry entry) {
for (Entry subEntry : entry.getEntries()) {
subEntry.accept(this);
}
}

@Override
public void visit(PublicEntry entry) {

}

@Override
public void visit(SystemEntry entry) {

}

@Override
public void visit(RewriteSystemEntry entry) {

}

@Override
public void visit(DelegatePublicEntry entry) {

}

@Override
public void visit(DelegateSystemEntry entry) {

}

@Override
public void visit(RewriteUriEntry entry) {

}

@Override
public void visit(DelegateUriEntry entry) {

}

@Override
public void visit(NextCatalogEntry entry) {

try {
XMLCatalog catalog = entry.getParsedCatalog();
for (Entry subEntry : catalog.getEntries()) {
subEntry.accept(this);
}
} catch (IOException e) {
LoggerFactory.getLogger(GetImportsVisitor.class)
.error("Ad error occurred whilst attempting to process the XMLCatalog file: {}", e);
}
}
}
}
Loading