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

Refactor Reactor::{connect, connectMulti, canConnect} #232

Merged
merged 7 commits into from
Oct 12, 2024

Conversation

axmmisaka
Copy link
Collaborator

@axmmisaka axmmisaka commented Sep 7, 2023

  1. canConnect doesn't experience situations where throwing Error is required.
  2. canConnect is solely for testing and should not throw.
  3. connect and connectMulti should throw. Simply return result from canConnect and throw in functions that calls them.
  4. A mutation referencing a port for connection/disconnection should NOT result in any causality change per se between the port and the mutation
  • Refactor tests
  • Add descriptions for the enum members
  • Implement ConnectablePort
    Note: this task ended up being a rather insane one - I told @lhstrh on Friday that I will make some slight restructuring to make it more sane, but it turns out that my attempts are rather futile and I will commit the rough and crazy one I made on Friday. The issue is that ConnectablePort must still be able to be a valid Variable and cannot be a copy/wrapper that could be passed into connect. After all, connect taking an IOPort is too big to be changed as of now. So my approach is to make a simple wrapper to bypass the causality interface update. I think a more radical solution (such as making connect only take ConnectablePort, and unwrap/get the IOPort inside of it) deserves another branch/PR.
  • Fix connection bugs (which is why jest is not passing)
    • Do not automatically fail direct feedthrough if it does not create any causality issue
    • Do not limit the scope on local check if causality interface has changed Always do global check
  • Discuss with @lhstrh if in-mutation casting should be allowed

@axmmisaka axmmisaka mentioned this pull request Sep 7, 2023
9 tasks
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from 0a40ff3 to 7570459 Compare September 12, 2023 04:53
@axmmisaka axmmisaka marked this pull request as ready for review September 13, 2023 18:48
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from 7570459 to e165c8c Compare September 13, 2023 19:33
src/core/reactor.ts Outdated Show resolved Hide resolved
@axmmisaka
Copy link
Collaborator Author

axmmisaka commented Sep 13, 2023

After discussion today with @lhstrh we discovered that the current logic of detecting causality interface/direct feedthrough is a bit problematic. I will keep as-is as of now (i.e. no change on enum type/return type) and add tests to make sure that the causality interface issue is resolved first.

This is honestly something that needs to be done a long time ago but it is really seriously overdue......

@axmmisaka axmmisaka changed the base branch from master to mutation/full September 13, 2023 22:53
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from 83e015e to 33fab10 Compare September 26, 2023 10:41
@axmmisaka
Copy link
Collaborator Author

axmmisaka commented Sep 29, 2023

@lhstrh :
For reference, the decision regarding code generator:
If we want to connect a port in one microstep as a source, and write to it (or as a destination, but the current implementation wouldn't allow us to read from it I'm afraid), we have two approaches to do this, and they will have the same effect on the causality interface:

Or, should we NOT allow this do be done at all in one tag? This will be logically hard to achieve, I'm afraid, as this way we will need to record if a port is newly connected in the current tag.

  1. Declare two Variables:
this.addMutation(
  [this.trig],
  [this.writable(this.myport), this.myport.asConnectable(), this.destport.asConnectable()],
  function (this, wport, cport, dest) {
    this.connect(cport, dest);
    wport.set(123);
  }
);
  1. Declare one Variable and cast inside:
this.addMutation(
  [this.trig],
  [this.writable(this.myport), this.destport.asConnectable()],
  function (this, port, dest) {
    this.connect(dest, cport);
    port.getPort().asConnectable().set(123);
  }
);

Here are some approaches, but I'm still thinking about tradeoff/reason to allow only one:

  1. Allow both -> do nothing
  2. Allow only 1 -> mimic asWritable() and require the reactor key for it to be casted
  3. Allow only 2 -> check for duplication in reaction variables. We currently don't need to check for duplication, because if there's conflict (e.g. as a connection destination and an InPort to be read, a cycle will be formed in the causality graph).

@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch 2 times, most recently from 14d18a3 to a0dd502 Compare October 2, 2023 22:39
@axmmisaka axmmisaka changed the title [WIP] Refactor Reactor::{connect, connectMulti, canConnect} Refactor Reactor::{connect, connectMulti, canConnect} Oct 2, 2023
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from a0dd502 to 3d33c61 Compare October 3, 2023 00:53
@axmmisaka axmmisaka mentioned this pull request Oct 3, 2023
1 task
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from 3d33c61 to 3248225 Compare October 18, 2023 00:41
@lhstrh
Copy link
Member

lhstrh commented Oct 20, 2023

One problem with the diff is that all of the logging stuff is in it too. I think mutation/full should have master merged into it so that the diff shown in this PR is smaller.

@axmmisaka axmmisaka changed the base branch from mutation/full to master October 20, 2023 21:24
@axmmisaka axmmisaka changed the base branch from master to mutation/full October 20, 2023 21:25
@axmmisaka
Copy link
Collaborator Author

One problem with the diff is that all of the logging stuff is in it too. I think mutation/full should have master merged into it so that the diff shown in this PR is smaller.

Fixed! Sorry

@axmmisaka axmmisaka closed this Oct 20, 2023
@axmmisaka axmmisaka reopened this Oct 20, 2023
src/core/reactor.ts Outdated Show resolved Hide resolved
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from 61237d7 to 286bc49 Compare December 13, 2023 00:56
@axmmisaka axmmisaka changed the base branch from mutation/full to master December 13, 2023 01:02
@axmmisaka axmmisaka force-pushed the mutation/sidetask/refactor-canconnect branch from 286bc49 to 0e1f736 Compare December 13, 2023 01:04
0. `canConnect` doesn't experience situations where throwing Error is required.
1. `canConnect` is solely for testing and should not throw.
2. `connect` and `connectMulti` should throw. Simply return result from `canConnect` and throw in functions that calls them.
Most of them were achieved by a dirty hack: by changing `toBe(true)` to be `toBeFalsy()` and vice versa.
The negation of truthiness is because if connection can be established, we return 0, which is falsy.
Otherwise we return an error enum which is truthy. This is a bit C-esque and not JS-y, but let's keep it this way as of now.
Throw-catch would technically work, but it's not technically an error because we are **testing** if a connection can be established. Returning an error would also technically work, and we can add some type matching to make it work more smoothly, but as of now I intend to keep it this way.

One WIP would be to change `toBeTruthy` to the actual error code, but then some tests might break. We will need to investigate instead of coding tests based on the results.
Kagamihara Nadeshiko added 5 commits October 12, 2024 15:05
1. Implemented ConnectablePort that can be used as `Variable`
2. Refactored ConnectablePort with function overloading at the same time to facilitate better type check (but due to TS limitation there's no type narrowing, see microsoft/TypeScript#22609)
3. Made tests conform to the new connection API
1. Adapt Sieve by simply using ConnectablePort
2. Adapt Online Facility Location by using ConnectablePort *and* making necessary changes to reflect the actual logic, as discussed with @lhstrh. In this case, two sources: an OutPort (port A), and a mutation, wants to write to an OutPort (port B), but at different times. The correct thing to do, then, will be to make the mutation have its own OutPort (port C) to write to, and when it wants to write to port B, it disconnects A->B, makes connection C->B, and write to C.
Specifically ones that are related to causality graph and `ConnectablePort` - that is, declaring `ConnectablePort` in a mutation should not create dependency between them, but connection between ports should.
…lways check against the global precedence graph.
@lhstrh lhstrh force-pushed the mutation/sidetask/refactor-canconnect branch from 0e1f736 to d140fdc Compare October 12, 2024 22:05
@lhstrh
Copy link
Member

lhstrh commented Oct 12, 2024

While there may be some rough edges in the PR, it's generally a solid improvement, and the tests are running fine, so we should probably just go ahead and merge this...

@lhstrh lhstrh merged commit 8eee9e2 into master Oct 12, 2024
4 checks passed
@lhstrh lhstrh deleted the mutation/sidetask/refactor-canconnect branch October 12, 2024 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants