Skip to content

Commit

Permalink
Update vtl-prov
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoLaval committed Sep 10, 2024
1 parent a36b2e1 commit 03afa55
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ blueprintMetaShapes:SourceCodeDetail a blueprintMetaShapes:ClassDetailShape ;
sh:path sdth:hasSourceCode ;
sh:order 1 ;
blueprint:showAs blueprintMetaShapes:Plain ;
sh:targetClass sdth:Program ;
sh:targetClass sdth:ProgramStep .
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class ProvenanceListener extends VtlBaseListener {

private String currentProgramStep;

public ProvenanceListener(String programName) {
public ProvenanceListener(String id, String programName) {
program.setId(id);
program.setLabel(programName);
}

Expand Down Expand Up @@ -88,12 +89,12 @@ public List<Object> getObjects() {
return obj;
}

public static List<Object> parseAndListen(String expr) {
public static List<Object> parseAndListen(String expr, String id, String programName) {
CodePointCharStream stream = CharStreams.fromString(expr);
VtlLexer lexer = new VtlLexer(stream);
VtlParser parser = new VtlParser(new CommonTokenStream(lexer));

ProvenanceListener provenanceListener = new ProvenanceListener("test");
ProvenanceListener provenanceListener = new ProvenanceListener(id, programName);
ParseTreeWalker.DEFAULT.walk(provenanceListener, parser.start());
return provenanceListener.getObjects();
}
Expand Down
32 changes: 27 additions & 5 deletions vtl-prov/src/main/java/fr/insee/vtl/prov/RDFUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package fr.insee.vtl.prov;

import fr.insee.vtl.prov.prov.Program;
import fr.insee.vtl.prov.prov.ProgramStep;
import fr.insee.vtl.prov.utils.PROV;
import org.apache.jena.rdf.model.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.rdfconnection.RDFConnectionFactory;
import org.apache.jena.vocabulary.RDF;
Expand All @@ -23,30 +27,48 @@ public static Model buildModel(List<Object> objects) {
if (o instanceof Program) {
handleProgram(model, (Program) o);
}
if (o instanceof ProgramStep) {
handleProgramStep(model, (ProgramStep) o);
}
});
return model;
}

public static void handleProgram(Model model, Program program) {
Resource SDTH_PROGRAM = model.createResource(SDTH_BASE_URI + "Program");
String id = program.getId();
String label = program.getLabel();
Resource programURI = model.createResource(TREVAS_BASE_URI + "program/" + label);
Resource programURI = model.createResource(TREVAS_BASE_URI + "program/" + id);
programURI.addProperty(RDF.type, SDTH_PROGRAM);
programURI.addProperty(RDFS.label, label);
String sourceCode = program.getSourceCode();
Property SDTH_HAS_SOURCE_CODE = model.createProperty(SDTH_BASE_URI + "hasSourceCode");
programURI.addProperty(SDTH_HAS_SOURCE_CODE, sourceCode);
Set<String> stepIds = program.getProgramStepIds();
stepIds.forEach(id -> {
stepIds.forEach(stepId -> {
Property SDTH_HAS_PROGRAM_STEP = model.createProperty(SDTH_BASE_URI + "hasProgramStep");
Resource SDTH_PROGRAM_STEP = model.createResource(SDTH_BASE_URI + "ProgramStep");
Resource programStepURI = model.createResource(TREVAS_BASE_URI + "program-step/" + id);
Resource programStepURI = model.createResource(TREVAS_BASE_URI + "program-step/" + stepId);
programURI.addProperty(SDTH_HAS_PROGRAM_STEP, programStepURI);
programStepURI.addProperty(RDF.type, SDTH_PROGRAM_STEP);
programStepURI.addProperty(RDFS.label, id);
programStepURI.addProperty(RDFS.label, "Create " + stepId + " dataset");
});
}

public static void handleProgramStep(Model model, ProgramStep programStep) {
String label = programStep.getLabel();
Resource programStepURI = model.createResource(TREVAS_BASE_URI + "program-step/" + label);
String sourceCode = programStep.getSourceCode();
Property SDTH_HAS_SOURCE_CODE = model.createProperty(SDTH_BASE_URI + "hasSourceCode");
programStepURI.addProperty(SDTH_HAS_SOURCE_CODE, sourceCode);
Resource SDTH_DATAFRAME = model.createResource(SDTH_BASE_URI + "DataframeInstance");
Resource dfProducesURI = model.createResource(TREVAS_BASE_URI + "dataset/" + label);
dfProducesURI.addProperty(RDF.type, SDTH_DATAFRAME);
dfProducesURI.addProperty(RDFS.label, label);
Property SDTH_PRODUCES_DATAFRAME = model.createProperty(SDTH_BASE_URI + "producesDataframe");
programStepURI.addProperty(SDTH_PRODUCES_DATAFRAME, dfProducesURI);
}

public static Model initModel(String baseFilePath) {
Model model = ModelFactory.createDefaultModel();
model.read(baseFilePath);
Expand Down
12 changes: 11 additions & 1 deletion vtl-prov/src/main/java/fr/insee/vtl/prov/prov/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class Program {

String id;
String label;
Set<String> programStepIds = new HashSet<>();

Expand All @@ -13,10 +14,19 @@ public class Program {
public Program() {
}

public Program(String label) {
public Program(String id, String label) {
this.id = id;
this.label = label;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getLabel() {
return label;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void simpleTest() {
"ds_mul := ds_sum * 3; \n" +
"ds_res <- ds_mul[filter mod(var1, 2) = 0][calc var_sum := var1 + var2];";

List<Object> obj = ProvenanceListener.parseAndListen(script);
List<Object> obj = ProvenanceListener.parseAndListen(script, "trevas-simple-test", "Simple test from Trevas tests");
assertThat(obj).hasSize(4);
}
}
2 changes: 1 addition & 1 deletion vtl-prov/src/test/java/fr/insee/vtl/prov/RDFTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void simpleTest() {
"ds_mul := ds_sum * 3; \n" +
"ds_res <- ds_mul[filter mod(var1, 2) = 0][calc var_sum := var1 + var2];";

List<Object> obj = ProvenanceListener.parseAndListen(script);
List<Object> obj = ProvenanceListener.parseAndListen(script, "trevas-simple-test", "Simple test from Trevas tests");
Model model = RDFUtils.buildModel(obj);
RDFUtils.loadModelWithCredentials(model, sparqlEndpoint, sparqlEndpointUser, sparlqEndpointPassword);
assertThat(obj).hasSize(4);
Expand Down

0 comments on commit 03afa55

Please sign in to comment.