How to drain your laptop battery using Jigsaw

Jesse Glick jesse.glick at oracle.com
Wed Dec 21 14:37:24 PST 2011


On 12/21/2011 05:23 PM, Jesse Glick wrote:
> The attached program

Including inline since I am not sure the original made it through the list manager:

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.module.Dependence;
import java.lang.module.ModuleId;
import java.lang.module.ModuleIdQuery;
import java.lang.module.ModuleInfo;
import java.lang.module.ModuleSystem;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.openjdk.jigsaw.Catalog;
import org.openjdk.jigsaw.Configuration;
import org.openjdk.jigsaw.ConfigurationException;
import org.openjdk.jigsaw.Configurator;
import org.openjdk.jigsaw.PathContext;
public class Jigsaw3SAT {
     public static void main(String[] args) throws Exception {
         Random rand = new Random();
         ModuleSystem ms = ModuleSystem.base();
         for (int vars = 3; ; vars++) {
             for (int round = 0; round < 5; round++) {
                 int clauses = (int) (vars * 4.24);
                 Collection<ModuleInfo> r = new ArrayList<>();
                 List<Integer> varNums = new ArrayList<>();
                 for (int i = 0 ; i < vars; i++) {
                     varNums.add(i);
                     for (int j = 0; j < 2; j++) {
                         r.add(new MockModule(ms.parseModuleId("v" + i + " @ " + j)));
                     }
                 }
                 ModuleIdQuery[] tReqs = new ModuleIdQuery[clauses];
                 for (int i = 0; i < clauses; i++) {
                     Collections.shuffle(varNums, rand);
                     for (int j = 0; j < 3; j++) {
                         r.add(new MockModule(ms.parseModuleId("f" + i + " @ " + j), ms.parseModuleIdQuery("v" + varNums.get(j) + "@" + rand.nextInt(2))));
                     }
                     tReqs[i] = ms.parseModuleIdQuery("f" + i);
                 }
                 r.add(new MockModule(ms.parseModuleId("t"), tReqs));
                 System.out.println("with " + vars + " variables...");
                 long start = System.currentTimeMillis();
                 try {
                     Configuration<PathContext> cfg = Configurator.configurePaths(new MockCatalog(r), Collections.singleton(ms.parseModuleIdQuery("t")));
                     long end = System.currentTimeMillis();
                     System.out.printf("solved in %.3fs:\n", (0.0 + end - start) / 1000);
                     for (int i = 0 ; i < vars; i++) {
                         System.out.println("v" + i + "=" + cfg.getContextForModuleName("v" + i).modules().iterator().next().version());
                     }
                 } catch (ConfigurationException x) {
                     long end = System.currentTimeMillis();
                     System.out.printf("unsatisfiable in %.3fs\n", (0.0 + end - start) / 1000);
                 }
                 System.out.println();
             }
         }
     }
     static class MockModule implements ModuleInfo {
         final ModuleId id;
         final Set<Dependence> r;
         MockModule(ModuleId id, ModuleIdQuery... reqs) {
             this.id = id;
             r = new HashSet<>();
             for (ModuleIdQuery req : reqs) {
                 r.add(new Dependence(EnumSet.noneOf(Dependence.Modifier.class), req));
             }
         }
         @Override public ModuleId id() {
             return id;
         }
         @Override public Set<Dependence> requires() {
             return r;
         }
         @Override public Set<ModuleId> provides() {
             return Collections.emptySet();
         }
         @Override public Set<String> permits() {
             return Collections.emptySet();
         }
         @Override public String mainClass() {
             return null;
         }
         @Override public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
             return false;
         }
         @Override public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
             return null;
         }
     }
     static class MockCatalog extends Catalog {
         final Map<String,Set<ModuleId>> byName;
         final Map<ModuleId,ModuleInfo> byId;
         MockCatalog(Collection<ModuleInfo> modules) {
             byName = new HashMap<>();
             byId = new HashMap<>();
             for (ModuleInfo m : modules) {
                 ModuleId id = m.id();
                 String n = id.name();
                 Set<ModuleId> ids = byName.get(n);
                 if (ids == null) {
                     ids = new HashSet<>();
                     byName.put(n, ids);
                 }
                 ids.add(id);
                 byId.put(id, m);
             }
         }
         @Override public String name() {
             return "test";
         }
         @Override public Catalog parent() {
             return null;
         }
         @Override protected void gatherLocalModuleIds(String moduleName, Set<ModuleId> mids) throws IOException {
             mids.addAll(byName.get(moduleName));
         }
         @Override protected ModuleInfo readLocalModuleInfo(ModuleId mid) throws IOException {
             return byId.get(mid);
         }
     }
     private Jigsaw3SAT() {}
}



More information about the jigsaw-dev mailing list