# Could Vaticle work for a rule-based expert system?

**URL:** <https://forum.typedb.com/t/could-vaticle-work-for-a-rule-based-expert-system/116>\
**Category:** TypeDB\
**Created:** [15 April 2022 23:02 UTC](https://forum.typedb.com/t/could-vaticle-work-for-a-rule-based-expert-system/116 "2022-04-15T23:02:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dogweather](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/dogweather/32/32_2.png) [@dogweather](https://forum.typedb.com/u/dogweather)\
**Post date:** [15 April 2022 23:02 UTC](https://forum.typedb.com/t/could-vaticle-work-for-a-rule-based-expert-system/116/1 "2022-04-15T23:02:24Z")

</div>

I’ve sketched out my idea in this Prolog snippet. I imagine that I could definitely model the objects with Vaticle. But I’m not sure if there’s built in support for inference and solving that e.g. Prolog provides.

```nohighlight
/*
 * Crimes within the jurisdiction of the International Criminal Court.
 * 
 * https://world.public.law/rome_statute/article_5
 */
crime(genocide).
crime(war_crime).
crime(crime_against_humanity).
crime(crime_of_aggression).

/*
 * A first, simple attempt at Protected Persons under
 * the Geneva Conventions of 1949.
 */
protected_by_geneva_convention(P) :- civilian(P).
protected_by_geneva_convention(P) :- prisoner_of_war(P).
protected_by_geneva_convention(P) :- medical_personnel(P).
protected_by_geneva_convention(P) :- religious_personnel(P).

/*
 * D = Defendant
 * V = Victim
 */

/*
 * Genocide
 * https://world.public.law/rome_statute/article_6
 */
criminal_liability(genocide, Statute, D, V) :-
	elements(Statute, D, V).

/*
 * War crimes
 * https://world.public.law/rome_statute/article_8
 */
criminal_liability(war_crime, Statute, D, V) :-
	protected_by_geneva_convention(V),
	international_conflict(D, V),
	elements(Statute, D, V).

elements(article_8_2_a_i, D, V) :-
	act(D, killed, V).

elements(article_8_2_a_ii, D, V) :-
	act(D, tortured, V).     

```

**Edit:** I see examples of queries that are similar to what I could ask a Prolog system: [Queries the Phone Calls Knowledge Graph | Vaticle](https://docs.vaticle.com/docs/examples/phone-calls-queries)

My other question: Do you have an example of Vaticle support a browser-based app?

---

<div class="post-metadata">

**Author:** ![alex](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/alex/32/148_2.png) [@alex](https://forum.typedb.com/u/alex)\
**Post date:** [19 April 2022 11:55 UTC](https://forum.typedb.com/t/could-vaticle-work-for-a-rule-based-expert-system/116/2 "2022-04-19T11:55:56Z")

</div>

> [@dogweather](#):
>
> My other question: Do you have an example of Vaticle support a browser-based app?

I’ve responded to this specific point in the following topic:

> [@Is there a way to make a web UI?](https://forum.typedb.com/t/is-there-a-way-to-make-a-web-ui/117):
>
> I noticed that all of the supported languages are server-side. Has anyone made a web UI for a TypeDB-powered app? I’m wondering what kind of stack and architecture would support this. One nice feature would be running the TypeDB in a stateless mode in order to support web users at scale.

(I’ll leave the remainder of this question to the experts 🙂 )

---

<div class="post-metadata">

**Author:** ![james](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.typedb.com/james/32/19_2.png) [@james](https://forum.typedb.com/u/james)\
**Post date:** [19 April 2022 13:46 UTC](https://forum.typedb.com/t/could-vaticle-work-for-a-rule-based-expert-system/116/3 "2022-04-19T13:46:16Z")

</div>

Prolog is a great source of inspiration for TypeDB! It does indeed support inference and solving akin to Prolog. There are two inference engines in TypeDB: [type inference](https://docs.vaticle.com/docs/general/introduction#inference) and [rule inference](https://docs.vaticle.com/docs/schema/rules).

The problem you posed above could be solved as follows making use of both kinds of inference.

A roughly equivalent schema, define this using a schema session in a write transaction:

```auto
define

crime sub entity, 
  owns name,
  plays criminal-liability:crime;

name sub attribute, 
  value string;

person sub entity,
    owns classification, # Declaring this as an attribute rather than a subtype of person means that a person can be classified in multiple ways
    owns geneva-convention-protection,
    plays elements:defendant,
    plays elements:victim,
    plays criminal-liability:defendant,
    plays criminal-liability:victim,
    plays international-conflict:participant,
    plays killing:perpetrator,
    plays killing:victim,
    plays torture:perpetrator,
    plays torture:victim;
    
classification sub attribute, 
  value string;

geneva-convention-protection sub attribute, 
  value boolean;

rule geneva-convention-protects-civilians:
when {
  $p isa person, has classification "civilian";
}
then {
  $p has geneva-convention-protection true;
};

rule geneva-convention-protects-prisoners-of-war:
when {
  $p isa person, has classification "prisoner of war";
}
then {
  $p has geneva-convention-protection true;
};

rule geneva-convention-protects-medical-personnel:
when {
  $p isa person, has classification "medical personnel";
}
then {
  $p has geneva-convention-protection true;
};

rule geneva-convention-protects-religious-personnel:
when {
  $p isa person, has classification "religious personnel";
}
then {
  $p has geneva-convention-protection true;
};

elements sub relation,
  relates defendant,
  relates victim, 
  relates statute;

criminal-liability sub relation,
  relates crime,
  relates defendant,
  relates victim, 
  relates statute;

statute sub entity,
  owns name,
  plays elements:statute,
  plays criminal-liability:statute;

rule genocide-liability:
when {
  $c isa crime, has name "Genocide";
  (defendant: $d, victim: $v, statute: $s) isa elements; 
}
then {
  (crime: $c, defendant: $d, victim: $v, statute: $s) isa criminal-liability;
};

rule conflict-geneva-convention-liability:
when {
  $p isa person, has geneva-convention-protection true;
  ($d, $v) isa international-conflict;
  (defendant: $d, victim: $v, statute: $s) isa elements;
  $c isa crime, has name "War Crime"; 
}
then {
  (crime: $c, defendant: $d, victim: $v, statute: $s) isa criminal-liability;
};

# This relation needs to be inserted directly relate two people involved in a conflict as it is defined here. It may 
# be better to infer that the two are on opposing sides each fighting for a nation where those nations are in conflict
international-conflict sub relation,
  relates participant;

act sub relation,
  relates party;

killing sub act,
  relates victim as party,
  relates perpetrator as party;

torture sub act,
  relates victim as party,
  relates perpetrator as party;

rule killing-under-article-8-2-a-i:
when {
  (perpetrator: $pp, victim: $v) isa killing;
   $s isa statute, has name "article-8-2-a-i";
}
then {
  (defendant: $pp, victim: $v, statute: $s) isa elements;
};

rule turture-under-article-8-2-a-ii:
when {
  (perpetrator: $pp, victim: $v) isa torture;
   $s isa statute, has name "article-8-2-a-ii";
}
then {
  (defendant: $pp, victim: $v, statute: $s) isa elements;
};

```

The data necessary for the rules in the schema:

```auto
insert $c isa crime, has name "Genocide";
insert $c isa crime, has name "War Crime";
insert $c isa crime, has name "Crime Against Humanity";
insert $c isa crime, has name "Crime of Aggression";
insert $s isa statute, has name "article-8-2-a-i";
insert $s isa statute, has name "article-8-2-a-ii";

```

Data to give us some examples to work with:

```auto
insert 
$p1 isa person, has classification "civilian";
$p2 isa person;
(victim: $p1, perpetrator: $p2) isa killing;

insert 
$p1 isa person, has classification "prisoner-of-war";
$p2 isa person;
(victim: $p1, perpetrator: $p2) isa torture;
(participant: $p1, participant:$p2) isa international-conflict;

```

Add all data using a data session and a write transaction.

Then, in a data session, in a read transaction ensuring `infer=true` we can look for the insight we’re looking for: who faces criminal liability?

```auto
match
$cl(crime: $c, defendant: $d, victim: $v, statute: $s) isa criminal-liability; 
$c has name $crime-name;
$s has name $statute-name;
$v has classification $classification;

```

You should get some answers back! Without meaning anything political it seems like the rule for genocide applies too widely. I’ve just interpreted it from your prolog snippet, any suggestions?

Thanks for posting such a poignant example! Hope this helps.
