v-individual-model 0.2.6

individual model, part of the veda platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# Examples

This document provides practical examples of using the V-Individual-Model library.

## Basic Usage

### Creating and Populating Individuals

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::datatype::Lang;

fn main() {
    // Create a new individual
    let mut person = Individual::default();

    // Set the identifier
    person.set_id("example:person:john-doe");

    // Add basic information
    person.add_string("rdfs:label", "John Doe", Lang::none());
    person.add_string("rdfs:label", "Джон Доу", Lang::new_from_str("ru"));
    person.add_integer("foaf:age", 30);
    person.add_uri("rdf:type", "foaf:Person");
    person.add_bool("schema:active", true);

    // Add contact information
    person.add_uri("foaf:mbox", "mailto:john.doe@example.com");
    person.add_string("foaf:phone", "+1-555-0123", Lang::none());

    // Add work information
    person.add_string("schema:jobTitle", "Software Engineer", Lang::new_from_str("en"));
    person.add_uri("schema:worksFor", "example:organization:acme-corp");

    println!("Created person: {}", person.get_id());
}
```

### Working with Different Data Types

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::datatype::Lang;

fn create_product() {
    let mut product = Individual::default();
    product.set_id("example:product:widget-123");

    // String with language support
    product.add_string("schema:name", "Super Widget", Lang::new_from_str("en"));
    product.add_string("schema:name", "Супер Виджет", Lang::new_from_str("ru"));

    // Numeric types
    product.add_integer("schema:sku", 12345);
    product.add_decimal_from_str("schema:price", "99.99");
    product.add_decimal_from_i64("schema:stockQuantity", 150);

    // URI references
    product.add_uri("schema:category", "example:category:electronics");
    product.add_uri("schema:manufacturer", "example:organization:acme-corp");

    // Date/time
    product.add_datetime_from_str("schema:releaseDate", "2023-06-15T00:00:00Z");

    // Boolean flags
    product.add_bool("schema:isAvailable", true);
    product.add_bool("schema:isDiscontinued", false);
}
```

## Serialization Examples

### JSON Operations

```rust
use v_individual_model::onto::json2individual;
use serde_json::Value;

fn json_example() -> Result<(), Box<dyn std::error::Error>> {
    // Create individual from JSON
    let json_str = r#"
    {
        "@": "example:person:123",
        "rdfs:label": [
            {"data": "Alice Smith", "lang": "en", "type": "String"}
        ],
        "foaf:age": [{"data": 28, "type": "Integer"}],
        "schema:email": [{"data": "alice@example.com", "type": "String"}]
    }
    "#;

    let json_value: Value = serde_json::from_str(json_str)?;
    let mut person = Individual::default();

    json2individual::parse_json_to_individual(&json_value, &mut person);

    // Access the data
    let name = person.get_first_literal("rdfs:label").unwrap();
    let age = person.get_first_integer("foaf:age").unwrap();

    println!("Name: {}, Age: {}", name, age);

    // Convert back to JSON
    let json_output = person.get_obj().as_json_str();
    println!("JSON output: {}", json_output);

    Ok(())
}
```

### MessagePack Operations

```rust
use v_individual_model::onto::individual::{Individual, RawObj};
use v_individual_model::onto::individual2msgpack;
use v_individual_model::onto::msgpack2individual;
use v_individual_model::onto::datatype::Lang;

fn msgpack_example() -> Result<(), Box<dyn std::error::Error>> {
    // Create and populate individual
    let mut person = Individual::default();
    person.set_id("example:person:456");
    person.add_string("rdfs:label", "Bob Johnson", Lang::none());
    person.add_integer("foaf:age", 35);

    // Serialize to MessagePack
    let mut msgpack_out = Vec::new();
    individual2msgpack::to_msgpack(&person, &mut msgpack_out)?;

    // Deserialize from MessagePack
    let mut raw_obj = RawObj::new(msgpack_out);
    let parsed_uri = msgpack2individual::parse_msgpack(&mut raw_obj)?;

    let mut parsed_person = Individual::new_raw(raw_obj);
    parsed_person.parse_all();

    let name = parsed_person.get_first_literal("rdfs:label").unwrap();
    println!("Parsed: {} - {}", parsed_uri, name);

    Ok(())
}
```

## Query and Retrieval Examples

### Retrieving Data

```rust
use v_individual_model::onto::individual::Individual;

fn query_examples(person: &mut Individual) {
    // Get single values
    if let Some(name) = person.get_first_literal("rdfs:label") {
        println!("Name: {}", name);
    }

    if let Some(age) = person.get_first_integer("foaf:age") {
        println!("Age: {}", age);
    }

    // Get all values for a predicate
    if let Some(emails) = person.get_literals("foaf:mbox") {
        println!("Email addresses:");
        for email in emails {
            println!("  - {}", email);
        }
    }

    // Get all predicates
    let predicates = person.get_predicates();
    println!("Available predicates: {:?}", predicates);

    // Check existence
    if person.is_exists("foaf:knows") {
        println!("Person has known contacts");
    }

    // Check for specific values
    if person.any_exists("rdf:type", &["foaf:Person", "schema:Person"]) {
        println!("This is a person entity");
    }
}
```

### Working with Language Tags

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::datatype::Lang;

fn language_examples() {
    let mut article = Individual::default();
    article.set_id("example:article:123");

    // Add title in multiple languages
    article.add_string("dct:title", "Introduction to Rust", Lang::new_from_str("en"));
    article.add_string("dct:title", "Введение в Rust", Lang::new_from_str("ru"));
    article.add_string("dct:title", "Rust 简介", Lang::new_from_str("zh"));

    // Get title with language preference
    let preferred_langs = vec![
        Lang::new_from_str("ru"),  // Russian first
        Lang::new_from_str("en"),  // English second
        Lang::none()               // Any language as fallback
    ];

    if let Some(title) = article.get_first_literal_with_lang("dct:title", &preferred_langs) {
        println!("Title (preferred language): {}", title);
    }

    // Get all titles with their languages
    if let Some(titles) = article.get_literals("dct:title") {
        println!("All titles:");
        for title in titles {
            println!("  - {}", title);
        }
    }
}
```

## Advanced Operations

### Merging and Manipulating Individuals

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::datatype::Lang;

fn merge_examples() -> Result<(), Box<dyn std::error::Error>> {
    let mut person1 = Individual::default();
    person1.set_id("example:person:1");
    person1.add_string("foaf:firstName", "John", Lang::none());
    person1.add_string("foaf:lastName", "Doe", Lang::none());

    let mut person2 = Individual::default();
    person2.set_id("example:person:1"); // Same ID
    person2.add_string("foaf:phone", "+1-555-0100", Lang::none());
    person2.add_uri("foaf:knows", "example:person:2");

    // Apply predicates from person2 to person1
    person1.apply_predicate_as_add_unique("foaf:knows", &mut person2);

    // Override phone number
    person1.apply_predicate_as_set("foaf:phone", &mut person2);

    println!("Merged person has {} predicates", person1.get_predicates().len());

    Ok(())
}
```

### Working with Collections

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::datatype::Lang;

fn collection_example() {
    let mut organization = Individual::default();
    organization.set_id("example:org:acme");

    // Add multiple employees
    organization.add_uri("schema:employee", "example:person:alice");
    organization.add_uri("schema:employee", "example:person:bob");
    organization.add_uri("schema:employee", "example:person:charlie");

    // Add multiple addresses
    organization.add_string("schema:address", "123 Main St, Anytown, USA", Lang::none());
    organization.add_string("schema:address", "456 Second Ave, Somewhere, USA", Lang::none());

    // Get all employees
    if let Some(employees) = organization.get_literals("schema:employee") {
        println!("Organization has {} employees", employees.len());
        for employee in employees {
            println!("  - {}", employee);
        }
    }
}
```

## Error Handling

### Comprehensive Error Handling

```rust
use v_individual_model::onto::individual::{Individual, RawObj};
use v_individual_model::onto::msgpack2individual;
use std::error::Error;

fn robust_example() -> Result<(), Box<dyn Error>> {
    let mut person = Individual::default();

    // Safe parsing with error handling
    match person.get_first_literal_or_err("foaf:name") {
        Ok(name) => println!("Name: {}", name),
        Err(err) => println!("Name not found: {}", err),
    }

    // Safe numeric conversion
    if let Some(age_str) = person.get_first_literal("foaf:age") {
        match age_str.parse::<i64>() {
            Ok(age) => println!("Parsed age: {}", age),
            Err(_) => println!("Invalid age format"),
        }
    }

    // Safe MessagePack operations
    let mut raw_obj = RawObj::new(vec![1, 2, 3]); // Invalid data
    match msgpack2individual::parse_msgpack(&mut raw_obj) {
        Ok(uri) => println!("Parsed URI: {}", uri),
        Err(code) => println!("Parse error code: {}", code),
    }

    Ok(())
}
```

## Performance Patterns

### Efficient Bulk Operations

```rust
use v_individual_model::onto::individual::Individual;

fn bulk_processing_example(individuals: Vec<Individual>) {
    // Parse all data at once for bulk operations
    let mut parsed_individuals = Vec::new();

    for mut indv in individuals {
        indv.parse_all(); // Parse everything at once
        parsed_individuals.push(indv);
    }

    // Now all data is readily available
    for indv in &parsed_individuals {
        if let (Some(name), Some(age)) = (
            indv.get_first_literal("foaf:name"),
            indv.get_first_integer("foaf:age")
        ) {
            println!("{} is {} years old", name, age);
        }
    }
}
```

### Memory-Efficient Processing

```rust
use v_individual_model::onto::individual::{Individual, RawObj};
use v_individual_model::onto::msgpack2individual;

fn memory_efficient_example(large_raw_data: RawObj) {
    // Process large datasets without loading everything into memory
    let mut person = Individual::new_raw(large_raw_data);

    // Only parse what you need
    if person.get_first_bool("schema:active").unwrap_or(false) {
        // Parse additional data only for active users
        msgpack2individual::parse_msgpack_to_predicate("foaf:name", &mut person).ok();
        msgpack2individual::parse_msgpack_to_predicate("foaf:mbox", &mut person).ok();

        // Process active user
        if let Some(email) = person.get_first_literal("foaf:mbox") {
            println!("Sending email to: {}", email);
        }
    }
}
```

## Real-World Use Cases

### User Profile Management

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::json2individual;
use v_individual_model::onto::datatype::Lang;
use std::error::Error;

fn create_user_profile(user_data: serde_json::Value) -> Result<Individual, Box<dyn Error>> {
    let mut profile = Individual::default();

    // Parse JSON data
    if !json2individual::parse_json_to_individual(&user_data, &mut profile) {
        return Err("Failed to parse user profile JSON".into());
    }

    // Validate required fields
    if profile.get_first_literal("foaf:name").is_none() {
        return Err("Name is required".into());
    }

    // Set defaults
    if !profile.is_exists("schema:active") {
        profile.add_bool("schema:active", true);
    }

    // Add metadata
    profile.add_datetime("dct:created", chrono::Utc::now().timestamp());
    profile.add_uri("rdf:type", "foaf:Person");

    Ok(profile)
}
```

### Product Catalog

```rust
use v_individual_model::onto::individual::Individual;
use v_individual_model::onto::json2individual;
use std::error::Error;

fn create_product_catalog(products: Vec<serde_json::Value>) -> Result<Vec<Individual>, Box<dyn Error>> {
    let mut catalog = Vec::new();

    for product_data in products {
        let mut product = Individual::default();
        if !json2individual::parse_json_to_individual(&product_data, &mut product) {
            return Err(format!("Failed to parse product data: {:?}", product_data).into());
        }

        // Validate product data
        if product.get_first_literal("schema:name").is_none() {
            return Err(format!("Product missing name: {:?}", product_data).into());
        }

        // Add catalog metadata
        product.add_datetime("schema:dateModified", chrono::Utc::now().timestamp());
        product.add_bool("schema:inStock", true);

        catalog.push(product);
    }

    Ok(catalog)
}
```