Skip to content

Commit

Permalink
Update provenance
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoLaval committed Aug 28, 2024
1 parent 7ce9c56 commit 8b6ea61
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package fr.insee.vtl.provenance;

import fr.insee.vtl.parser.VtlLexer;
import fr.insee.vtl.parser.VtlParser;
import fr.insee.vtl.provenance.model.VTLDataset;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;

import java.util.*;

public class ProvenanceUtils {

public static ProvenanceListener getProvenance(String script) {
CodePointCharStream stream = CharStreams.fromString(script);
VtlLexer lexer = new VtlLexer(stream);
VtlParser parser = new VtlParser(new CommonTokenStream(lexer));

ProvenanceListener provenanceListener = new ProvenanceListener();
ParseTreeWalker.DEFAULT.walk(provenanceListener, parser.start());

return provenanceListener;
}

public static void printTree(ProvenanceListener.Node node, String prefix, boolean isLast) {
if (node == null) {
return;
}

// Print the current node with appropriate formatting
System.out.println(prefix + (isLast ? "└── " : "├── ") + node.name + "\t[ " + node.expression + " ]");

// Get the list of parent nodes
Collection<ProvenanceListener.Node> parents = node.parents.values();
int count = parents.size();
int index = 0;

// Recursively print each parent node
for (ProvenanceListener.Node parent : parents) {
printTree(parent, prefix + (isLast ? " " : "│ "), ++index == count);
}
}

public static List<Object> toBusinessModel(ProvenanceListener listener) {
ArrayList<Object> model = new ArrayList<>();
LinkedHashMap<String, ProvenanceListener.Node> variables = listener.variables;
variables.values().forEach(node -> {
String name = node.name;
Map<String, ProvenanceListener.Node> parents = node.parents;
VTLDataset vtlDataset = new VTLDataset(name);
model.add(vtlDataset);
});
return model;
}

public static void toJSON(ProvenanceListener.Node node) {

}

public static void toRDF(ProvenanceListener.Node node) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package fr.insee.vtl.provenance.model;

public class Activity {

private String name;
//TODO: to handle later
//private List<VTLDataset> used;
private String label;

public Activity(String name, String label) {
this.name = name;
this.label = label;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLabel() {
return label;
}

public void setLabel(String label) {
this.label = label;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package fr.insee.vtl.provenance.model;

import java.util.List;

public class VTLDataset {

private String name;
private Activity wasGeneratedBy;
private List<VTLDataset> wasDerivedFrom;

public VTLDataset(String name) {
this.name = name;
}

public VTLDataset(String name, Activity wasGeneratedBy) {
this.name = name;
this.wasGeneratedBy = wasGeneratedBy;
}

public VTLDataset(String name, Activity wasGeneratedBy, List<VTLDataset> wasDerivedFrom) {
this.name = name;
this.wasGeneratedBy = wasGeneratedBy;
this.wasDerivedFrom = wasDerivedFrom;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Activity getWasGeneratedBy() {
return wasGeneratedBy;
}

public void setWasGeneratedBy(Activity wasGeneratedBy) {
this.wasGeneratedBy = wasGeneratedBy;
}

public List<VTLDataset> getWasDerivedFrom() {
return wasDerivedFrom;
}

public void setWasDerivedFrom(List<VTLDataset> wasDerivedFrom) {
this.wasDerivedFrom = wasDerivedFrom;
}


}
Original file line number Diff line number Diff line change
@@ -1,40 +1,14 @@
package fr.insee.vtl.provenance;

import fr.insee.vtl.parser.VtlLexer;
import fr.insee.vtl.parser.VtlParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CodePointCharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

import java.util.Collection;
import java.util.List;


public class ProvenanceListenerTest {

public static void printTree(ProvenanceListener.Node node, String prefix, boolean isLast) {
if (node == null) {
return;
}

// Print the current node with appropriate formatting
System.out.println(prefix + (isLast ? "└── " : "├── ") + node.name + "\t[ " + node.expression + " ]");

// Get the list of parent nodes
Collection<ProvenanceListener.Node> parents = node.parents.values();
int count = parents.size();
int index = 0;

// Recursively print each parent node
for (ProvenanceListener.Node parent : parents) {
printTree(parent, prefix + (isLast ? " " : "│ "), ++index == count);
}
}

@Test
void testProvenance(TestInfo testInfo) {
void testProvenance() {
String expr;

expr = "" +
Expand All @@ -57,23 +31,16 @@ void testProvenance(TestInfo testInfo) {
// entity(ds1, [ prov:type="prov:Entity", prov:label="ds1" ])
// wasDerivedFrom(ds1, ds2, ds1)

CodePointCharStream stream = CharStreams.fromString(expr, testInfo.getTestMethod().get().getName());
VtlLexer lexer = new VtlLexer(stream);
VtlParser parser = new VtlParser(new CommonTokenStream(lexer));

ProvenanceListener provenanceListener = new ProvenanceListener();
ParseTreeWalker.DEFAULT.walk(provenanceListener, parser.start());

// Edge [ { a } { b } ....]
// Vertices [ [a, b] [a, c]



// Node { a, parent: [ Node { b :

System.out.println(provenanceListener.variables.keySet());

printTree(provenanceListener.variables.get("ds8"), "", true);
ProvenanceListener provListener = ProvenanceUtils.getProvenance(expr);
System.out.println(provListener.variables.keySet());
ProvenanceUtils.toBusinessModel(provListener)
ProvenanceUtils.printTree(provListener.variables.get("ds8"), "", true);

}
}

0 comments on commit 8b6ea61

Please sign in to comment.