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

Cleaned code, added javadoc to remaining classes.

parent 6909e363
......@@ -18,7 +18,8 @@
<include>**/*TestSuite.java</include>
<include>**/*Test.java</include>
</includes>
<argLine>-XX:-UseJVMCIClassLoader</argLine>
<!-- COMMENT THE FOLLOWING LINE IF YOU USE GRAALJDK FOR JAVA 11 -->
<!-- <argLine>-XX:-UseJVMCIClassLoader</argLine> -->
</configuration>
</plugin>
<plugin>
......
......@@ -12,7 +12,6 @@ import com.oracle.truffle.api.dsl.TypeSystem;
*/
@TypeSystem({int.class, double.class, boolean.class})
public abstract class ProloGraalTypes {
@TypeCheck(ProloGraalTerm.class)
public static boolean isProloGraalTerm(Object value) {
return value instanceof ProloGraalTerm;
......
......@@ -11,7 +11,7 @@ import java.util.Map;
import java.util.function.Function;
/**
*
* Class defining and initialing is/2 operators
* @author Tony Licata
* @see ch.heiafr.prolograal.builtins.predicates.ProloGraalIsBuiltin
*/
......
......@@ -38,10 +38,20 @@ public abstract class ProloGraalBuiltinClause extends ProloGraalClause implement
super(variables);
}
/**
* Overrided method from InstrumentableNode, has to return true to make builtins Truffle executable nodes.
* More informations: https://www.graalvm.org/truffle/javadoc/com/oracle/truffle/api/instrumentation/InstrumentableNode.html
* @return true
*/
public boolean isInstrumentable() {
return true;
}
/**
* Overrided method from InstrumentableNode, necessary to handle Truffle node execution
* @param probe
* @return new Wrapper for the given ProbeNode
*/
public InstrumentableNode.WrapperNode createWrapper(ProbeNode probe) {
// ASTNodeWrapper is generated by @GenerateWrapper
return new ProloGraalBuiltinClauseWrapper(this, probe);
......@@ -50,11 +60,11 @@ public abstract class ProloGraalBuiltinClause extends ProloGraalClause implement
/**
* Overridable execute method for built-ins.
* May have side effects, like writing or opening a file.
* @param frame necessary param to make this method specializable in the Truffle way.
* @return The result of the builtin execution.
*/
public abstract ProloGraalBoolean executeBuiltin(VirtualFrame frame);
//public abstract ProloGraalObject executeGeneric(VirtualFrame frame);
/**
* We want the built-ins to override this because otherwise they will lose their "ProloGraalBultinClause" status.
* @return a copy of the built-in
......
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.parser.ProloGraalParserImpl;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import com.oracle.truffle.api.source.Source;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Class representing the consult/1 predicate.
......@@ -25,56 +17,18 @@ import java.util.stream.Collectors;
public abstract class ProloGraalConsultBuiltin extends ProloGraalBuiltinClause {
private final ProloGraalContext context; // we keep the context for the copy method
private ProloGraalVariable arg; //The variable A in consult(A). We keep it to use it in the execute method
public ProloGraalConsultBuiltin(ProloGraalContext context) {
super(new HashMap<>());
// get printer from context
this.context = context;
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
setHead(head);
}
/**
* Consult the filename present in arg variable.
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
ProloGraalTerm consultContentTerm = arg.getRootValue();
if(consultContentTerm instanceof ProloGraalAtom){
String filename = ((ProloGraalAtom) consultContentTerm).getName();
File loadedFile = new File(filename);
if(loadedFile.exists()) {
String fileContent;
try {
fileContent = Files.readAllLines(loadedFile.toPath(), Charset.forName("UTF-8")).stream().collect(Collectors.joining("\n"));
}catch (Exception e){
System.out.println("could not read file "+filename);
return new ProloGraalFailure();
}
Source source = Source.newBuilder(ProloGraalLanguage.ID, fileContent, null).build();
//we filter clauses to remove goal lists from returned clauses
List<ProloGraalClause> clauses = ProloGraalParserImpl.parseClauses(source).stream().filter(proloGraalClause -> proloGraalClause.getHead() != null).collect(Collectors.toList());
context.getRuntime().addProloGraalClauses(clauses);
return new ProloGraalSuccess(getVariables());
}else{
System.out.println("file "+filename+" does not exist");
return new ProloGraalFailure();
}
}else{
System.out.println("consult/1 predicate takes an atom as argument.");
return new ProloGraalFailure();
}
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
......
package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.ProloGraalLanguage;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.parser.ProloGraalParserImpl;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import com.oracle.truffle.api.source.Source;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Class representing the consult/1 predicate.
* Class representing the consultstring/1 predicate.
* It permit the user to load Prolog rules into the runtime.
* @author Tony Licata
* @see ProloGraalBuiltinClause
......@@ -22,42 +18,18 @@ import java.util.Map;
public abstract class ProloGraalConsultStringBuiltin extends ProloGraalBuiltinClause {
private final ProloGraalContext context; // we keep the context for the copy method
private ProloGraalVariable arg; //The variable A in consult(A). We keep it to use it in the execute method
public ProloGraalConsultStringBuiltin(ProloGraalContext context) {
super(new HashMap<>());
// get printer from context
this.context = context;
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
setHead(head);
}
/**
* Consult the clauses string present in arg variable.
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
ProloGraalTerm consultContentTerm = arg.getRootValue();
if(consultContentTerm instanceof ProloGraalAtom){
String consultContentString = ((ProloGraalAtom) consultContentTerm).getName();
Source source = Source.newBuilder(ProloGraalLanguage.ID, consultContentString, null).build();
//we filter clauses to remove goal lists from returned clauses
List<ProloGraalClause> clauses = ProloGraalParserImpl.parseClauses(source);
context.getRuntime().addProloGraalClauses(clauses);
return new ProloGraalSuccess(getVariables());
}else{
System.out.println("consultstring/1 predicate takes an atom as argument.");
return new ProloGraalFailure();
}
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
......
......@@ -3,7 +3,6 @@ package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import java.util.HashMap;
......@@ -18,35 +17,18 @@ import java.util.Map;
*/
public abstract class ProloGraalEqualBuiltin extends ProloGraalBuiltinClause {
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() {
super(new HashMap<>());
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
arg2 = head.getArguments().get(1).asVariable();
setHead(head);
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
return head;
}
/**
* Execute the '='/2 predicate, returning success if a part can be unified with the other one
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
return arg.unify(arg2)||arg2.unify(arg)?new ProloGraalSuccess(getVariables()):new ProloGraalFailure();
@Specialization
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
......
......@@ -3,7 +3,6 @@ package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import java.util.HashMap;
......@@ -18,36 +17,18 @@ import java.util.Map;
*/
public abstract class ProloGraalGEBuiltin extends ProloGraalBuiltinClause {
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 ProloGraalGEBuiltin() {
super(new HashMap<>());
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
arg2 = head.getArguments().get(1).asVariable();
setHead(head);
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
return head;
}
/**
* Execute the '>='/2 predicate, returning success if left part is greater or equal right part
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() >= arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
@Specialization
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
......
......@@ -3,7 +3,6 @@ package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import java.util.HashMap;
......@@ -18,37 +17,18 @@ import java.util.Map;
*/
public abstract class ProloGraalGTBuiltin extends ProloGraalBuiltinClause {
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() {
super(new HashMap<>());
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
arg2 = head.getArguments().get(1).asVariable();
setHead(head);
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
return head;
}
/**
* Execute the '>'/2 predicate, returning success if left part is greater than right part
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() > arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
@Specialization
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
......
......@@ -5,7 +5,6 @@ import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.nodes.ProloGraalIsHeadNode;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.nodes.NodeUtil;
......@@ -21,90 +20,26 @@ import java.util.function.Function;
@NodeInfo(shortName = "ProloGraalIsBuiltinNode")
public abstract class ProloGraalIsBuiltin extends ProloGraalBuiltinClause {
ProloGraalVariable arg; // the variable A in is(A,B). We keep it to use it in the execute method
ProloGraalVariable arg2; // the variable B in is(A,B). We keep it to use it in the execute method
// we fill the operations map to check the operator used by the user later in the execute method
private static final Map<ProloGraalAtom, Function<List<ProloGraalNumber>,ProloGraalNumber>> operations = ProloGraalIsOperators.getOperationsMap();
public ProloGraalIsBuiltin(Map<ProloGraalVariable,ProloGraalVariable> variables) {
super(variables);
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
arg2 = head.getArguments().get(1).asVariable();
setHead(head);
}
/**
* Execute the is/2 predicate, unifying the left variable with the result
*/
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
/**
* Execute the is/2 predicate, unifying the left variable with the result
* Get is/2 operators as a Java Map< atom , function >
* @return is/2 operators as a Map
*/
/*
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
return initExecute();
}*/
private ProloGraalBoolean initExecute(){
ProloGraalTerm leftTerm = arg.getRootValue();
ProloGraalTerm rightTerm = arg2.getRootValue();
if(leftTerm.isVariable() || leftTerm.isNumber()){
ProloGraalNumber result = consultTerm(rightTerm);
if(result==null){
return new ProloGraalFailure();
}
if(leftTerm.unify(result)){
return new ProloGraalSuccess(getVariables());
} else {
return new ProloGraalFailure();
}
}else{
return new ProloGraalFailure();
}
}
private ProloGraalNumber consultTerm(ProloGraalTerm term){
ProloGraalNumber number = null;
if(term.isNumber()){
number = term.asNumber();
}else if(term.isStructure()){
number = consultOperation((ProloGraalStructure)term);
}else if(term.isAtom()){
ProloGraalStructure arglessStruct = new ProloGraalStructure(getVariables());
arglessStruct.setFunctor(term.asAtom());
number = consultOperation(arglessStruct);
}
return number;
}
private ProloGraalNumber consultOperation(ProloGraalStructure operation){
List<ProloGraalNumber> results = new ArrayList<>();
for( ProloGraalTerm term : operation.getArguments() ){
ProloGraalNumber res = consultTerm(term);
if(res == null) return null;
results.add(res);
}
return computeOperation(operation.getFunctor(), results);
}
private ProloGraalNumber computeOperation(ProloGraalAtom functor,
List<ProloGraalNumber> args){
if(operations.containsKey(functor)){
return operations.get(functor).apply(args);
}
return null;
}
public static Map<ProloGraalAtom, Function<List<ProloGraalNumber>, ProloGraalNumber>> getOperations() {
return operations;
}
......
......@@ -3,7 +3,6 @@ package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import java.util.HashMap;
......@@ -18,37 +17,19 @@ import java.util.Map;
*/
public abstract class ProloGraalLEBuiltin extends ProloGraalBuiltinClause {
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 ProloGraalLEBuiltin() {
super(new HashMap<>());
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
arg2 = head.getArguments().get(1).asVariable();
setHead(head);
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
return head;
}
/**
* Execute the '=<'/2 predicate, returning success if left part is less or equal right part
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() <= arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
@Specialization
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
......
......@@ -3,7 +3,6 @@ package ch.heiafr.prolograal.builtins.predicates;
import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeUtil;
import java.util.HashMap;
......@@ -18,37 +17,18 @@ import java.util.Map;
*/
public abstract class ProloGraalLTBuiltin extends ProloGraalBuiltinClause {
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() {
super(new HashMap<>());
// create the head of this clause
// since we do not need custom unification, a simple structure is enough
ProloGraalStructure head = createStandardHead(getVariables()).asStructure();
arg = head.getArguments().get(0).asVariable();
arg2 = head.getArguments().get(1).asVariable();
setHead(head);
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
return head;
}
/**
* Execute the '<'/2 predicate, returning success if left part is less than right part
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
if(arg.isNumber() && arg2.isNumber())
if(arg.asNumber().asDouble() < arg2.asNumber().asDouble())return new ProloGraalSuccess(getVariables());
return new ProloGraalFailure();
@Specialization
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
// override the default copy so we do not lose the built-in status
@Override
public ProloGraalClause copy() {
......
......@@ -19,14 +19,12 @@ import java.util.Map;
*/
public abstract class ProloGraalNoTraceBuiltin extends ProloGraalBuiltinClause {
private final PrintWriter writer; // used for outputting
private final ProloGraalContext context; // we keep the context to use it later
public static final String TRACE_OFF_TEXT = "The debugger is switched off\n";
public ProloGraalNoTraceBuiltin(ProloGraalContext context) {
super(new HashMap<>());
// get printer from context
this.writer = new PrintWriter(context.getOutput(), true);
this.context = context;
// create the head of this clause
......@@ -39,16 +37,8 @@ public abstract class ProloGraalNoTraceBuiltin extends ProloGraalBuiltinClause {
/**
* Execute the notrace, even if the trace is already OFF (like usual Prolog interpreter).
*/
@Override
public ProloGraalBoolean executeBuiltin(VirtualFrame frame) {
context.setTraceFlag(false);
writer.print(TRACE_OFF_TEXT);
writer.flush();
return new ProloGraalSuccess(getVariables());
}
@Specialization
public ProloGraalBoolean returnHead(VirtualFrame frame, ProloGraalBoolean head){
public ProloGraalBoolean returnHead(ProloGraalBoolean head){
return head;
}
......
......@@ -4,7 +4,6 @@ import ch.heiafr.prolograal.nodes.*;
import ch.heiafr.prolograal.nodes.ProloGraalPolyglotEvalHeadNode;
import ch.heiafr.prolograal.runtime.*;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.NodeInfo;
import com.oracle.truffle.api.nodes.NodeUtil;
......@@ -20,34 +19,16 @@ import java.util.Map;
@NodeInfo(shortName = "ProloGraalPolyglotBindBuiltin")
public abstract class ProloGraalPolyglotEvalBuiltin extends ProloGraalBuiltinClause {
ProloGraalVariable arg; // the variable A in is(A,B). We keep it to use it in the execute method
ProloGraalVariable arg2; // the variable B in is(A,B). We keep it to use it in the execute method
ProloGraalVariable arg3; // the variable B in is(A,B). We keep it to use it in the execute method
public ProloGraalPolyglotEvalBuiltin(Map<ProloGraalVariable,ProloGraalVariable> variables) {
super(variables);