@@ -128,9 +128,13 @@ public static void ProcessQuery(ILinks<uint> links, Options options)
128128 {
129129 var substitutionLinks = substitutionInternalPatterns
130130 . Select ( pattern => ApplySolutionToPattern ( links , solution , pattern ) )
131+ . Where ( link => link != null )
132+ . Select ( link => new DoubletLink ( link ! ) )
131133 . ToList ( ) ;
132134 var restrictionLinks = restrictionInternalPatterns
133135 . Select ( pattern => ApplySolutionToPattern ( links , solution , pattern ) )
136+ . Where ( link => link != null )
137+ . Select ( link => new DoubletLink ( link ! ) )
134138 . ToList ( ) ;
135139
136140 var operations = DetermineOperationsFromPatterns ( restrictionLinks , substitutionLinks ) ;
@@ -449,7 +453,7 @@ private static IEnumerable<Dictionary<string, uint>> MatchPattern(
449453
450454 private static IEnumerable < Dictionary < string , uint > > RecursiveMatchSubPattern (
451455 ILinks < uint > links ,
452- Pattern pattern ,
456+ Pattern ? pattern ,
453457 uint linkId ,
454458 Dictionary < string , uint > currentSolution )
455459 {
@@ -553,8 +557,8 @@ private static bool DetermineIfSolutionIsNoOperation(
553557 List < Pattern > substitutions ,
554558 ILinks < uint > links )
555559 {
556- var substitutedRestrictions = restrictions . Select ( r => ApplySolutionToPattern ( links , solution , r ) ) . ToList ( ) ;
557- var substitutedSubstitutions = substitutions . Select ( s => ApplySolutionToPattern ( links , solution , s ) ) . ToList ( ) ;
560+ var substitutedRestrictions = restrictions . Select ( r => ApplySolutionToPattern ( links , solution , r ) ) . Where ( link => link != null ) . Select ( link => new DoubletLink ( link ! ) ) . ToList ( ) ;
561+ var substitutedSubstitutions = substitutions . Select ( s => ApplySolutionToPattern ( links , solution , s ) ) . Where ( link => link != null ) . Select ( link => new DoubletLink ( link ! ) ) . ToList ( ) ;
558562 substitutedRestrictions . Sort ( ( a , b ) => a . Index . CompareTo ( b . Index ) ) ;
559563 substitutedSubstitutions . Sort ( ( a , b ) => a . Index . CompareTo ( b . Index ) ) ;
560564 if ( substitutedRestrictions . Count != substitutedSubstitutions . Count ) return false ;
@@ -577,20 +581,27 @@ private static List<DoubletLink> ExtractMatchedLinks(
577581 foreach ( var pattern in patterns )
578582 {
579583 var appliedPattern = ApplySolutionToPattern ( links , solution , pattern ) ;
580- var matches = links . All ( appliedPattern ) ;
581- foreach ( var match in matches )
584+ if ( appliedPattern != null )
582585 {
583- matchedLinks . Add ( new DoubletLink ( match ) ) ;
586+ var matches = links . All ( appliedPattern ) ;
587+ foreach ( var match in matches )
588+ {
589+ matchedLinks . Add ( new DoubletLink ( match ) ) ;
590+ }
584591 }
585592 }
586593 return matchedLinks . Distinct ( ) . ToList ( ) ;
587594 }
588595
589- private static DoubletLink ApplySolutionToPattern (
596+ private static DoubletLink ? ApplySolutionToPattern (
590597 ILinks < uint > links ,
591598 Dictionary < string , uint > solution ,
592- Pattern pattern )
599+ Pattern ? pattern )
593600 {
601+ if ( pattern == null )
602+ {
603+ return null ;
604+ }
594605 if ( pattern . IsLeaf )
595606 {
596607 uint index = ResolveId ( links , pattern . Index , solution ) ;
@@ -604,8 +615,9 @@ private static DoubletLink ApplySolutionToPattern(
604615 var targetLink = ApplySolutionToPattern ( links , solution , pattern . Target ) ;
605616
606617 var any = links . Constants . Any ;
607- uint finalSource = sourceLink . Index == 0 ? any : sourceLink . Index ;
608- uint finalTarget = targetLink . Index == 0 ? any : targetLink . Index ;
618+ uint finalSource = sourceLink ? . Index ?? any ;
619+ uint finalTarget = targetLink ? . Index ?? any ;
620+
609621 if ( finalSource == 0 ) finalSource = any ;
610622 if ( finalTarget == 0 ) finalTarget = any ;
611623
@@ -629,7 +641,7 @@ private static void CreateOrUpdateLink(ILinks<uint> links, DoubletLink link, Opt
629641 if ( existingDoublet . Source != link . Source || existingDoublet . Target != link . Target )
630642 {
631643 LinksExtensions . EnsureCreated ( links , link . Index ) ;
632- options . ChangesHandler ? . Invoke ( null , new DoubletLink ( link . Index , nullConstant , nullConstant ) ) ;
644+ options . ChangesHandler ? . Invoke ( new DoubletLink ( link . Index , nullConstant , nullConstant ) , new DoubletLink ( link . Index , nullConstant , nullConstant ) ) ;
633645 links . Update ( new DoubletLink ( link . Index , anyConstant , anyConstant ) , link , ( b , a ) =>
634646 options . ChangesHandler ? . Invoke ( b , a ) ?? links . Constants . Continue ) ;
635647 }
@@ -668,10 +680,10 @@ private static void CreateOrUpdateLink(ILinks<uint> links, DoubletLink link, Opt
668680
669681 private static void RemoveLinks ( ILinks < uint > links , DoubletLink restriction , Options options )
670682 {
671- var linksToRemove = links . All ( restriction ) . ToList ( ) ;
683+ var linksToRemove = links . All ( restriction ) . Where ( l => l != null ) . Select ( l => new DoubletLink ( l ) ) . ToList ( ) ;
672684 foreach ( var link in linksToRemove )
673685 {
674- if ( link != null && links . Exists ( link [ 0 ] ) )
686+ if ( links . Exists ( link . Index ) )
675687 {
676688 links . Delete ( link , ( before , after ) =>
677689 options . ChangesHandler ? . Invoke ( before , after ) ?? links . Constants . Continue ) ;
@@ -743,17 +755,17 @@ private static Pattern CreatePatternFromLino(LinoLink lino)
743755 {
744756 if ( lino . Values == null || lino . Values . Count == 0 )
745757 {
746- return new Pattern ( lino . Id ) ;
758+ return new Pattern ( lino . Id ?? "" ) ;
747759 }
748760
749761 if ( lino . Values . Count == 2 )
750762 {
751763 var sourcePattern = CreatePatternFromLino ( lino . Values [ 0 ] ) ;
752764 var targetPattern = CreatePatternFromLino ( lino . Values [ 1 ] ) ;
753- return new Pattern ( lino . Id , sourcePattern , targetPattern ) ;
765+ return new Pattern ( lino . Id ?? "" , sourcePattern , targetPattern ) ;
754766 }
755767
756- return new Pattern ( lino . Id ) ;
768+ return new Pattern ( lino . Id ?? "" ) ;
757769 }
758770
759771 private static uint EnsureLinkCreated ( ILinks < uint > links , DoubletLink link , Options options )
0 commit comments