Пример #1
0
  public static boolean wolframAlpha(Queries qu) {
    String queryStr = qu.getQuery();
    StringBuilder result = new StringBuilder(".\n");

    // Basic engine setup
    WAEngine engine = new WAEngine();
    engine.setAppID("7AHUTR-UV58KYXA8Q");

    // Set up query with necessary parameters
    WAQuery query = engine.createQuery();
    query.setInput(queryStr);
    // query.addIncludePodID(arg0);

    // Query retrieval and error handling
    WAQueryResult queryResult = null;
    try {
      queryResult = engine.performQuery(query);
    } catch (WAException e) {
      // WIP: Handle WA engine errors
      e.printStackTrace(); // Might not want this; need to generate a response
      qu.setSuccessful(false);
      qu.setResponseTime(((double) System.currentTimeMillis() - SMSServlet.queryTime) / 1000);
      ProcessUser.persistWolfram(qu);
      return false;
    }

    // Error case
    if (queryResult.isError()) {
      qu.setSuccessful(false);
      qu.setResponseTime(((double) System.currentTimeMillis() - SMSServlet.queryTime) / 1000);
      ProcessUser.persistWolfram(qu);
      return false;
    } else if (!queryResult.isSuccess()) {
      qu.setSuccessful(false);
      qu.setResponseTime(((double) System.currentTimeMillis() - SMSServlet.queryTime) / 1000);
      ProcessUser.persistWolfram(qu);
      return false;
    }
    // Response generation
    else {
      // To do:
      // 1. Determine what the query is relevant to
      // 2. Organize the query as needed (if needed)
      // 3. Set up assumptions (as needed)
      // 4. Get WA engine response
      // 5. Sort through response
      // 6. Generate result with parts of response

      // Alt method:
      // Include first few pods in output
      // Have some pods, which if they exist for this entry, are always included

      // Retrieve result pods
      WAPod[] pods = queryResult.getPods();

      // Result generation

      // Include pods which must always be included first (if they exist)
      // WIP
      // First few pods included
      int podI = 0;
      if (pods[podI].getID().equals("Input")) // Exclude 'Input interpretation' pod
      {
        podI++;
      }
      for (
      /* int podI = 0 */ ; podI < NO_PODS_TO_INCL && podI < pods.length; podI++) {
        if (!pods[podI].isError()) {
          result.append(pods[podI].getTitle());
          result.append(": \n");
          WASubpod[] subpods = pods[podI].getSubpods();
          for (WASubpod subpod : subpods) {
            // Iterate through elements of the subpod and include plaintext elements
            for (Object element : subpod.getContents()) {
              if (element instanceof WAPlainText) {
                result.append(((WAPlainText) element).getText());
              }
            }
            result.append('\n');
          }
        } else {
          podI--;
        }
      }

      // Include pods which must always be included at the end (if they exist)
      // WIP
    }

    // Send results through Communicate Module
    qu.setSuccessful(true);
    qu.setResponseTime(((double) System.currentTimeMillis() - SMSServlet.queryTime) / 1000);
    ProcessUser.persistWolfram(qu, result.toString());

    return true;
  } // wolframAlpha method
Пример #2
0
  public static String getWolframResult(String queryToExecute) {

    // Use "pi" as the default query, or caller can supply it as the lone command-line argument.
    String input = "pi";
    String result = "";
    // if (args.length > 0)
    //    input = args[0];
    logger.info("1");
    // The WAEngine is a factory for creating WAQuery objects,
    // and it also used to perform those queries. You can set properties of
    // the WAEngine (such as the desired API output format types) that will
    // be inherited by all WAQuery objects created from it. Most applications
    // will only need to crete one WAEngine object, which is used throughout
    // the life of the application.
    WAEngine engine = new WAEngine();

    // These properties will be set in all the WAQuery objects created from this WAEngine.
    engine.setAppID(appid);
    engine.addFormat("plaintext");
    logger.info("2");
    // Create the query.
    WAQuery query = engine.createQuery();
    logger.info("3");
    // Set properties of the query.
    query.setInput(queryToExecute);
    logger.info("4");
    try {
      // For educational purposes, print out the URL we are about to send:
      logger.info("Query URL:");
      logger.info(engine.toURL(query));
      logger.info("");

      // This sends the URL to the Wolfram|Alpha server, gets the XML result
      // and parses it into an object hierarchy held by the WAQueryResult object.
      WAQueryResult queryResult = engine.performQuery(query);

      if (queryResult.isError()) {
        logger.info("Query error");
        logger.info("  error code: " + queryResult.getErrorCode());
        logger.info("  error message: " + queryResult.getErrorMessage());
      } else if (!queryResult.isSuccess()) {
        logger.info("Query was not understood; no results available.");
      } else {
        // Got a result.
        logger.info("Successful query. Pods follow:\n");
        for (WAPod pod : queryResult.getPods()) {
          logger.info(pod.getTitle());
          // logger.info(WolframAlphaConstants.POD_DECIMAL_APROXIMATION);
          if (!pod.isError()
              && (pod.getTitle().equals(WolframAlphaConstants.POD_DECIMAL_APROXIMATION)
                  || pod.getTitle().equals(WolframAlphaConstants.POD_RESULT)
                  || pod.getTitle().equals(WolframAlphaConstants.POD_REAL_SOLUTION))) {
            logger.info(pod.getTitle());
            logger.info("------------");
            for (WASubpod subpod : pod.getSubpods()) {
              for (Object element : subpod.getContents()) {
                if (element instanceof WAPlainText) {
                  logger.info(((WAPlainText) element).getText());
                  logger.info("");
                  result = (((WAPlainText) element).getText());
                }
              }
            }
            logger.info("");
          }
        }
        // We ignored many other types of Wolfram|Alpha output, such as warnings, assumptions, etc.
        // These can be obtained by methods of WAQueryResult or objects deeper in the hierarchy.
      }
    } catch (WAException e) {
      logger.info(e.getMessage());
    }
    return result;
  }