Exemplo n.º 1
0
  @Override
  public boolean unify(Hashtable<Variable, Variable> varsTbl) {
    PrologObject closure = getParam(1);
    ConsCell params = (ConsCell) getParams().getTail();

    if (closure instanceof Variable) closure = ((Variable) closure).getObject();

    PrologObject goal;

    if (closure instanceof Atom) {
      if (((Atom) closure).equals(Atom.createAtom(","))) {
        goal = (ConsCell) params;
      } else {
        goal = new Functor((Atom) closure);
        ((Functor) goal).setParams((ConsCell) params);

        if (BuiltInFactory.isBuiltIn(((Functor) goal).getName()))
          goal = new BuiltInPredicate(((Functor) goal));
      }
    } else if (closure instanceof Functor) {
      Atom functorName = ((Functor) closure).getAtom();

      if (functorName.equals(Atom.createAtom(",/1"))) {
        goal = (Functor) closure;

        ConsCell params1 = (ConsCell) ((Functor) closure).getParams().copy(true);

        ((Functor) closure).getParams().unify(params1, varsTbl);

        goal = ConsCell.append(params1, (ConsCell) params);
      } else {
        goal = (Functor) closure;

        ConsCell params1 = (ConsCell) ((Functor) goal).getParams().copy(true);

        Functor goal1 = new Functor(((Functor) goal).getName(), params1);

        goal.unify(goal1, varsTbl);

        ConsCell newParams = ConsCell.append(params1, (ConsCell) params);

        goal1.setParams(newParams);

        goal = goal1;

        if (BuiltInFactory.isBuiltIn(((Functor) goal).getName()))
          goal = new BuiltInPredicate(((Functor) goal));
      }
    } else if (closure instanceof List) {
      if (((List) closure).getHeight() + params.getHeight() > 2)
        throw new JIPExistenceException(
            "procedure", (new Functor("./3", null)).getPredicateIndicator());
      goal = new List(closure, params);
    } else if (closure instanceof ConsCell) {
      if (((ConsCell) closure).getHeight() + params.getHeight() > 2)
        throw new JIPExistenceException(
            "procedure", (new Functor("(,)/3", null)).getPredicateIndicator());

      goal = new ConsCell(closure, params);
    } else if (closure == null) {
      throw new JIPInstantiationException();
    } else {
      throw new JIPTypeException(JIPTypeException.CALLABLE, closure);
    }

    //		if(wam == null)
    //		{
    //			wam = getNewWAM();
    //			if(wam.query(goal))
    //				return true;
    //			else
    //			{
    //				wam.closeQuery();
    //				wam = null;
    //			}
    //		}
    //		else
    //		{
    //			if(wam.nextSolution())
    //				return true;
    //			else
    //			{
    //				wam.closeQuery();
    //				wam = null;
    //			}
    //
    //		}
    //
    //		return false;

    // estrae il nodo corrente
    final WAM.Node curNode = getWAM().getCurNode();

    curNode.m_callList =
        new ConsCell(curNode.m_callList.m_head, new ConsCell(goal, curNode.m_callList.m_tail));

    return true;
  }
Exemplo n.º 2
0
  public static final void pack(List fileList, String destinationFile, final JIPEngine engine)
      throws FileNotFoundException, IOException {
    InputStream ins = null;

    File outf = new File(destinationFile);
    if (!outf.isAbsolute()) outf = new File(engine.getSearchPath(), destinationFile);

    final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(outf));

    String strPath = null;
    int len = fileList.getHeight();
    for (int i = 1; i <= len; i++) {
      try {
        PrologObject path = getRealTerm(fileList.getTerm(i));
        if (!(path instanceof Atom))
          throw new JIPTypeException(JIPTypeException.ATOM_OR_STRING, path);

        strPath = ((Atom) path).getName();
        String strFileName[] = new String[1];
        String strCurDir[] = new String[1];
        ins =
            StreamManager.getStreamManager()
                .getInputStream(strPath, engine.getSearchPath(), strFileName, strCurDir);

        String file = new File(strPath).getName();
        final int nPos = file.lastIndexOf('.');
        file = new StringBuilder(file.substring(0, nPos)).append(".jip").toString();
        PrologParser parser =
            new PrologParser(
                new ParserReader(new PushbackLineNumberInputStream(ins)),
                engine.getOperatorManager(),
                engine,
                strPath);

        try {
          PrologObject term;

          while ((term = parser.parseNext()) != null) {
            out.writeObject(term);
          }

          ins.close();
          ins = null;
        } catch (IOException ex) {
          ins.close();
          ins = null;
          out.close();

          ex.printStackTrace();
          throw new JIPJVMException(ex);
        }
      } catch (FileNotFoundException ex) {
        try {
          if (ins != null) ins.close();

          out.close();

        } catch (IOException ex1) {
        }
        ;

        throw JIPExistenceException.createSourceSynkException(Atom.createAtom(strPath));
      } catch (IOException ex) {
        try {
          if (ins != null) ins.close();
        } catch (IOException ex1) {
        }

        throw new JIPJVMException(ex);
      } catch (SecurityException ex) {
        try {
          if (ins != null) ins.close();
        } catch (IOException ex1) {
        }

        throw new JIPPermissionException("access", "source_sink", Atom.createAtom(strPath));
      } catch (JIPRuntimeException ex) {
        ex.m_strFileName = strPath;

        throw ex;
      } finally {
        try {
          if (ins != null) ins.close();
        } catch (IOException ex1) {
        }
      }
    }

    out.close();
  }