Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bmspt 283 delegate methods #23

Open
wants to merge 30 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b73d91a
[BMSPT-283] Added delegate methods next to in memory bcf representation.
eriadam May 29, 2024
18d09f4
[BMSPT-283] added bcf builder delegate
BalintBende Jun 4, 2024
6db9ad0
[NOISSUE] fixed bug, entries in unordered list, added file builder test
BalintBende Jun 3, 2024
dd34edc
[NOISSUE] updated dotsettings
BalintBende Jun 3, 2024
7bec3fe
[NOISSUE] fixed comments
BalintBende Jun 4, 2024
1d65441
[BMSPT-289] fixed location and direction instance creation in bitmap,…
BalintBende Jun 4, 2024
f5780a9
[BMSPT-289] fixed location and direction instance creation in bitmap,…
BalintBende Jun 4, 2024
4ec8849
[BMSPT-289] added viewpoint builder, format, unit tests
BalintBende Jun 4, 2024
78be45f
[BMSPT-291] fixed possible null pointer issues
BalintBende Jun 5, 2024
eed0a80
Fixing incomplete byte read for images
eriadam Jun 10, 2024
639cd55
lint
eriadam Jun 10, 2024
6e7b798
Reorganised tests to separate the delegate pattern tests
eriadam Jun 12, 2024
cd29e7b
[BMSPT-292] organized schema xsd resources
BalintBende Jun 10, 2024
ac7add0
[BMSPT-292] added nullable types bcf 2.1
BalintBende Jun 10, 2024
eba078d
[BMSPT-292] added nullable types bcf 3.0
BalintBende Jun 10, 2024
d50ac3c
[BMSPT-292] fixed builder nullable inputs, fixed unit tests
BalintBende Jun 11, 2024
21e82f9
Feature/bmspt 275 bcfzip with stream (#17)
BalintBende Jun 13, 2024
aec1973
Fixing merge conflict
Jun 13, 2024
386aa93
[BMSPT-293] Added cancellation token to Converter stream operations. …
eriadam Jun 17, 2024
12a18d8
extended bcf builder with new functions, project, document, formating…
BalintBende Jun 18, 2024
4df1e0b
Updated Readme files
BalintBende Jun 18, 2024
c9767eb
[NOISSUE] fixed viewpoint extension in FileWriter
BalintBende Jun 25, 2024
81ecdb0
[BMSPT-283] Added delegate methods next to in memory bcf representation.
eriadam May 29, 2024
2da6f9f
[BMSPT-283] on markup created
BalintBende Aug 6, 2024
f7c3488
[BMSPT-283] builder delegate
BalintBende Aug 6, 2024
6957093
Merge branch 'develop' of github.com:bimspot/bcf-converter into featu…
BalintBende Aug 6, 2024
9b8583f
[BMSPT-283] builder delegate draft
BalintBende Aug 12, 2024
6e81181
[BMSPT-283] fixed BCF21 version, added proces stream fns to bcf build…
BalintBende Sep 5, 2024
93499c1
[BMSPT-283] deleted bcf21 process stream test
BalintBende Sep 5, 2024
7c099c7
[BMSPT-283] added nullable Orthogonal and Perspective camera in Visinfo
BalintBende Sep 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,42 @@ worker.ToBcf(bcf, BcfVersionEnum.Bcf30, outputStream, token);
await outputStream.FlushAsync();
```

#### Stream processing with delegate callbacks
The converters support processing BCF file streams with delegate-based callback
functions. This allows for the incremental construction of an in-memory BCF
representation, where a delegate function is invoked after each part of the BCF
is processed. It allows a more equal memory usage.

To use this feature, the user must create a custom delegate class which
implements the `IBcfBuilderDelegate` interface. This interface defines the
callback methods that will be triggered after each part of the BCF is built.

Implement custom delegate class:
```csharp
public class BcfBuilderDelegate : IBcfBuilderDelegate {
public IBcfBuilderDelegate.OnMarkupCreated<IMarkup>
MarkupCreated { get; } = Console.WriteLine;

public IBcfBuilderDelegate.OnExtensionsCreated<IExtensions>
ExtensionsCreated { get; } = Console.WriteLine;

public IBcfBuilderDelegate.OnProjectCreated<IProject>
ProjectCreated { get; } = Console.WriteLine;

public IBcfBuilderDelegate.OnDocumentCreated<IDocumentInfo>
DocumentCreatedCreated { get; } = Console.WriteLine;
}
```

Process BCF stream:
```csharp
var bcfBuilderDelegate = new BcfBuilderDelegate();
builder = new BcfBuilder();
builder.SetDelegate(bcfBuilderDelegate);
await using var stream = new FileStream(source, FileMode.Open, FileAccess.Read);
await builder.ProcessStream(stream);
```

## File Structure

The structure of the BCF is per [the standard][3]. There is, however, no
Expand Down
42 changes: 20 additions & 22 deletions bcf-toolkit.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">/Users/balintbende/Library/Caches/JetBrains/Rider2024.1/resharper-host/temp/Rider/vAny/CoverageData/_bcf-toolkit.-1315391344/Snapshot/snapshot.utdcvr</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=00488d9b_002Ddfb9_002D47fc_002Da7da_002D60da862eab61/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from Solution #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=4dc5af78_002D6a8f_002D4f5b_002D8833_002Df0ff81e0c049/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5c6b8aad_002D6497_002D435b_002D9344_002D7fb70eae79ba/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>


<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=64259359_002Daf01_002D4100_002Db5a8_002D06c7d51d9f83/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="BuildMaximumInformationBcfFromStreamTest" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::8113526D-8A68-4E3E-B4DB-CE235875DDD1::net8.0::tests.Builder.Bcf21.BcfBuilderTests.BuildMaximumInformationBcfFromStreamTest&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String>


<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=7d7fd643_002Dccec_002D4fc7_002D8938_002D22e83b255cb0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="BuildEmptyBcfFromStream" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::8113526D-8A68-4E3E-B4DB-CE235875DDD1::net8.0::tests.Builder.Bcf21.BcfBuilderTests.BuildEmptyBcfFromStream&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String>



Expand All @@ -35,21 +22,32 @@



<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=87607a24_002D24bc_002D46e1_002Da035_002D28c18ba9b643/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String>







<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=b2d5c176_002Dc751_002D42d4_002Da842_002D3ad7ad02438d/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="WorkerTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;NUnit3x::8113526D-8A68-4E3E-B4DB-CE235875DDD1::net8.0::Tests.WorkerTests&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String>






















Expand Down
11 changes: 8 additions & 3 deletions src/bcf-toolkit/Builder/Bcf21/BcfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ public partial class BcfBuilder : IBcfBuilder<
private readonly Bcf _bcf = new();

public BcfBuilder() {
_bcf.Version = new VersionBuilder()
.WithDefaults()
.Build();
SetVersion();
}

public BcfBuilder AddMarkup(Action<MarkupBuilder> builder) {
Expand All @@ -34,6 +32,13 @@ public BcfBuilder SetProject(Action<ProjectExtensionBuilder> builder) {
return this;
}

public BcfBuilder SetVersion() {
_bcf.Version = new VersionBuilder()
.WithDefaults()
.Build();
return this;
}

public BcfBuilder WithDefaults() {
this.AddMarkup(m => m.WithDefaults());
return this;
Expand Down
24 changes: 22 additions & 2 deletions src/bcf-toolkit/Builder/Bcf21/BcfBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf21;
using BcfToolkit.Utils;

namespace BcfToolkit.Builder.Bcf21;

public partial class BcfBuilder {
public async Task<Bcf> BuildFromStream(Stream source) {
private IBcfBuilderDelegate? _delegate;

public void SetDelegate(IBcfBuilderDelegate? builderDelegate) {
this._delegate = builderDelegate;
}

public Task ProcessStream(Stream source) {
if (_delegate is null) {
Console.WriteLine("IBcfBuilderDelegate is not set.");
return Task.CompletedTask;
}

var tasks = new List<Task> {
BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(source, _delegate.MarkupCreated),
BcfExtensions.ParseProject<ProjectExtension>(source, _delegate.ProjectCreated)
};

return Task.WhenAll(tasks);
}

public async Task<Bcf> BuildInMemoryFromStream(Stream source) {
_bcf.Markups =
await BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(source);
_bcf.Project = await BcfExtensions.ParseProject<ProjectExtension>(source);
Expand Down
1 change: 0 additions & 1 deletion src/bcf-toolkit/Builder/Bcf21/VisibilityBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using BcfToolkit.Builder.Bcf21.Interfaces;
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf21;
using BcfToolkit.Model.Interfaces;

Expand Down
12 changes: 9 additions & 3 deletions src/bcf-toolkit/Builder/Bcf30/BcfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public partial class BcfBuilder : IBcfBuilder<
ExtensionsBuilder,
DocumentInfoBuilder>,
IDefaultBuilder<BcfBuilder> {

private readonly Bcf _bcf = new();

public BcfBuilder() {
_bcf.Version = new VersionBuilder()
.WithDefaults()
.Build();
SetVersion();
}

public BcfBuilder AddMarkup(Action<MarkupBuilder> builder) {
Expand Down Expand Up @@ -50,6 +49,13 @@ public BcfBuilder SetDocument(Action<DocumentInfoBuilder> builder) {
return this;
}

public BcfBuilder SetVersion() {
_bcf.Version = new VersionBuilder()
.WithDefaults()
.Build();
return this;
}

public BcfBuilder WithDefaults() {
this
.AddMarkup(m => m.WithDefaults())
Expand Down
39 changes: 36 additions & 3 deletions src/bcf-toolkit/Builder/Bcf30/BcfBuilderExtensions.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls consider the following solutions for the "return Task.Completed case":
if (_delegate is null) {
throw new InvalidOperationException("IBcfBuilderDelegate is not set.");
}

if (_delegate is null) {
    return Task.FromException(new InvalidOperationException("IBcfBuilderDelegate is not set."));
}

Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Model.Bcf30;
using BcfToolkit.Utils;

namespace BcfToolkit.Builder.Bcf30;

public partial class BcfBuilder {

private IBcfBuilderDelegate? _delegate;

public void SetDelegate(IBcfBuilderDelegate? builderDelegate) {
this._delegate = builderDelegate;
}

public Task ProcessStream(Stream source) {
if (_delegate is null) {
Console.WriteLine("IBcfBuilderDelegate is not set.");
return Task.CompletedTask;
}

var tasks = new List<Task> {
BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(
source,
_delegate.MarkupCreated),
BcfExtensions.ParseExtensions<Extensions>(
source,
_delegate.ExtensionsCreated),
BcfExtensions.ParseProject<ProjectInfo>(
source,
_delegate.ProjectCreated),
BcfExtensions.ParseDocuments<DocumentInfo>(
source,
_delegate.DocumentCreatedCreated)
};

return Task.WhenAll(tasks);
}

//
/// <summary>
/// The method builds and returns an instance of BCF 3.0 object from the
/// specified file stream.
/// </summary>
/// <param name="source">The file stream.</param>
/// <returns>Returns the built object.</returns>
public async Task<Bcf> BuildFromStream(Stream source) {
_bcf.Markups =
await BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(source);
public async Task<Bcf> BuildInMemoryFromStream(Stream source) {
_bcf.Markups = await BcfExtensions.ParseMarkups<Markup, VisualizationInfo>(source);
_bcf.Extensions = await BcfExtensions.ParseExtensions<Extensions>(source);
_bcf.Project = await BcfExtensions.ParseProject<ProjectInfo>(source);
_bcf.Document = await BcfExtensions.ParseDocuments<DocumentInfo>(source);
Expand Down
1 change: 1 addition & 0 deletions src/bcf-toolkit/Builder/Bcf30/Interfaces/IBcfBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface IBcfBuilder<
out TDocumentInfoBuilder> :
IBuilder<Bcf>,
IFromStreamBuilder<Bcf> {

/// <summary>
/// Returns the builder object extended with a new `Markup`.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/bcf-toolkit/Builder/Bcf30/ViewPointBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public ViewPointBuilder SetGuid(string guid) {
return this;
}


public ViewPointBuilder SetSnapshotData(FileData? snapshotData) {
_viewPoint.SnapshotData = snapshotData;
return this;
Expand Down
25 changes: 25 additions & 0 deletions src/bcf-toolkit/Builder/Interfaces/IBcfBuilderDelegate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using BcfToolkit.Model.Interfaces;

namespace BcfToolkit.Builder.Interfaces;

public interface IBcfBuilderDelegate {
public delegate void OnMarkupCreated<in TMarkup>(TMarkup markup)
where TMarkup : IMarkup;

public delegate void OnProjectCreated<in TProjectInfo>(
TProjectInfo projectInfo)
where TProjectInfo : IProject;

public delegate void OnExtensionsCreated<in TExtensions>(
TExtensions extensions)
where TExtensions : IExtensions;

public delegate void OnDocumentCreated<in TDocumentInfo>(
TDocumentInfo documentInfo)
where TDocumentInfo : IDocumentInfo;

public OnMarkupCreated<IMarkup> MarkupCreated { get; }
public OnExtensionsCreated<IExtensions> ExtensionsCreated { get; }
public OnProjectCreated<IProject> ProjectCreated { get; }
public OnDocumentCreated<IDocumentInfo> DocumentCreatedCreated { get; }
}
2 changes: 1 addition & 1 deletion src/bcf-toolkit/Builder/Interfaces/IFromStreamBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ public interface IFromStreamBuilder<TItem> {
/// It builds and returns the specified item from a specified stream.
/// </summary>
/// <returns>Returns the built item.</returns>
Task<TItem> BuildFromStream(Stream source);
Task<TItem> BuildInMemoryFromStream(Stream source);
}
12 changes: 10 additions & 2 deletions src/bcf-toolkit/Converter/Bcf21/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading;
using System.Threading.Tasks;
using BcfToolkit.Builder.Bcf21;
using BcfToolkit.Builder.Interfaces;
using BcfToolkit.Utils;
using BcfToolkit.Model;
using BcfToolkit.Model.Bcf21;
Expand Down Expand Up @@ -57,7 +58,7 @@ private readonly Dictionary<
};

public async Task BcfToJson(Stream source, string targetPath) {
var bcf = await _builder.BuildFromStream(source);
var bcf = await _builder.BuildInMemoryFromStream(source);
await FileWriter.WriteBcfToJson(bcf, targetPath);
}

Expand Down Expand Up @@ -146,9 +147,16 @@ public Task ToJson(IBcf bcf, string target) {
}

public async Task<T> BcfFromStream<T>(Stream stream) {
var bcf = await _builder.BuildFromStream(stream);
var bcf = await _builder.BuildInMemoryFromStream(stream);
var targetVersion = BcfVersion.TryParse(typeof(T));
var converterFn = _converterFn[targetVersion];
return (T)converterFn(bcf);
}

public Task ProcessStream(
Stream stream,
IBcfBuilderDelegate builderDelegate) {
_builder.SetDelegate(builderDelegate);
return _builder.ProcessStream(stream);
}
}
20 changes: 19 additions & 1 deletion src/bcf-toolkit/Converter/Bcf21/SchemaConverterToBcf30.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the commented-out parts remain for future use?

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using BcfToolkit.Builder.Bcf30;
using BcfToolkit.Utils;

namespace BcfToolkit.Converter.Bcf21;

// public class BcfBuilderDelegate : BcfToolkit.Builder.Bcf21.Interfaces.IBcfBuilderDelegate {
// public BcfToolkit.Builder.Bcf21.Interfaces.IBcfBuilderDelegate.OnMarkupCreated<Model.Bcf21.Markup>
// MarkupCreated { get; } = markup => {
// var converted = SchemaConverterToBcf30.ConvertMarkup(markup);
// };
//
// public BcfToolkit.Builder.Bcf21.Interfaces.IBcfBuilderDelegate.OnProjectCreated<Model.Bcf21.ProjectExtension>
// ProjectCreated { get; } = Console.WriteLine;
//
// }

public static class SchemaConverterToBcf30 {

/// <summary>
Expand Down Expand Up @@ -43,7 +55,13 @@ public static Model.Bcf30.Bcf Convert(Model.Bcf21.Bcf from) {
return builder.Build();
}

private static Model.Bcf30.Markup ConvertMarkup(Model.Bcf21.Markup from) {
// public static async Task ProcessStream(Stream stream) {
// var del = new BcfBuilderDelegate();
// var fromBuilder = new BcfToolkit.Builder.Bcf21.BcfBuilder(del);
// await fromBuilder.ProcessStream(stream);
// }

public static Model.Bcf30.Markup ConvertMarkup(Model.Bcf21.Markup from) {
var builder = new MarkupBuilder();
var topic = from.Topic;
builder
Expand Down
Loading
Loading