hyf

Context-aware query service for Radroots
git clone https://radroots.dev/git/hyf.git
Log | Files | Refs | README | LICENSE

question_plan.mojo (1845B)


      1 from std.collections import List
      2 
      3 from hyf_assist.questions import Question
      4 
      5 
      6 @fieldwise_init
      7 struct QuestionStage(Copyable, Movable):
      8     var stage_id: String
      9     var depends_on: List[String]
     10     var questions: List[Question]
     11 
     12 
     13 def question_stage(
     14     stage_id: String, depends_on: List[String], questions: List[Question]
     15 ) raises -> QuestionStage:
     16     if stage_id.strip() == "":
     17         raise Error("question stage requires an id")
     18     if len(questions) == 0:
     19         raise Error("question stage requires at least one question")
     20     var deps = List[String]()
     21     for dependency in depends_on:
     22         deps.append(String(dependency))
     23     var copied = List[Question]()
     24     for question in questions:
     25         copied.append(question.copy())
     26     return QuestionStage(
     27         stage_id=String(stage_id), depends_on=deps^, questions=copied^
     28     )
     29 
     30 
     31 def _reaches(
     32     stages: List[QuestionStage], start: String, target: String
     33 ) -> Bool:
     34     var stack = List[String]()
     35     stack.append(String(start))
     36     var visited = List[String]()
     37     while len(stack) > 0:
     38         var current = stack.pop()
     39         if current == target:
     40             return True
     41         var already = False
     42         for seen in visited:
     43             if seen == current:
     44                 already = True
     45         if already:
     46             continue
     47         visited.append(String(current))
     48         for stage in stages:
     49             if stage.stage_id == current:
     50                 for dependency in stage.depends_on:
     51                     stack.append(String(dependency))
     52     return False
     53 
     54 
     55 def stages_are_acyclic(stages: List[QuestionStage]) -> Bool:
     56     for stage in stages:
     57         for dependency in stage.depends_on:
     58             if _reaches(stages, dependency, stage.stage_id):
     59                 return False
     60     return True
     61 
     62 
     63 def independent_questions_share_one_state() -> Bool:
     64     return True