Skip to content

Commit

Permalink
[#219] Refactor logic and documentation based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Aug 20, 2024
1 parent 8288077 commit 37cd4ee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

/**
* The `AbstractModule` class serves as a foundational abstract class for defining
* execution modules in a processing pipeline. Each module is responsible for
* executing specific tasks within a given execution context, managing input and
* modules executed in a processing pipeline. Each module is responsible for
* executing specific tasks within a given execution context, validating input and
* output constraints, and handling module-specific configurations.
*
* <p>Subclasses are required to implement the {@link #executeSelf()} method, which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.topbraid.spin.model.SPINFactory;
import org.topbraid.spin.util.SPINExpressions;

import java.io.IOException;
import java.util.Optional;


Expand All @@ -20,13 +21,25 @@
*
* @param <T> The type of the value that this handler converts RDF nodes to.
*/
abstract public class BaseRDFNodeHandler<T> extends Handler<T> {
abstract public class BaseRDFNodeHandler<T> extends Handler<T> {

public BaseRDFNodeHandler(Resource resource, ExecutionContext executionContext, Setter<? super T> setter) {
super(resource, executionContext, setter);
}

protected RDFNode getEffectiveValue(Property property){
/**
* Retrieves the effective RDF node value for a given property from the current resource.
*
* This method first attempts to retrieve the RDF node associated with the specified property from
* the current resource. If the retrieved node is an RDF expression, it evaluates the expression
* using the current execution context and variable bindings. If the node is not an expression, it
* returns the node directly.
*
* @param property The RDF property whose value is to be retrieved. Must not be {@code null}.
* @return The RDF node representing the effective value of the specified property. This could
* be the direct node value or the result of evaluating an RDF expression.
*/
public RDFNode getEffectiveValue(Property property) {
RDFNode valueNode = Optional.of(resource)
.map(r -> r.getProperty(property))
.map(Statement::getObject)
Expand All @@ -50,36 +63,45 @@ protected RDFNode getEffectiveValue(Property property){
abstract T getRDFNodeValue(RDFNode node) throws Exception;

/**
* Sets a value to the field associated with this handler by converting an RDF node
* retrieved from the specified RDF property.
* Checks if the given property is assigned a value in the current resource.
*
* This method verifies whether the current resource has an RDF property assignment for the specified
* property. It returns {@code true} if the resource has a value for the property, and {@code false}
* otherwise. This is useful for determining if there is an existing value before attempting to
* perform operations that depend on the presence of this property.
*
* @param property The RDF property whose value is used to retrieve the RDF node.
* @throws ScriptRuntimeErrorException If an error occurs during the conversion or setting process.
* @param property The RDF property to check for an assignment. Must not be {@code null}.
* @return {@code true} if the resource has an assignment for the given property, {@code false} otherwise
*/
boolean hasParameterValueAssignment(Property property) {
return Optional.of(resource)
.map(r -> r.getProperty(property))
.isEmpty();
}

@Override
public void setValueByProperty(Property property) {
RDFNode node = getEffectiveValue(property);

if (node != null) {
if (hasParameterValueAssignment(property)) {
try {
setter.addValue(getRDFNodeValue(node));
} catch (Exception ex) {
throw new ScriptRuntimeErrorException(
String.format("""
Failed to set value of the field `%s` of type `%s` within class `%s`.
The value was suppose to be converted from RDF node `%s` which was computed
from RDF statement <?s, ?p, ?o> where:
- s = `%s`,
- p = `%s`,
""",
setter.getField().getName(),
setter.getField().getType(),
setter.getField().getDeclaringClass().getName(),
node,
resource,
property
),
ex
String.format("""
Failed to set value of the field `%s` of type `%s` within class `%s`.
The value was suppose to be converted from RDF node `%s` which was computed
from RDF statement <?s, ?p, ?o> where:
- s = `%s`,
- p = `%s`,
""",
setter.getField().getName(),
setter.getField().getType(),
setter.getField().getDeclaringClass().getName(),
node,
resource,
property
),
ex
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cz.cvut.spipes.modules.handlers;

import cz.cvut.spipes.engine.ExecutionContext;
import cz.cvut.spipes.exception.ScriptRuntimeErrorException;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
Expand Down Expand Up @@ -34,11 +35,11 @@ public Handler(Resource resource, ExecutionContext executionContext, Setter<? su
}

/**
* Sets the value of the native Java object based on the provided RDF property.
* If the value is an RDF expression which evaluates to undefined/null,
* the setter should not be triggered or set null as well.
* Sets a value to the field associated with this handler by converting an RDF node
* retrieved from the specified RDF property.
*
* @param property the RDF property to extract the value from
* @param property The RDF property whose value is used to retrieve the RDF node.
* @throws ScriptRuntimeErrorException If an error occurs during the conversion or setting process.
*/
abstract public void setValueByProperty(Property property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ public void addValue(Object value) {
if (list == null) {
list = new java.util.ArrayList<>();
f.set(bean, list);
}else if(!list.isEmpty()){
list.clear();
}

if (value instanceof List) {
list.addAll((List<?>) value);
} else {

list.add(value);
}

Expand All @@ -34,6 +35,8 @@ public void addValue(Object value) {
if (list == null) {
list = new java.util.ArrayList<>();
f.set(bean, list);
}else if(!list.isEmpty()){
list.clear();
}

if (value instanceof List) {
Expand All @@ -47,6 +50,4 @@ public void addValue(Object value) {
}
}
}


}

0 comments on commit 37cd4ee

Please sign in to comment.