#!/usr/bin/python3 # from lark import Lark, InlineTransformer, inline_args import sys, re from rdflib import Graph, URIRef as U, Literal as L from rdflib.namespace import SKOS, XSD, RDFS, OWL, RDF #from rdflib.namespace SKOS, XSD, RDFS, OWL, RDF ## from rtdlib import * grammar=r''' start : ontologia ontologia : "ontologia"i ID (conceitos | relacoes | individuos| triplos)* conceitos : "conceitos"i "{" conceito ("," conceito )* "}" relacoes : "relacoes"i "{" relacao ("," relacao )* "}" individuos : "individuos"i "{" individuo ("," individuo)* "}" triplos : "triplos"i "{" triplo* "}" conceito : id | id "[" props "]" -> conceito_2 props : (id ":" type /,/? )("," (id ":" type))* type : id relacao : id | id "/" id -> relacao_inv individuo : id triplo : id "=" id "=>" val ("," id "=>" val)* ";" val : id -> id | STR -> id | TOK -> id id : ID ID .3 : /\w+(-\w+)*/ STR .1 : /"([^"]|\\")*"/ TOK .1 : /'([^"]|\\')*'/ WS .1 : /[ \t\n]/ COMS .1 : /#.*/ %ignore COMS %ignore WS ''' def l2dict(v): return dict(zip(v[0::2],v[1::2])) ## list to dict def l2lofpairs(v): return zip(v[0::2],v[1::2]) ## list to listOfPairs ## def addtrip(self,a,b,c): class CalculateOnto(InlineTransformer): def ontologia(self,name,*v): self.onto["meta"]["name"]=name def __init__(self) : self.onto = { "meta":{"name":None}, "con":{}, "rel":{"is-a":{},"iof":{},"pof":{}}, "inv":{}, "ind":{}, "tri":{}} def start(self,o): return(self.onto) def id(self,i): return str(i) def props(self, *v): return l2dict(v) def type(self, v): return v def individuo(self,c): self.onto["ind"][c] = {} return c def relacao(self,c): self.onto["rel"][c] = {} return c def relacao_inv(self,i1,i2): self.onto["rel"][i1] = {} self.onto["rel"][i2] = {} self.onto["inv"][i2] = i1 self.onto["inv"][i1] = i2 return (i1,i2) def triplo(self,i1,i2,i3,*p): self.onto["tri"][(i1,i2,i3)] = {} for x,y in l2lofpairs(p): self.onto["tri"][(i1,x,y)] = {} return (i1,i2,i3,p) def triplo_2(self,i1,i2,i3): self.onto["tri"][(i1,i2,i3)] = {} return (i1,i2,i3) def conceito_2(self, c, p): self.onto["con"][c] = { "args" : p } return c def conceito(self,c): self.onto["con"][c] = {} return c parser = Lark(grammar, parser='lalr', transformer=CalculateOnto()) ON = "nada" def gera(ont): ON=Graph() ON.bind("jj","jj:/") ## ONTO = get_ontology("jj:"+ont["meta"]["name"]) ## ONTO.name = ont["meta"]["name"] ## ONTO.base_iri = "jj:" ## with ONTO: ## class name(DataProperty,FunctionalProperty): ## range=[str] # for c in ont["con"]: ## type(c,(Thing,),{"name":c}) # for c in ont["rel"]: ## if c in ont["inv"]: ## i = ont["inv"][c] ## type(c,(ObjectProperty,),{"name":c,"inverse_property" : i}) ## else: ## type(c,(ObjectProperty,),{"name":c}) for o,p,c in ont["tri"]: if p == "is-a": p = "OWL:ISA" if p == "iof": p = "OWL:IOF" if p == "pof": p = "OWL:POF" if p == "nome": p = "OWL:NOME" o = f"jj:{o}" p = f"jj:{p}" c = f"jj:{c}" if re.match(r'\w+$',c) : ON.add((U(o),U(p),U(c))) else: ON.add((U(o),U(p),L(c))) ON.serialize(destination=f"ONT-{ont['meta']['name']}") ## print(list(ONTO.classes())) ## ONTO.save(file= f"ONT-{ont['meta']['name']}") def main(): with open(sys.argv[1],"r") as f: p=parser.parse(f.read()) gera(p) # print(p) if __name__ == '__main__': main() # print(p.pretty())