Skip to content

Commit

Permalink
#333 begin modeling support for compound literals (TODO: serializatio…
Browse files Browse the repository at this point in the history
…n, deserialization)
  • Loading branch information
Marco De Salvo committed Mar 12, 2024
1 parent af16635 commit ee67dbb
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 6 deletions.
168 changes: 167 additions & 1 deletion RDFSharp.Test/Model/RDFPlainLiteralTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void ShouldCreatePlainLiteral(string value)

Assert.IsNotNull(pl);
Assert.IsFalse(pl.HasLanguage());
Assert.IsFalse(pl.HasDirection());
Assert.IsTrue(pl.ToString().Equals(value ?? ""));
}

Expand All @@ -61,6 +62,7 @@ public void ShouldCreatePlainLiteralWithEmptyLanguage(string value, string langu

Assert.IsNotNull(pl);
Assert.IsFalse(pl.HasLanguage());
Assert.IsFalse(pl.HasDirection());
Assert.IsTrue(pl.ToString().Equals(value ?? ""));
}

Expand All @@ -78,8 +80,172 @@ public void ShouldCreatePlainLiteralWithLanguage(string value, string language)

Assert.IsNotNull(pl);
Assert.IsTrue(pl.HasLanguage());
Assert.IsFalse(pl.HasDirection());
Assert.IsTrue(pl.ToString().Equals(string.Concat(value, "@", language.ToUpperInvariant())));
}
}

[DataTestMethod]
[DataRow("donal duck")]
public void ShouldCreateUnlanguagedPlainLiteralWithLTRDirection(string value)
{
RDFPlainLiteral pl = new RDFPlainLiteral(value);
pl.SetLeftToRightDirection();

Assert.IsNotNull(pl);
Assert.IsFalse(pl.HasLanguage());
Assert.IsTrue(pl.HasDirection());
Assert.IsTrue(string.Equals(pl.Direction, "ltr"));
Assert.IsTrue(pl.ToString().Equals(value));
}

[DataTestMethod]
[DataRow("donal duck")]
public void ShouldCreateUnlanguagedPlainLiteralWithRTLDirection(string value)
{
RDFPlainLiteral pl = new RDFPlainLiteral(value);
pl.SetRightToLeftDirection();

Assert.IsNotNull(pl);
Assert.IsFalse(pl.HasLanguage());
Assert.IsTrue(pl.HasDirection());
Assert.IsTrue(string.Equals(pl.Direction, "rtl"));
Assert.IsTrue(pl.ToString().Equals(value));
}

[DataTestMethod]
[DataRow("donal duck", "en")]
[DataRow("donal duck", "en-US")]
[DataRow("donal duck", "en-US-25")]
[DataRow("donal duck@en-US", "en-US")]
[DataRow("donal duck@", "en")]
[DataRow("", "en")]
[DataRow(null, "en")]
public void ShouldCreatePlainLiteralWithLanguageWithLTRDirection(string value, string language)
{
RDFPlainLiteral pl = new RDFPlainLiteral(value, language);
pl.SetLeftToRightDirection();

Assert.IsNotNull(pl);
Assert.IsTrue(pl.HasLanguage());
Assert.IsTrue(pl.HasDirection());
Assert.IsTrue(string.Equals(pl.Direction, "ltr"));
Assert.IsTrue(pl.ToString().Equals(string.Concat(value, "@", language.ToUpperInvariant())));
}

[DataTestMethod]
[DataRow("donal duck", "en")]
[DataRow("donal duck", "en-US")]
[DataRow("donal duck", "en-US-25")]
[DataRow("donal duck@en-US", "en-US")]
[DataRow("donal duck@", "en")]
[DataRow("", "en")]
[DataRow(null, "en")]
public void ShouldCreatePlainLiteralWithLanguageWithRTLDirection(string value, string language)
{
RDFPlainLiteral pl = new RDFPlainLiteral(value, language);
pl.SetRightToLeftDirection();

Assert.IsNotNull(pl);
Assert.IsTrue(pl.HasLanguage());
Assert.IsTrue(pl.HasDirection());
Assert.IsTrue(string.Equals(pl.Direction, "rtl"));
Assert.IsTrue(pl.ToString().Equals(string.Concat(value, "@", language.ToUpperInvariant())));
}

[TestMethod]
public void ShouldReifyUnlanguagedToCompoundLiteral()
{
RDFPlainLiteral pl = new RDFPlainLiteral("hello");

RDFGraph cl = pl.ReifyToCompoundLiteral();
RDFResource clRepresentative = new RDFResource(string.Concat("bnode:", pl.PatternMemberID.ToString()));

Assert.IsNotNull(cl);
Assert.IsTrue(cl.TriplesCount == 2);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL, null].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.VALUE, null, new RDFPlainLiteral("hello")].TriplesCount == 1);
}

[TestMethod]
public void ShouldReifyUnlanguagedLTRToCompoundLiteral()
{
RDFPlainLiteral pl = new RDFPlainLiteral("hello");
pl.SetLeftToRightDirection();

RDFGraph cl = pl.ReifyToCompoundLiteral();
RDFResource clRepresentative = new RDFResource(string.Concat("bnode:", pl.PatternMemberID.ToString()));

Assert.IsNotNull(cl);
Assert.IsTrue(cl.TriplesCount == 3);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL, null].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.VALUE, null, new RDFPlainLiteral("hello")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.DIRECTION, null, new RDFPlainLiteral("ltr")].TriplesCount == 1);
}

[TestMethod]
public void ShouldReifyUnlanguagedRTLToCompoundLiteral()
{
RDFPlainLiteral pl = new RDFPlainLiteral("hello");
pl.SetRightToLeftDirection();

RDFGraph cl = pl.ReifyToCompoundLiteral();
RDFResource clRepresentative = new RDFResource(string.Concat("bnode:", pl.PatternMemberID.ToString()));

Assert.IsNotNull(cl);
Assert.IsTrue(cl.TriplesCount == 3);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL, null].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.VALUE, null, new RDFPlainLiteral("hello")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.DIRECTION, null, new RDFPlainLiteral("rtl")].TriplesCount == 1);
}

[TestMethod]
public void ShouldReifyLanguagedToCompoundLiteral()
{
RDFPlainLiteral pl = new RDFPlainLiteral("hello", "en-US");

RDFGraph cl = pl.ReifyToCompoundLiteral();
RDFResource clRepresentative = new RDFResource(string.Concat("bnode:", pl.PatternMemberID.ToString()));

Assert.IsNotNull(cl);
Assert.IsTrue(cl.TriplesCount == 3);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL, null].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.VALUE, null, new RDFPlainLiteral("hello")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.LANGUAGE, null, new RDFPlainLiteral("EN-US")].TriplesCount == 1);
}

[TestMethod]
public void ShouldReifyLanguagedLTRToCompoundLiteral()
{
RDFPlainLiteral pl = new RDFPlainLiteral("hello","en-US");
pl.SetLeftToRightDirection();

RDFGraph cl = pl.ReifyToCompoundLiteral();
RDFResource clRepresentative = new RDFResource(string.Concat("bnode:", pl.PatternMemberID.ToString()));

Assert.IsNotNull(cl);
Assert.IsTrue(cl.TriplesCount == 4);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL, null].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.VALUE, null, new RDFPlainLiteral("hello")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.LANGUAGE, null, new RDFPlainLiteral("EN-US")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.DIRECTION, null, new RDFPlainLiteral("ltr")].TriplesCount == 1);
}

[TestMethod]
public void ShouldReifyLanguagedRTLToCompoundLiteral()
{
RDFPlainLiteral pl = new RDFPlainLiteral("hello", "en-US");
pl.SetRightToLeftDirection();

RDFGraph cl = pl.ReifyToCompoundLiteral();
RDFResource clRepresentative = new RDFResource(string.Concat("bnode:", pl.PatternMemberID.ToString()));

Assert.IsNotNull(cl);
Assert.IsTrue(cl.TriplesCount == 4);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL, null].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.VALUE, null, new RDFPlainLiteral("hello")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.LANGUAGE, null, new RDFPlainLiteral("EN-US")].TriplesCount == 1);
Assert.IsTrue(cl[clRepresentative, RDFVocabulary.RDF.DIRECTION, null, new RDFPlainLiteral("rtl")].TriplesCount == 1);
}
#endregion
}
}
41 changes: 41 additions & 0 deletions RDFSharp/Model/RDFPlainLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public class RDFPlainLiteral : RDFLiteral
/// Optional language of the plain literal
/// </summary>
public string Language { get; internal set; }

/// <summary>
/// Optional direction of the plain literal (ltr,rtl)
/// </summary>
public string Direction { get; internal set; }
#endregion

#region Ctors
Expand Down Expand Up @@ -71,6 +76,42 @@ public override string ToString()
/// </summary>
public bool HasLanguage()
=> !string.IsNullOrEmpty(Language);

/// <summary>
/// Checks if the plain literal has a direction
/// </summary>
public bool HasDirection()
=> !string.IsNullOrEmpty(Direction);

/// <summary>
/// Sets the plain literal to have "ltr" direction
/// </summary>
public void SetLeftToRightDirection()
=> Direction = "ltr";

/// <summary>
/// Sets the plain literal to have "rtl" direction
/// </summary>
public void SetRightToLeftDirection()
=> Direction = "rtl";

/// <summary>
/// Gets a graph representation of the plain literal as rdf:CompoundLiteral
/// </summary>
public RDFGraph ReifyToCompoundLiteral()
{
RDFGraph compoundLiteralGraph = new RDFGraph();

RDFResource compoundLiteralRepresentative = new RDFResource(string.Concat("bnode:", PatternMemberID.ToString()));
compoundLiteralGraph.AddTriple(new RDFTriple(compoundLiteralRepresentative, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.COMPOUND_LITERAL));
compoundLiteralGraph.AddTriple(new RDFTriple(compoundLiteralRepresentative, RDFVocabulary.RDF.VALUE, new RDFPlainLiteral(Value)));
if (HasLanguage())
compoundLiteralGraph.AddTriple(new RDFTriple(compoundLiteralRepresentative, RDFVocabulary.RDF.LANGUAGE, new RDFPlainLiteral(Language)));
if (HasDirection())
compoundLiteralGraph.AddTriple(new RDFTriple(compoundLiteralRepresentative, RDFVocabulary.RDF.DIRECTION, new RDFPlainLiteral(Direction)));

return compoundLiteralGraph;
}
#endregion
}
}
5 changes: 0 additions & 5 deletions RDFSharp/Model/Vocabularies/RDF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,6 @@ public static class RDF
/// </summary>
public static readonly RDFResource REST = new RDFResource(string.Concat(RDF.BASE_URI,"rest"));

/// <summary>
/// rdf:dir
/// </summary>
public static readonly RDFResource DIR = new RDFResource(string.Concat(RDF.BASE_URI, "dir"));

/// <summary>
/// rdf:CompoundLiteral
/// </summary>
Expand Down

0 comments on commit ee67dbb

Please sign in to comment.