SPARQL BT Branch Nodes - aantakli/AJAN-service GitHub Wiki

SPARQL-BT Executor

Implementation: https://github.com/aantakli/AJAN-service/blob/master/behaviour/src/main/java/de/dfki/asr/ajan/behaviour/nodes/branch/Executor.java

States: SUCCEEDED || RUNNING || FAILED depending on child state.

Description: A SBT Executor composite node can have multiple child nodes, where only one child node is executed per Executor call, depending on the agent knowledge. The selection of the next child node to execute is done by a SPARQL 1.1 SELECT query. The complete SPARQL 1.1 language space with regard to SELECT operations can be used for this purpose. Here an integer value representing the position or index of the respective child, starting with 0, is defined through the WHERE clause of the query and bound to a specified SPARQL 1.1 variable (?intValue). If multiple values are bound to this variable, one is selected randomly. A SBT Executor node returns the status of the executed child node (SUCCEEDED, FAILED or RUNNING) or FAILED if the selected child node does not exist.

Properties

Property (RDF) Value (RDF)
Namespace (@prefix bt:) URL (http://www.ajan.de/behavior/bt-ns#)
Type (rdf:type) Executor Node (bt:Executor)
Class (rdfs:subClassOf) Composite Node (bt:Composite)
Label (rdfs:label) <string> ("some label"^^xsd:string)
Selection (bt:selectChild) Int Value as a SELECT Query (bt:IntValue)
Children (bt:hasChildren) List of SBT-Nodes (bt:Task)

Example in Turtle/RDF (comments strat with: #)

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix bt: <http://www.ajan.de/behavior/bt-ns#> .
@prefix ajan: <http://www.ajan.de/ajan-ns#> .

_:ExecutorX # Instance URI
	a bt:Executor ;
	rdfs:label "some executor"^^xsd:string ;
	bt:selectChild [
		a bt:IntValue ;
		bt:query [
			a bt:SelectQuery ;
			bt:originBase ajan:AgentKnowledge ;
			bt:sparql """
				PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
				PREFIX mosim: <http://www.dfki.de/mosim-ns#>

				SELECT ?intValue
				WHERE {
					?task rdf:type mosim:Task .
					?task mosim:operation ?operation .
					BIND (IF ( CONTAINS(?operation,"Rough"), 1, 
						  IF ( CONTAINS(?operation,"Exact"), 1, 
						  IF ( CONTAINS(?operation,"Stick"), 1,
						  IF ( CONTAINS(?operation,"Remove"), 1, 
						  IF ( CONTAINS(?operation,"Loose"), 2,
						  IF ( CONTAINS(?operation,"Fully"), 2,
						  IF ( CONTAINS(?operation,"Torque"), 2,
						  IF ( CONTAINS(?operation,"Untighten"), 2,
						  IF ( CONTAINS(?operation,"Visual"), 3,
						  IF ( CONTAINS(?operation,"Manual"), 3,
						  IF ( CONTAINS(?operation,"Adapt"), 3,
						  0))))))))))) as ?intValue)
					{ SELECT ?task
					WHERE {
						?running mosim:runningTask ?task .
					}}
				}"""^^xsd:string ;
		]
	] ;
	bt:hasChildren (
		# all executable child nodes
		_:SomeNode1
		_:SomeNode2
		_:SomeNode3
	) .

SPARQL-BT Repeater

Implementation: https://github.com/aantakli/AJAN-service/blob/master/behaviour/src/main/java/de/dfki/asr/ajan/behaviour/nodes/branch/Repeater.java

States: SUCCEEDED || RUNNING || FAILED depending on child state.

Description: The SBT repeater decorator node can have only one child node. This child node is executed several times per repeater call, depending on the agent knowledge. The definition of the iterations is done by a SPARQL 1.1 SELECT query. The complete SPARQL 1.1 language space with regard to SELECT operations can be used for this purpose. A specified SPARQL 1.1 variable (?intValue) is used to bind a positive integer value, defined by the WHERE clause of the query, that represents the number of rounds, starting with 0. If multiple values are bound to this variable, one is selected randomly. A SBT Repeater node returns three states after execution: SUCCEEDED, if all iterations are executed; FAILED if a malformed iteration number is defined; and RUNNING while the decorator is running.

Properties

Property (RDF) Value (RDF)
Namespace (@prefix bt:) URL (http://www.ajan.de/behavior/bt-ns#)
Type (rdf:type) Repeater Node (bt:Repeater)
Class (rdfs:subClassOf) Decorator Node (bt:Decorator)
Label (rdfs:label) <string> ("some label"^^xsd:string)
Times (bt:setTimes) <int> ("123"^^xsd:integer) or URL (bt:IntValue)
Child (bt:hasChild) SBT-Node to execute (bt:Task)

Example in Turtle/RDF (comments strat with: #)

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix bt: <http://www.ajan.de/behavior/bt-ns#> .
@prefix ajan: <http://www.ajan.de/ajan-ns#> .

_:RepeaterX # Instance URI
	a bt:Repeater ;
	bt:setTimes [
		a bt:IntValue ;
		bt:query [
			a bt:SelectQuery ;
			bt:originBase ajan:AgentKnowledge ;
			bt:sparql """
				PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
				PREFIX mosim: <http://www.dfki.de/mosim-ns#>
			
				SELECT (count(?task) as ?intValue)
				WHERE {
					?task rdf:type mosim:Task .
					?some mosim:runningTask ?running .
					FILTER (?task != ?running)
				}"""^^xsd:string ;
			]
		] ;
	bt:hasChild :someSubTree # some SBT node .

SPARQL-BT Load Behavior

Implementation: https://github.com/aantakli/AJAN-service/blob/master/behaviour/src/main/java/de/dfki/asr/ajan/behaviour/nodes/branch/LoadBehavior.java

States: SUCCEEDED || FAILED depending on the loaded and executed child state

Description: A SBT Load Behavior node is a SBT branch node that loads and executes RDF defined SBTs. It returns two states after execution: SUCCEEDED and FAILED. The parameter bt:resetBehavior is used to decide whether the defined SBT is reloaded with each execution of this Load Behavior node. This node type can be used to execute predefined SBTs but also SBTs generated dynamically by the agent itself (e.g. by a planner). Thereby, it performs one SPARQL 1.1 Construct query on a defined RDF dataset, to get the URI of the SBT (from http://www.w3.org/1999/02/22-rdf-syntax-ns#type http://www.ajan.de/behavior/bt-ns#BehaviorTree) to be loaded. In the constructed graph, the desired URI MUST be marked with the predicate http://www.ajan.de/ajan-ns#bt. The dataset can be a default graph or a named graph and is represented by its SPARQL endpoint URI. To define a SPARQL Construct query, the complete language space of the SPARQL 1.1 language with regard to Construct operations can be used.

Properties

Property (RDF) Value (RDF)
Namespace (@prefix bt:) URL (http://www.ajan.de/behavior/bt-ns#)
Type (rdf:type) Load Behavior Node (bt:LoadBehavior)
Class (rdfs:subClassOf) Branch Node (bt:Branch)
Label (rdfs:label) <string> ("some label"^^xsd:string)
Reset (bt:resetBehavior) boolean (true or false)
Query (bt:behaviorUri) Construct query (bt:ConstructQuery)

Example in Turtle/RDF (comments strat with: #)

@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix bt: <http://www.ajan.de/behavior/bt-ns#> .
@prefix ajan: <http://www.ajan.de/ajan-ns#> .

:LoadDynamicBehavior
	a bt:LoadBehavior ;
	rdfs:label "load(DynamicBehavior)" ;
	bt:resetBehavior true ;
	bt:behaviorUri [
		a bt:ConstructQuery ;
		bt:originBase ajan:BehaviorKnowledge ;
		bt:sparql """
			PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
			PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
			PREFIX bt: <http://www.ajan.de/behavior/bt-ns#>
		
			CONSTRUCT {
				<http://ajan/some> <http://www.ajan.de/ajan-ns#bt> ?behavior .
			}
			WHERE {
				?behavior rdf:type bt:BehaviorTree .
				?behavior rdfs:label "DynamicBehavior" .
			}"""^^xsd:string ;
		] .
⚠️ **GitHub.com Fallback** ⚠️