EntityValue 평가 동작

Sep 29 2020

의 행동에 의아해하며 EntityValue평가되지 않은 것으로 나타났습니다. 다음은 최소한의 예입니다. 다음 사용자 정의를 고려하십시오 EntityStore.

store = EntityStore[{
   "Group" -> <|
     "Entities" -> <|
       "GroupA" -> <|
         "name" -> "Group A",
         "size" -> 2,
         "members" -> {
           Entity["Individual", "Claus"],
           Entity["Individual", "Andrea"]
           }
         |>,
       "GroupB" -> <|
         "name" -> "Group B",
         "size" -> 2,
         "members" -> {
           Entity["Individual", "John"],
           Entity["Individual", "Claudia"]
           }
         |>
       |>
     |>,
   "Individual" -> <|
     "Entities" -> <|
       "Claus" -> <|"name" -> "Claus Smith", "age" -> 25|>,
       "Andrea" -> <|"name" -> "Andrea Fisher", "age" -> 35|>,
       "John" -> <|"name" -> "John Allen", "age" -> 45|>,
       "Claudia" -> <|"name" -> "Claudia Wilson", "age" -> 60|>
       |>
     |>
   }
  ]

등록합시다 :

EntityRegister[store]

그런 다음 쿼리하십시오.

assoc = EntityValue["Group", "PropertyAssociation"]

예상대로 제공됩니다.

{<|
  EntityProperty["Group", "members"] -> {Entity["Individual", "Claus"], Entity["Individual", "Andrea"]}, 
  EntityProperty["Group", "name"] -> "Group A", 
  EntityProperty["Group", "size"] -> 2
|>, 
<|
  EntityProperty["Group", "members"] -> {Entity["Individual", "John"], Entity["Individual", "Claudia"]}, 
  EntityProperty["Group", "name"] -> "Group B", 
  EntityProperty["Group", "size"] -> 2
|>}

이제 개별 속성을 자체 속성 연결로 대체하여이 연결을 계층 적으로 만들고 싶다고 가정 해 보겠습니다.

ds = assoc /. {Entity["Individual", n_] :> EntityValue[Entity["Individual", n], "PropertyAssociation"]}

이것은 예기치 않게 나에게 내부 EntityValue 표현식을 평가하지 않은 상태로 둡니다.

{<|
  EntityProperty["Group", "members"] -> {EntityValue[Entity["Individual", "Claus"], "PropertyAssociation"], EntityValue[Entity["Individual", "Andrea"], "PropertyAssociation"]}, 
  EntityProperty["Group", "name"] -> "Group A", 
  EntityProperty["Group", "size"] -> 2|>,
 <|EntityProperty["Group", "members"] -> {EntityValue[Entity["Individual", "John"],  "PropertyAssociation"], EntityValue[Entity["Individual", "Claudia"], "PropertyAssociation"]}, 
  EntityProperty["Group", "name"] -> "Group B", 
  EntityProperty["Group", "size"] -> 2
|>
}

무슨 일이야? 이상하게도 내부 EntityValue표현식은 어떻게 든 직접 쿼리 할 때 평가됩니다.

Lookup[ds, EntityProperty["Group", "members"]]

올바르게 산출

{{<|EntityProperty["Individual", "age"] -> 25, 
   EntityProperty["Individual", "name"] -> "Claus Smith"|>,
  <|EntityProperty["Individual", "age"] -> 35, 
   EntityProperty["Individual", "name"] -> "Andrea Fisher"|>},
 {<|EntityProperty["Individual", "age"] -> 45, 
   EntityProperty["Individual", "name"] -> "John Allen"|>,
  <|EntityProperty["Individual", "age"] -> 60, 
   EntityProperty["Individual", "name"] -> "Claudia Wilson"
|>}}

이것이 왜 나를 괴롭히는가? 이러한 종류의 대체 메커니즘을 사용하여 계층 적 엔터티 저장소에서 계층 적 연결 식을 만들고 싶습니다. Continent-> Country-> Region-> City-> Neighbourhood "또는 이와 유사한 일대 다 관계의 계단식을 상상해보십시오. 하위 항목 목록을 반복적으로 대체하여 연관 계층을 구축하고 싶습니다. 그들의 재산 협회에 의해.

그렇다면 평가를 EntityValue어떻게 제어 할 수 있습니까?

감사.

답변

2 Jean-Pierre Sep 29 2020 at 04:53
ds = Replace[
  assoc, {Entity["Individual", n_] :> 
    EntityValue[Entity["Individual", n], "PropertyAssociation"]}, 
  Infinity]

또는 사용할 규칙 목록을 만듭니다 (사용 Normal) ReplaceAll.

ds = Normal[assoc] /. 
 Entity["Individual", n_] :> 
  EntityValue[Entity["Individual", n], "PropertyAssociation"]