Counting Sources that Contain a CRC
Counting Sources that Contain a CRC
The following program example returns the count of sources that contain the specified CRCs. To specify CRCs to the GetCountByCrcs()Opens in a new tab method, you must specify each CRC as a %List (using $LB), and then group these CRCs together as a %List. This is shown in the following example:
#include %IKPublic
DomainCreateOrOpen
SET dname="mydomain"
IF (##class(%iKnow.Domain).NameIndexExists(dname))
{ WRITE "The ",dname," domain already exists",!
SET domoref=##class(%iKnow.Domain).NameIndexOpen(dname)
GOTO DeleteOldData }
ELSE
{ WRITE "The ",dname," domain does not exist",!
SET domoref=##class(%iKnow.Domain).%New(dname)
DO domoref.%Save()
WRITE "Created the ",dname," domain with domain ID ",domoref.Id,!
GOTO ListerAndLoader }
DeleteOldData
SET stat=domoref.DropData()
IF stat { WRITE "Deleted the data from the ",dname," domain",!!
GOTO ListerAndLoader }
ELSE { WRITE "DropData error ",$System.Status.DisplayError(stat)
QUIT}
ListerAndLoader
SET domId=domoref.Id
SET flister=##class(%iKnow.Source.SQL.Lister).%New(domId)
SET myloader=##class(%iKnow.Source.Loader).%New(domId)
QueryBuild
SET myquery="SELECT TOP 100 ID AS UniqueVal,Type,NarrativeFull FROM Aviation.Event"
SET idfld="UniqueVal"
SET grpfld="Type"
SET dataflds=$LB("NarrativeFull")
UseLister
SET stat=flister.AddListToBatch(myquery,idfld,grpfld,dataflds)
IF stat '= 1 {WRITE "The lister failed: ",$System.Status.DisplayError(stat) QUIT }
UseLoader
SET stat=myloader.ProcessBatch()
IF stat '= 1 {WRITE "The loader failed: ",$System.Status.DisplayError(stat) QUIT }
CRCCount
SET numSrcD=##class(%iKnow.Queries.SourceQAPI).GetCountByDomain(domId)
SET mycrcs=$LB($LB("leading edge","of","wing"),$LB("leading edge","of","right wing"),
$LB("leading edge","of","left wing"),$LB("leading edges","of","wings"),
$LB("leading edges","of","both wings"))
SET numSrc=##class(%iKnow.Queries.SourceAPI).GetCountByCrcs(domId,mycrcs)
WRITE "From ",numSrcD," indexed sources there are ",!
WRITE numSrc," sources containing one or more of the following CRCs:",!
FOR i=1:1:$LISTLENGTH(mycrcs) {
WRITE $LISTTOSTRING($LIST(mycrcs,i)," "),!
}
The GetCountByCrcs() method returns the count of sources that contain any of the specified CRCs.