Skip to content
Merged
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
@@ -0,0 +1,29 @@
package fr.inria.corese.command.utils.coreseCoreWrapper;

import fr.inria.corese.core.Graph;

/**
* A wrapper class for Corese Graph operations.
* This class provides static methods to create and manipulate Graph instances.
*/
public class GraphWrapper {

/**
* Creates a new empty Graph instance.
*
* @return a new Graph object
*/
public static Graph createGraph() {
return Graph.create();
}

/**
* Merges the contents of the resultGraphUrl into the given graph.
*
* @param graph the graph to merge into
* @param resultGraphUrl the graph whose contents are to be merged
*/
public static void mergeGraph(Graph graph, Graph resultGraphUrl) {
graph.merge(resultGraphUrl);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fr.inria.corese.command.utils.coreseCoreWrapper;

import java.io.InputStream;

import fr.inria.corese.core.Graph;
import fr.inria.corese.core.api.Loader;
import fr.inria.corese.core.load.Load;
import fr.inria.corese.core.load.LoadException;
import fr.inria.corese.core.load.LoadFormat;

/** A class to gather all dependencies to Corese-core in one place
* This one is for everything related to RDFLoading
*/
public class RDFLoaderWrapper {

/**
* Creates a Load instance for the given graph.
*
* @param graph the graph to load data into
* @return a Load instance
*/
public static Load graphLoader(Graph graph) {
return Load.create(graph);
}

/**
* Determines the load format from the input string.
*
* @param input the input string representing the format
* @return the corresponding Loader.format
*/
public static Loader.format getLoadFormat(String input) {
return LoadFormat.getFormat(input);
}

/**
* Parses RDF data from an InputStream into the given Load instance using the specified format.
*
* @param loader the Load instance to use for parsing
* @param inputStream the InputStream containing RDF data
* @param inputFormat the format of the RDF data
* @throws LoadException if an error occurs during parsing
*/
public static void parse(Load loader, InputStream inputStream, Load.format inputFormat) throws LoadException {
loader.parse(inputStream, inputFormat);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import fr.inria.corese.command.utils.ConvertString;
import fr.inria.corese.command.utils.InputTypeDetector;
import fr.inria.corese.command.utils.InputTypeDetector.InputType;
import fr.inria.corese.core.Graph;

import fr.inria.corese.command.utils.coreseCoreWrapper.RDFLoaderWrapper;
import fr.inria.corese.core.load.Load;
import fr.inria.corese.core.load.LoadFormat;
import fr.inria.corese.command.utils.coreseCoreWrapper.GraphWrapper;
import fr.inria.corese.core.Graph;

import picocli.CommandLine.Model.CommandSpec;

/**
Expand Down Expand Up @@ -64,15 +67,15 @@ public Graph load(String[] inputs, EnumRdfInputFormat inputFormat, boolean recur
return this.LoadFromStdin(inputFormat);
}

Graph graph = Graph.create();
Graph graph = GraphWrapper.createGraph();

for (String input : inputs) {
InputType type = InputTypeDetector.detect(input);

switch (type) {
case URL:
Graph resultGraphUrl = this.loadFromURL(ConvertString.toUrlOrThrow(input), inputFormat);
graph.merge(resultGraphUrl);
GraphWrapper.mergeGraph(graph, resultGraphUrl);
break;

case FILE_PATH:
Expand All @@ -85,7 +88,7 @@ public Graph load(String[] inputs, EnumRdfInputFormat inputFormat, boolean recur
} else {
resultGraph = this.loadFromFile(path, inputFormat);
}
graph.merge(resultGraph);
GraphWrapper.mergeGraph(graph, resultGraph);
break;

default:
Expand Down Expand Up @@ -200,7 +203,7 @@ private void loadFromDirectoryRecursive(Path path, EnumRdfInputFormat inputForma
this.loadFromDirectoryRecursive(childFile.toPath(), inputFormat, recursive, graph);
} else if (childFile.isFile()) {
Graph resultGraph = this.loadFromFile(childFile.toPath(), inputFormat);
graph.merge(resultGraph);
GraphWrapper.mergeGraph(graph, resultGraph);
}
}
}
Expand All @@ -215,7 +218,7 @@ private void loadFromDirectoryRecursive(Path path, EnumRdfInputFormat inputForma
* @return The Corese Graph containing the RDF data.
*/
private Graph loadFromDirectory(Path path, EnumRdfInputFormat inputFormat, boolean recursive) {
Graph graph = Graph.create();
Graph graph = GraphWrapper.createGraph();
this.loadFromDirectoryRecursive(path, inputFormat, recursive, graph);

if (this.verbose) {
Expand All @@ -233,21 +236,21 @@ private Graph loadFromDirectory(Path path, EnumRdfInputFormat inputFormat, boole
*/
private Graph loadFromInputStream(InputStream inputStream, EnumRdfInputFormat inputFormat) {

Graph graph = Graph.create();
Load load = Load.create(graph);

if (inputFormat == null) {
throw new IllegalArgumentException(
"The input format cannot be automatically determined if you use standard input or na URL. "
+ "Please specify the input format with the option -f.");
} else {
try {
load.parse(inputStream, inputFormat.getCoreseFormat());
return graph;
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse RDF file. Check if file is well-formed and that "
+ "the input format is correct. " + e.getMessage(), e);
}
}

Graph graph = GraphWrapper.createGraph();
Load load = RDFLoaderWrapper.graphLoader(graph);

try {
RDFLoaderWrapper.parse( load, inputStream, inputFormat.getCoreseFormat());
return graph;
} catch (Exception e) {
throw new IllegalArgumentException("Failed to parse RDF file. Check if file is well-formed and that "
+ "the input format is correct. " + e.getMessage(), e);
}
}

Expand All @@ -259,7 +262,7 @@ private Graph loadFromInputStream(InputStream inputStream, EnumRdfInputFormat in
*/
private Optional<EnumRdfInputFormat> guessInputFormat(String input) {

EnumRdfInputFormat inputFormat = EnumRdfInputFormat.create(LoadFormat.getFormat(input));
EnumRdfInputFormat inputFormat = EnumRdfInputFormat.create( RDFLoaderWrapper.getLoadFormat(input));

if (inputFormat == null) {
if (this.verbose) {
Expand Down