Skip to content

Commit

Permalink
Solved many problems
Browse files Browse the repository at this point in the history
  • Loading branch information
rgudwin committed Nov 11, 2023
1 parent 4b9b69a commit 4c71757
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 16 deletions.
21 changes: 12 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ dependencies {
api 'com.google.code.gson:gson:2.10.1'
api 'net.openhft:compiler:2.3.0' // Used by representation/owrl/CodeBuilder.java
api group: 'org.json', name: 'json', version: '20230227'
api group: 'org.opt4j', name: 'opt4j-core', version: '3.1' // Used in GLAS
api group: 'org.opt4j', name: 'opt4j-optimizers', version: '3.1'
api group: 'org.opt4j', name: 'opt4j-viewer', version: '3.1'
api group: 'org.opt4j', name: 'opt4j-benchmarks', version: '3.1'
api group: 'org.opt4j', name: 'opt4j-operators', version: '3.1'
api group: 'org.opt4j', name: 'opt4j-satdecoding', version: '3.1'
api group: 'org.opt4j', name: 'opt4j-tutorial', version: '3.1'
api 'com.google.inject:guice:7.0.0'
api group: 'com.github.sdarg', name: 'opt4j', version: '3.3.0' // Used in GLAS
//api group: 'com.github.sdarg', name: 'opt4j-core', version: '3.3.0' // Used in GLAS
//api group: 'com.github.sdarg', name: 'opt4j-optimizers', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-viewer', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-benchmarks', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-operators', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-satdecoding', version: '3.3.0'
//api group: 'com.github.sdarg', name: 'opt4j-tutorial', version: '3.3.0'
api 'org.antlr:antlr4-runtime:4.5.3' // Used in OwrlBaseListener
api 'com.github.masecla22:java-express:0.2.2'
//api 'io.vacco.murmux:murmux:2.2.2'
implementation 'ch.qos.logback:logback-classic:1.3.6'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.3'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.1'

}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions src/main/java/br/unicamp/cst/attention/CFM.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
* (i) the sum of the values​generated by the bottom-up feature maps and
* (ii) the sum of the values ​​generated by the top-down feature maps.
*
* If (i) (@code >} (ii), the winner of the attentional cycle is classified as bottom-up.
* If (ii) (@code >} (i), the winner of the attentional cycle is classified as top-down.
* If (i) {@code >} (ii), the winner of the attentional cycle is classified as bottom-up.
* If (ii) {@code >} (i), the winner of the attentional cycle is classified as top-down.
*
* @author leolellisr
*
Expand Down
126 changes: 122 additions & 4 deletions src/main/java/br/unicamp/cst/representation/idea/Idea.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
import br.unicamp.cst.support.ToString;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
Expand Down Expand Up @@ -1081,6 +1085,48 @@ else if (field.getType().getCanonicalName().equals("java.util.List")) {
return(ret);
}

private Object checkIfObjectHasSetGet(Object bean, String propertyName) {
// access a no-arg method through reflection
// following bean naming conventions
try {
Method m = bean.getClass().getMethod(
"get"
+propertyName.substring(0,1).toUpperCase()
+propertyName.substring(1)
, null);
return m.invoke(bean);
}
catch (Exception e) {
try {
Method m = bean.getClass().getMethod(propertyName, null);
return m.invoke(bean);
}
catch (Exception e2) {
// (gulp) -- swallow exception and move on
}
}
return null; // it would be better to throw an exception, wouldn't it?
}

private void insertAlreadyExistObject(String fullname, String fname, Field field, Object fo, Idea ao) {
String ideaname = getFullName()+"."+ToString.getSimpleName(fullname)+"."+fname;
Idea fi = createIdea(ideaname,"",2);
if (!Modifier.isStatic(field.getModifiers())) {
for (Map.Entry<String,Idea> entry : repo.entrySet()) {
String key = entry.getKey();
Idea v = entry.getValue();
if (ToString.getSimpleName(ideaname).equals(ToString.getSimpleName(key))) {
System.out.println("The Idea "+ideaname+" is already in the repository");
}
}
System.out.println(fo.getClass().getCanonicalName());
Idea alternative = repo.get(ideaname);
if (alternative != null) System.out.println("Ah ... I already found "+ideaname);
else System.out.println("Strange ... it seems that "+ideaname+" is already in the repo but I can't find it");
}
ao.add(fi);
}

/**
* This method uses a Java Object as template, creating an Idea with a similar structure.
* This new Idea is added to the current Idea
Expand Down Expand Up @@ -1169,6 +1215,42 @@ else if (obj instanceof List) {
this.add(onode);
return;
}
else if (obj instanceof Enumeration) {
Enumeration ll = (Enumeration) obj;
String label = "";
int size = 0;
ArrayList components = new ArrayList();
while (ll.hasMoreElements()) {
components.add(ll.nextElement());
size++;
}
if (size == 0) label = "{0}";
else label = "{"+size+"} of "+components.get(0).getClass().getSimpleName();
Idea onode = createIdea(getFullName()+"."+fullname,label,0);
int i=0;
for (Object o : components) {
onode.addObject(o,ToString.el(ToString.getSimpleName(fullname),i),false);
listtoavoidloops.add(obj);
i++;
}
this.add(onode);
return;
}
else if (obj instanceof Vector) {
Vector ll = (Vector) obj;
String label = "";
if (ll.size() > 0) label = "{"+ll.size()+"} of "+ll.get(0).getClass().getSimpleName();
else label = "{0}";
Idea onode = createIdea(getFullName()+"."+fullname,label,0);
int i=0;
for (Object o : ll) {
onode.addObject(o,ToString.el(ToString.getSimpleName(fullname),i),false);
listtoavoidloops.add(obj);
i++;
}
this.add(onode);
return;
}
else if (obj instanceof Idea) {
Idea ao = (Idea) obj;
this.add(ao);
Expand All @@ -1181,17 +1263,49 @@ else if (obj instanceof Idea) {
Field[] fields = obj.getClass().getDeclaredFields();
for (Field field : fields) {
String fname = field.getName();
int modifiers = field.getModifiers();
//if (Modifier.isProtected(modifiers)) System.out.println(ao.getName()+"."+fname+": PROTECTED");
//if (Modifier.isPrivate(modifiers)) System.out.println(ao.getName()+"."+fname+": PRIVATE");
//if (Modifier.isStatic(modifiers)) System.out.println(ao.getName()+"."+fname+": STATIC");
//if (Modifier.isVolatile(modifiers)) System.out.println(ao.getName()+"."+fname+": VOLATILE");
try {
field.setAccessible(true);
Object toget = null;
if (!((modifiers & Modifier.STATIC) > 0)) toget = obj;
//if (!field.canAccess(toget)) {
//System.out.println("I can't access field "+field.getName()+" of object "+getFullName()+" of class "+obj.getClass().getCanonicalName());
//System.out.println(field.toString());
//}
boolean trysetaccessible = field.trySetAccessible();
if (!trysetaccessible) {// This is the case the object is inaccessible
//System.out.println("I tried to make the field "+fname+" accessible, but didn't succeeded");
Object fo = checkIfObjectHasSetGet(obj, fname);
if (fo != null) {
//System.out.println("Hey .. it worked ... "+fname+" is a JavaBean");
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else insertAlreadyExistObject(fullname, fname, field, fo, ao);
}
else {
//System.out.println("OK .. giving up ... "+fname+" is not a JavaBean ... I will not include it in the Idea");
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else insertAlreadyExistObject(fullname, fname, field, fo, ao);
}
}
else {// This is the case the object is accessible
Object fo=null;
try {
fo = field.get(obj);
if (Modifier.isStatic(modifiers)) fo = field.get(null);
else fo = field.get(obj);
} catch (Exception e) {
System.out.println(e.getClass().getCanonicalName());
e.printStackTrace();}
if (!already_exists(fo)) {
ao.addObject(fo,fname,false);
}
else {
else {// This the object already exists in listtoavoidloops
String ideaname = getFullName()+"."+ToString.getSimpleName(fullname)+"."+fname;
Idea fi = createIdea(ideaname,"",2);
Idea alternative2 = null;
Expand All @@ -1210,7 +1324,11 @@ else if (obj instanceof Idea) {
}
ao.add(fi);
}
} catch (Exception e) {
}} catch (InaccessibleObjectException ioe) {
System.out.println("I got an InaccessibleObjectException with "+ioe.getMessage());
}
catch (Exception e) {
System.out.println("I got a "+e.getClass().getName()+" Exception in field "+fname+" in class Idea: "+e.getMessage()+"-->"+e.getLocalizedMessage());
}
}
this.add(ao);
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/br/unicamp/cst/representation/idea/TestIdea.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,25 @@ public void testIdea2() {
assertEquals(idea.getType(),0);
}

@Test
public void testIdea3() {
DefaultMutableTreeNode tn = new DefaultMutableTreeNode();
tn.setUserObject("tn");
DefaultMutableTreeNode tn21 = new DefaultMutableTreeNode();
tn21.setUserObject("tn21");
DefaultMutableTreeNode tn22 = new DefaultMutableTreeNode();
tn22.setUserObject("tn22");
DefaultMutableTreeNode tn3 = new DefaultMutableTreeNode();
tn3.setUserObject("tn3");
tn.add(tn21);
tn22.add(tn3);
tn.add(tn22);

Idea i = new Idea("dm");
i.addObject(tn, "tn");
System.out.println(i.toStringFull());
}

@Test
public void testIsMethods() {
Locale.setDefault(Locale.US);
Expand Down

0 comments on commit 4c71757

Please sign in to comment.