Skip to content

Commit

Permalink
Update ConvertJenaCorese.java
Browse files Browse the repository at this point in the history
  • Loading branch information
AnnaBobasheva committed Nov 21, 2024
1 parent 3d0256a commit 25a3030
Showing 1 changed file with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
import fr.inria.corese.sparql.api.IDatatype;
import fr.inria.corese.sparql.datatype.DatatypeMap;

/**
* Converts data structures between Apache Jena and Corese libraries.
*
* Provides methods for converting individual nodes, contexts, and more complex
* data structures such as triples, quads, and lists of quads. Primarily used for
* translating data between the RDF-star and RDF data models. Also includes utility
* methods for extracting timestamps and rule names from nodes.
*
* This docstring was generated by AI.
*/
public class ConvertJenaCorese {

// Factories
Expand Down Expand Up @@ -140,16 +150,43 @@ public static Edge quadToEdge(Quad quad) {
return edge;
}

/**
* Converts a basic quad to an Edge object.
*
* This method takes a Quad object and converts its components into Corese nodes,
* then creates and returns an EdgeGeneric object using these nodes.
*
* @param quad The Quad object to convert
* @return The EdgeGeneric object representing the converted quad
*
* This docstring was generated by AI.
*/
static Edge basicQuadToEdge(Quad quad) {
Node subject_corese = ConvertJenaCorese.JenaNodeToCoreseNode(quad.getSubject());
Node predicate_corese = ConvertJenaCorese.JenaNodeToCoreseNode(quad.getPredicate());
Node object_corese = ConvertJenaCorese.JenaNodeToCoreseNode(quad.getObject());
Node context_corese = ConvertJenaCorese.jenaContextToCoreseContext(quad.getGraph());

return EdgeGeneric.create(context_corese, subject_corese, predicate_corese, object_corese);
}
}

// iterate edge with edge.index >= index
/**
* Converts a quad to an edge and sets the edge index with the timestamp.
*
* If the timestamp from the quad's graph is greater than or equal to the given
* timestamp, an edge object is created using the quad's subject, predicate,
* object and context. The edge's index is then set with the timestamp and
* returned. Otherwise, the method returns null.
*
* @param quad The quad to be converted
* @param oper The operation type
* @param timestamp The timestamp value
* @return An edge object created from the quad and the timestamp, or null
* if the quad's graph timestamp is less than the given timestamp
*
* This docstring was generated by AI.
*/
public static Edge quadToEdge(Quad quad, int oper, int timestamp) {
int time = timestamp(quad.getGraph());
if (time >= timestamp) {
Expand Down Expand Up @@ -193,6 +230,20 @@ public static Iterable<Edge> quadsToEdges(Iterable<Quad> jena_quad_list) {
public static final String RULE_NAME = Entailment.RULE+"_";

// insert edge with index i with graph kg:rule_i
/**
* Converts a Corese context to a Jena context.
*
* The method first checks if the edge index is negative. If it is,
* the method returns the Jena context of the graph associated with
* the input edge. Otherwise, it creates a new resource with a name
* derived from the rule name and the edge index, then returns
* the Jena context of this resource.
*
* @param edge The input edge from which to extract the context.
* @return The Jena context equivalent to the Corese context.
*
* This docstring was generated by AI.
*/
static org.apache.jena.graph.Node context(Edge edge) {
if (edge.getEdgeIndex()<0) {
return ConvertJenaCorese.coreseContextToJenaContext(edge.getGraph());
Expand All @@ -203,13 +254,35 @@ static org.apache.jena.graph.Node context(Edge edge) {
}

// iterate edge kg:rule_i set edge index(i)
/**
* Adjusts the edge index based on the rule name of a graph label.
*
* The method checks if the label of the graph associated with the edge starts
* with a specific rule name string. If it does, an integer value is extracted
* from the substring following the rule name, and set as the edge index.
*
* @param edge The edge to be adjusted
* This docstring was generated by AI.
*/
static void tune(Edge edge) {
if (edge.getGraph().getLabel().startsWith(RULE_NAME)) {
int i = Integer.valueOf(edge.getGraph().getLabel().substring(RULE_NAME.length()));
edge.setEdgeIndex(i);
}
}

/**
* Extracts the timestamp from a given node's URI.
*
* This method checks if the node's URI starts with a specific string (RULE_NAME),
* and if so, extracts the integer value from the substring starting after RULE_NAME.
* If the node's URI does not start with RULE_NAME, the method returns -1.
*
* @param node The node to extract the timestamp from
* @return The timestamp as an integer, or -1 if the node's URI does not match the expected format
*
* This docstring was generated by AI.
*/
static int timestamp(org.apache.jena.graph.Node node) {
if (node.getURI().startsWith(RULE_NAME)) {
int i = Integer.valueOf(node.getURI().substring(RULE_NAME.length()));
Expand Down

0 comments on commit 25a3030

Please sign in to comment.