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 44705270 authored by Tony Licata's avatar Tony Licata

added new specs, planning and '<'/2, '=<'/2, ... predicates

parent e1dc12a0
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.runtime.*;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* Class representing the is/2(A,B) predicate.
* Compute B if B is a mathematical expression and unify it with A if A is an unbound variable.
* @author Licata Tony
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalGTBuiltin 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 ProloGraalGTBuiltin(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 left part is greater than right part
*/
@Override
public ProloGraalBoolean execute() {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() > arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalGTBuiltin(context);
}
}
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.runtime.*;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* Class representing the is/2(A,B) predicate.
* Compute B if B is a mathematical expression and unify it with A if A is an unbound variable.
* @author Licata Tony
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
private final PrintWriter writer; // used for outputting
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
//TODO: do not use a bifunction to permet functions like sin(X).
private final Map<ProloGraalAtom, BiFunction<ProloGraalNumber, ProloGraalNumber, ProloGraalNumber>> operations;
public ProloGraalIsBuiltin(ProloGraalContext context) {
super();
this.writer = new PrintWriter(context.getOutput(), true);
this.context = context;
// 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(),"'+'"),(a,b)->new ProloGraalDoubleNumber(getVariables(),a.asDouble()+b.asDouble()));
operations.put(new ProloGraalAtom(getVariables(),"'-'"),(a,b)->new ProloGraalDoubleNumber(getVariables(),a.asDouble()-b.asDouble()));
operations.put(new ProloGraalAtom(getVariables(),"'*'"),(a,b)->new ProloGraalDoubleNumber(getVariables(),a.asDouble()*b.asDouble()));
operations.put(new ProloGraalAtom(getVariables(),"'/'"),(a,b)->new ProloGraalDoubleNumber(getVariables(),a.asDouble()/b.asDouble()));
operations.put(new ProloGraalAtom(getVariables(),"'**'"),(a,b)->new ProloGraalDoubleNumber(getVariables(),Math.pow(a.asDouble(),b.asDouble())));
operations.put(new ProloGraalAtom(getVariables(),"'mod'"),(a,b)->{double ad = a.asDouble();double bd = b.asDouble();double res = ((ad % bd + bd) % bd);return new ProloGraalDoubleNumber(getVariables(),res);});
operations.put(new ProloGraalAtom(getVariables(),"'<'"),(a,b)->{double res = a.asDouble()<b.asDouble()?1:0;return new ProloGraalDoubleNumber(getVariables(),res);});
operations.put(new ProloGraalAtom(getVariables(),"'>'"),(a,b)->{double res = a.asDouble()>b.asDouble()?1:0;return new ProloGraalDoubleNumber(getVariables(),res);});
// 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(), "is"));
// we create and store the variables to access them more easily later in the execute method
arg = new ProloGraalVariable(getVariables(), "_");
head.addSubterm(arg);
arg2 = new ProloGraalVariable(getVariables(), "op");
head.addSubterm(arg2);
setHead(head);
}
/**
* Execute the is/2 predicate, unifying the left variable with the result
*/
@Override
public ProloGraalBoolean execute() {
ProloGraalTerm leftTerm = arg.getRootValue();
ProloGraalTerm rightTerm = arg2.getRootValue();
if(leftTerm instanceof ProloGraalVariable || leftTerm instanceof ProloGraalNumber){
ProloGraalNumber result = consultTerm(rightTerm);
if(result==null){
writer.print("op nok");
writer.flush();
return new ProloGraalFailure();
}
if(leftTerm.unify(result)){
return new ProloGraalSuccess(getVariables());
} else {
return new ProloGraalFailure();
}
}else{
writer.print("left term nok");
writer.flush();
return new ProloGraalFailure();
}
}
private ProloGraalNumber consultTerm(ProloGraalTerm term){
ProloGraalNumber number = null;
if(term.isNumber()){
number = term.asNumber();
}else if(term instanceof ProloGraalStructure && ((ProloGraalStructure) term).getArguments().size()==2){
number = consultOperation((ProloGraalStructure)term);
}
return number;
}
private ProloGraalNumber consultOperation(ProloGraalStructure operation){
ProloGraalTerm leftTerm = operation.getArguments().get(0);
ProloGraalTerm rightTerm = operation.getArguments().get(1);
ProloGraalNumber leftNumber = consultTerm(leftTerm);
ProloGraalNumber rightNumber = consultTerm(rightTerm);
if(leftNumber == null || rightNumber == null) return null;
return computeOperation(operation.getFunctor(), leftNumber, rightNumber);
}
private ProloGraalNumber computeOperation(ProloGraalAtom functor,
ProloGraalNumber leftNumber, ProloGraalNumber rightNumber){
if(operations.containsKey(functor)){
return operations.get(functor).apply(leftNumber,rightNumber);
}
return null;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalIsBuiltin(context);
}
}
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.runtime.*;
/**
* Class representing the '<'(A,B) predicate.
* return success if A is less than b, else return a failure.
* @author Licata Tony
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalLTBuiltin 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 ProloGraalLTBuiltin(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 left part is less than right part
*/
@Override
public ProloGraalBoolean execute() {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() < arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalLTBuiltin(context);
}
}
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.runtime.*;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
/**
* Class representing the '>'(A,B) predicate.
* return success if A is bigger than b, else return a failure.
* @author Licata Tony
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalGTBuiltin 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 ProloGraalGTBuiltin(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 left part is greater than right part
*/
@Override
public ProloGraalBoolean execute() {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() > arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalGTBuiltin(context);
}
}
This diff was suppressed by a .gitattributes entry.
This diff was suppressed by a .gitattributes entry.
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