1 package it.unical.mat.embasp.languages;
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Field;
5 import java.lang.reflect.InvocationTargetException;
6 import java.lang.reflect.Method;
7 import java.util.HashMap;
10 import it.unical.mat.embasp.languages.asp.IllegalTermException;
11 import it.unical.mat.embasp.languages.asp.SymbolicConstant;
20 protected final Map<String, Class<?>> predicateClass =
new HashMap<>();
22 protected final Map<Class<?>, Map<String, Method>> classSetterMethod =
new HashMap<>();
24 protected abstract String getActualString(String predicate, HashMap<Integer, Object> parametersMap)
throws IllegalTermException;
26 public Class<?> getClass(
final String predicate) {
27 return predicateClass.get(predicate);
39 public Object
getObject(
final String atom)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
40 SecurityException, InstantiationException {
41 final String predicate =
getId(atom);
46 final Class<?> cl = getClass(predicate);
51 final String[] parameters =
getParam(atom);
53 if(parameters ==
null)
56 final Object
object = cl.newInstance();
58 populateObject(cl, parameters,
object);
66 protected abstract String
getId(
final String atom);
71 protected abstract String[]
getParam(
final String atom);
82 public String
getString(
final Object obj)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException,
86 final HashMap<Integer, Object> parametersMap =
new HashMap<>();
87 for (
final Field field : obj.getClass().getDeclaredFields()) {
88 if (field.isSynthetic())
90 if (field.isAnnotationPresent(
Param.class)) {
91 final Object value = obj.getClass().getMethod(
"get" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1))
93 parametersMap.put(field.getAnnotation(
Param.class).value(), value);
96 return getActualString(predicate, parametersMap);
99 private void populateObject(
final Class<?> cl,
final String[] parameters,
final Object obj)
throws IllegalAccessException, InvocationTargetException {
100 for (
final Field field : cl.getDeclaredFields())
101 if (field.isAnnotationPresent(
Param.class)) {
103 final int term = field.getAnnotation(
Param.class).value();
104 final String nameMethod =
"set" + Character.toUpperCase(field.getName().charAt(0)) + field.getName().substring(1);
105 final Method method = classSetterMethod.get(cl).get(nameMethod);
107 if (method.getParameterTypes()[0].getName().equals(
int.class.getName())
108 || method.getParameterTypes()[0].getName().equals(Integer.class.getName()))
109 method.invoke(obj, Integer.valueOf(parameters[term]));
110 else if (method.getParameterTypes()[0].getName().equals(
SymbolicConstant.class.getName()))
113 method.invoke(obj, parameters[term]);
125 final Annotation annotation = cl.getAnnotation(
Id.class);
127 if (annotation ==
null)
130 final String predicate = ((
Id) annotation).value();
132 if (predicate.contains(
" "))
136 predicateClass.put(predicate, cl);
137 final Map<String, Method> namesMethods =
new HashMap<>();
138 for (
final Method method : cl.getMethods())
139 if (method.getName().startsWith(
"set"))
140 namesMethods.put(method.getName(), method);
141 classSetterMethod.put(cl, namesMethods);
147 final Annotation annotation = cl.getAnnotation(
Id.class);
149 if (annotation ==
null)
152 final String predicate = ((
Id) annotation).value();
154 predicateClass.remove(predicate);
155 classSetterMethod.remove(cl);