You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ri...@apache.org on 2010/01/06 17:51:35 UTC

svn commit: r896527 - in /felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver: Main.java proto3/Proto3Resolver.java

Author: rickhall
Date: Wed Jan  6 16:51:33 2010
New Revision: 896527

URL: http://svn.apache.org/viewvc?rev=896527&view=rev
Log:
Create correct wiring.

Modified:
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java
    felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java

Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java?rev=896527&r1=896526&r2=896527&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java (original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/Main.java Wed Jan  6 16:51:33 2010
@@ -162,6 +162,7 @@
 
     // SOLUTION:
     // A: bar->B, woz->C
+    // C: bar->B, dit->D
     private static Module scenario2(List<Module> moduleList)
     {
         Module m, target;
@@ -617,7 +618,9 @@
     }
 
     // SOLUTION:
-    // A: bar->[B,C]
+    // A: require->[C,D]
+    // B: foo->D
+    // C: foo->E
     private static Module scenario12(List<Module> moduleList)
     {
         Module m, target;
@@ -690,7 +693,7 @@
     }
 
     // SOLUTION:
-    // A: bar->[A,B,C]
+    // A: require->[B,C]
     private static Module scenario14(List<Module> moduleList)
     {
         Module m, target;
@@ -717,7 +720,7 @@
 
     // SOLUTION:
     // A: bar->B, foo->F
-    // B: bar->[B,C,D]
+    // B: require->[C,D]
     // D: foo->F
     private static Module scenario15(List<Module> moduleList)
     {
@@ -758,8 +761,8 @@
     }
 
     // SOLUTION:
-    // A: bar->C, foo->B
-    // B: foo->B, bar->[B,C,D]
+    // A: foo->B, bar->C
+    // B: require->[C,D]
 // TODO: PROTO3 RESOLVER - Uses constraints are not added to the package space if
 //       there is a matching imported/exported package. It probably still needs to
 //       be to detect future conflicts or does it since the hard constraint rules?
@@ -845,8 +848,8 @@
 
     // SOLUTION:
     // A: foo->B, bar->D, woz->BB
-    // B: bar->[B,C,D]
-    // BB: bar->[BB,CC,D]
+    // B: require->[C,D]
+    // BB: require->[CC,D]
     private static Module scenario18(List<Module> moduleList)
     {
         Module m, target;

Modified: felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java
URL: http://svn.apache.org/viewvc/felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java?rev=896527&r1=896526&r2=896527&view=diff
==============================================================================
--- felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java (original)
+++ felix/sandbox/rickhall/resolver/src/main/java/org/apache/felix/resolver/proto3/Proto3Resolver.java Wed Jan  6 16:51:33 2010
@@ -97,11 +97,12 @@
             m_invokeCounts.put(methodName, count);
         }
 
+        Map<Module, Packages> modulePkgMap = new HashMap<Module, Packages>();
+
         if (!module.isResolved())
         {
             Map<Requirement, Set<Capability>> candidateMap =
                 new HashMap<Requirement, Set<Capability>>();
-            Map<Module, Packages> modulePkgMap = new HashMap<Module, Packages>();
 
             populateCandidates(module, candidateMap, new HashSet<Module>());
             m_candidatePermutations.add(candidateMap);
@@ -139,7 +140,15 @@
 dumpModulePkgMap(modulePkgMap);
         }
 
-        return null;
+        Map<Module, List<Wire>> wireMap =
+            populateWireMap(module, modulePkgMap, new HashMap<Module, List<Wire>>());
+
+        if (m_isInvokeCount)
+        {
+            System.out.println("INVOKE COUNTS " + m_invokeCounts);
+        }
+
+        return wireMap;
     }
 
     private void dumpCandidateMap(Map<Requirement, Set<Capability>> candidateMap)
@@ -221,7 +230,7 @@
         }
         cycleMap.put(module, module);
 
-System.out.println("+++ RESOLVING " + module);
+//System.out.println("+++ RESOLVING " + module);
         calculateExportedPackages(module, incomingReqs, modulePkgMap);
 
         List<Requirement> reqs = module.getRequirements();
@@ -241,7 +250,7 @@
             for (Iterator<Capability> it = candCaps.iterator(); it.hasNext(); )
             {
                 Capability candCap = it.next();
-System.out.println("+++ TRYING CAND " + candCap + " FOR " + req);
+//System.out.println("+++ TRYING CAND " + candCap + " FOR " + req);
                 try
                 {
                     // Try to resolve the candidate.
@@ -467,7 +476,7 @@
     }
 
     private void mergeCandidatePackages(
-        Module current, List<Requirement> blameReqs,
+        Module current, List<Requirement> outgoingReqs,
         Capability candCap, Map<Module, Packages> modulePkgMap,
         Map<Requirement, Set<Capability>> candidateMap)
     {
@@ -482,7 +491,7 @@
         if (candCap.getNamespace().equals(Capability.PACKAGE_NAMESPACE))
         {
             mergeCandidatePackage(
-                current, false, new Blame(blameReqs, candCap), modulePkgMap, candidateMap);
+                current, false, new Blame(outgoingReqs, candCap), modulePkgMap, candidateMap);
         }
         else if (candCap.getNamespace().equals(Capability.MODULE_NAMESPACE))
         {
@@ -497,7 +506,7 @@
                 mergeCandidatePackage(
                     current,
                     true,
-                    new Blame(blameReqs, entry.getValue().m_cap),
+                    new Blame(outgoingReqs, entry.getValue().m_cap),
                     modulePkgMap,
                     candidateMap);
             }
@@ -519,7 +528,7 @@
 
         if (candBlame.m_cap.getNamespace().equals(Capability.PACKAGE_NAMESPACE))
         {
-System.out.println("+++ MERGING " + candBlame.m_cap + " INTO " + current);
+//System.out.println("+++ MERGING " + candBlame.m_cap + " INTO " + current);
             String pkgName = (String)
                 candBlame.m_cap.getAttribute(Capability.PACKAGE_ATTR).getValue();
             Packages candPkgs = modulePkgMap.get(candBlame.m_cap.getModule());
@@ -544,7 +553,6 @@
                 || ((currentImportedBlame != null) && !currentImportedBlame.m_cap.equals(candBlame.m_cap))
                 || ((currentRequiredBlames != null) && !currentRequiredBlames.contains(candBlame))))
             {
-dumpModulePkgs(current, currentPkgs);
                 throw new ResolveException("Constraint violation between "
                     + current + " and " + candBlame.m_cap.getModule()
                     + " for " + pkgName);
@@ -584,7 +592,13 @@
             }
 
             // Verify and merge the candidate's transitive uses constraints.
-            verifyAndMergeUses(current, currentPkgsCopy, candBlame, modulePkgMap, candidateMap);
+            verifyAndMergeUses(
+                current,
+                currentPkgsCopy,
+                candBlame,
+                modulePkgMap,
+                candidateMap,
+                new HashMap<String, List<Module>>());
 
             // If we are here, then there were no conflict, so we should update
             // the module's package space.
@@ -592,7 +606,7 @@
             currentPkgs.m_importedPkgs.putAll(currentPkgsCopy.m_importedPkgs);
             currentPkgs.m_requiredPkgs.putAll(currentPkgsCopy.m_requiredPkgs);
             currentPkgs.m_usedPkgs.putAll(currentPkgsCopy.m_usedPkgs);
-dumpModulePkgs(current, currentPkgs);
+//dumpModulePkgs(current, currentPkgs);
         }
     }
 
@@ -603,7 +617,7 @@
     {
         for (int i = 0; (currentUsedBlames != null) && (i < currentUsedBlames.size()); i++)
         {
-System.out.println("+++ CHECK " + candBlame + " IN EXISTING " + currentUsedBlames.get(i));
+//System.out.println("+++ CHECK " + candBlame + " IN EXISTING " + currentUsedBlames.get(i));
             if (!isCompatible(currentUsedBlames.get(i).m_cap, candBlame.m_cap, modulePkgMap))
             {
                 // Try to remove the previously selected candidate associated
@@ -645,9 +659,9 @@
     private void verifyAndMergeUses(
         Module current, Packages currentPkgs,
         Blame candBlame, Map<Module, Packages> modulePkgMap,
-        Map<Requirement, Set<Capability>> candidateMap)
+        Map<Requirement, Set<Capability>> candidateMap,
+        Map<String, List<Module>> cycleMap)
     {
-System.out.println("+++ VERIFYING USES " + current + " FOR " + candBlame);
         if (m_isInvokeCount)
         {
             String methodName = new Exception().fillInStackTrace().getStackTrace()[0].getMethodName();
@@ -656,6 +670,19 @@
             m_invokeCounts.put(methodName, count);
         }
 
+        // Check for cycles.
+        String pkgName = (String)
+            candBlame.m_cap.getAttribute(Capability.PACKAGE_ATTR).getValue();
+        List<Module> list = cycleMap.get(pkgName);
+        if ((list != null) && list.contains(current))
+        {
+            return;
+        }
+        list = (list == null) ? new ArrayList<Module>() : list;
+        list.add(current);
+        cycleMap.put(pkgName, list);
+
+//System.out.println("+++ VERIFYING USES " + current + " FOR " + candBlame);
         for (Capability candCapSource : getPackageSources(candBlame.m_cap, modulePkgMap))
         {
             for (String usedPkgName : candCapSource.getUses())
@@ -694,7 +721,7 @@
                         usedCaps = new ArrayList<Blame>();
                         currentPkgs.m_usedPkgs.put(usedPkgName, usedCaps);
                     }
-System.out.println("+++ MERGING CB " + candBlame + " SB " + sourceBlame);
+//System.out.println("+++ MERGING CB " + candBlame + " SB " + sourceBlame);
 //                    usedCaps.add(new Blame(candBlame.m_reqs, sourceBlame.m_cap));
                     usedCaps.add(sourceBlame);
                     return;
@@ -712,7 +739,7 @@
                 else if ((currentImportedBlame != null)
                     && !isCompatible(currentImportedBlame.m_cap, sourceBlame.m_cap, modulePkgMap))
                 {
-System.out.println("+++ CIB " + currentImportedBlame + " SB " + sourceBlame);
+//System.out.println("+++ CIB " + currentImportedBlame + " SB " + sourceBlame);
                     // Try to remove the previously selected candidate associated
                     // with the requirement blamed for adding the constraint. This
                     // Permutate the candidate map.
@@ -744,7 +771,13 @@
                 }
 
                 // Verify the candidate's uses constraints do not conflict.
-                verifyAndMergeUses(current, currentPkgs, sourceBlame, modulePkgMap, candidateMap);
+                verifyAndMergeUses(
+                    current,
+                    currentPkgs,
+                    sourceBlame,
+                    modulePkgMap,
+                    candidateMap,
+                    cycleMap);
             }
         }
     }
@@ -756,7 +789,7 @@
         {
             List<Capability> currentSources = getPackageSources(currentCap, modulePkgMap);
             List<Capability> candSources = getPackageSources(candCap, modulePkgMap);
-System.out.println("+++ currentSources " + currentSources + " - candSources " + candSources);
+//System.out.println("+++ currentSources " + currentSources + " - candSources " + candSources);
             return currentSources.containsAll(candSources) || candSources.containsAll(currentSources);
         }
         return true;
@@ -768,18 +801,26 @@
         List<Capability> sources = null;
         if (cap.getNamespace().equals(Capability.PACKAGE_NAMESPACE))
         {
-            Packages pkgs = modulePkgMap.get(cap.getModule());
             sources = new ArrayList<Capability>();
-            sources.add(cap);
-            String pkgName = cap.getAttribute(Capability.PACKAGE_ATTR).getValue().toString();
-            List<Blame> required = pkgs.m_requiredPkgs.get(pkgName);
-            if (required != null)
+            if (!cap.getModule().isResolved())
             {
-                for (Blame blame : required)
+                Packages pkgs = modulePkgMap.get(cap.getModule());
+                sources.add(cap);
+                String pkgName = cap.getAttribute(Capability.PACKAGE_ATTR).getValue().toString();
+                List<Blame> required = pkgs.m_requiredPkgs.get(pkgName);
+                if (required != null)
                 {
-                    sources.add(blame.m_cap);
+                    for (Blame blame : required)
+                    {
+                        sources.add(blame.m_cap);
+                    }
                 }
             }
+            else
+            {
+// TODO: PROTO3 RESOLVER - Need to properly calculate resolved package sources.
+                sources.add(cap);
+            }
         }
         return sources;
     }
@@ -798,6 +839,101 @@
         return copy;
     }
 
+    private static Map<Module, List<Wire>> populateWireMap(
+        Module module, Map<Module, Packages> modulePkgMap,
+        Map<Module, List<Wire>> wireMap)
+    {
+        if (m_isInvokeCount)
+        {
+            String methodName = new Exception().fillInStackTrace().getStackTrace()[0].getMethodName();
+            Long count = m_invokeCounts.get(methodName);
+            count = (count == null) ? new Long(1) : new Long(count.longValue() + 1);
+            m_invokeCounts.put(methodName, count);
+        }
+
+        if (wireMap.get(module) == null)
+        {
+            List<Wire> moduleWires = new ArrayList<Wire>();
+            wireMap.put(module, moduleWires);
+
+            Packages pkgs = modulePkgMap.get(module);
+            for (Entry<String, Blame> entry : pkgs.m_importedPkgs.entrySet())
+            {
+                if (!entry.getValue().m_cap.getModule().isResolved())
+                {
+                    populateWireMap(entry.getValue().m_cap.getModule(), modulePkgMap, wireMap);
+                }
+
+                // Ignore modules that import themselves.
+                if (!module.equals(entry.getValue().m_cap.getModule()))
+                {
+                    moduleWires.add(
+                        new Wire(module,
+                            entry.getValue().m_reqs.get(entry.getValue().m_reqs.size() - 1),
+                            entry.getValue().m_cap.getModule(),
+                            entry.getValue().m_cap));
+                }
+            }
+            List<Requirement> rbReqs = new ArrayList<Requirement>();
+            for (Requirement req : module.getRequirements())
+            {
+                if (req.getNamespace().equals(Capability.MODULE_NAMESPACE))
+                {
+                    rbReqs.add(req);
+                }
+            }
+            if (!rbReqs.isEmpty())
+            {
+                Map<Requirement, Capability> rbMap = new HashMap<Requirement, Capability>();
+                for (Entry<String, List<Blame>> entry : pkgs.m_requiredPkgs.entrySet())
+                {
+                    for (Blame blame : entry.getValue())
+                    {
+                        for (Requirement rbReq : rbReqs)
+                        {
+                            if (blame.m_reqs.get(blame.m_reqs.size() - 1).equals(rbReq))
+                            {
+                                rbMap.put(rbReq, getModuleCapability(blame.m_cap.getModule()));
+                                if (!blame.m_cap.getModule().isResolved())
+                                {
+                                    populateWireMap(
+                                        blame.m_cap.getModule(), modulePkgMap, wireMap);
+                                }
+                                break;
+                            }
+                        }
+                    }
+
+                    // Ignore modules that import themselves.
+                    for (Entry<Requirement, Capability> rbEntry : rbMap.entrySet())
+                    {
+                        if (!module.equals(rbEntry.getValue().getModule()))
+                        {
+                            moduleWires.add(
+                                new Wire(module,
+                                    rbEntry.getKey(),
+                                    rbEntry.getValue().getModule(),
+                                    rbEntry.getValue()));
+                        }
+                    }
+                }
+            }
+        }
+        return wireMap;
+    }
+
+    private static Capability getModuleCapability(Module module)
+    {
+        for (Capability cap : module.getCapabilities())
+        {
+            if (cap.getNamespace().equals(Capability.MODULE_NAMESPACE))
+            {
+                return cap;
+            }
+        }
+        return null;
+    }
+
     private class Packages
     {
         public final Map<String, Blame> m_exportedPkgs