Projog provides a mechanism for "plugging in" or "injecting" implementations of ArithmeticOperator
at runtime. This mechanism provides an easy way to configure and extend the arithmetic operations supported by Projog.
Example usage
If your project is configured using Maven then you can add a dependency on Projog by adding the following to your project's pom.xml
:
<dependency>
<groupId>org.projog</groupId>
<artifactId>projog-core</artifactId>
<version>0.10.0</version>
</dependency>
Contents of com/example/ArithmeticOperatorExample.java
:
package org.projog.example;
import static org.projog.core.term.TermUtils.castToNumeric;
import org.projog.core.math.ArithmeticOperator;
import org.projog.core.math.Numeric;
import org.projog.core.term.IntegerNumber;
import org.projog.core.term.Term;
public class CalculatableExample implements ArithmeticOperator {
@Override
public Numeric calculate(Term[] args) {
Numeric input = castToNumeric(args[0]);
long rounded = Math.round(input.getDouble());
return new IntegerNumber(rounded);
}
}
Example of integrating com.example.ArithmeticOperatorExample
into Projog:
?- pj_add_arithmetic_operator(triple/1, 'com.example.ArithmeticOperatorExample').
yes
?- X is triple(3).
X = 9
yes
?- X is round(147).
X = 441
yes
?- X is round(-42.5).
X = 127.5
yes