-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
4 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
qendpoint-store/src/main/java/com/the_qa_company/qendpoint/functions/Log.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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.the_qa_company.qendpoint.functions; | ||
|
||
import com.the_qa_company.qendpoint.store.EndpointStore; | ||
import org.eclipse.rdf4j.model.Literal; | ||
import org.eclipse.rdf4j.model.Value; | ||
import org.eclipse.rdf4j.model.ValueFactory; | ||
import org.eclipse.rdf4j.model.base.CoreDatatype; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.ValueExprEvaluationException; | ||
import org.eclipse.rdf4j.query.algebra.evaluation.function.Function; | ||
|
||
public class Log implements Function { | ||
@Override | ||
public String getURI() { | ||
return EndpointStore.BASE_URI + "log"; | ||
} | ||
|
||
@Override | ||
public Value evaluate(ValueFactory valueFactory, Value... args) throws ValueExprEvaluationException { | ||
if (args.length < 1 || args.length > 2) { | ||
throw new ValueExprEvaluationException(getURI() + "(value, base = 10)"); | ||
} | ||
|
||
Value valueVal = args[0]; | ||
|
||
if (!valueVal.isLiteral() || !(valueVal instanceof Literal lit)) { | ||
throw new ValueExprEvaluationException(getURI() + " : value should be a literal"); | ||
} | ||
CoreDatatype cdt = lit.getCoreDatatype(); | ||
|
||
if (!cdt.isXSDDatatype() || !cdt.asXSDDatatype().orElseThrow().isNumericDatatype()) { | ||
throw new ValueExprEvaluationException(getURI() + " : value should be a numeric literal"); | ||
} | ||
|
||
double val = lit.doubleValue(); | ||
|
||
if (val <= 0) { | ||
throw new ValueExprEvaluationException(getURI() + " : value should be a positive number"); | ||
} | ||
|
||
int base; | ||
|
||
if (args.length >= 2) { | ||
Value baseVal = args[1]; | ||
|
||
if (!baseVal.isLiteral() || !(baseVal instanceof Literal litb)) { | ||
throw new ValueExprEvaluationException(getURI() + " : base should be a literal"); | ||
} | ||
CoreDatatype bcdt = litb.getCoreDatatype(); | ||
|
||
if (!bcdt.isXSDDatatype() || !bcdt.asXSDDatatype().orElseThrow().isIntegerDatatype()) { | ||
throw new ValueExprEvaluationException(getURI() + " : base should be an int literal"); | ||
} | ||
|
||
base = litb.intValue(); | ||
} else { | ||
base = 10; | ||
} | ||
if (base == 10) { | ||
return valueFactory.createLiteral(Math.log10(val)); | ||
} | ||
return valueFactory.createLiteral(Math.log(val) / Math.log(base)); | ||
} | ||
} |
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
3 changes: 2 additions & 1 deletion
3
.../resources/META-INF/services/org.eclipse.rdf4j.query.algebra.evaluation.function.Function
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 +1,2 @@ | ||
com.the_qa_company.qendpoint.functions.ParseDateFunction | ||
com.the_qa_company.qendpoint.functions.ParseDateFunction | ||
com.the_qa_company.qendpoint.functions.Log |
49 changes: 49 additions & 0 deletions
49
qendpoint-store/src/test/java/com/the_qa_company/qendpoint/functions/LogTest.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.the_qa_company.qendpoint.functions; | ||
|
||
import com.the_qa_company.qendpoint.store.EndpointStore; | ||
import org.eclipse.rdf4j.model.Literal; | ||
import org.eclipse.rdf4j.query.TupleQueryResult; | ||
import org.eclipse.rdf4j.repository.sail.SailRepository; | ||
import org.eclipse.rdf4j.repository.util.Repositories; | ||
import org.eclipse.rdf4j.sail.memory.MemoryStore; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class LogTest { | ||
@Test | ||
public void funcTest() { | ||
MemoryStore ms = new MemoryStore(); | ||
SailRepository repo = new SailRepository(ms); | ||
repo.init(); | ||
try { | ||
Repositories.consumeNoTransaction(repo, conn -> { | ||
try (TupleQueryResult res = conn.prepareTupleQuery(""" | ||
PREFIX qep: <%s> | ||
SELECT * { | ||
VALUES ?number { 10 80 420 10000 20000 1000000 } | ||
BIND (qep:log(?number) AS ?logNumberDef) | ||
BIND (qep:log(?number, 10) AS ?logNumber10) | ||
BIND (qep:log(?number, 16) AS ?logNumber16) | ||
BIND (qep:log(?number, 2) AS ?logNumber2) | ||
} | ||
""".formatted(EndpointStore.BASE_URI)).evaluate()) { | ||
res.forEach(bind -> { | ||
double value = ((Literal) bind.getValue("number")).doubleValue(); | ||
|
||
assertEquals(((Literal) bind.getValue("logNumberDef")).doubleValue(), Math.log10(value), 0.001); | ||
assertEquals(((Literal) bind.getValue("logNumber10")).doubleValue(), Math.log10(value), 0.001); | ||
assertEquals(((Literal) bind.getValue("logNumber16")).doubleValue(), | ||
Math.log10(value) / Math.log10(16), 0.001); | ||
assertEquals(((Literal) bind.getValue("logNumber2")).doubleValue(), | ||
Math.log10(value) / Math.log10(2), 0.001); | ||
}); | ||
} | ||
}); | ||
} finally { | ||
repo.shutDown(); | ||
} | ||
} | ||
|
||
} |