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

cleaned code, prepared code in ProofTreeNode to optimize it

parent e7a24adb
......@@ -2,6 +2,7 @@ package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.runtime.*;
import java.util.Date;
import java.util.Map;
/**
......@@ -10,55 +11,37 @@ import java.util.Map;
* @author Martin Spoto
* @see ProloGraalBuiltinClause
*/
public final class ProloGraalVarBuiltin extends ProloGraalBuiltinClause {
public final class ProloGraalRealTimeBuiltin extends ProloGraalBuiltinClause {
/**
* Class representing the head of this predicate.
* Necessary to override the unification behaviour.
*/
private static class VarPredicateHead extends ProloGraalStructure {
private static final long startTime = System.currentTimeMillis();
private ProloGraalVariable arg; // the variable A in real_time(A). We keep it to use it in the execute method
public VarPredicateHead(Map<ProloGraalVariable, ProloGraalVariable> variables) {
super(variables);
// add the correct functor for this predicate, and an anonymous variable since we do not need it
setFunctor(new ProloGraalAtom(variables, "var"));
addSubterm(new ProloGraalVariable(variables, "_"));
}
public ProloGraalRealTimeBuiltin() {
super();
/**
* Overrides the default structure unification behaviour.
* Checks that the param has the same functor and exactly one argument, then checks that the argument
* resolves to a variable
* @return true if other has the same functor and exactly one argument, and that argument is an unbound variable
*/
@Override
public boolean unify(ProloGraalTerm<?> other) {
if (other instanceof ProloGraalStructure) {
ProloGraalStructure struct = (ProloGraalStructure) other;
if (struct.getFunctor().equals(getFunctor()) && struct.getArity() == 1) {
return struct.getArguments().get(0).getRootValue() instanceof ProloGraalVariable;
}
}
return false;
}
// 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(), "real_time"));
// override the default copy so we do not lose the custom unification behaviour
@Override
public ProloGraalStructure copy(Map<ProloGraalVariable, ProloGraalVariable> variables) {
return new VarPredicateHead(variables);
}
}
// we create and store the variables to access them more easily later in the execute method
arg = new ProloGraalVariable(getVariables(), "A");
head.addSubterm(arg);
public ProloGraalVarBuiltin() {
super();
// creates our custom head and set it
VarPredicateHead head = new VarPredicateHead(getVariables());
setHead(head);
}
@Override
public ProloGraalBoolean execute(){
if(arg.isNumber() || arg.isBound() && arg.getRootValue().isVariable())
if(arg.unify(new ProloGraalDoubleNumber(getVariables(),System.currentTimeMillis()-startTime)))
return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
}
// override the default copy so we do not lose the custom head
@Override
public ProloGraalClause copy() {
return new ProloGraalVarBuiltin();
return new ProloGraalRealTimeBuiltin();
}
}
......@@ -6,6 +6,7 @@ import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.TruffleLanguage;
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.RootNode;
......@@ -70,14 +71,6 @@ public final class ProloGraalEvalRootNode extends RootNode {
Deque<Integer> currentBranches = new ArrayDeque<>();
ProloGraalClause goal = new ProloGraalClause();
goal.setHead(new ProloGraalAtom(request.getVariables(),"goals"));
/*
if(request.getGoals().size()==0){
goal.addGoal(request.getHead());
}else if(request.getGoals().size()>0 && request.getHead() == null){
request.getGoals().forEach(proloGraalTerm -> goal.addGoal(proloGraalTerm));
} else{
continue;
} */
request.getGoals().forEach(proloGraalTerm -> goal.addGoal(proloGraalTerm));
ProloGraalRuntime runtime;
runtime = new ProloGraalRuntime(language.getContextReference().get());
......
......@@ -31,7 +31,6 @@ public class ProloGraalInterpreterNode extends RootNode {
private final ProloGraalLanguage language;
private final ProloGraalContext context;
private final String returnStr = "return";
public ProloGraalInterpreterNode(ProloGraalLanguage language) {
super(language);
......
......@@ -22,10 +22,16 @@ import java.util.stream.IntStream;
@NodeInfo(shortName = "ProofTreeNode")
public class ProloGraalProofTreeNode extends Node {
private class LocalVariables{
public Deque<ProloGraalTerm<?>> goals;
public int start;
public List<ProloGraalClause> unifiableClauses;
}
private final Map<ProloGraalTerm<?>, List<ProloGraalClause>> clauses; // reference to the context clauses
//Stacks used to simulate a recursive call with a while loop
private final Stack<Deque<Integer>> branchesStack; // the goals of this node
private final Stack<LocalVariables> localVarsStack; // the goals of this node
private final Stack<Deque<ProloGraalTerm<?>>> goalsStack; // the goals of this node
private final Stack<Integer> startStack; // used to skip visited branches in case of redo
private final Stack<List<ProloGraalClause>> unifiableClausesStack; // store clauses that can resolve the current goal
......@@ -49,7 +55,7 @@ public class ProloGraalProofTreeNode extends Node {
Deque<ProloGraalTerm<?>> goals, boolean traceFlag) {
this.clauses = clauses;
this.traceFlag = traceFlag;
this.branchesStack = new Stack<>();
this.localVarsStack = new Stack<>();
this.goalsStack = new Stack<>();
goalsStack.push(goals);
this.startStack = new Stack<>();
......
......@@ -74,6 +74,8 @@ public final class ProloGraalRuntime {
ProloGraalClause leBuiltin = new ProloGraalLEBuiltin(context);
clauses.put(leBuiltin.getHead(), Collections.singletonList(leBuiltin));
ProloGraalClause realTimeBuiltin = new ProloGraalRealTimeBuiltin();
clauses.put(realTimeBuiltin.getHead(), Collections.singletonList(realTimeBuiltin));
}
public final Map<ProloGraalTerm<?>, List<ProloGraalClause>> getClauses() {
......
recommendedN(100000).
benchmark2(N, Time):-
benchmark(N),
benchmark(N),
benchmark(N),
benchmark(N),
real_time(A),
benchmark(N),
real_time(B),
is(C,'-'(B,A)),
'='(Time,C).
benchmark_text('time for N equal ').
benchmark_one:-
do_benchmark(100).
benchmark_two:-
do_benchmark(1000).
benchmark_three:-
do_benchmark(10000).
benchmark_four:-
do_benchmark(30000).
benchmark_five:-
do_benchmark(50000).
do_benchmark(N):-
benchmark_text(BenchText),
benchmark(N,Time),
write(BenchText), write(N), write(': '), write(Time).
benchmark(N, Time):-
real_time(A),
benchmark(N),
real_time(B),
is(C,'-'(B,A)),
'='(Time,C).
benchmark(0).
benchmark(N):-
......
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