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

pushed operators predicates ('<'/2, '>='/2) predicates

parent 44705270
......@@ -2,31 +2,26 @@ 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.
* 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 ProloGraalGTBuiltin extends ProloGraalBuiltinClause {
public final class ProloGraalGEBuiltin 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) {
public ProloGraalGEBuiltin(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(), "'>'"));
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");
......@@ -39,12 +34,12 @@ public final class ProloGraalGTBuiltin extends ProloGraalBuiltinClause {
}
/**
* Execute the '>'/2 predicate, returning success if left part is greater than right part
* Execute the '>='/2 predicate, returning success if left part is greater or equal right part
*/
@Override
public ProloGraalBoolean execute() {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() > arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
if(arg.asNumber().asDouble() >= arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
......@@ -52,7 +47,7 @@ public final class ProloGraalGTBuiltin extends ProloGraalBuiltinClause {
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalGTBuiltin(context);
return new ProloGraalGEBuiltin(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.
* 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 ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
public final class ProloGraalGTBuiltin 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) {
public ProloGraalGTBuiltin(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"));
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(), "_");
arg = new ProloGraalVariable(getVariables(), "A");
head.addSubterm(arg);
arg2 = new ProloGraalVariable(getVariables(), "op");
arg2 = new ProloGraalVariable(getVariables(), "B");
head.addSubterm(arg2);
setHead(head);
}
/**
* Execute the is/2 predicate, unifying the left variable with the result
* Execute the '>'/2 predicate, returning success if left part is greater than right part
*/
@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;
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() > arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
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);
return new ProloGraalGTBuiltin(context);
}
}
......@@ -3,25 +3,25 @@ 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.
* Class representing the '=<'(A,B) predicate.
* return success if A is less or equal B, else return a failure.
* @author Licata Tony
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalLTBuiltin extends ProloGraalBuiltinClause {
public final class ProloGraalLEBuiltin 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) {
public ProloGraalLEBuiltin(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(), "'<'"));
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");
......@@ -34,12 +34,12 @@ public final class ProloGraalLTBuiltin extends ProloGraalBuiltinClause {
}
/**
* Execute the '<'/2 predicate, returning success if left part is less than right part
* Execute the '=<'/2 predicate, returning success if left part is less or equal right part
*/
@Override
public ProloGraalBoolean execute() {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() < arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
if(arg.asNumber().asDouble() <= arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
......@@ -47,7 +47,7 @@ public final class ProloGraalLTBuiltin extends ProloGraalBuiltinClause {
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalLTBuiltin(context);
return new ProloGraalLEBuiltin(context);
}
}
......@@ -2,31 +2,26 @@ 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.
* 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 ProloGraalGTBuiltin extends 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 ProloGraalGTBuiltin(ProloGraalContext context) {
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(), "'>'"));
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");
......@@ -39,12 +34,12 @@ public final class ProloGraalGTBuiltin extends ProloGraalBuiltinClause {
}
/**
* Execute the '>'/2 predicate, returning success if left part is greater than right part
* 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());
if(arg.asNumber().asDouble() < arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
......@@ -52,7 +47,7 @@ public final class ProloGraalGTBuiltin extends ProloGraalBuiltinClause {
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
return new ProloGraalGTBuiltin(context);
return new ProloGraalLTBuiltin(context);
}
}
......@@ -59,6 +59,18 @@ public final class ProloGraalRuntime {
ProloGraalClause consultBuiltin = new ProloGraalConsultBuiltin(context);
clauses.put(consultBuiltin.getHead(), Collections.singletonList(consultBuiltin));
ProloGraalClause gtBuiltin = new ProloGraalGTBuiltin(context);
clauses.put(gtBuiltin.getHead(), Collections.singletonList(gtBuiltin));
ProloGraalClause geBuiltin = new ProloGraalGEBuiltin(context);
clauses.put(geBuiltin.getHead(), Collections.singletonList(geBuiltin));
ProloGraalClause ltBuiltin = new ProloGraalLTBuiltin(context);
clauses.put(ltBuiltin.getHead(), Collections.singletonList(ltBuiltin));
ProloGraalClause leBuiltin = new ProloGraalLEBuiltin(context);
clauses.put(leBuiltin.getHead(), Collections.singletonList(leBuiltin));
}
public final Map<ProloGraalTerm<?>, List<ProloGraalClause>> getClauses() {
......
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