SISQSIM ;SIS/LM - Reverse $Query Simulation - 08/02/2006 ;;1.0;MISCELLANEOUS;; ; ; Note: This routine may be used to simulate the two-parameter form ; of the MUMPS $Query function. ; ; However, the author does not warrant the code to be free of errors, ; or to meet any specific need. ; ; The author will not be liable for any direct or indirect consequence ; arising from the use of this code. ; Q Q(X,P) ;[Public] Simulate reverse $query ; $$Q(X) = $Q(@X) ; Q:'$L($G(X)) "" N Y,Z S Y=$$GRO(X),Z=$$LD(Y) Q $S($G(P)=-1:$S(X=Z!$$O(X,Z):Y,1:Z),1:$Q(@X)) ;Coding in progress ; GRO(X) ;Generalized reverse $order of @X ; If reverse $order subscript is non-empty return the full $name through ; that subscript level. ; ; If reverse $order is the empty string, back up one subscript level, ; and if the parent node is defined, return it. Otherwise recurse. ; I $L($G(X)) N L,S S L=$QL(X) I L S S=$O(@X,-1) E Q "" S X=$NA(@X,L-1) Q $S(S]"":$NA(@X@(S)),$D(@X)#2:$NA(@X),1:$$GRO(X)) ; LD(X) ;Last descendant of @X ; If the node named in X has descendants, return the last descendant ; (deepest sub-node). Otherwise return the node named in X. ; ; X=[Required] node to check for descendants ($name format) ; ; Example of usage - ; ; >WRITE $$LD($NA(^DD(0))) ; ^DD(0,"SB",.3,20) ; I $L($G(X)) N S S S=$O(@X@(""),-1) I S]"" Q $$LD($NA(@X@(S))) Q $G(X) ; O(A,B) ;[Private] - Return '1' iff B follows A in $order, where ... ; ; A=[Required] GVN or LVN in $name format ; B=[Required] GVN or LVN in $name format ; S A=$G(A),B=$G(B) I $L(A),$L(B) E Q $L(B)>0 ; Different names - Q:$NA(@A,0)]$NA(@B,0) 0 Q:$NA(@B,0)]$NA(@A,0) 1 ; Same name (the usual case) - N I,L,S1,S2,Y S L=$$MAX($QL(A),$QL(B)),Y=0 ; Test first subscript that differs - F I=1:1:L S S1=$QS(A,I),S2=$QS(B,I) I '(S1=S2) S Y=$$F(S1,S2) Q Q Y ; F(A,B) ;[Private] - Return '1' iff B follows A in $order, where ... ; ; A=[Required] string (one subscript value) ; B=[Required] string (one subscript value) ; S A=$G(A),B=$G(B) Q $S(A=+A&(B=+B):B>A,A=+A&(B]""):1,B=+B&(A]""):0,1:B]A) ; MAX(X,Y) ;[Private] - Return the maximum of X or Y (numbers) ; S X=+$G(X),Y=+$G(Y) Q $S(X>Y:X,1:Y) ; TEST ;[Public] Test $$Q(...,-1) on a platform that supports this two-parameter ; form (reverse $query). ; ; No output means that the test succeeded. ; N X,Y S (X,Y)=$NA(^DD("VERSION")) ;Last defined node in ^DD F S X=$Q(@X,-1) Q:X="" S Y=$$Q(Y,-1) I '(X=Y) W !,X,!,Y,! Q ;Discrepancy found Q GTEST ;[Public] Test $$Q(...,-1) on a platform that DOES NOT support this form. ; No output means that the test succeeded. ; N X,Y S (X,Y)=$NA(^DD("VERSION")) ;Last defined node in ^DD F S Y=X,X=$$Q(X,-1) Q:X="" I '($Q(@X)=Y) W !,X,!,Y,! Q ;Discrepancy found Q