Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
interact_with_knowrob_via_ros [2013/01/09 21:16] – [C++ client] tenorthinteract_with_knowrob_via_ros [2014/06/05 11:38] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Use KnowRob from your program ====== +#REDIRECT doc:interact_with_knowrob_via_ros
- +
-The interactive Prolog shell that [[http://ros.org/wiki/rosprolog|rosprolog]] provides is good for exploring KnowRob, visualizing knowledge, developing new functions and debugging Prolog code. However, if you would like to use KnowRob in your robot's control program, you need a way to send queries from your program. This functionality is provided by the [[http://ros.org/wiki/json_prolog|json_prolog]] package. It provides a service that exposes a Prolog shell via ROS. +
- +
-You can run the //json_prolog// service using the following command. It has the same structure as rosprolog, taking a KnowRob package as final argument. +
-<code> +
-rosrun json_prolog json_prolog ias_semantic_map +
-</code> +
- +
-===== Client libraries ===== +
-The communication with the //json_prolog// service uses a [[http://json.org/|JSON-encoded]] format for representing the Prolog terms that form the query. While you can construct these terms manually and call the service directly, it is usally much easier to use one of the provided client libraries. There are libraries for Python, C++, Java and Lisp. Example clients are available in the //json_prolog/examples// folder; below is the code for sending KnowRob queries from Python via //json_prolog//+
- +
- +
-==== Python client ==== +
- +
-<code python> +
- +
-#!/usr/bin/env python +
- +
-import roslib; roslib.load_manifest('json_prolog'+
- +
-import rospy +
-import json_prolog +
- +
-if __name__ == '__main__': +
-    rospy.init_node('test_json_prolog'+
-    prolog = json_prolog.Prolog() +
-    query = prolog.query("member(A, [1, 2, 3, 4]), B = ['x', A]") +
-    for solution in query.solutions(): +
-        print 'Found solution. A = %s, B = %s' % (solution['A'], solution['B']) +
-    query.finish() +
- +
-</code> +
- +
-==== C++ client ==== +
- +
-<code c++> +
-#include <string> +
-#include <iostream> +
- +
-#include <ros/ros.h> +
-#include <json_prolog/prolog.h> +
- +
-using namespace std; +
-using namespace json_prolog; +
- +
-int main(int argc, char *argv[]) +
-+
-  ros::init(argc, argv, "test_json_prolog"); +
- +
-  Prolog pl; +
- +
-  PrologQueryProxy bdgs = pl.query("member(A, [1, 2, 3, 4]), B = ['x', A], C = foo(bar, A, B)"); +
- +
-  for(PrologQueryProxy::iterator it=bdgs.begin(); +
-      it != bdgs.end(); it++) +
-  { +
-    PrologBindings bdg = *it; +
-    cout << "Found solution: " << (bool)(it == bdgs.end()) << endl; +
-    cout << "A = "<< bdg["A"] << endl; +
-    cout << "B = " << bdg["B"] << endl; +
-    cout << "C = " << bdg["C"] << endl; +
-  } +
-  return 0; +
-+
- +
-</code> +
- +
-==== Java client ==== +
- +
-<code java>  +
-import edu.tum.cs.ias.knowrob.json_prolog.Prolog; +
-import edu.tum.cs.ias.knowrob.json_prolog.PrologBindings; +
-import edu.tum.cs.ias.knowrob.json_prolog.PrologQueryProxy; +
- +
- +
-public class JSONPrologTestClient { +
- +
- public static void main(String args[]) { +
- +
- Prolog pl = new Prolog(); +
- PrologQueryProxy bdgs = pl.query("member(A, [1, 2, 3, 4]), B = ['x', A], C = foo(bar, A, B)"); +
- +
-  +
- for(PrologBindings bdg : bdgs) { +
- +
-      System.out.println("Found solution"); +
-      System.out.println("A = " + bdg.getBdgs_().get("A") ); +
-      System.out.println("B = " + bdg.getBdgs_().get("B") ); +
-      System.out.println("C = " + bdg.getBdgs_().get("C") ); +
-    } +
-  } +
-+
-</code> +