/*
  * Returns the process:components value for a Control Construct. If it is
  * null, it will be set to list:nil, and this will be returned.
  */
 private OWLIndividual getComponents(OWLIndividual inst) {
   OWLIndividual components =
       (OWLIndividual) OWLUtils.getNamedSlotValue(inst, "process:components", okb);
   if (components == null) {
     components = okb.getOWLIndividual("list:nil");
     OWLUtils.setNamedSlotValue(inst, "process:components", components, okb);
   }
   return components;
 }
  /** Creates the tree for a composite process instance, and returns the root node of this tree. */
  private OWLSTreeNode getCompositionTreeNodes(OWLIndividual inst) {
    OWLIndividual i = (OWLIndividual) OWLUtils.getNamedSlotValue(inst, "process:composedOf", okb);

    if (i != null) {
      OWLSTreeNodeInfo newni = new OWLSTreeNodeInfo(i, okb);
      OWLSTreeNode node = createTree(newni, inst, new HashSet());
      // PerformTreeMapper mapper = PerformTreeMapper.getInstance();
      // mapper.printAll();
      return node;
    } else return null;
  }
  /**
   * Creates the tree from a ControlConstruct. It returns the root node created even though the
   * caller already has it, since the method is recursive, and this is needed internally. This
   * method is only called when the user switches to a different composite process, to recreate the
   * whole tree.
   *
   * <p>Note: This method is not used to create RootNode instances - those are created directly
   * (i.e. using new RootNode()).
   *
   * <p>We also generate Vectors of unconditional predecessor Performs, and associate those with the
   * Perform constructs using the PerformTreeMapper class. See that class for more info.
   */
  private OWLSTreeNode createTree(OWLSTreeNodeInfo ni, OWLIndividual cp, Set preds) {
    OWLIndividual inst = ni.getInstance();
    if (inst == null) {
      System.out.println("ERROR! OWLSTreeMapper.createTree(): inst is null!");
      return null;
    }

    OWLSTreeNode newnode = OWLSTreeNodeFactory.createTreeNode(ni);
    // System.out.println("Building tree for " + newnode.toString());

    if (newnode instanceof ProduceNode) {
      // Store the mapping from produce to composite process in the
      // PerformTreeMapper. This will allow us to generate the Produce's
      // parents, which we need for the ProducedBindingWidget.
      OWLIndividual produce = newnode.getInstance();
      PerformTreeMapper mapper = PerformTreeMapper.getInstance();
      mapper.put(produce, cp);
      mapper.addPredecessors(produce, preds);
    } else if (newnode instanceof PerformNode) {
      // Store the mapping from perform to composite process in the
      // PerformTreeMapper. This will allow us to generate the Performs's
      // parents, which we need for the HasDataFromWidget.
      OWLIndividual perform = newnode.getInstance();
      PerformTreeMapper mapper = PerformTreeMapper.getInstance();
      mapper.put(perform, cp);
      mapper.addPredecessors(perform, preds);
      preds.add(perform);
      // mapper.printAll();
    } else if (newnode instanceof SequenceNode) {
      OWLIndividual components = getComponents(inst);
      // OWLSList clist = new OWLSList(components, OWLSList.CC_LIST, okb);
      OWLSList clist = new OWLSList(inst, okb);
      while (clist.hasNext()) {
        OWLIndividual i = (OWLIndividual) clist.next();
        if (i == null) System.out.println("instance in list is null!");
        OWLSTreeNodeInfo newni = new OWLSTreeNodeInfo(i, okb);
        newnode.add(createTree(newni, cp, preds));
      }
    } else if (newnode instanceof SplitNode || newnode instanceof ChoiceNode) {
      OWLIndividual components = getComponents(inst);
      // OWLSList clist = new OWLSList(components, OWLSList.CC_BAG, okb);
      OWLSList clist = new OWLSList(inst, okb);
      while (clist.hasNext()) {
        HashSet mypreds = new HashSet(preds);
        OWLIndividual i = (OWLIndividual) clist.next();
        OWLSTreeNodeInfo newni = new OWLSTreeNodeInfo(i, okb);
        newnode.add(createTree(newni, cp, mypreds));
      }
    } else if (newnode instanceof SplitJoinNode || newnode instanceof AnyOrderNode) {
      OWLIndividual components = getComponents(inst);
      // OWLSList clist = new OWLSList(components, OWLSList.CC_BAG, okb);
      OWLSList clist = new OWLSList(inst, okb);
      HashSet predsIn = new HashSet(preds);
      while (clist.hasNext()) {
        OWLIndividual i = (OWLIndividual) clist.next();
        OWLSTreeNodeInfo newni = new OWLSTreeNodeInfo(i, okb);
        HashSet mypreds = new HashSet(predsIn);
        newnode.add(createTree(newni, cp, mypreds));
        preds.addAll(mypreds);
      }
    }
    // We put the then-node first and the else-node second as a convention
    // This may need to be handled better (marking the nodes somehow).
    else if (newnode instanceof IfThenElseNode) {
      OWLIndividual thenc = (OWLIndividual) OWLUtils.getNamedSlotValue(inst, "process:then", okb);
      OWLSTreeNodeInfo thenni = new OWLSTreeNodeInfo(thenc, okb);
      HashSet mypreds = new HashSet(preds);
      newnode.add(createTree(thenni, cp, mypreds));
      OWLIndividual elsec = (OWLIndividual) OWLUtils.getNamedSlotValue(inst, "process:else", okb);
      OWLSTreeNodeInfo elseni = new OWLSTreeNodeInfo(elsec, okb);
      if (elsec != null) {
        mypreds = new HashSet(preds);
        newnode.add(createTree(elseni, cp, mypreds));
      }
    } else if (newnode instanceof RepeatWhileNode) {
      OWLIndividual whileProcess =
          (OWLIndividual) OWLUtils.getNamedSlotValue(inst, "process:whileProcess", okb);
      OWLSTreeNodeInfo newni = new OWLSTreeNodeInfo(whileProcess, okb);
      HashSet mypreds = new HashSet(preds);
      newnode.add(createTree(newni, cp, mypreds));
    } else if (newnode instanceof RepeatUntilNode) {
      OWLIndividual untilProcess =
          (OWLIndividual) OWLUtils.getNamedSlotValue(inst, "process:untilProcess", okb);
      OWLSTreeNodeInfo newni = new OWLSTreeNodeInfo(untilProcess, okb);
      newnode.add(createTree(newni, cp, preds));
    } else {
      System.out.println("Unsupported control construct");
      return null;
    }
    return newnode;
  }