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

Feature/optimalize development env #62

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
@@ -1,5 +1,6 @@
package cz.cvut.spipes.manager;

import com.google.common.collect.HashMultimap;
import cz.cvut.spipes.config.CompatibilityConfig;
import cz.cvut.spipes.util.JenaUtils;
import cz.cvut.spipes.util.SparqlMotionUtils;
Expand All @@ -18,10 +19,7 @@
import org.slf4j.LoggerFactory;
import org.topbraid.spin.system.SPINModuleRegistry;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.net.URISyntaxException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.Files;
Expand Down Expand Up @@ -58,7 +56,8 @@
public class OntoDocManager implements OntologyDocumentManager {

private static final Logger LOG = LoggerFactory.getLogger(OntoDocManager.class);
private static Instant lastTime = Instant.now();
private static final HashMap<Path, Instant> filesModificationTime = new HashMap<>();
private static final HashMultimap<Path, Path> loadedFiles = HashMultimap.create(); // {directory: filesInDirectory}
private static boolean reloadFiles = false;

// TODO remove !!!!!!! this is workaround for registering SPIN related things.
Expand Down Expand Up @@ -115,7 +114,6 @@ public void registerDocuments(Path directoryOrFilePath) {
ontDocumentManager.addAltEntry(e.getKey(), e.getValue());
}
);
lastTime = Instant.now();
}

@Override
Expand Down Expand Up @@ -154,11 +152,11 @@ private static boolean isFileNameSupported(String fileName) {
return Arrays.stream(SUPPORTED_FILE_EXTENSIONS).anyMatch(ext -> fileName.endsWith("." + ext));
}

private static boolean wasModified(Path fileName){
private static boolean wasModifiedOrNewlyAdded(Path file){
BasicFileAttributes attr;
try {
attr = Files.readAttributes(fileName, BasicFileAttributes.class);
return attr.lastModifiedTime().toInstant().isAfter(lastTime);
attr = Files.readAttributes(file, BasicFileAttributes.class);
return !filesModificationTime.containsKey(file) || attr.lastModifiedTime().toInstant().isAfter(filesModificationTime.get(file));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -168,25 +166,37 @@ private static boolean wasModified(Path fileName){
public static Map<String, Model> getAllFile2Model(Path directoryOrFilePath) {
Map<String, Model> file2Model = new HashMap<>();

if(reloadFiles){
checkFileExistence(directoryOrFilePath);
}

try (Stream<Path> stream = Files.walk(directoryOrFilePath)) {
stream
.filter(f -> !Files.isDirectory(f))
.filter(f -> {
String fileName = f.getFileName().toString();
return isFileNameSupported(fileName);
})
.filter(f -> !reloadFiles || wasModifiedOrNewlyAdded(f))
.forEach(file -> {
if(reloadFiles && !wasModified(file)){
return;
}
String lang = FileUtils.guessLang(file.getFileName().toString());
loadedFiles.put(directoryOrFilePath, file);

LOG.debug("Loading model from {} ...", file.toUri().toString());
Model model = loadModel(file, lang);

if (model != null) {
OntoDocManager.addSPINRelevantModel(file.toAbsolutePath().toString(), model);
}
if(reloadFiles){
BasicFileAttributes attr;
try {
attr = Files.readAttributes(file, BasicFileAttributes.class);
filesModificationTime.put(file, attr.lastModifiedTime().toInstant());
} catch (IOException e) {
e.printStackTrace();
}
}

file2Model.put(file.toString(), model);

Expand All @@ -199,6 +209,20 @@ public static Map<String, Model> getAllFile2Model(Path directoryOrFilePath) {
return file2Model;
}

private static void checkFileExistence(Path directoryOrFilePath) {
for (Path p: loadedFiles.get(directoryOrFilePath)){
String file = p.toString();
try{
File f = new File(file);
if (!f.exists()) {
throw new FileNotFoundException(file);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

// TODO remove this method !!!
private static void addSPINRelevantModel(String fileName, Model model) {
String baseURI = JenaUtils.getBaseUri(model);
Expand Down