pub struct Subject {
pub identity: Symbol,
pub labels: HashSet<String>,
pub properties: PropertyRecord,
}Expand description
Self-descriptive object with identity, labels, and properties.
Subject is designed to be the primary content type for patterns
(i.e., Pattern<Subject> will be the common use case).
A Subject contains:
- Identity: A required symbol identifier that uniquely identifies the subject
- Labels: A set of label strings that categorize or classify the subject
- Properties: A key-value map storing properties with rich value types
Note: This type only implements PartialEq, not Eq, because it contains Value
which uses f64 (f64 doesn’t implement Eq due to NaN != NaN).
§Examples
use pattern_core::{Subject, Symbol, Value};
use std::collections::{HashSet, HashMap};
let subject = Subject {
identity: Symbol("n".to_string()),
labels: {
let mut s = HashSet::new();
s.insert("Person".to_string());
s
},
properties: {
let mut m = HashMap::new();
m.insert("name".to_string(), Value::VString("Alice".to_string()));
m.insert("age".to_string(), Value::VInteger(30));
m
},
};§Usage with Pattern
use pattern_core::{Pattern, Subject, Symbol};
use std::collections::HashSet;
let subject = Subject {
identity: Symbol("n".to_string()),
labels: HashSet::new(),
properties: std::collections::HashMap::new(),
};
let pattern: Pattern<Subject> = Pattern {
value: subject,
elements: vec![],
};Fields§
§identity: SymbolSymbol identifier that uniquely identifies the subject.
The identity is always required. In gram notation, identities appear before labels and properties.
labels: HashSet<String>Set of label strings that categorize or classify the subject.
Labels provide classification information. The set can be empty (no labels)
or contain one or more unique labels. In gram notation, labels are prefixed
with : or :: and appear after the identity and before properties.
properties: PropertyRecordKey-value property map storing structured data about the subject.
Properties store attributes and metadata. The property record can be empty
(no properties) or contain any number of key-value pairs. In gram notation,
properties appear in curly braces: {name:"Alice", age:30}.
Implementations§
Source§impl Subject
impl Subject
Sourcepub fn from_id(identity: impl Into<String>) -> Subject
pub fn from_id(identity: impl Into<String>) -> Subject
Creates an identity-only Subject with no labels or properties.
Useful as a reference handle when passing to methods that accept &Subject
and only need the identity (e.g., add_relationship source/target args).
§Examples
use pattern_core::Subject;
let mut g = pattern_core::graph::StandardGraph::new();
let alice = Subject::build("alice").label("Person").done();
let bob = Subject::build("bob").label("Person").done();
g.add_node(alice.clone());
g.add_node(bob.clone());
g.add_relationship(Subject::build("r1").label("KNOWS").done(), &alice, &bob);
// When you only have an ID string, use from_id as a lightweight reference:
g.add_relationship(
Subject::build("r2").label("KNOWS").done(),
&Subject::from_id("alice"),
&Subject::from_id("bob"),
);Sourcepub fn build(identity: impl Into<String>) -> SubjectBuilder
pub fn build(identity: impl Into<String>) -> SubjectBuilder
Creates a SubjectBuilder with the given identity.
§Examples
use pattern_core::Subject;
let subject = Subject::build("alice")
.label("Person")
.property("name", "Alice")
.done();
assert_eq!(subject.identity.0, "alice");
assert!(subject.labels.contains("Person"));Trait Implementations§
Source§impl Combinable for Subject
Combination strategy for Subject that merges labels and properties.
impl Combinable for Subject
Combination strategy for Subject that merges labels and properties.
This strategy combines two subjects by:
- Using the identity from the first subject
- Taking the union of labels from both subjects
- Merging properties (values from the second subject overwrite the first)
§Semigroup Laws
This implementation satisfies associativity:
- Identity choice is associative (always picks leftmost)
- Label union is associative (set union is associative)
- Property merge is associative with right-bias (latter values win)
§Examples
use pattern_core::{Subject, Symbol, Combinable};
use std::collections::{HashMap, HashSet};
let s1 = Subject {
identity: Symbol("n1".to_string()),
labels: {
let mut s = HashSet::new();
s.insert("Person".to_string());
s
},
properties: HashMap::new(),
};
let s2 = Subject {
identity: Symbol("n2".to_string()),
labels: {
let mut s = HashSet::new();
s.insert("Employee".to_string());
s
},
properties: HashMap::new(),
};
// Merge combines labels and uses first identity
let merged = s1.combine(s2);
assert_eq!(merged.identity.0, "n1");
assert!(merged.labels.contains("Person"));
assert!(merged.labels.contains("Employee"));