Skip to content
This repository has been archived by the owner on Jan 28, 2025. It is now read-only.

Commit

Permalink
Refactor ZeroaryFunction and Binding classes to fix now() function
Browse files Browse the repository at this point in the history
The Binding class has been updated to store and retrieve the current time value, allowing the now() function to return the same value during processing. This improves the consistency and reliability of the now() function. Fix #168.
  • Loading branch information
remiceres committed Jan 12, 2024
1 parent bd80f41 commit 82eb9ed
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
package fr.inria.corese.sparql.triple.function.core;

import java.util.Optional;

import fr.inria.corese.kgram.api.core.ExprType;
import fr.inria.corese.kgram.api.query.Environment;
import fr.inria.corese.kgram.api.query.Producer;
import fr.inria.corese.sparql.api.Computer;
import fr.inria.corese.sparql.api.IDatatype;
import fr.inria.corese.sparql.datatype.CoreseDouble;
import fr.inria.corese.sparql.datatype.DatatypeMap;
import fr.inria.corese.sparql.exceptions.EngineException;
import fr.inria.corese.sparql.triple.function.term.Binding;
import fr.inria.corese.sparql.triple.function.term.TermEval;
import fr.inria.corese.kgram.api.core.ExprType;
import fr.inria.corese.kgram.api.query.Environment;
import fr.inria.corese.sparql.exceptions.EngineException;
import fr.inria.corese.kgram.api.query.Producer;

/**
*
* @author Olivier Corby, Wimmics INRIA I3S, 2017
*
*/
public class ZeroaryFunction extends TermEval {

public ZeroaryFunction(){}

public ZeroaryFunction(String name){

public ZeroaryFunction() {
}

public ZeroaryFunction(String name) {
super(name);
}

@Override
public IDatatype eval(Computer eval, Binding b, Environment env, Producer p) throws EngineException {
switch (oper()){
case ExprType.RANDOM: return CoreseDouble.create(Math.random());
case ExprType.NOW: return DatatypeMap.newDate();
}
switch (oper()) {
case ExprType.RANDOM:
return CoreseDouble.create(Math.random());
case ExprType.NOW:
return this.getOrSetCurrentTime(b);

}
return null;
}

/**
* Returns the current time, or sets it if it is not already set.
*
* @param binding The Binding to get or set the current time in
* @return The current time
*/
private IDatatype getOrSetCurrentTime(Binding binding) {

// Check if current time is already set in the Binding
Optional<IDatatype> savedNowTime = binding.getNowValue();

if (savedNowTime.isPresent()) {
return savedNowTime.get();
} else {
// If not set, create a new time, save it, and return it
IDatatype nowValue = DatatypeMap.newDate();
binding.setNowValue(nowValue);
return nowValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

Expand Down Expand Up @@ -112,6 +114,9 @@ public class Share {
// transformer also records its Binding and hence its Visitor
private Object transformerVisitor;

// Enables function now() to return the same value during processing
private Optional<IDatatype> savNowValue = Optional.empty();

public Object getTransformerVisitor() {
return transformerVisitor;
}
Expand All @@ -120,6 +125,14 @@ public void setTransformerVisitor(Object transformerVisitor) {
this.transformerVisitor = transformerVisitor;
}

public Optional<IDatatype> getSavNowValue() {
return savNowValue;
}

public void setNowValue(IDatatype nowValue) {
this.savNowValue = Optional.ofNullable(nowValue);
}

}

public static Binding create() {
Expand Down Expand Up @@ -662,6 +675,14 @@ void shareGlobalVariable(Binding b) {
setGlobalVariableNames(b.getGlobalVariableNames());
setGlobalVariableValues(b.getGlobalVariableValues());
}

public Optional<IDatatype> getNowValue() {
return getShare().getSavNowValue();
}

public void setNowValue(IDatatype nowValue) {
getShare().setNowValue(nowValue);
}

public HashMap<String, Variable> getGlobalVariableNames() {
return globalVariable;
Expand Down

0 comments on commit 82eb9ed

Please sign in to comment.