Skip to content

Commit

Permalink
Global refactoring of the Desktop part
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanogermano committed Dec 14, 2016
1 parent 1fa9efc commit c49e233
Show file tree
Hide file tree
Showing 25 changed files with 702 additions and 597 deletions.
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="it/unical/mat/embasp/platforms/android/|it/unical/mat/embasp/specializations/dlv/android/" kind="src" path="app/src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea/*
.idea/*
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>EmbASP</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import it.unical.mat.embasp.base.OptionDescriptor;


/**Generic filter option for ASP solver*/
/** Generic filter option for ASP solver */

public abstract class ASPFilterOption extends OptionDescriptor {
public ASPFilterOption() {
super("-filter=");
}
public ASPFilterOption() {
super("-filter=");
}
}
64 changes: 35 additions & 29 deletions app/src/main/java/it/unical/mat/embasp/asp/ASPInputProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,42 @@

import it.unical.mat.embasp.base.InputProgram;


/**a generic ASP program , with the capabilities of retrieve data by objects */
/** a generic ASP program , with the capabilities of retrieve data by objects */

public class ASPInputProgram extends InputProgram {


public ASPInputProgram() {
}

public ASPInputProgram(String initial_program) {
super(initial_program);
}

public ASPInputProgram(Object inputObj) throws InvocationTargetException, NoSuchMethodException, IllegalTermException, IllegalAccessException, IllegalAnnotationException, PredicateNotValidException {
super(inputObj);
}


/**transforms a given Object class into a {@link InputProgram} and adds it to the current {@link #programs}
* @param inputObj an object to be transformed
* @see ASPMapper
* @throws IllegalAccessException , IllegalArgumentException , InvocationTargetException , NoSuchMethodException , SecurityException , IllegalTermException*/
@Override
public void addObjectInput(Object inputObj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException, IllegalAnnotationException, PredicateNotValidException {
addProgram(ASPMapper.getInstance().getString(inputObj) + ".");
}
/**transforms a set of objects @see #addObjectInput(Object)*/
@Override
public void addObjectsInput(Set<Object> inputObjs) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException, PredicateNotValidException, IllegalAnnotationException {
for (Object inputObj : inputObjs)
addObjectInput(inputObj);
}
public ASPInputProgram() {
}

public ASPInputProgram(final Object inputObj) throws InvocationTargetException, NoSuchMethodException, IllegalTermException, IllegalAccessException,
IllegalAnnotationException, PredicateNotValidException {
super(inputObj);
}

public ASPInputProgram(final String initial_program) {
super(initial_program);
}

/**
* transforms a given Object class into a {@link InputProgram} and adds it to the current {@link #programs}
*
* @param inputObj
* an object to be transformed
* @see ASPMapper
* @throws IllegalAccessException
* , IllegalArgumentException , InvocationTargetException , NoSuchMethodException , SecurityException , IllegalTermException
*/
@Override
public void addObjectInput(final Object inputObj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, IllegalTermException, IllegalAnnotationException, PredicateNotValidException {
addProgram(ASPMapper.getInstance().getString(inputObj) + ".");
}

/** transforms a set of objects @see #addObjectInput(Object) */
@Override
public void addObjectsInput(final Set<Object> inputObjs) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException,
NoSuchMethodException, SecurityException, IllegalTermException, PredicateNotValidException, IllegalAnnotationException {
for (final Object inputObj : inputObjs)
addObjectInput(inputObj);
}
}
262 changes: 131 additions & 131 deletions app/src/main/java/it/unical/mat/embasp/asp/ASPMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,141 +7,141 @@
import java.util.HashMap;
import java.util.Map;


/**
* Contains methods used to transform Objects into {@link it.unical.mat.embasp.base.InputProgram}
*/

public class ASPMapper {

private static ASPMapper mapper;

private Map<String, Class<?>> predicateClass;
private Map<Class, Map<String, Method>> classSetterMethod;

private ASPMapper() {
predicateClass = new HashMap<>();
classSetterMethod = new HashMap<>();
}

public static ASPMapper getInstance() {
if (mapper == null) {
mapper = new ASPMapper();
}
return mapper;
}

public Class<?> getClass(String predicate) {
return predicateClass.get(predicate);
}


/**
* insert an object into {@link #predicateClass} and {@link #classSetterMethod}
*
* @return String representing pairing key of {@link #predicateClass}
*/
public String registerClass(Class<?> cl) throws PredicateNotValidException, IllegalAnnotationException {

Annotation annotation = cl.getAnnotation(Predicate.class);

if(annotation == null){
throw new IllegalAnnotationException();
}

String predicate = ((Predicate) annotation).value();

if(predicate.contains(" "))
throw new PredicateNotValidException();

// String predicate = cl.getAnnotation(Predicate.class).value();
predicateClass.put(predicate, cl);
Map<String, Method> namesMethods = new HashMap<>();
for (Method method : cl.getMethods()) {
if (method.getName().startsWith("set"))
namesMethods.put(method.getName(), method);
}
classSetterMethod.put(cl, namesMethods);
return predicate;
}


/**
* Returns data for the given Object
*
* @param obj Object from witch data are extrapolated
* @return String data for the given Object in a String format
* @throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException
*/
public String getString(Object obj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException, PredicateNotValidException, IllegalAnnotationException {
String predicate = registerClass(obj.getClass());
String atom = predicate + "(";
HashMap<Integer, Object> mapTerm = new HashMap<>();
for (Field field : obj.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(Term.class)) {
Object value = obj.getClass().getMethod("get" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1)).invoke(obj);
mapTerm.put(field.getAnnotation(Term.class).value(), value);
}else{
throw new IllegalAnnotationException();
}
}
for (int i = 0; i < mapTerm.size(); i++) {
if (i != 0)
atom += ",";
Object objectTerm = mapTerm.get(i);
if (objectTerm == null)
throw new IllegalTermException("Wrong term number of class " + obj.getClass().getName());
if (objectTerm instanceof Integer) {
atom += objectTerm + "";
} else
atom += "\"" + objectTerm.toString() + "\"";

}
atom += ")";
return atom;
}

/**
* Returns an Object for the given atom
*
* @param atom String from witch data are extrapolated
* @return Object for the given String data
* @throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException
*/
public Object getObject(String atom) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException { // TODO
String predicate;

int indexOf = atom.indexOf("(");
if (indexOf == -1) {
//Arity 0
predicate = atom;
} else
predicate = atom.substring(0, (atom.indexOf("(")));
Class<?> cl = getClass(predicate);
// Not exist mapping between the predicate and the class
if (cl == null) return null;
Object obj = cl.newInstance();
//Term with arity 0 return obj
if (indexOf == -1)
return obj;
//FIXME Not work with "a("asd,"). fix the split
String[] paramiter = atom.substring(atom.indexOf("(") + 1, atom.lastIndexOf(")")).split(",");
for (Field field : cl.getDeclaredFields()) {

if (field.isAnnotationPresent(Term.class)) {

int term = field.getAnnotation(Term.class).value();
String nameMethod = "set" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
Method method = classSetterMethod.get(cl).get(nameMethod);

if (method.getParameterTypes()[0].getName().equals(int.class.getName()) || method.getParameterTypes()[0].getName().equals(Integer.class.getName()))
method.invoke(obj, Integer.valueOf(paramiter[term]));
else
method.invoke(obj, paramiter[term]);

}
}

return obj;
}
private static ASPMapper mapper;

public static ASPMapper getInstance() {
if (ASPMapper.mapper == null)
ASPMapper.mapper = new ASPMapper();
return ASPMapper.mapper;
}

private final Map<String, Class<?>> predicateClass;

private final Map<Class, Map<String, Method>> classSetterMethod;

private ASPMapper() {
predicateClass = new HashMap<>();
classSetterMethod = new HashMap<>();
}

public Class<?> getClass(final String predicate) {
return predicateClass.get(predicate);
}

/**
* Returns an Object for the given atom
*
* @param atom
* String from witch data are extrapolated
* @return Object for the given String data
* @throws IllegalAccessException,
* IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException
*/
public Object getObject(final String atom) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, InstantiationException { // TODO
String predicate;

final int indexOf = atom.indexOf("(");
if (indexOf == -1)
// Arity 0
predicate = atom;
else
predicate = atom.substring(0, atom.indexOf("("));
final Class<?> cl = getClass(predicate);
// Not exist mapping between the predicate and the class
if (cl == null)
return null;
final Object obj = cl.newInstance();
// Term with arity 0 return obj
if (indexOf == -1)
return obj;
// FIXME Not work with "a("asd,"). fix the split
final String[] paramiter = atom.substring(atom.indexOf("(") + 1, atom.lastIndexOf(")")).split(",");
for (final Field field : cl.getDeclaredFields())
if (field.isAnnotationPresent(Term.class)) {

final int term = field.getAnnotation(Term.class).value();
final String nameMethod = "set" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
final Method method = classSetterMethod.get(cl).get(nameMethod);

if (method.getParameterTypes()[0].getName().equals(int.class.getName())
|| method.getParameterTypes()[0].getName().equals(Integer.class.getName()))
method.invoke(obj, Integer.valueOf(paramiter[term]));
else
method.invoke(obj, paramiter[term]);

}

return obj;
}

/**
* Returns data for the given Object
*
* @param obj
* Object from witch data are extrapolated
* @return String data for the given Object in a String format
* @throws IllegalAccessException,
* IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IllegalTermException
*/
public String getString(final Object obj) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
SecurityException, IllegalTermException, PredicateNotValidException, IllegalAnnotationException {
final String predicate = registerClass(obj.getClass());
String atom = predicate + "(";
final HashMap<Integer, Object> mapTerm = new HashMap<>();
for (final Field field : obj.getClass().getDeclaredFields())
if (field.isAnnotationPresent(Term.class)) {
final Object value = obj.getClass().getMethod("get" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1))
.invoke(obj);
mapTerm.put(field.getAnnotation(Term.class).value(), value);
} else
throw new IllegalAnnotationException();
for (int i = 0; i < mapTerm.size(); i++) {
if (i != 0)
atom += ",";
final Object objectTerm = mapTerm.get(i);
if (objectTerm == null)
throw new IllegalTermException("Wrong term number of class " + obj.getClass().getName());
if (objectTerm instanceof Integer)
atom += objectTerm + "";
else
atom += "\"" + objectTerm.toString() + "\"";

}
atom += ")";
return atom;
}

/**
* insert an object into {@link #predicateClass} and {@link #classSetterMethod}
*
* @return String representing pairing key of {@link #predicateClass}
*/
public String registerClass(final Class<?> cl) throws PredicateNotValidException, IllegalAnnotationException {

final Annotation annotation = cl.getAnnotation(Predicate.class);

if (annotation == null)
throw new IllegalAnnotationException();

final String predicate = ((Predicate) annotation).value();

if (predicate.contains(" "))
throw new PredicateNotValidException();

// String predicate = cl.getAnnotation(Predicate.class).value();
predicateClass.put(predicate, cl);
final Map<String, Method> namesMethods = new HashMap<>();
for (final Method method : cl.getMethods())
if (method.getName().startsWith("set"))
namesMethods.put(method.getName(), method);
classSetterMethod.put(cl, namesMethods);
return predicate;
}
}
Loading

0 comments on commit c49e233

Please sign in to comment.