The position of an entity can be Head Concept, Relation, or Tail Concept. By default, the GetRelated()Opens in a new tab method returns all related concepts regardless of position and does not return relations. You can change this default by specifying a macro constant for the 8th parameter (positiontomatch). The available constants are as follows:
The following example separates the related head concepts and the related tail concepts. (Note that $$$USEPOSM means that the supplied string is the head concept in the CRC, and the related entities are the tail concepts.)
#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 }
SourceCountQuery
WRITE ##class(%iKnow.Queries.SourceAPI).GetCountByDomain(domId)," total sources",!!
ContainCRCQuery
SET crccount = ##class(%iKnow.Queries.CrcAPI).GetCountByEntities(domId,$LB("student pilot"))
WRITE crccount," CRCs contain 'student pilot'",!
DO ##class(%iKnow.Queries.CrcAPI).GetByEntities(.result,domId,$LB("student pilot"),1,crccount)
SET i=1
WHILE $DATA(result(i)) {
WRITE $LISTTOSTRING(result(i),",",1),!
SET i=i+1 }
SET relcount = ##class(%iKnow.Queries.EntityAPI).GetRelatedCount(domId,$LB("student pilot"))
WRITE !,relcount," entities are related to 'student pilot':",!
ListRelatedHeadsQuery
DO ##class(%iKnow.Queries.EntityAPI).GetRelated(.mresult,domId,$LB("student pilot"),1,relcount,"","",$$$USEPOSM)
WRITE !,"The following have 'student pilot' as a head:",!
SET j=1
WHILE $DATA(mresult(j)) {
WRITE $LISTTOSTRING(mresult(j),",",1),!
SET j=j+1 }
ListRelatedTailsQuery
DO ##class(%iKnow.Queries.EntityAPI).GetRelated(.sresult,domId,$LB("student pilot"),1,relcount,"","",$$$USEPOSS)
WRITE !,"The following have 'student pilot' as a tail:",!
SET k=1
WHILE $DATA(sresult(k)) {
WRITE $LISTTOSTRING(sresult(k),",",1),!
SET k=k+1 }