### SHACLC Constraint Syntax Examples Source: https://context7.com/jeswr/shaclcjs/llms.txt Illustrates the compact syntax for defining various SHACL constraints using the `shaclc-parse` library in JavaScript. This covers cardinality, datatype restrictions, value ranges, pattern matching, and language tag constraints. ```javascript const { parse } = require('shaclc-parse'); // Comprehensive example of SHACLC constraint types const quads = parse(` PREFIX ex: PREFIX xsd: shape ex:DataValidationShape { # Cardinality constraints ex:requiredProperty [1..1] ; ex:optionalProperty [0..1] ; ex:multiValueProperty [1..*] ; ex:upToThree [0..3] ; # Datatype and value constraints ex:stringProp datatype=xsd:string minLength=5 maxLength=50 ; ex:integerProp datatype=xsd:integer minInclusive=1 maxInclusive=100 ; ex:decimalProp datatype=xsd:decimal minExclusive=0.0 maxExclusive=1000.0 ; # Pattern matching ex:email pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$" ; ex:zipCode pattern="^\\d{5}(-\\d{4})?$" ; # Value constraints ex:status in=[ex:Active ex:Pending ex:Closed] ; ex:category in=["electronics" "books" "clothing"] ; # Language tags ex:description languageIn=["en" "es" "fr"] . `) console.log(`Validation shape contains ${quads.length} constraint quads`) ``` -------------------------------- ### Integration with N3.js for RDF Storage, Querying, and Serialization Source: https://context7.com/jeswr/shaclcjs/llms.txt Demonstrates parsing SHACLC shapes and integrating the resulting RDFJS quads with the N3.js library. This enables storing the quads in an N3.Store, querying the data, and serializing it back to RDF formats like Turtle. The example shows basic store operations and serialization. ```javascript const { parse } = require('shaclc-parse'); const N3 = require('n3'); // Parse SHACLC and work with quads const quads = parse( ` PREFIX ex: shape ex:BookShape -> ex:Book { ex:title [1..1] datatype=xsd:string ; ex:author [1..*] datatype=xsd:string ; ex:isbn [1..1] pattern="^\\d{3}-\\d{10}$" ; ex:publishYear [1..1] datatype=xsd:integer minInclusive=1000 . } `, { baseIRI: 'http://example.org/library/' }); // Store quads in N3.Store const store = new N3.Store(); store.addQuads(quads); console.log(`Store contains ${store.size} quads`); // Query the store const bookShapeQuads = store.getQuads( N3.DataFactory.namedNode('http://example.org/test#BookShape'), null, null ); console.log(`BookShape has ${bookShapeQuads.length} related quads`); // Serialize back to Turtle const writer = new N3.Writer({ prefixes: quads.prefixes }); writer.addQuads(quads); writer.end((error, result) => { if (!error) { console.log('Turtle serialization:'); console.log(result); } }); ``` -------------------------------- ### Parse SHACLC with TypeScript Options Source: https://context7.com/jeswr/shaclcjs/llms.txt Demonstrates parsing SHACLC syntax in TypeScript using the `shaclc-parse` library with custom `ParseOptions`. This includes defining options for extended syntax and base IRI, and shows the type safety provided by TypeScript interfaces for configuration. ```typescript import { parse, ParseOptions } from 'shaclc-parse'; import type { Quad } from '@rdfjs/types'; // TypeScript usage with full type safety const options: ParseOptions = { extendedSyntax: true, baseIRI: 'http://example.org/base/' }; const quads = parse(` PREFIX ex: shape ex:ProductShape -> ex:Product { ex:sku [1..1] pattern="^[A-Z]{3}-\\d{4}$" ; ex:price [1..1] minInclusive=0 datatype=xsd:decimal ; ex:inStock [1..1] datatype=xsd:boolean . } `, options); // quads has type: Quad[] & { prefixes: Record } quads.forEach((quad: Quad) => { console.log(`${quad.subject.termType}: ${quad.subject.value}`); }); console.log('Prefixes:', quads.prefixes); ``` -------------------------------- ### Instantiate and Use Parser Class (JavaScript) Source: https://context7.com/jeswr/shaclcjs/llms.txt Demonstrates the usage of the reusable Parser class for processing multiple SHACLC documents. This approach is efficient when parsing several documents, allowing for stateful parsing operations. Each call to `parse` returns an array of RDF quads. ```javascript const { Parser } = require('shaclc-parse'); const parser = new Parser(); // Parse multiple documents with the same parser instance const shapes1 = parser.parse(` PREFIX ex: shape ex:Shape1 { ex:property1 [1..1] . } `); const shapes2 = parser.parse(` PREFIX ex: shape ex:Shape2 { ex:property2 [0..*] . } `, { baseIRI: 'http://example.org/shapes2' }); console.log(`Document 1: ${shapes1.length} quads`); console.log(`Document 2: ${shapes2.length} quads`); ``` -------------------------------- ### Parser Class Source: https://context7.com/jeswr/shaclcjs/llms.txt Provides a reusable parser instance for processing multiple SHACLC documents. Allows for stateful parsing operations. ```APIDOC ## Class: Parser ### Description The `Parser` class provides a reusable instance for parsing multiple SHACLC documents. This is useful when you need to parse several documents sequentially without re-initializing the parser. ### Methods #### new Parser() Creates a new instance of the `Parser` class. #### parse(str, options) Parses a SHACLC string using the parser instance and returns an array of RDF quads. Accepts the same `str` and `options` as the standalone `parse` function. ### Request Example ```javascript const { Parser } = require('shaclc-parse'); const parser = new Parser(); const shapes1 = parser.parse(` PREFIX ex: shape ex:Shape1 { ex:property1 [1..1] . } `); const shapes2 = parser.parse(` PREFIX ex: shape ex:Shape2 { ex:property2 [0..*] . } `, { baseIRI: 'http://example.org/shapes2' }); console.log(`Document 1: ${shapes1.length} quads`); console.log(`Document 2: ${shapes2.length} quads`); ``` ### Response #### Success Response (200) - **quads** (array) - An array of RDF quads representing the parsed SHACLC content for each call to `parse`. #### Response Example ```json { "quads": [ { "subject": {"value": "http://example.org/test#Shape1", "termType": "NamedNode"}, "predicate": {"value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "termType": "NamedNode"}, "object": {"value": "http://www.w3.org/ns/shacl#Shape", "termType": "NamedNode"} } ] } ``` ``` -------------------------------- ### parse(str, options) Source: https://context7.com/jeswr/shaclcjs/llms.txt Parses a SHACLC string and returns an array of RDF quads. Supports options for base IRI resolution and extended syntax. ```APIDOC ## POST /shaclc-parse ### Description Parses a SHACLC string into RDF quads. This function can be used with basic SHACLC syntax, with options to resolve relative IRIs using a base IRI, or to enable extended syntax for custom annotations and arbitrary Turtle statements. ### Method POST ### Endpoint /shaclc-parse ### Parameters #### Query Parameters - **baseIRI** (string) - Optional - The base IRI to resolve relative references. - **extendedSyntax** (boolean) - Optional - Enables extended SHACLC syntax mode. #### Request Body - **str** (string) - Required - The SHACLC content to parse. ### Request Example ```json { "str": "PREFIX ex: \n\nshape ex:PersonShape -> ex:Person {\n ex:name [1..1] minLength=1 maxLength=100 ;\n ex:age [0..1] minInclusive=0 maxInclusive=150 ;\n ex:email [1..*] pattern=\"^[\\w._%+-]+@[\\w.-]+\\.[A-Za-z]{2,}$\" .\n}", "options": { "baseIRI": "http://example.org/shapes/", "extendedSyntax": false } } ``` ### Response #### Success Response (200) - **quads** (array) - An array of RDF quads representing the parsed SHACLC content. - **prefixes** (object) - An object containing the defined prefixes. #### Response Example ```json { "quads": [ { "subject": {"value": "http://example.org/shapes/ex:PersonShape", "termType": "NamedNode"}, "predicate": {"value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "termType": "NamedNode"}, "object": {"value": "http://www.w3.org/ns/shacl#Shape", "termType": "NamedNode"} }, ... ], "prefixes": { "ex": "http://example.org/test#", "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#" } } ``` ``` -------------------------------- ### Extended SHACL: Arbitrary Statements (SHACLC) Source: https://github.com/jeswr/shaclcjs/blob/main/README.md Shows how to include arbitrary RDF statements using Turtle syntax at the end of a SHACL compact syntax file. This is useful for defining additional data alongside the shapes. ```shaclc shape ex:TestShape -> ex:TestClass1 ex:TestClass2 { targetNode=ex:TestNode targetSubjectsOf=ex:subjectProperty targetObjectsOf=ex:objectProperty . } ex:bob a ex:Person . ``` -------------------------------- ### Extended SHACL: Annotations on NodeShape (SHACLC) Source: https://github.com/jeswr/shaclcjs/blob/main/README.md Demonstrates how to add custom annotations to a NodeShape using extended SHACL compact syntax. This includes adding custom properties and blank node annotations directly before the shape's body. ```shaclc shape ex:TestShape -> ex:TestClass1 ex:TestClass2 ; ex:myCustomAnnotation ex:myCustomValue ; ex:myCustomBlankNodeAnnotation [ ex:myCustomList ( 1 ex:myCustomValue ) ] { targetNode=ex:TestNode targetSubjectsOf=ex:subjectProperty targetObjectsOf=ex:objectProperty . } ``` -------------------------------- ### Extended SHACL: Annotations on sh:property (SHACLC) Source: https://github.com/jeswr/shaclcjs/blob/main/README.md Illustrates how to add custom annotations to a property within a SHACL shape using extended syntax. This allows for metadata specific to the property path definition. ```shaclc shape ex:TestShape -> ex:TestClass1 ex:TestClass2 { ex:myPath [0..1] % ex:myCustomPropertyAnnotation ex:myCustomPropertyValue ; ex:myCustomPropertyAnnotation2 ex:myCustomPropertyValue2 ; % . } ``` -------------------------------- ### SHACLC Target Declarations for Node Validation Source: https://context7.com/jeswr/shaclcjs/llms.txt Shows how to specify target nodes, classes, or properties for SHACL shapes using the `shaclc-parse` library in JavaScript. This demonstrates directing validation to specific RDF resources or nodes related by properties. ```javascript const { parse } = require('shaclc-parse'); // Various target declaration methods const quads = parse(` PREFIX ex: shape ex:EmployeeShape -> ex:Employee ex:Manager { targetNode=ex:JohnDoe targetNode=ex:JaneSmith targetSubjectsOf=ex:employeeId targetObjectsOf=ex:reportsTo . ex:employeeId [1..1] datatype=xsd:string ; ex:department [1..1] ; ex:salary [0..1] datatype=xsd:decimal . } shape ex:SpecificNodeShape { targetNode=ex:SpecificInstance . ex:verified [1..1] datatype=xsd:boolean . } `, { baseIRI: 'http://example.org/hr/' }); console.log('Shapes with multiple target types:', quads.length, 'quads'); ``` -------------------------------- ### Parse SHACLC with Extended Syntax (JavaScript) Source: https://context7.com/jeswr/shaclcjs/llms.txt Enables an extended syntax mode for SHACLC parsing, allowing for custom annotations and arbitrary Turtle statements alongside standard SHACL constraints. This provides greater flexibility for complex RDF modeling. The output is an array of RDF quads. ```javascript const { parse } = require('shaclc-parse'); // Extended syntax with custom annotations and Turtle statements const quads = parse(` PREFIX ex: PREFIX rdfs: shape ex:PersonShape -> ex:Person ; rdfs:label "Person Shape" ; rdfs:comment "Validates person data" ; ex:version "1.0" { ex:name [1..1] % rdfs:label "Person Name" ; ex:priority "high" % ; ex:age [0..1] minInclusive=0 . } ex:Person a rdfs:Class ; rdfs:label "Person" . ex:SamplePerson a ex:Person ; ex:name "John Doe" ; ex:age 30 . `, { extendedSyntax: true }); console.log(`Generated ${quads.length} quads including custom annotations`); // The quads array includes SHACL constraints, custom annotations, and the additional RDF statements ``` -------------------------------- ### Parse SHACL Compact Syntax (TypeScript) Source: https://github.com/jeswr/shaclcjs/blob/main/README.md Parses a string containing SHACL compact syntax into RDF quads. It requires the 'shaclc-parse' library and can optionally take a base IRI for resolving relative URIs. ```typescript import { parse } from 'shaclc-parse' const quads = parse(` PREFIX ex: shape ex:TestShape -> ex:TestClass1 ex:TestClass2 { targetNode=ex:TestNode targetSubjectsOf=ex:subjectProperty targetObjectsOf=ex:objectProperty . } `, { baseIRI: "http://example.org/basic-shape-with-targets" }) ``` -------------------------------- ### Parse SHACLC String to RDF Quads (JavaScript) Source: https://context7.com/jeswr/shaclcjs/llms.txt Parses a SHACLC string into an array of RDF quads using the standard syntax. The function takes the SHACLC content as a string and an optional options object for base IRI resolution. It returns an array of RDFJS-compatible quads and available prefixes. ```javascript const { parse } = require('shaclc-parse'); // Basic usage with standard SHACLC syntax const quads = parse(` PREFIX ex: shape ex:PersonShape -> ex:Person { ex:name [1..1] minLength=1 maxLength=100 ; ex:age [0..1] minInclusive=0 maxInclusive=150 ; ex:email [1..*] pattern="^[\\w._%+-]+@[\\w.-]+\\.[A-Za-z]{2,}$" . } `, { baseIRI: "http://example.org/shapes/" }); console.log(`Generated ${quads.length} RDF quads`); console.log('Available prefixes:', quads.prefixes); // Output: Available prefixes: { rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', ... } ``` -------------------------------- ### Extended Syntax: Custom Property Annotations Source: https://context7.com/jeswr/shaclcjs/llms.txt Parses SHACLC shapes with custom metadata and annotations using extended syntax. This allows for richer documentation and tooling integration by embedding application-specific information directly within shape definitions. The output is a list of RDFJS quads. ```javascript const { parse } = require('shaclc-parse'); // Custom property annotations in extended mode const quads = parse( ` PREFIX ex: PREFIX ui: PREFIX doc: shape ex:FormSchema -> ex:UserInput ; doc:description "User registration form validation" ; doc:version "2.1.0" { ex:username [1..1] % ui:widget "text" ; ui:placeholder "Enter username" ; ui:helpText "Alphanumeric, 3-20 characters" ; doc:example "john_doe123" % pattern="^[a-zA-Z0-9_]{3,20}$" ; ex:password [1..1] % ui:widget "password" ; ui:strength "required" ; doc:securityNote "Must contain uppercase, lowercase, number, and special char" % minLength=8 ; ex:email [1..1] % ui:widget "email" ; ui:validationTiming "onBlur" % pattern="^[\\w._%+-]+@[\\w.-]+\\.[A-Za-z]{2,}$" . } `, { extendedSyntax: true, baseIRI: 'http://example.org/forms/' }); console.log(`Extended shape with ${quads.length} quads including UI annotations`); ``` -------------------------------- ### Nested SHACLC Shapes and Node Kinds Source: https://context7.com/jeswr/shaclcjs/llms.txt Demonstrates defining complex, hierarchical data validation rules in SHACLC using nested shapes and node kinds with the `shaclc-parse` library in JavaScript. This enables validation of structured data and relationships. ```javascript const { parse } = require('shaclc-parse'); // Complex nested shape example const quads = parse(` PREFIX ex: PREFIX xsd: shape ex:CompanyShape -> ex:Company { ex:name [1..1] datatype=xsd:string ; ex:employee [1..*] node=ex:EmployeeShape nodeKind=IRI ; ex:headquarters [1..1] node=ex:AddressShape nodeKind=BlankNode . } shape ex:EmployeeShape { ex:firstName [1..1] datatype=xsd:string ; ex:lastName [1..1] datatype=xsd:string ; ex:email [1..1] nodeKind=IRI ; ex:address [0..1] node=ex:AddressShape . } shape ex:AddressShape { ex:street [1..1] datatype=xsd:string ; ex:city [1..1] datatype=xsd:string ; ex:zipCode [1..1] pattern="^\\d{5}$" ; ex:country [1..1] in=["USA" "Canada" "Mexico"] . } `, { baseIRI: 'http://example.org/company/' }); console.log(`Nested shape structure with ${quads.length} constraint quads`); ``` -------------------------------- ### Extended Syntax: Arbitrary Turtle Statements Source: https://context7.com/jeswr/shaclcjs/llms.txt Parses a document containing both SHACLC shape definitions and arbitrary Turtle RDF statements. This allows for combining shape constraints with ontology definitions, instance data, or other RDF resources in a single file. The parser outputs a combined list of RDFJS quads. ```javascript const { parse } = require('shaclc-parse'); // Mixed SHACLC and Turtle syntax const quads = parse( ` PREFIX ex: PREFIX rdfs: PREFIX owl: # SHACLC shape definition shape ex:PersonShape -> ex:Person { ex:name [1..1] datatype=xsd:string ; ex:knows [0..*] node=ex:PersonShape . } # Standard Turtle statements follow ex:Person a owl:Class ; rdfs:label "Person"@en ; rdfs:comment "Represents a human person"@en ; rdfs:subClassOf ex:Agent . ex:Agent a owl:Class ; rdfs:label "Agent"@en . ex:knows a owl:ObjectProperty ; rdfs:domain ex:Person ; rdfs:range ex:Person ; rdfs:label "knows"@en . # Instance data ex:Alice a ex:Person ; ex:name "Alice Johnson" ; ex:knows ex:Bob . ex:Bob a ex:Person ; ex:name "Bob Smith" . `, { extendedSyntax: true, baseIRI: 'http://example.org/people/' }); console.log(`Combined document: ${quads.length} total quads`); console.log('Includes shapes, ontology, and instance data in one parse'); ``` -------------------------------- ### Enable Extended SHACL Syntax Parsing (TypeScript) Source: https://github.com/jeswr/shaclcjs/blob/main/README.md Enables the parsing of extended SHACL compact syntax, which allows for making arbitrary RDF statements within the syntax. This feature is enabled by setting the 'extendedSyntax' option to true. ```typescript const quads = parse(/*shaclc extended string*/, { extendedSyntax: true }) ``` -------------------------------- ### Parse SHACLC with Base IRI Resolution (JavaScript) Source: https://context7.com/jeswr/shaclcjs/llms.txt Parses SHACLC content while resolving relative IRIs against a specified base IRI. This is crucial for correctly interpreting references like fragments or relative paths within shape definitions. The function returns an array of RDF quads. ```javascript const { parse } = require('shaclc-parse'); // Using relative IRIs with baseIRI resolution const quads = parse(` PREFIX ex: shape <#MyShape> -> { <#myProperty> [1..1] . } `, { baseIRI: 'http://www.example.org/myontology' }); // All relative IRIs are resolved against the baseIRI // <#MyShape> becomes http://www.example.org/myontology#MyShape // becomes http://www.example.org/MyClass quads.forEach(quad => { console.log(`${quad.subject.value} ${quad.predicate.value} ${quad.object.value}`); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.