Example #1
0
  @Override
  public void slotChanged(ChunkEvent ce) {
    IChunk iChunk = ce.getSource();
    Object oldValue = ce.getOldSlotValue();
    Object newValue = ce.getNewSlotValue();

    if (LOGGER.isDebugEnabled())
      LOGGER.debug(iChunk + "." + ce.getSlotName() + "=" + newValue + " (was " + oldValue + ")");

    /*
     * we can only do this if ISubsymbolicChunk is ISubsymbolicChunk4
     */
    if (!(iChunk.getSubsymbolicChunk() instanceof ISubsymbolicChunk4)) {
      if (LOGGER.isWarnEnabled())
        LOGGER.warn(
            "Can only adjust associative links if the chunk's subsymbolic is derived from ISubsymbolicChunk4");
      return;
    }

    /*
     * I chunk is the owner that references J
     */
    ISubsymbolicChunk4 sscI = (ISubsymbolicChunk4) iChunk.getSubsymbolicChunk();

    /*
     * first the old chunk value..
     */
    if (oldValue instanceof IChunk) {
      IChunk jChunk = (IChunk) oldValue;
      if (jChunk.getSubsymbolicChunk() instanceof ISubsymbolicChunk4) {
        ISubsymbolicChunk4 sscJ = (ISubsymbolicChunk4) jChunk.getSubsymbolicChunk();

        Link sJI = sscJ.getIAssociation(iChunk);

        if (sJI != null) {
          sJI.decrement();
          if (sJI.getCount() == 0) {
            if (LOGGER.isDebugEnabled())
              LOGGER.debug("Removing link between " + iChunk + " and " + jChunk + " : " + sJI);
            sscI.removeLink(sJI);
            sscJ.removeLink(sJI);
          } else if (LOGGER.isDebugEnabled())
            LOGGER.debug(
                "Multiple links established between "
                    + iChunk
                    + " and "
                    + jChunk
                    + " decrementing : "
                    + sJI);
        }
      } else if (LOGGER.isWarnEnabled())
        LOGGER.warn(
            "old value " + jChunk + " doesn't have a ISubsymbolicChunk4, nothing to be done");
    }

    /*
     * now for the new one
     */
    if (newValue instanceof IChunk) {
      IChunk jChunk = (IChunk) newValue;
      if (jChunk.getSubsymbolicChunk() instanceof ISubsymbolicChunk4) {
        ISubsymbolicChunk4 sscJ = (ISubsymbolicChunk4) jChunk.getSubsymbolicChunk();

        Link sJI = sscJ.getIAssociation(iChunk);
        /*
         * is this a new linkage?
         */
        if (sJI == null) {
          sJI = new Link(jChunk, iChunk);
          if (LOGGER.isDebugEnabled())
            LOGGER.debug("Adding link between " + iChunk + " and " + jChunk + " : " + sJI);
          sscI.addLink(sJI);
          sscJ.addLink(sJI);
        } else {
          sJI.increment(); // not new, but we need to increment the link
          if (LOGGER.isDebugEnabled())
            LOGGER.debug(
                "Link already established between "
                    + iChunk
                    + " and "
                    + jChunk
                    + " incrementing : "
                    + sJI);
        }
      } else if (LOGGER.isWarnEnabled())
        LOGGER.warn(
            "new value " + jChunk + " doesn't have a ISubsymbolicChunk4, nothing to be done");
    }
  }