Access Control is now enabled for Gitlab Pages so you can now restrict Pages to authorized users only. If needed, make your website publicly available : Navigate to your project's Settings > General > Visibility and select Everyone in pages section.

Commit 9fb1e1fe authored by Tony Licata's avatar Tony Licata

is/2 can now compute operators with number of paramaters != 2, added lot of...

is/2 can now compute operators with number of paramaters != 2, added lot of operators life sign/1, sin/1, log/1 or abs/1. Added '='/2 predicate, trying to unify both left and right part.
parent 0ac3436a
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.runtime.*;
/**
* Class representing the '>='(A,B) predicate.
* return success if A is bigger or equal B, else return a failure.
* @author Licata Tony
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalEqualBuiltin extends ProloGraalBuiltinClause {
private final ProloGraalContext context; // we keep the context for the copy method
private ProloGraalVariable arg; // the variable A in is(A,B). We keep it to use it in the execute method
private ProloGraalVariable arg2; // the variable B in is(A,B). We keep it to use it in the execute method
public ProloGraalEqualBuiltin(ProloGraalContext context) {
super();
this.context = context;
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = new ProloGraalStructure(getVariables());
head.setFunctor(new ProloGraalAtom(getVariables(), "'='"));
// we create and store the variables to access them more easily later in the execute method
arg = new ProloGraalVariable(getVariables(), "A");
head.addSubterm(arg);
arg2 = new ProloGraalVariable(getVariables(), "B");
head.addSubterm(arg2);
setHead(head);
}
/**
* Execute the '='/2 predicate, returning success if a part can be unified with the other one
*/
@Override
public ProloGraalBoolean execute() {
return arg.unify(arg2)||arg2.unify(arg)?new ProloGraalSuccess(getVariables()):new ProloGraalFailure();
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalEqualBuiltin(context);
}
}
......@@ -27,20 +27,7 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
// we fill the operations map to check the operator used by the user later in the execute method
operations = new HashMap<>();
operations.put(new ProloGraalAtom(getVariables(),"'pi'"),this::pi);
operations.put(new ProloGraalAtom(getVariables(),"'e'"),this::e);
operations.put(new ProloGraalAtom(getVariables(),"'tau'"),this::tau);
operations.put(new ProloGraalAtom(getVariables(),"'+'"),this::add);
operations.put(new ProloGraalAtom(getVariables(),"'-'"),this::substract);
operations.put(new ProloGraalAtom(getVariables(),"'*'"),this::multiply);
operations.put(new ProloGraalAtom(getVariables(),"'/'"),this::divide);
operations.put(new ProloGraalAtom(getVariables(),"'**'"),this::pow);
operations.put(new ProloGraalAtom(getVariables(),"'^'"), this::pow);
operations.put(new ProloGraalAtom(getVariables(),"'mod'"),this::mod);
operations.put(new ProloGraalAtom(getVariables(),"'sign'"),this::sign);
operations.put(new ProloGraalAtom(getVariables(),"'sin'"),this::sin);
operations.put(new ProloGraalAtom(getVariables(),"'cos'"),this::cos);
operations.put(new ProloGraalAtom(getVariables(),"'abs'"),this::abs);
fillOperations(operations);
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
......@@ -121,6 +108,8 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
return new ProloGraalIsBuiltin(context);
}
/* is/2 operators */
private ProloGraalNumber pi(List<ProloGraalNumber> args){
if(!args.isEmpty())return null;
return new ProloGraalDoubleNumber(getVariables(),Math.PI);
......@@ -161,6 +150,16 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
return new ProloGraalDoubleNumber(getVariables(),Math.pow(args.get(0).asDouble(),args.get(1).asDouble()));
}
private ProloGraalNumber sqrt(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.sqrt(args.get(0).asDouble()));
}
private ProloGraalNumber rem(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),args.get(0).asDouble()%args.get(1).asDouble());
}
private ProloGraalNumber mod(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
double ad = args.get(0).asDouble();
......@@ -178,6 +177,16 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
return new ProloGraalDoubleNumber(getVariables(),res);
}
private ProloGraalNumber exp(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.exp(args.get(0).asDouble()));
}
private ProloGraalNumber log(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.log(args.get(0).asDouble()));
}
private ProloGraalNumber sin(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.sin(args.get(0).asDouble()));
......@@ -188,9 +197,66 @@ public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
return new ProloGraalDoubleNumber(getVariables(),Math.cos(args.get(0).asDouble()));
}
private ProloGraalNumber tan(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.tan(args.get(0).asDouble()));
}
private ProloGraalNumber asin(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.asin(args.get(0).asDouble()));
}
private ProloGraalNumber acos(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.acos(args.get(0).asDouble()));
}
private ProloGraalNumber atan(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.atan(args.get(0).asDouble()));
}
private ProloGraalNumber atan2(List<ProloGraalNumber> args){
if(args.size()!=2)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.atan2(args.get(0).asDouble(),args.get(1).asDouble()));
}
private ProloGraalNumber abs(List<ProloGraalNumber> args){
if(args.size()!=1)return null;
return new ProloGraalDoubleNumber(getVariables(),Math.abs(args.get(0).asDouble()));
}
private void fillOperations(Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> operations){
addOpperation(operations,"pi",this::pi);
addOpperation(operations,"e",this::e);
addOpperation(operations,"tau",this::tau);
addOpperation(operations,"+",this::add);
addOpperation(operations,"-",this::substract);
addOpperation(operations,"*",this::multiply);
addOpperation(operations,"/",this::divide);
addOpperation(operations,"**",this::pow);
addOpperation(operations,"^",this::pow);
addOpperation(operations,"sqrt",this::sqrt);
addOpperation(operations,"rem",this::rem);
addOpperation(operations,"mod",this::mod);
addOpperation(operations,"sign",this::sign);
addOpperation(operations,"exp",this::exp);
addOpperation(operations,"log",this::log);
addOpperation(operations,"sin",this::sin);
addOpperation(operations,"cos",this::cos);
addOpperation(operations,"tan",this::tan);
addOpperation(operations,"asin",this::asin);
addOpperation(operations,"acos",this::acos);
addOpperation(operations,"atan",this::atan);
addOpperation(operations,"atan2",this::atan2);
addOpperation(operations,"abs",this::abs);
}
private void addOpperation(Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> operations,
String atomName,
Function<List<ProloGraalNumber>,ProloGraalNumber> function){
operations.put(new ProloGraalAtom(getVariables(),atomName), function);
operations.put(new ProloGraalAtom(getVariables(),"'"+atomName+"'"), function);
}
}
......@@ -59,6 +59,9 @@ public final class ProloGraalRuntime {
ProloGraalClause consultBuiltin = new ProloGraalConsultBuiltin(context);
clauses.put(consultBuiltin.getHead(), Collections.singletonList(consultBuiltin));
ProloGraalClause equalBuiltin = new ProloGraalEqualBuiltin(context);
clauses.put(equalBuiltin.getHead(), Collections.singletonList(equalBuiltin));
ProloGraalClause gtBuiltin = new ProloGraalGTBuiltin(context);
clauses.put(gtBuiltin.getHead(), Collections.singletonList(gtBuiltin));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment