This repository has been archived by the owner on Jan 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ZeroaryFunction and Binding classes to fix now() function
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
Showing
2 changed files
with
61 additions
and
13 deletions.
There are no files selected for viewing
53 changes: 40 additions & 13 deletions
53
sparql/src/main/java/fr/inria/corese/sparql/triple/function/core/ZeroaryFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters