Skip to content

Commit bfa9451

Browse files
authored
Merge pull request #6 from corese-stack/wrapper-core
Wrapper core
2 parents 1cbbf5f + 91f4a03 commit bfa9451

File tree

3 files changed

+99
-19
lines changed

3 files changed

+99
-19
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package fr.inria.corese.command.utils.coreseCoreWrapper;
2+
3+
import fr.inria.corese.core.Graph;
4+
5+
/**
6+
* A wrapper class for Corese Graph operations.
7+
* This class provides static methods to create and manipulate Graph instances.
8+
*/
9+
public class GraphWrapper {
10+
11+
/**
12+
* Creates a new empty Graph instance.
13+
*
14+
* @return a new Graph object
15+
*/
16+
public static Graph createGraph() {
17+
return Graph.create();
18+
}
19+
20+
/**
21+
* Merges the contents of the resultGraphUrl into the given graph.
22+
*
23+
* @param graph the graph to merge into
24+
* @param resultGraphUrl the graph whose contents are to be merged
25+
*/
26+
public static void mergeGraph(Graph graph, Graph resultGraphUrl) {
27+
graph.merge(resultGraphUrl);
28+
}
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package fr.inria.corese.command.utils.coreseCoreWrapper;
2+
3+
import java.io.InputStream;
4+
5+
import fr.inria.corese.core.Graph;
6+
import fr.inria.corese.core.api.Loader;
7+
import fr.inria.corese.core.load.Load;
8+
import fr.inria.corese.core.load.LoadException;
9+
import fr.inria.corese.core.load.LoadFormat;
10+
11+
/** A class to gather all dependencies to Corese-core in one place
12+
* This one is for everything related to RDFLoading
13+
*/
14+
public class RDFLoaderWrapper {
15+
16+
/**
17+
* Creates a Load instance for the given graph.
18+
*
19+
* @param graph the graph to load data into
20+
* @return a Load instance
21+
*/
22+
public static Load graphLoader(Graph graph) {
23+
return Load.create(graph);
24+
}
25+
26+
/**
27+
* Determines the load format from the input string.
28+
*
29+
* @param input the input string representing the format
30+
* @return the corresponding Loader.format
31+
*/
32+
public static Loader.format getLoadFormat(String input) {
33+
return LoadFormat.getFormat(input);
34+
}
35+
36+
/**
37+
* Parses RDF data from an InputStream into the given Load instance using the specified format.
38+
*
39+
* @param loader the Load instance to use for parsing
40+
* @param inputStream the InputStream containing RDF data
41+
* @param inputFormat the format of the RDF data
42+
* @throws LoadException if an error occurs during parsing
43+
*/
44+
public static void parse(Load loader, InputStream inputStream, Load.format inputFormat) throws LoadException {
45+
loader.parse(inputStream, inputFormat);
46+
}
47+
48+
}

src/main/java/fr/inria/corese/command/utils/loader/rdf/RdfDataLoader.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212
import fr.inria.corese.command.utils.ConvertString;
1313
import fr.inria.corese.command.utils.InputTypeDetector;
1414
import fr.inria.corese.command.utils.InputTypeDetector.InputType;
15-
import fr.inria.corese.core.Graph;
15+
16+
import fr.inria.corese.command.utils.coreseCoreWrapper.RDFLoaderWrapper;
1617
import fr.inria.corese.core.load.Load;
17-
import fr.inria.corese.core.load.LoadFormat;
18+
import fr.inria.corese.command.utils.coreseCoreWrapper.GraphWrapper;
19+
import fr.inria.corese.core.Graph;
20+
1821
import picocli.CommandLine.Model.CommandSpec;
1922

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

67-
Graph graph = Graph.create();
70+
Graph graph = GraphWrapper.createGraph();
6871

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

7275
switch (type) {
7376
case URL:
7477
Graph resultGraphUrl = this.loadFromURL(ConvertString.toUrlOrThrow(input), inputFormat);
75-
graph.merge(resultGraphUrl);
78+
GraphWrapper.mergeGraph(graph, resultGraphUrl);
7679
break;
7780

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

9194
default:
@@ -200,7 +203,7 @@ private void loadFromDirectoryRecursive(Path path, EnumRdfInputFormat inputForma
200203
this.loadFromDirectoryRecursive(childFile.toPath(), inputFormat, recursive, graph);
201204
} else if (childFile.isFile()) {
202205
Graph resultGraph = this.loadFromFile(childFile.toPath(), inputFormat);
203-
graph.merge(resultGraph);
206+
GraphWrapper.mergeGraph(graph, resultGraph);
204207
}
205208
}
206209
}
@@ -215,7 +218,7 @@ private void loadFromDirectoryRecursive(Path path, EnumRdfInputFormat inputForma
215218
* @return The Corese Graph containing the RDF data.
216219
*/
217220
private Graph loadFromDirectory(Path path, EnumRdfInputFormat inputFormat, boolean recursive) {
218-
Graph graph = Graph.create();
221+
Graph graph = GraphWrapper.createGraph();
219222
this.loadFromDirectoryRecursive(path, inputFormat, recursive, graph);
220223

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

236-
Graph graph = Graph.create();
237-
Load load = Load.create(graph);
238-
239239
if (inputFormat == null) {
240240
throw new IllegalArgumentException(
241241
"The input format cannot be automatically determined if you use standard input or na URL. "
242242
+ "Please specify the input format with the option -f.");
243-
} else {
244-
try {
245-
load.parse(inputStream, inputFormat.getCoreseFormat());
246-
return graph;
247-
} catch (Exception e) {
248-
throw new IllegalArgumentException("Failed to parse RDF file. Check if file is well-formed and that "
249-
+ "the input format is correct. " + e.getMessage(), e);
250-
}
243+
}
244+
245+
Graph graph = GraphWrapper.createGraph();
246+
Load load = RDFLoaderWrapper.graphLoader(graph);
247+
248+
try {
249+
RDFLoaderWrapper.parse( load, inputStream, inputFormat.getCoreseFormat());
250+
return graph;
251+
} catch (Exception e) {
252+
throw new IllegalArgumentException("Failed to parse RDF file. Check if file is well-formed and that "
253+
+ "the input format is correct. " + e.getMessage(), e);
251254
}
252255
}
253256

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

262-
EnumRdfInputFormat inputFormat = EnumRdfInputFormat.create(LoadFormat.getFormat(input));
265+
EnumRdfInputFormat inputFormat = EnumRdfInputFormat.create( RDFLoaderWrapper.getLoadFormat(input));
263266

264267
if (inputFormat == null) {
265268
if (this.verbose) {

0 commit comments

Comments
 (0)