npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

openmath-rdf-parser

v3.0.1

Published

A parser to read and write textual formulas from / to OpenMath RDF syntax

Downloads

18

Readme

Test Status

OpenMath RDF Parser

A utility tool to convert between string representation and OpenMath RDF representation of mathematical expressions.

Motivation

Even simple expressions are super cumbersome to model in OpenMath RDF syntax. Consider the following expression:

x = y + 5;

In OpenMath RDF, this equation looks like that:

@prefix m: <http://openmath.org/vocab/math#>.

<http://example.org/ontology#f6c2045d-6053-4167-8da8-81882aa18532_eq> a m:Application;
    m:operator <http://www.openmath.org/cd/relation1#eq>.
_:n3-0 a m:Variable;
    m:name "x".
<http://example.org/ontology#87b80ac1-63c4-4eea-807d-fae477595c80_add> a m:Application;
    m:operator <http://www.openmath.org/cd/arith1#plus>.
_:n3-1 a m:Variable;
    m:name "y".
_:n3-2 a m:Literal;
    m:value 5.
<http://example.org/ontology#87b80ac1-63c4-4eea-807d-fae477595c80_add> m:arguments (_:n3-1 _:n3-2).
<http://example.org/ontology#f6c2045d-6053-4167-8da8-81882aa18532_eq> m:arguments (_:n3-0 <http://example.org/ontology#87b80ac1-63c4-4eea-807d-fae477595c80_add>).

The expression is broken down into a tree. On the top level, there is an OpenMath Application with operator "=" and two arguments for the left and right of the equation. The right side is itself an Application with operator "+" and the two arguments "y" and "5".

But everybody will agree that this RDF structure is not easy to create manually. This is where this OpenMathRdfParser comes into play. It automatically generates OpenMath RDF from a string-based formula and vice versa.

How to use

Install the library into your project using npm install openmath-rdf-parser and create an instance of the OmRdfParser class:

import { OmRdfParser } from "openmath-rdf-parser";
const parser = new OmRdfParser();

Use the functions below to convert from Open Math RDF to plain string formulas or from plain string formulas to an Open Math RDF representation.

Convert Open Math RDF to string formulas

There are two functions to get formulas in RDF and convert them to a string representation. The function fromOpenMath() converts a single formula (identified by an IRI of one Open Math application) and returns it as a FormulaResult object.

allFromOpenMath

The function allFromOpenMath(rdfString: string) finds all OpenMath formulas inside rdfString and convert them into an array of FormulaResult objects, which have the following structure:

class FormulaResult {
	context: string, 
	formula: string,
	rootApplicationIri: string,
}

In this object, formula stores the actual formula. context contains the IRI of the element that the OpenMath application is connected to. And rootApplicationIri is the IRI of the root application, i.e., the application acting as the root element of a formula.

fromOpenMath

Use fromOpenMath(rdfString: string, rootApplicationIri: string) to convert from one particular OpenMath RDF representation to a string representation. The parameters to pass are:

  • rdfString: RDF dataset that contains an OpenMath application to be converted to its string representation
  • rootApplicationIri: IRI of the application to be transformed. Your rdfString may contain multiple expressions in the form of OpenMath applications. The parameter rootApplicationIri refers to the one you want to conver to its string representation.

The function fromOpenMath() always returns one individual FormulaResult object (structure see above).

Convert string formulas to Open Math RDF

toOpenMath

Use toOpenMath(formula: PrefixedFormula | string) to convert from a mathematical formula in plain string representation to its OpenMath RDF representation. There is only one parameter to pass, but this parameter allows for multiple options.

  • formula: In the simplest case, this is a string representation of a formula without any references to ontological entities, e.g., x + y <= 2. For such cases, blank nodes are created representing x and y.

At some point, you might want to express a formula containing RDF resources and thus you have to integrate IRIs into your formulas. You can use full IRIs without any issues. As a simple example, consider the following. You can use IRIs like http://example.org/x#a and pass the formula as a string.

const input = "http://example.org/x#a = 5";
const result = await p.toOpenMath(input);

If you're feeling lazy and prefer to use prefixes to shorten IRIs, you can do so. But in these cases, you need to provide the prefixes so that the parser can handle them. In such cases, you must pass an object of type PrefixedFormula to the toOpenMath function. See this examples from the tests:

const input = 'x:a = y:b + 5';
const prefixes = new Map<string, string>();
prefixes.set('x', 'http://example.org/x#');
prefixes.set('y', 'http://example.org/y#');

const prefixedFormula: PrefixedFormula = {
	prefixes: prefixes, 
	formula: input
};
const result = await p.toOpenMath(prefixedFormula);