Skip to content

Commit

Permalink
Merge branch 'feature/bt2agent-generation'
Browse files Browse the repository at this point in the history
  • Loading branch information
anan02-admin authored and anan02-admin committed Feb 21, 2023
2 parents 440327b + 36297c0 commit 00798f0
Show file tree
Hide file tree
Showing 23 changed files with 477 additions and 87 deletions.
5 changes: 5 additions & 0 deletions app/components/behaviors/action-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export default Component.extend({
this.get('dataBus').cloneBT();
},

generateAgent() {
console.log("Generate Agent!");
this.get('dataBus').generateAgent();
},

sort() {
graphOperations.updateGraph(globals.cy);
},
Expand Down
146 changes: 143 additions & 3 deletions app/components/behaviors/behavior-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import actions from "ajan-editor/helpers/behaviors/actions";
import actionsAgnt from "ajan-editor/helpers/agents/actions";
import {cleanDOM} from "ajan-editor/helpers/graph/cy-cleanup";
import Ember from "ember";
import events from "ajan-editor/helpers/behaviors/event-bindings";
import globals from "ajan-editor/helpers/global-parameters";
import nodeDefs from "ajan-editor/helpers/RDFServices/node-definitions/common";
import rdfGraph from "ajan-editor/helpers/RDFServices/RDF-graph";
import rdfManager from "ajan-editor/helpers/RDFServices/RDF-manager";
import utility from "ajan-editor/helpers/RDFServices/utility";
import { AGENTS, XSD, RDF, RDFS } from "ajan-editor/helpers/RDFServices/vocabulary";
import Split from "npm:split.js";

let $ = Ember.$;
Expand All @@ -43,6 +46,9 @@ export default Ember.Component.extend({
cytoscapeService: Ember.inject.service("behaviors/cytoscape"),
dataBus: Ember.inject.service(),
availableBTs: undefined,
availableEvents: undefined,
availableBehaviors: undefined,
availableEndpoints: undefined,
cyRef: undefined,

init() {
Expand All @@ -51,6 +57,9 @@ export default Ember.Component.extend({
this.get('dataBus').on('addBT', function (bt) {
createBT(bt);
});
this.get('dataBus').on('generateAgent', function (bt) {
generateAgent();
});
this.get('dataBus').on('cloneBT', function () {
cloneBT();
});
Expand Down Expand Up @@ -127,10 +136,11 @@ function initializeSplitPanes() {
}

function loadNodeDefinitionsThenGraph() {
nodeDefs(ajax, cy).then(loadRdfGraphData);
nodeDefs(ajax, cy).then(loadBTRdfGraphData);
loadAgentsRdfGraphData();
}

function loadRdfGraphData() {
function loadBTRdfGraphData() {
let repo =
(localStorage.currentStore || "http://localhost:8090/rdf4j/repositories") +
"/" +
Expand All @@ -150,10 +160,140 @@ function rdfDataHasLoaded(rdfData) {
}

function setAvailableBTs() {
let behaviorTrees = actions.getBehaviorTrees();
let behaviorTrees = actions.getBehaviorTrees();
that.set("availableBTs", behaviorTrees);
}

function loadAgentsRdfGraphData() {
let repo = (localStorage.currentStore || "http://localhost:8090/rdf4j/repositories")
+ globals.agentsRepository;
actionsAgnt.getFromServer(ajax, repo)
.then(setAvailableBehaviors)
.then(setAvailableEvents)
.then(setAvailableEndpoints);
}

function setAvailableBehaviors() {
let behaviorsLists = actionsAgnt.getBehaviors();
that.set("availableBehaviors", behaviorsLists.regular);
}

function setAvailableEvents() {
let eventLists = actionsAgnt.getEvents();
that.set("availableEvents", eventLists);
}

function setAvailableEndpoints() {
let endpointList = actionsAgnt.getEndpoints();
that.set("availableEndpoints", endpointList);
}

function generateAgent() {
let agentRepo = (localStorage.currentStore || "http://localhost:8090/rdf4j/repositories/") + "agents";
let selected = localStorage.getItem("bt-selected");
let selectedBt = that.get("availableBTs").filter(item => item.uri == selected);
console.log(selectedBt);
let includedEvents = { all: new Array(), handle: new Array(), produce: new Array()};
getEvents(includedEvents, selectedBt[0], that.get("availableBehaviors").filter(item => item.bt.uri != selectedBt[0].uri), true);
let includedBehaviors = getBehaviors(selectedBt[0], includedEvents);
let includedEndpoints = getEndpoints(includedEvents);
let agentDef = {};
agentDef.event = actionsAgnt.createDefinedEvent(agentRepo, selectedBt[0], includedEvents);
agentDef.behavior = actionsAgnt.createDefinedBehavior(agentRepo, selectedBt[0], includedEvents, includedBehaviors);
agentDef.endpoint = actionsAgnt.createDefinedEndpoint(agentRepo, selectedBt[0], agentDef.event, includedEndpoints);
agentDef.template = actionsAgnt.createDefinedAgent(agentRepo, selectedBt[0], includedEvents, includedBehaviors, includedEndpoints);
console.log(agentDef);
let stringRDF = actionsAgnt.createAgentRDFString(agentDef);
saveGeneratedAgent(agentRepo, stringRDF);
}

function saveGeneratedAgent(repo, stringRDF) {
try {
actions.saveAgentGraph(globals.ajax, repo, stringRDF);
} catch (e) {
$("#error-message").trigger("showToast", [
"Error while saving generated Agent"
]);
throw e;
}
}

function getEvents(includedEvents, bt, behaviors, root) {
bt.nodes.forEach(function (item) {
addEventURI(item, includedEvents, root);
});
behaviors.forEach(function (bhvs) {
bhvs.triggers.forEach(function (event) {
if (includedEvents.produce.includes(event.uri)) {
bhvs.triggers.forEach(function (triggers) {
includedEvents.all.push(triggers.uri);
});
let newBt = that.get("availableBTs").filter(item => item.uri == bhvs.bt.uri);
console.log("Visit BT: " + newBt[0].uri);
getEvents(includedEvents, newBt[0], behaviors.filter(item => item.uri != bhvs.uri), false);
}
});
});
return includedEvents;
}

function addEventURI(item, events, root) {
let addableUri = "";
if (item.category == "ProduceGoal") {
addableUri = getGoalURI(item.uri);
events.produce.push(addableUri);
} else if (item.category == "ProduceEvent") {
addableUri = getEventURI(item.uri);
events.produce.push(addableUri);
} else if (item.category == "HandleMappingEvent" || item.category == "HandleEvent" || item.category == "HandleQueueEvent") {
addableUri = getEventURI(item.uri);
if (root && addableUri != "" && !events.handle.includes(addableUri)) {
events.handle.push(addableUri);
}
}
if (that.get("availableEvents").filter(item => item.uri == addableUri).length > 0 && addableUri != "" && !events.all.includes(addableUri)) {
events.all.push(addableUri);
}
}

function getGoalURI(uri) {
return rdfGraph.getObject(uri, AGENTS.goal).value;
}

function getEventURI(uri) {
return rdfGraph.getObject(uri, AGENTS.event).value;
}

function getBehaviors(bt, includedEvents) {
let addableBehaviors = new Array();
let behaviors = that.get("availableBehaviors");
includedEvents.all.forEach(function (event) {
behaviors.forEach(function (bhvs) {
bhvs.triggers.forEach(function (item) {
if (item.uri == event && !addableBehaviors.includes(event) && bhvs.bt.uri != bt.uri) {
addableBehaviors.push(bhvs.uri);
}
})
});
});
return addableBehaviors;
}

function getEndpoints(includedEvents) {
let addableEndpoints = new Array();
let endpoints = that.get("availableEndpoints");
includedEvents.all.forEach(function (event) {
endpoints.forEach(function (endpt) {
endpt.events.forEach(function (item) {
if (item.uri == event && !addableEndpoints.includes(event)) {
addableEndpoints.push(endpt.uri);
}
})
});
});
return addableEndpoints;
}

function createBT(bt) {
that.get("availableBTs").push(bt);
that.dataBus.save();
Expand Down
4 changes: 2 additions & 2 deletions app/components/behaviors/properties/parameter/event-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default Component.extend({
this._super(...arguments);
that = this;
initializeGlobals(this);
loadActionsRdfGraphData();
loadAgentsRdfGraphData();
},

selectedChanged: observer("selected", function () {
Expand All @@ -66,7 +66,7 @@ function initializeAjax() {
globals.ajax = ajax;
}

function loadActionsRdfGraphData() {
function loadAgentsRdfGraphData() {
let repo = (localStorage.currentStore || "http://localhost:8090/rdf4j/repositories")
+ globals.agentsRepository;
actionsAgnt.getFromServer(ajax, repo).then(setAvailableEvents);
Expand Down
23 changes: 23 additions & 0 deletions app/components/ui/menu-bar/create-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Created on Tue Nov 10 2020
*
* The MIT License (MIT)
* Copyright (c) 2020 André Antakli, Alex Grethen (German Research Center for Artificial Intelligence, DFKI).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import Component from "@ember/component";

export default class UiMenuBarImportButtonComponent extends Component {}
100 changes: 57 additions & 43 deletions app/helpers/RDFServices/agentsRDFProducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,67 +18,81 @@
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import {ACTN, SPIN, RDF, RDFS, HTTP, XSD, AGENTS} from "ajan-editor/helpers/RDFServices/vocabulary";
import { RDF, RDFS, AGENTS, XSD} from "ajan-editor/helpers/RDFServices/vocabulary";
import rdfGraph from "ajan-editor/helpers/RDFServices/RDF-graph";
import rdfManager from "ajan-editor/helpers/RDFServices/RDF-manager";
import rdfFact from "ajan-editor/helpers/RDFServices/RDF-factory";
import rdf from "npm:rdf-ext";

export default {
createAgent: createAgent
createAgent: createAgent,
createAgentRDFString: createAgentRDFString
};
// editing ajan

function createAgent(definition) {
rdfGraph.add(rdfFact.quad(definition.uri, RDF.type,AGENTS.AgentTemplate ));
rdfGraph.add(rdfFact.quad(definition.uri, RDF.type, AGENTS.AgentTemplate));
rdfGraph.add(rdfFact.quadLiteral(definition.uri, RDFS.label, definition.label));
}

function createAgentRDFString(definition) {
let quads = rdf.dataset();
createTeamplateQuads(quads, definition.template);
createEventQuads(quads, definition.event);
createBehaviorQuads(quads, definition.behavior);
createEndpointQuads(quads, definition.endpoint);
return rdfGraph.toString(quads);
}

function createVariables(rootUri, definition) {
let root = rdfFact.blankNode();
rdfGraph.add(rdfFact.quad(rootUri, ACTN.variables, root));
definition.forEach(function(item, index, arr) {
let first = rdfFact.blankNode();
rdfGraph.add(rdfFact.quad(root, RDF.first, first));
createVariable(first, item);
item.pointerUri = first.value;
if (index < arr.length) {
let rest = rdfFact.blankNode();
rdfGraph.add(rdfFact.quad(root, RDF.rest, rest));
root = rest;
} else {
rdfGraph.add(rdfFact.quad(root, RDF.rest, RDF.nil));
}
});
function createTeamplateQuads(quads, template) {
quads.add(rdfFact.quad(template.uri, RDF.type, AGENTS.AgentTemplate));
quads.add(rdfFact.quadLiteral(template.uri, RDFS.label, template.label));
if (template.events.length > 0) {
addEvents(quads, template)
}
if (template.behaviors.length > 0) {
addBehaviors(quads, template)
}
if (template.endpoints.length > 0) {
addEndpoints(quads, template)
}
}

function appendVariable(root, variable, list) {
var last_element = list[list.length-1];
variable.pointerUri = rdfManager.listInsert(root,last_element.pointerUri);
function createEventQuads(quads, event) {
quads.add(rdfFact.quad(event.uri, RDF.type, AGENTS.Event));
quads.add(rdfFact.quadLiteral(event.uri, RDFS.label, event.label));
}

function createVariable(root, definition) {
rdfGraph.add(rdfFact.quad(root, RDF.type, ACTN.ActionVariable));
rdfGraph.add(rdfFact.quadLiteral(root, SPIN.varName, definition.var, XSD.string));
function createBehaviorQuads(quads, behavior) {
quads.add(rdfFact.quad(behavior.uri, RDF.type, AGENTS.Behavior));
quads.add(rdfFact.quadLiteral(behavior.uri, RDFS.label, behavior.label));
quads.add(rdfFact.quad(behavior.uri, AGENTS.bt, behavior.bt.uri));
behavior.triggers.forEach(function (event) {
quads.add(rdfFact.quad(behavior.uri, AGENTS.trigger, event));
})
quads.add(rdfFact.quadLiteral(behavior.uri, AGENTS.clearEKB, behavior.clearEKB, XSD.boolean));
}

function createBinding(type, rootUri, definition) {
if (type === "abort") {
rdfGraph.add(rdfFact.quad(rootUri, ACTN.abortBinding, definition.uri));
} else {
rdfGraph.add(rdfFact.quad(rootUri, ACTN.runBinding, definition.uri));
}
rdfGraph.add(rdfFact.quad(definition.uri, RDF.type, ACTN.Binding));
rdfGraph.add(rdfFact.quad(definition.uri, RDF.type, HTTP.Request));
rdfGraph.add(rdfFact.quadLiteral(definition.uri, HTTP.version, definition.version, XSD.string));
rdfGraph.add(rdfFact.quadLiteral(definition.uri, HTTP.mthd, definition.mthd, XSD.anyURI));
rdfGraph.add(rdfFact.quadLiteral(definition.uri, HTTP.uri, definition.requestUri, XSD.anyURI));
if(definition.payload != null)
createPayload(definition.uri, definition.payload);
function createEndpointQuads(quads, endpoint) {
quads.add(rdfFact.quad(endpoint.uri, RDF.type, AGENTS.Endpoint));
quads.add(rdfFact.quadLiteral(endpoint.uri, RDFS.label, endpoint.label));
quads.add(rdfFact.quad(endpoint.uri, AGENTS.event, endpoint.events[0].uri));
quads.add(rdfFact.quadLiteral(endpoint.uri, AGENTS.capability, endpoint.capability));
}

function addEvents(quads, definition) {
definition.events.forEach(function (event) {
quads.add(rdfFact.quad(definition.uri, AGENTS.event, event));
})
}

function addBehaviors(quads, definition) {
definition.behaviors.forEach(function (behavior) {
quads.add(rdfFact.quad(definition.uri, AGENTS.behavior, behavior));
})
}

function createPayload(rootUri, definition) {
rdfGraph.add(rdfFact.quad(rootUri, HTTP.body, definition.uri));
rdfGraph.add(rdfFact.quad(definition.uri, RDF.type, ACTN.Payload));
rdfGraph.add(rdfFact.quadLiteral(definition.uri, ACTN.sparql, definition.sparql, XSD.string));
function addEndpoints(quads, definition) {
definition.endpoints.forEach(function (endpoint) {
quads.add(rdfFact.quad(definition.uri, AGENTS.endpoint, endpoint));
})
}
8 changes: 8 additions & 0 deletions app/helpers/RDFServices/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ WHERE {?s ?p ?o}`,
);
},

insert: function (str) {
return (
`INSERT DATA{` +
str +
`}`
);
},

update: function(str) {
return (
`DELETE { ?s ?p ?o } WHERE { ?s ?p ?o };
Expand Down
1 change: 1 addition & 0 deletions app/helpers/RDFServices/vocabulary.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ let AGENTS = {
Goal: "http://www.ajan.de/ajan-ns#Goal",
Variable: "http://www.ajan.de/ajan-ns#Variable",
event: "http://www.ajan.de/ajan-ns#event",
goal: "http://www.ajan.de/ajan-ns#goal",
agentKnowledge: "http://www.ajan.de/ajan-ns#agentKnowledge",
endpoint: "http://www.ajan.de/ajan-ns#endpoint",
initialBehavior: "http://www.ajan.de/ajan-ns#initialBehavior",
Expand Down
Loading

0 comments on commit 00798f0

Please sign in to comment.