From 4c71757906a3a2757a62a3e454b62f3eba2cc24a Mon Sep 17 00:00:00 2001 From: rgudwin Date: Fri, 10 Nov 2023 22:22:06 -0300 Subject: [PATCH] Solved many problems --- build.gradle | 21 +-- gradle/wrapper/gradle-wrapper.properties | 2 +- .../java/br/unicamp/cst/attention/CFM.java | 4 +- .../unicamp/cst/representation/idea/Idea.java | 126 +++++++++++++++++- .../cst/representation/idea/TestIdea.java | 19 +++ 5 files changed, 156 insertions(+), 16 deletions(-) diff --git a/build.gradle b/build.gradle index a3ca8d67..7749bdf3 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 15de9024..e411586a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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 diff --git a/src/main/java/br/unicamp/cst/attention/CFM.java b/src/main/java/br/unicamp/cst/attention/CFM.java index db0e1164..1d1ba3e1 100644 --- a/src/main/java/br/unicamp/cst/attention/CFM.java +++ b/src/main/java/br/unicamp/cst/attention/CFM.java @@ -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 * diff --git a/src/main/java/br/unicamp/cst/representation/idea/Idea.java b/src/main/java/br/unicamp/cst/representation/idea/Idea.java index 3d09739c..61c0a54d 100644 --- a/src/main/java/br/unicamp/cst/representation/idea/Idea.java +++ b/src/main/java/br/unicamp/cst/representation/idea/Idea.java @@ -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; @@ -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 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 @@ -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); @@ -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; @@ -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); diff --git a/src/test/java/br/unicamp/cst/representation/idea/TestIdea.java b/src/test/java/br/unicamp/cst/representation/idea/TestIdea.java index 327f7eb7..1c1efe6d 100644 --- a/src/test/java/br/unicamp/cst/representation/idea/TestIdea.java +++ b/src/test/java/br/unicamp/cst/representation/idea/TestIdea.java @@ -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);