Giter VIP home page Giter VIP logo

koreografeye's Introduction

Koreografeye

npm

Koreografeye is a choreography / orchestration engine for linked data services using Notation3 (N3) rule engines such as EYE.

Koreografeye was created to facilitate automated processes against Solid pods. Our main use case is monitoring the LDN Inbox of Solid Pods for new notifications and running scripts when new data arrives.

Usage

Configuration

Create a project directory.

mkdir demo
cd demo

Add the koreografeye dependency (and solid-bashlib in case you want to monitor private Solid resources).

npm install koreografeye
npm install solid-bashlib

Create input, output and rules directories.

mkdir input output rules

Prepare some input data

Put a demo.jsonld AS2 notification in the input directory.

{
  "@context": [
    "https://www.w3.org/ns/activitystreams",
    "https://purl.org/coar/notify"
  ],
  "id": "urn:uuid:0370c0fb-bb78-4a9b-87f5-bed307a509dd",
  "type": "Offer",
  "actor": {
    "id": "https://mypod.org/profile/card#me",
    "name": "Freddy Test",
    "type": "Person"
  },
  "object": "https://research-organisation.org/repository/preprint/201203/421/"
}

Create a demo.n3 N3 rule file in the rules directory.

@prefix ex:   <http://example.org/> .
@prefix as:   <https://www.w3.org/ns/activitystreams#> .
@prefix pol:  <https://www.example.org/ns/policy#> .
@prefix fno:  <https://w3id.org/function/ontology#> .

{
    # if we get an offer 
    ?id a as:Offer .
    ?id as:actor ?actor .
}
=>
{
    # Send an accept notification to an LDN inbox
    ex:MySendNotificationDemo pol:policy ex:MySendNotificationDemoExecution.
    ex:MySendNotificationDemoExecution 
        a fno:Execution ;
        fno:executes ex:sendNotification ;
        ex:to <http://httpbin.org/post> ;
        ex:notification ex:MyNotification .
    
    ex:MyNotification 
        a as:Accept ;
        as:inReplyTo ?id ;
        as:actor     <http://my.service.edu/profile/card#me> ;
        as:object    ?id .
}.

Run orch

The orch command will take the input data and use the N3 rules to decide what to do with the data. No actions are taken yet. These will be done by the pol command.

npx orch --info --keep --in input --out output rules/demo.n3

The processed notifications will end up in the output directory

Run pol

The pol command will take the output of the orch command and execute the requested policies defined with the N3 rules.

npx pol --info --keep --in output

The result will be an AS2 that is send to the http://httpbin.org/post address.

Experiment

Using Koreografeye plugins you can experiment with:

  • Sending emails
  • Sending notifications
  • Sending push messages to your phone
  • Updating Solid Pods
  • Sending toot messages to a Mastodon account

Check the Git repository https://github.com/eyereasoner/Koreografeye for more examples.

We use the Bashlib to monitor remote Solid LDN inboxes for new notification.

E.g. move the contents of your inbox to the input directory.

npx sld mv https://yourpod.org/inbox/ input/

This assumes you have an authenticated Bashlib session. Use the bashlib auth create-token command to create a token for the CSS Solid pod.

Typescript/javascript

One can also use JavaScript to execute the orch and pol commands:

const { 
    parseAsN3Store, 
    readText, 
    topGraphIds, 
    storeAddPredicate, 
    makeComponentsManager,
    executePolicies
} = require('koreografeye');

main();

async function main() {
    const config = './config/config.jsonld';
    const inputData  = './input/demo.jsonld';
    const inputRules = './rules/demo.n3';

    // Read the input graph as an N3 store
    const store  = await parseAsN3Store(inputData); 
    // Read the N3 rules as an array of strings
    const rules  = [readText(inputRules)]; 

    // Load the components we need for reasoning
    const manager = await makeComponentsManager(config,'.');

    // Get a reasoner
    const reasoner = await manager.instantiate('urn:koreografeye:reasonerInstance');

    // Execute the reasoner (orch)
    const resultStore = await reasoner.reason(store, rules);

    // Execute the policies (pol)
    const results = await executePolicies(manager, resultStore);

    if (results) {
        let success = 0;
        let errors = 0;
      	results?.forEach( (r) => { 
        	if (r.result) { success += 1; }
        	else { errors += 1; }
    	});  
    	console.log(`success: ${success} ; errors: ${errors}`);
	}
	else {
		console.log(`no policies executed`);
	}
}

Run this code with:

node demo.js

Commands

orch

Run the orchstrator with one or more N3 rule files on an input directory of notifications.

bin/orch [options] rule [rule ...]

Options:

  • --config file : orchestrator configuration file
  • --in directory : directory with input notifications
  • --out directory : directory with orchestrator output
  • --err directory : directory with failed notifications
  • --single file : process only a single file
  • --keep : keep the --in data (don't delete after processing)
  • --info : verbose messages
  • --debug : debug messages
  • --trace : trace messages

pol

Run a policy executor on one of the output files of the orchestrator

bin/pol [options]

Options:

  • --config file : orchestrator configuration file
  • --in directory : directory with input notifications
  • --out directory : directory with orchestrator output
  • --err directory : directory with failed notifications
  • --single file : process only a single file
  • --keep : keep the --in data (don't delete after processing)
  • --info : verbose messages
  • --debug : debug messages
  • --trace : trace messages

Examples implementations

koreografeye's People

Contributors

dependabot[bot] avatar jeswr avatar phochste avatar woutermont avatar woutslabbinck avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

koreografeye's Issues

Policy extractor code loops for every policy parameter

https://github.com/eyereasoner/Koreografeye/blob/main/src/policy/Extractor.ts#L46

Take policy:

    ex:MySendNotificationDemo pol:policy [
        a fno:Execution ;
        fno:executes ex:sendNotification ;
        ex:to <http://patrickhochstenbach.net/inbox/> ;
        ex:notification [
                a as:Accept ;
                as:inReplyTo ?id ;
                as:actor     <http://patrickhochstenbach.net/profile/card#me> ;
                as:origin    <http://patrickhochstenbach.net/orchestrator/profile/card#me> ;
                as:target    ?actor;
                as:object    ?id
        ]
    ] .

The ex:sendNotification as two parameters ex:to and ex:notification this means that block L46 is executed two times. Refactor this into one loop.

Policy IPolicyType arguments can only be one RDF Term

args: { // Name/Value pairs of policy arguments
[key : string]: RDF.Term | undefined
}

Due to the IPolicyType type having for the arguments only the possibility to have an RDF.Term with cardinality one, the extractPolicies algorithm will pick a random RDF.Term for the arguments where in the conclusion of an N3 rule where this cardinality was N.

My proposal is to fix this by introducing a breaking change that will fix this problem:

export type IPolicyType = {
    node: N3.NamedNode | N3.BlankNode;
    path: string;
    policy: string;
    target: string;
    mainSubject: string;
    origin: string;
    order: number;
    args: {
        [key: string]: RDF.Term[];
    };
};

For this, extractPolicies must also be changed to make it possible to add a list of RDF.Term[].

Furthermore, there will be no undefined anymore since it can now have the empty list.

Refactor out the ComponentsManager?

Aparently the building and registration of the ComponentsManager has some overhead. It could be a future refactor step to move this to a higher level. When we eventually need a Koreagrafeye that need to have multiple runs of the reasoner this could remove some of this overhead.

https://github.com/eyereasoner/Koreografeye/blob/main/src/orchestrator/Reason.ts#L19

These lines:

const manager = await ComponentsManager.build({
    mainModulePath: path.join(__dirname, '../..') , // Path to your npm package's root
 });

 await manager.configRegistry.register(componentsPath);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.