<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -704,6 +704,61 @@ module ActiveFacts
         @constellation.Reading(fact_type, fact_type.all_reading.size, :role_sequence =&gt; role_sequence, :text =&gt; reading_words*&quot; &quot;)
       end
 
+      def role_sequence_for_matched_reading(fact_type, clause)
+        # When we have existing clauses that match, we might have matched using additional adjectives.
+        # These adjectives have been removed from the phrases. If there are any remaining adjectives,
+        # we need to make a new RoleSequence, otherwise we can use the existing one.
+        kind, qualifiers, phrases, context = clause
+        role_phrases = []
+        role_sequence = nil
+        reading_words = []
+        new_role_sequence_needed = false
+        phrases.each do |phrase|
+          if phrase.is_a?(Hash)
+            role_phrases &lt;&lt; phrase
+            reading_words &lt;&lt; &quot;{#{phrase[:role_ref].ordinal}}&quot;
+            new_role_sequence_needed = true if phrase[:leading_adjective] ||
+                phrase[:trailing_adjective] ||
+                phrase[:role_name]
+          else
+            reading_words &lt;&lt; phrase
+            false
+          end
+        end
+
+        reading_text = reading_words*&quot; &quot;
+        if new_role_sequence_needed
+          role_sequence = @constellation.RoleSequence(:new)
+          #extra_adjectives = []
+          role_phrases.each_with_index do |rp, i|
+            role_ref = @constellation.RoleRef(role_sequence, i, :role =&gt; rp[:role_ref].role)
+            if a = rp[:leading_adjective]
+              role_ref.leading_adjective = a
+              #extra_adjectives &lt;&lt; a+&quot;-&quot;
+            end
+            if a = rp[:trailing_adjective]
+              role_ref.trailing_adjective = a
+              #extra_adjectives &lt;&lt; &quot;-&quot;+a
+            end
+            if a = rp[:role_name]
+              #extra_adjectives &lt;&lt; &quot;(as #{a})&quot;
+            end
+          end
+          #puts &quot;Making new role sequence for #{reading_words*&quot; &quot;} due to #{extra_adjectives.inspect}&quot;
+        else
+          # Use existing RoleSequence
+          role_sequence = role_phrases[0][:role_ref].role_sequence
+          if role_sequence.all_reading.detect{|r| r.text == reading_text }
+            #puts &quot;No need to re-create identical reading for #{reading_words*&quot; &quot;}&quot;
+            return role_sequence
+          else
+            #puts &quot;Using existing role sequence for #{reading_words*&quot; &quot;}&quot;
+          end
+        end
+        @constellation.Reading(fact_type, fact_type.all_reading.size, :role_sequence =&gt; role_sequence, :text =&gt; reading_words*&quot; &quot;)
+        role_sequence
+      end
+
       def make_default_identifier_for_fact_type(fact_type)
         pc = @constellation.PresenceConstraint(
             :new,
@@ -724,8 +779,8 @@ module ActiveFacts
           resolve_players(clauses)
 
           cbt = clauses_by_terms(clauses)
-          player_names = cbt.keys[0]
-          raise &quot;Subsequent fact type clauses must involve the same players as the first (#{player_names*', '})&quot; unless cbt.size == 1
+          terms = cbt.keys[0]     # This is the sorted array of player's names
+          raise &quot;Subsequent fact type clauses must involve the same players as the first (#{terms*', '})&quot; unless cbt.size == 1
 
           # Find whether any clause matches an existing fact type.
           # Ensure that for any such clauses all match the same fact type.
@@ -740,10 +795,9 @@ module ActiveFacts
 
           # We know the role players are the same in all clauses, but we haven't matched them up.
           # If any player is duplicated and isn't used with consistent adjectives, we must use
-          # loose adjective binding or require subscripts
-          terms = cbt.keys[0]     # This is the sorted array of player's names
+          # loose adjective binding or require subscripts.
           if terms.uniq.size &lt; terms.size
-            raise &quot;REVISIT: disambiguate duplicate roles of (#{player_names*', '})&quot;
+            raise &quot;REVISIT: disambiguate duplicate roles of (#{terms*', '})&quot;
           end
 
           # Make a new fact type if we didn't match any reading
@@ -762,68 +816,16 @@ module ActiveFacts
             make_reading_for_fact_type(fact_type, clause)
           end
 
+          matched_clauses.each do |clause|
+            role_sequence_for_matched_reading(fact_type, clause)
+          end
+
           if matched_clauses.size == 0
             # REVISIT: This isn't the thing to do long term; it needs to be added if we find no other constraint
             make_default_identifier_for_fact_type(fact_type)
             @constellation.EntityType(@vocabulary, &quot;Blah&quot;, :fact_type =&gt; fact_type)
           end
 
-          matched_clauses.each do |clause|
-            # When we have existing clauses that match, we might have matched using additional adjectives.
-            # These adjectives have been removed from the phrases. If there are any remaining adjectives,
-            # we need to make a new RoleSequence, otherwise we can use the existing one.
-            kind, qualifiers, phrases, context = clause
-            role_phrases = []
-            role_sequence = nil
-            reading_words = []
-            new_role_sequence_needed = false
-            phrases.each do |phrase|
-              if phrase.is_a?(Hash)
-                role_phrases &lt;&lt; phrase
-                reading_words &lt;&lt; &quot;{#{phrase[:role_ref].ordinal}}&quot;
-                new_role_sequence_needed = true if phrase[:leading_adjective] ||
-                    phrase[:trailing_adjective] ||
-                    phrase[:role_name]
-              else
-                reading_words &lt;&lt; phrase
-                false
-              end
-            end
-
-            reading_text = reading_words*&quot; &quot;
-            if new_role_sequence_needed
-              role_sequence = @constellation.RoleSequence(:new)
-              #extra_adjectives = []
-              role_phrases.each_with_index do |rp, i|
-                role_ref = @constellation.RoleRef(role_sequence, i, :role =&gt; rp[:role_ref].role)
-                if a = rp[:leading_adjective]
-                  role_ref.leading_adjective = a
-                  #extra_adjectives &lt;&lt; a+&quot;-&quot;
-                end
-                if a = rp[:trailing_adjective]
-                  role_ref.trailing_adjective = a
-                  #extra_adjectives &lt;&lt; &quot;-&quot;+a
-                end
-                if a = rp[:role_name]
-                  #extra_adjectives &lt;&lt; &quot;(as #{a})&quot;
-                end
-              end
-              #puts &quot;Making new role sequence for #{reading_words*&quot; &quot;} due to #{extra_adjectives.inspect}&quot;
-            else
-              # Use existing RoleSequence
-              role_sequence = role_phrases[0][:role_ref].role_sequence
-              if role_sequence.all_reading.detect{|r| r.text == reading_text }
-                role_sequence = nil
-                #puts &quot;No need to re-create identical reading for #{reading_words*&quot; &quot;}&quot;
-              else
-                #puts &quot;Using existing role sequence for #{reading_words*&quot; &quot;}&quot;
-              end
-            end
-            if role_sequence
-              @constellation.Reading(fact_type, fact_type.all_reading.size, :role_sequence =&gt; role_sequence, :text =&gt; reading_words*&quot; &quot;)
-            end
-          end
-
 =begin
           #
           # The first step is to find all role references and definitions in the phrases</diff>
      <filename>lib/activefacts/cql/compiler.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>857f956b1eb243140907d3338c2181d0aec0c827</id>
    </parent>
  </parents>
  <author>
    <name>Clifford Heath</name>
    <email>clifford.heath@gmail.com</email>
  </author>
  <url>http://github.com/cjheath/activefacts/commit/dc09dcb1ec21060076b5eb908f49ad84dd9945a4</url>
  <id>dc09dcb1ec21060076b5eb908f49ad84dd9945a4</id>
  <committed-date>2009-10-30T05:46:43-07:00</committed-date>
  <authored-date>2009-10-30T05:46:43-07:00</authored-date>
  <message>Factored out some fact type matching code</message>
  <tree>1c2d4dc6374d09cf3a0014bb6bef518322ec3425</tree>
  <committer>
    <name>Clifford Heath</name>
    <email>clifford.heath@gmail.com</email>
  </committer>
</commit>
