Mentions légales du service

Skip to content
Snippets Groups Projects
Commit eadc196f authored by TORNIL Florent's avatar TORNIL Florent
Browse files

Release 1.2.3 : Improve functions and computed predicates

See merge request !2
parents c8ab93e1 64a295ee
Branches
No related tags found
1 merge request!2Cleaning and computed functions improvments
Pipeline #1026788 passed
......@@ -34,7 +34,7 @@ pages:
- mkdir -p public/
- cd dlgp-parser/
- mvn javadoc:aggregate
- cp -r target/site/apidocs/* ../public/
- cp -r target/reports/apidocs/* ../public/
artifacts:
paths:
- public
......@@ -15,7 +15,7 @@
<groupId>fr.lirmm.graphik</groupId>
<artifactId>dlgp-parser</artifactId>
<version>1.2.2</version>
<version>1.2.3</version>
<name>dlgp-parser</name>
<url>https://gitlab.inria.fr/rules/dlgp-parser</url>
......@@ -83,6 +83,27 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>javacc-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>process-javacc</id>
<phase>generate-sources</phase>
<goals>
<goal>javacc</goal>
</goals>
<configuration>
<sourceDirectory>src/main/java</sourceDirectory>
<includes>
<include>fr/lirmm/graphik/dlgp3/parser/DLGP2Parser.jj</include>
</includes>
<outputDirectory>target/generated-sources/javacc</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
package fr.lirmm.graphik.dlgp3.parser;
import javax.swing.event.EventListenerList;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
......@@ -22,7 +23,7 @@ public abstract class ADlgpItemFactory{
public static final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
public static String DEFAULT_BASE = "http://www.lirmm.fr/dlgp/";
public static String STANDARD_FUNCTION_PREFIX = "integraal/stdfct#";
public static String STANDARD_FUNCTION_PREFIX = "<stdfct>";
public static InvokeManager invokeManager = new InvokeManager();
......@@ -440,9 +441,16 @@ public abstract class ADlgpItemFactory{
}
protected Object getURI(String uri)
protected Object getURI(String uri) throws ParseException
{
invokeManager.addPrefix(getIRI(uri)+"#", uri);
if (!uri.equals(ADlgpItemFactory.STANDARD_FUNCTION_PREFIX)) {
try {
invokeManager.addPrefix(getIRI(uri) + "#", uri);
} catch (IOException | org.json.simple.parser.ParseException | ClassNotFoundException e) {
throw new ParseException(e.getMessage());
}
}
return uri;
}
......
package fr.lirmm.graphik.dlgp3.parser;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.net.URL;
import java.net.URLClassLoader;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ConfigFileReader {
public ConfigFileReader() { }
/*given a json Object this method return an instance of the class
* using informations in the json Objet*/
public Class<?> getClassFromJsonObject(JSONObject jsonObject){
Class<?> result = null;
try {
String className = this.getPackage(jsonObject) + "." + this.getClasse(jsonObject);
if(!jsonObject.containsKey("path"))
result= Class.forName(className);
else {
String path = this.getPath(jsonObject);
File file = new File(path);
InvokeManager.classToPath.put(className, path);
URL url = file.toURI().toURL();
URL[] urls = new URL[] {url};
URLClassLoader loader = new URLClassLoader(urls);
result = loader.loadClass(className);
loader.close();
/**
* Given a JSON Object, this method returns an instance of the class
* using information in the JSON Object.
*
* @param jsonObject the JSON object containing class information
* @return the class object represented by the information in the JSON object
* @throws ClassNotFoundException if the given class is not found
* @throws IOException if the given class file cannot be accessed
*/
public Class<?> getClassFromJsonObject(JSONObject jsonObject) throws ClassNotFoundException, IOException {
Class<?> result;
String className = this.getPackage(jsonObject) + "." + this.getClass(jsonObject);
if (!jsonObject.containsKey("path")) {
// Load the class directly from the classpath
result = Class.forName(className);
} else {
String path = this.getPath(jsonObject);
File directory = new File(path);
if (!directory.exists() || !directory.isDirectory()) {
throw new IOException("Invalid directory path: " + path);
}
} catch (Exception e) {
e.printStackTrace();
// Create a URLClassLoader to load the class from the given directory
URL url = directory.toURI().toURL();
URL[] urls = new URL[]{url};
try (URLClassLoader loader = new URLClassLoader(urls)) {
result = loader.loadClass(className);
InvokeManager.classToPath.put(className, path);
} catch (ClassNotFoundException e) {
// Try to compile and load the .java file if the .class file is not found
Path javaFilePath = Paths.get(path, className.replace('.', File.separatorChar) + ".java");
if (Files.exists(javaFilePath)) {
// Create a temporary directory for compiled .class files
File tempDir = createTempDirectory();
compileJavaFile(javaFilePath, tempDir.getAbsolutePath());
// Add the temporary directory to the class loader
URL tempUrl = tempDir.toURI().toURL();
URL[] tempUrls = new URL[]{tempUrl};
// Reload the class loader to include the newly compiled class
try (URLClassLoader tempLoader = new URLClassLoader(tempUrls)) {
result = tempLoader.loadClass(className);
InvokeManager.classToPath.put(className, tempDir.toString());
}
} else {
throw new ClassNotFoundException("Neither .class nor .java file found for " + className);
}
}
}
return result;
return result;
}
/**
* Compiles a Java file.
*
* @param javaFilePath the path to the .java file
* @param outputDir the directory where the .class files should be placed
* @throws IOException if the file cannot be compiled
*/
private void compileJavaFile(Path javaFilePath, String outputDir) throws IOException {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null, "-d", outputDir, javaFilePath.toString());
if (result != 0) {
throw new IOException("Failed to compile .java file: " + javaFilePath);
}
}
/**
* Creates a temporary directory.
*
* @return the temporary directory
* @throws IOException if the directory cannot be created
*/
private File createTempDirectory() throws IOException {
File tempDir = new File(System.getProperty("java.io.tmpdir"), "compiledClasses_" + System.nanoTime());
if (!tempDir.mkdir()) {
throw new IOException("Could not create temp directory: " + tempDir.getAbsolutePath());
}
return tempDir;
}
public JSONObject readJsonFile(String URI) {
JSONObject jsonObj = null;
try {
Reader reader = new FileReader(URI);
/**
* Reads a JSON file and returns a JSONObject.
*
* @param URI the URI of the JSON file
* @return the parsed JSONObject
* @throws IOException if the file cannot be read
* @throws ParseException if the file content cannot be parsed
*/
public JSONObject readJsonFile(String URI) throws IOException, ParseException {
JSONObject jsonObj;
try (Reader reader = new FileReader(URI)) {
JSONParser parser = new JSONParser();
jsonObj = (JSONObject) parser.parse(reader);
} catch(Exception e) {
//e.printStackTrace();
}
return jsonObj;
}
public JSONObject getDefault(JSONObject obj) {
if(obj.containsKey("default"))
return (JSONObject)obj.get("default");
......@@ -111,9 +176,7 @@ public class ConfigFileReader {
return (String) obj.get("package");
}
public String getClasse(JSONObject obj) {
public String getClass(JSONObject obj) {
return (String) obj.get("class");
}
}
package fr.lirmm.graphik.dlgp3.parser;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
/**
* This class manages the invocation of functions and types based on prefixes and configurations.
*/
public class InvokeManager {
private ConfigFileReader confReader = new ConfigFileReader();
public static HashMap<String,String> prefixUriMap=new HashMap<String,String>();
public static HashMap<Object,String> inversedTypesTable=new HashMap<Object,String>();
public static HashMap<String,Object> typesTable=new HashMap<String,Object>();
public static HashMap<String,Object> prefixTable=new HashMap<String,Object>();
public static HashMap<String, String> classToPath = new HashMap<String, String>();
/*default constructor which intilalizes the prefix table
* with special predicates & functions and the typesTable with standards type*/
private final ConfigFileReader confReader = new ConfigFileReader();
public static Map<String, String> prefixUriMap = new HashMap<>();
public static Map<Object, String> inverseTypesTable = new HashMap<>();
public static Map<String, Object> typesTable = new HashMap<>();
public static Map<String, Object> prefixTable = new HashMap<>();
public static Map<String, String> classToPath = new HashMap<>();
/**
* Default constructor initializes the prefix table with special predicates and functions
* and the typesTable with standard types.
*/
public InvokeManager() {
reverseMap();
}
/*this metod set the typesTable given the standardfilePath*/
/*public void setTypesTable(String filePath){
try {
//substring to remove angle brackets <typesFilePath> --> typesFilePath
if(filePath.startsWith("<"))
filePath=filePath.substring(1,filePath.length()-1);
JSONObject jsonObject=this.confReader.readJesonFile(filePath);
for(Object key :jsonObject.keySet())
typesTable.put((String)key,Class.forName((String)jsonObject.get(key)));
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* Adds a prefix and the object that allows us to call functions in the prefixTable,
* and adds a (key, value) pair in prefixUriMap.
*
* @param prefix the prefix to add
* @param filePath the file path associated with the prefix
*/
/*this method adds a prefix and the object that allows as to call functions, in the prefixTable
* and add a (key,value) <prefix,uri> in prefixUriMap
* and add a usersTypes if there exists*/
public void addPrefix(String prefix,String filePath) {
//substring to remove angle brackets <prefixFilePath> --> prefixFilePath
if(filePath.startsWith("<"))
filePath=filePath.substring(1,filePath.length()-1);
this.getObjectToInvoke(prefix,filePath);
prefixUriMap.put(prefix, filePath+"#");
public void addPrefix(String prefix, String filePath)
throws IOException, ParseException, ClassNotFoundException {
if (filePath.startsWith("<")) {
filePath = filePath.substring(1, filePath.length() - 1);
}
getObjectToInvoke(prefix, filePath);
prefixUriMap.put(prefix, filePath + "#");
}
/*return the object that allows to invok given a prefix*/
/**
* Returns the object that allows to invoke given a prefix.
*
* @param key the key to search in the prefix table
* @return the object associated with the key
*/
public Object getInvokerObject(String key) {
if(prefixTable.containsKey(key))
if (prefixTable.containsKey(key)) {
return prefixTable.get(key);
else {
int indexOf=key.indexOf("#");
String prefix=key.substring(0,indexOf+1);
} else {
int indexOf = key.indexOf("#");
String prefix = key.substring(0, indexOf + 1);
return prefixTable.get(prefix);
}
}
/**
* Returns the full URI associated with a prefix.
*
* @param prefix the prefix to search
* @return the full URI associated with the prefix
*/
public String getFull(String prefix) {
return prefixUriMap.get(prefix);
return prefixUriMap.get(prefix);
}
/*return the java type given a graal type*/
/**
* Returns the Java type given a Graal type.
*
* @param key the Graal type key to search
* @return the Java type associated with the key
*/
public Object getType(String key) {
if(typesTable.containsKey(key))
if (typesTable.containsKey(key)) {
return typesTable.get(key);
else {
int indexOf=key.indexOf("#");
String prefix=key.substring(0,indexOf);
} else {
int indexOf = key.indexOf("#");
String prefix = key.substring(0, indexOf);
return typesTable.get(prefix);
}
}
/*return the graal type given a java type*/
public String getGraalType(Object key) {
return inversedTypesTable.get(key);
}
/*given a path for a file config this method return an instance
* of the classe where are functions*/
/**
* Returns the Graal type given a Java type.
*
* @param key the Java type key to search
* @return the Graal type associated with the key
*/
public String getGraalType(Object key) {
return inverseTypesTable.get(key);
}
public void getObjectToInvoke(String prefix,String prefixFilePath){
JSONObject jsonObject=this.confReader.readJsonFile(prefixFilePath);
if(jsonObject == null) {
// standard functions
} else {
JSONObject defaultJsonObject=this.confReader.getDefault(jsonObject);
JSONObject elementsJsonObject=this.confReader.getElements(jsonObject);
if(!elementsJsonObject.isEmpty()) {
for(Object key :elementsJsonObject.keySet()) {
JSONObject currentJsonObject=(JSONObject)elementsJsonObject.get(key);
String type=this.confReader.getType(currentJsonObject);
try {
JSONObject locationJsonObject=this.confReader.getLocation(currentJsonObject);
switch(type) {
case "literal":
typesTable.put(prefix+((String) key), this.confReader.getClassFromJsonObject(locationJsonObject));
break;
case "predicate":
prefixTable.put(prefix+((String) key), this.confReader.getClassFromJsonObject(locationJsonObject));
break;
case "function":
prefixTable.put(prefix+((String) key), this.confReader.getClassFromJsonObject(locationJsonObject));
break;
default:
throw new ConfigFileException(type + " not in <literal,predicate,function>");
}
}catch(ConfigFileException e) {
e.printStackTrace();
}
/**
* Given a path for a config file, this method returns an instance of the class where the functions are defined.
*
* @param prefix the prefix to associate with the config
* @param prefixFilePath the path to the config file
*/
public void getObjectToInvoke(String prefix, String prefixFilePath)
throws IOException, ParseException, ClassNotFoundException {
JSONObject jsonObject = this.confReader.readJsonFile(prefixFilePath);
if (jsonObject != null) {
JSONObject defaultJsonObject = this.confReader.getDefault(jsonObject);
JSONObject elementsJsonObject = this.confReader.getElements(jsonObject);
for (Object key : elementsJsonObject.keySet()) {
JSONObject currentJsonObject = (JSONObject) elementsJsonObject.get(key);
String type = this.confReader.getType(currentJsonObject);
JSONObject locationJsonObject = this.confReader.getLocation(currentJsonObject);
switch (type) {
case "literal":
typesTable.put(prefix + key, this.confReader.getClassFromJsonObject(locationJsonObject));
break;
case "predicate":
case "function":
prefixTable.put(prefix + key, this.confReader.getClassFromJsonObject(locationJsonObject));
break;
default:
throw new RuntimeException(type + " not in <literal,predicate,function>");
}
}
if(!defaultJsonObject.isEmpty()) {
if (!defaultJsonObject.isEmpty()) {
JSONObject typesJsonObject = this.confReader.getTypes(defaultJsonObject);
JSONObject predicatesJsonObject = this.confReader.getPredicates(defaultJsonObject);
JSONObject functionsJsonObject = this.confReader.getFunctions(defaultJsonObject);
if(!typesJsonObject.isEmpty()) {
if (!typesJsonObject.isEmpty()) {
typesTable.put(prefix, this.confReader.getClassFromJsonObject(typesJsonObject));
}
if(!predicatesJsonObject.isEmpty()) {
if (!predicatesJsonObject.isEmpty()) {
prefixTable.put(prefix, this.confReader.getClassFromJsonObject(predicatesJsonObject));
}
if(!functionsJsonObject.isEmpty()) {
if (!functionsJsonObject.isEmpty()) {
prefixTable.put(prefix, this.confReader.getClassFromJsonObject(functionsJsonObject));
}
}
}
}
/**
* Reverses the typesTable map to create the inverseTypesTable map.
*/
private void reverseMap() {
for(Map.Entry<String,Object> entry:typesTable.entrySet())
inversedTypesTable.put(entry.getValue(), entry.getKey());
for (Map.Entry<String, Object> entry : typesTable.entrySet()) {
inverseTypesTable.put(entry.getValue(), entry.getKey());
}
}
}
......@@ -3,7 +3,8 @@ module fr.lirmm.graphik.dlgp3 {
requires json.simple;
requires java.desktop;
requires fr.inria.integraal.mapping.parser;
exports fr.lirmm.graphik.dlgp3.parser;
requires java.compiler;
exports fr.lirmm.graphik.dlgp3.parser;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment