private void addSavedUserCreatedNodes(
      ArtifactFragment parent,
      Resource instanceRes,
      Resource modelRes,
      Map<Resource, ArtifactFragment> instanceRes2AFMap,
      ReloRdfRepository repo) {
    ArtifactFragment childToAdd =
        SeqUtil.createAFForSavedResources(
            repo, modelRes, instanceRes, parent, (DiagramModel) getModel());
    if (childToAdd == null)
      logger.error("Could not create model for " + instanceRes + "in parent " + parent);

    // Add AF to parent and call same method with child
    int indexWhenSaved = findSavedIndex(repo, instanceRes);
    List<ArtifactFragment> childrenAddedToParent = parent.getShownChildren();
    if (parent instanceof DiagramModel)
      childrenAddedToParent = ((DiagramModel) parent).getChildren();
    else if (parent instanceof UserCreatedInstanceModel)
      childrenAddedToParent = ((NodeModel) parent).getChildren();

    int indexToAddAt = getCurrentIndex(indexWhenSaved, repo, childrenAddedToParent);
    parent.appendShownChild(childToAdd, indexToAddAt);
    instanceRes2AFMap.put(instanceRes, childToAdd);

    for (DiagramPolicy pol : childToAdd.getDiagramPolicies()) pol.readRDF(repo);

    addSavedContainee(childToAdd, instanceRes, instanceRes2AFMap, repo);
  }
 // this needs to run after the rest of the model has been added to the map
 private ArtifactFragment checkMapforRes(
     Map<Resource, ArtifactFragment> instanceRes2AFMap,
     Value cfTopRes,
     Resource cfInstanceModelRes) {
   ArtifactFragment retAF = null;
   for (ArtifactFragment resAF : instanceRes2AFMap.values()) {
     if (resAF.toString().equals(cfTopRes.toString())) {
       if (resAF instanceof MethodInvocationModel) retAF = resAF;
     }
   }
   return retAF;
 }
  private void addSavedCodeRes(
      ArtifactFragment parent,
      Resource codeRes,
      Map<Resource, ArtifactFragment> instanceRes2AFMap,
      ReloRdfRepository repo)
      throws Exception {
    ArtifactFragment childToAdd = SeqUtil.createChronoModelForResource(codeRes, parent, repo, this);
    if (childToAdd == null)
      throw new Exception("Could not create model for resource: " + codeRes.toString());

    if (parent instanceof DiagramModel) instanceResToAF.put(codeRes, childToAdd);

    //		Statement indexWhenSavedStmt = repo.getStatement(codeRes, RJCore.index, null);
    //		int indexWhenSaved = (indexWhenSavedStmt == null || indexWhenSavedStmt.getObject() == null)
    // ? -1 : Integer.parseInt(indexWhenSavedStmt.getObject().toString());
    int indexWhenSaved = findSavedIndex(repo, codeRes);
    //		int indexToAddAt = -1;

    List<ArtifactFragment> childrenAddedToParent = parent.getShownChildren();
    if (parent instanceof DiagramModel)
      childrenAddedToParent = ((DiagramModel) parent).getChildren();
    else if (parent instanceof NodeModel)
      childrenAddedToParent = ((NodeModel) parent).getChildren();
    //		for(ArtifactFragment addedChild : childrenAddedToParent) {
    //
    //			Resource addedChildSaveRes = addedChild.getInstanceRes();
    //			Statement addedChildIndexWhenSavedStmt = repo.getStatement(addedChildSaveRes, RJCore.index,
    // null);
    //			int addedChildIndexWhenSaved = -1;
    //			if(addedChildIndexWhenSavedStmt.getObject()!=null)
    //				addedChildIndexWhenSaved =
    // Integer.parseInt(addedChildIndexWhenSavedStmt.getObject().toString());
    //
    //			if(indexWhenSaved>-1 && indexWhenSaved<addedChildIndexWhenSaved)
    //				indexToAddAt = childrenAddedToParent.indexOf(addedChild);
    //		}
    int indexToAddAt = getCurrentIndex(indexWhenSaved, repo, childrenAddedToParent);
    parent.appendShownChild(childToAdd, indexToAddAt);
    instanceRes2AFMap.put(codeRes, childToAdd);

    for (DiagramPolicy pol : childToAdd.getDiagramPolicies()) {
      pol.readRDF(repo);
    }

    addSavedContainee(childToAdd, codeRes, instanceRes2AFMap, repo);
  }
  private int getCurrentIndex(
      int indexWhenSaved, ReloRdfRepository repo, List<ArtifactFragment> childrenAddedToParent) {
    for (ArtifactFragment addedChild : childrenAddedToParent) {

      Resource addedChildSaveRes = addedChild.getInstanceRes();
      Statement addedChildIndexWhenSavedStmt =
          repo.getStatement(addedChildSaveRes, RJCore.index, null);
      int addedChildIndexWhenSaved = -1;
      if (addedChildIndexWhenSavedStmt.getObject() != null)
        addedChildIndexWhenSaved =
            Integer.parseInt(addedChildIndexWhenSavedStmt.getObject().toString());

      if (indexWhenSaved > -1 && indexWhenSaved < addedChildIndexWhenSaved)
        return childrenAddedToParent.indexOf(addedChild);
    }
    return -1;
  }
 // TODO: is this implemented correctly?
 public static boolean removeArtFrag(ArtifactFragment childAF) {
   return childAF.getParentArt().removeShownChild(childAF);
 }