@hzhangxyz/ddss 0.0.33 → 0.0.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import*as t from"node:fs/promises";import*as e from"node:os";import*as n from"node:path";import{Command as o}from"commander";import{Sequelize as s,DataTypes as i,Model as a,Op as r}from"sequelize";import{Search as c,Term as d,List as l,Rule as f}from"atsds";import{format as h}from"node:util";import{stdout as u,stdin as m}from"node:process";import{unparse as g,parse as p}from"atsds-bnf";import{EGraph as w}from"atsds-egg";import*as y from"node:readline/promises";class S extends a{}class A extends a{}async function x(t){const e=new s(t,{logging:!1});return S.init({id:{type:i.INTEGER,primaryKey:!0,autoIncrement:!0},data:{type:i.TEXT,unique:!0,allowNull:!1}},{sequelize:e,tableName:"facts",timestamps:!1}),A.init({id:{type:i.INTEGER,primaryKey:!0,autoIncrement:!0},data:{type:i.TEXT,unique:!0,allowNull:!1}},{sequelize:e,tableName:"ideas",timestamps:!1}),await e.sync(),e}async function M(t,e){await t.bulkCreate([{data:e}],{ignoreDuplicates:!0})}function q(t){if(!t.startsWith("--")){return`----\n${t.split("\n")[0]}\n`}return null}function v(t){return new f(`----\n${t.toString()}\n`)}function C(t){if(0!==t.length())return null;const e=t.conclusion().term();return e instanceof l?4!==e.length()||"binary"!==e.getitem(0).toString()||"=="!==e.getitem(1).toString()?null:[e.getitem(2),e.getitem(3)]:null}function E(t,e){return new d(`(binary == ${t.toString()} ${e.toString()})`)}class b{core;mapping;constructor(){this.core=new w,this.mapping=new Map}getOrAdd(t){const e=t.toString();return this.mapping.has(e)||this.mapping.set(e,this.core.add(t)),this.mapping.get(e)}find(t){return this.core.find(this.getOrAdd(t))}setEquality(t,e){const n=this.getOrAdd(t),o=this.getOrAdd(e);this.core.merge(n,o)}getEquality(t,e){const n=this.getOrAdd(t),o=this.getOrAdd(e);return this.core.find(n)===this.core.find(o)}rebuild(){this.core.rebuild()}}class T{egraph;terms;facts;newlyAddedTerms;newlyAddedFacts;factMatchingCache;constructor(){this.egraph=new b,this.terms=new Set,this.facts=new Set,this.newlyAddedTerms=new Set,this.newlyAddedFacts=new Set,this.factMatchingCache=new Map}rebuild(){this.egraph.rebuild();const t=Array.from(this.newlyAddedTerms).map(t=>new d(t));for(const e of this.facts){const n=new d(e);this.factMatchingCache.has(e)||this.factMatchingCache.set(e,new Set);const o=this.collectMatchingCandidates(n,t);for(const t of o)this.factMatchingCache.get(e).add(t.toString())}const e=Array.from(this.terms).map(t=>new d(t));for(const t of this.newlyAddedFacts){const n=new d(t);this.factMatchingCache.has(t)||this.factMatchingCache.set(t,new Set);const o=this.collectMatchingCandidates(n,e);for(const e of o)this.factMatchingCache.get(t).add(e.toString())}this.newlyAddedTerms.clear(),this.newlyAddedFacts.clear()}add(t){this.addExpr(t),this.addFact(t)}addExpr(t){const e=C(t);if(!e)return;const[n,o]=e;this.terms.add(n.toString()),this.newlyAddedTerms.add(n.toString()),this.terms.add(o.toString()),this.newlyAddedTerms.add(o.toString()),this.egraph.setEquality(n,o)}addFact(t){if(0!==t.length())return;const e=t.conclusion();this.terms.add(e.toString()),this.newlyAddedTerms.add(e.toString()),this.facts.add(e.toString()),this.newlyAddedFacts.add(e.toString())}*execute(t){yield*this.executeExpr(t),yield*this.executeFact(t)}*executeExpr(t){const e=C(t);if(!e)return;const[n,o]=e;this.egraph.getEquality(n,o)&&(yield t);const s=Array.from(this.terms).map(t=>new d(t)),i=this.collectMatchingCandidates(n,s),a=this.collectMatchingCandidates(o,s);if(0===i.length||0===a.length)return;const r=this.groupByEquivalenceClass(i),c=this.groupByEquivalenceClass(a);for(const e of r)for(const n of c)if(e.size>0&&n.size>0){const o=new d(e.values().next().value),s=new d(n.values().next().value);if(this.egraph.getEquality(o,s))for(const o of e)for(const e of n){const n=E(new d(o),new d(e)),s=t.conclusion(),i=n.match(s);if(i){const t=n.ground(i,"1");t&&(yield v(t))}}}}*executeFact(t){if(0!==t.length())return;const e=t.conclusion();for(const n of this.facts)this.egraph.getEquality(e,new d(n))&&(yield t);const n=Array.from(this.terms).map(t=>new d(t)),o=this.collectMatchingCandidates(e,n);if(0===o.length)return;const s=this.groupByEquivalenceClass(o);for(const t of this.facts){const n=this.factMatchingCache.get(t);if(!n||0===n.size)continue;const o=Array.from(n).map(t=>new d(t)),i=this.groupByEquivalenceClass(o);for(const n of s)for(const o of i)if(n.size>0&&o.size>0){const s=new d(n.values().next().value),i=new d(o.values().next().value);if(this.egraph.getEquality(s,i))for(const s of n)for(const n of o){const o=E(new d(s),new d(n)),i=E(e,new d(t)),a=o.match(i);if(a){const t=o.ground(a,"1");if(t){const e=t.term();e instanceof l&&(yield v(e.getitem(2)))}}}}}}collectMatchingCandidates(t,e){return e.filter(e=>null!==t.match(e))}groupByEquivalenceClass(t){if(0===t.length)return[];const e=new Map;for(const n of t){const t=this.egraph.find(n).toString();e.has(t)||e.set(t,new Set),e.get(t).add(n.toString())}return Array.from(e.values())}}const D={ds:async function(t,e){e||(e=await x(t));const n=new c;let o=-1;for(;;){const t=Date.now();let e=0;const s=await S.findAll({where:{id:{[r.gt]:o}}});for(const t of s)o=Math.max(o,t.id),n.add(t.data);const i=[],a=t=>{const e=t.toString();i.push(M(S,e));const n=q(e);return n&&i.push(M(A,n)),!1};e=n.execute(a),await Promise.all(i);const c=(Date.now()-t)/1e3;if(0===e){const t=Math.max(0,.1-c);await new Promise(e=>setTimeout(e,1e3*t))}}},egg:async function(t,e){e||(e=await x(t));const n=new T;let o=[],s=-1,i=-1;for(;;){const t=Date.now();let e=0;const a=await A.findAll({where:{id:{[r.gt]:i}}});for(const t of a)i=Math.max(i,t.id),o.push(new f(t.data));const c=await S.findAll({where:{id:{[r.gt]:s}}});for(const t of c)s=Math.max(s,t.id),n.add(new f(t.data));n.rebuild();const d=[],l=[];for(const t of o){let o=!1;for(const s of n.execute(t))if(d.push(M(S,s.toString())),e++,t.toString()===s.toString()){o=!0;break}o||l.push(t)}o=l,await Promise.all(d);const h=(Date.now()-t)/1e3;if(0===e){const t=Math.max(0,.1-h);await new Promise(e=>setTimeout(e,1e3*t))}}},input:async function(t,e){e||(e=await x(t));const n=y.createInterface({input:m,output:u});n.setPrompt("input: ");const o=function(t){const e=u.write,n=console.log,o=console.error;let s=!1;return u.write=(n,o,i)=>{if(s)return e.call(u,n,o,i);if("\n"===n||"\r\n"===n)return e.call(u,n,o,i);if(n.includes("\n")){e.call(u,"\r");const a=e.call(u,n,o,i);return s=!0,t.prompt(),s=!1,a}return e.call(u,n,o,i)},console.log=(...t)=>{u.write(h(...t)+"\n")},console.error=(...t)=>{u.write(h(...t)+"\n")},()=>{u.write=e,console.log=n,console.error=o}}(n);n.prompt();for await(const t of n){const e=t.trim();if(""===e||e.startsWith("//"))n.prompt();else{try{const t=p(e).toString();await M(S,t);const n=q(t);n&&await M(A,n)}catch(t){console.error(`error: ${t}`)}n.prompt()}}o(),n.close()},output:async function(t,e){e||(e=await x(t));let n=-1,o=-1;for(;;){const t=Date.now();let e=0;const s=await A.findAll({where:{id:{[r.gt]:o}}});for(const t of s)o=Math.max(o,t.id),console.log("idea:",g(t.data)),e++;const i=await S.findAll({where:{id:{[r.gt]:n}}});for(const t of i)n=Math.max(n,t.id),console.log("fact:",g(t.data)),e++;const a=(Date.now()-t)/1e3;if(0===e){const t=Math.max(0,.1-a);await new Promise(e=>setTimeout(e,1e3*t))}}},load:async function(t,e){e||(e=await x(t));const n=y.createInterface({input:m,terminal:!1});for await(const t of n){const e=t.trim();if(""!==e&&!e.startsWith("//"))try{const t=p(e).toString();await M(S,t);const n=q(t);n&&await M(A,n)}catch(t){console.error(`error: ${t}`)}}n.close()},dump:async function(t,e){e||(e=await x(t));const n=await A.findAll();for(const t of n)console.log("idea:",g(t.data));const o=await S.findAll();for(const t of o)console.log("fact:",g(t.data))}};function $(){const s=new o;s.name("ddss").description("DDSS - Distributed Deductive System Sorts: Run DDSS with an interactive deductive environment.").option("-a, --addr <url>","Database address URL. If not provided, uses a temporary SQLite database.").option("-c, --component <names...>","Components to run.",["input","output","ds","egg"]).action(async o=>{let s,i=o.addr;if(!i){s=await t.mkdtemp(n.join(e.tmpdir(),"ddss-"));i=`sqlite:///${n.join(s,"ddss.db")}`}if(console.log(`addr: ${i}`),i.startsWith("sqlite://"))i=i.replace("sqlite:///","sqlite:");else if(i.startsWith("mysql://"));else if(i.startsWith("mariadb://"));else{if(!i.startsWith("postgresql://"))return void console.error(`error: unsupported database: ${i}`);i=i.replace("postgresql://","postgres://")}await async function(t,e){for(const t of e)if(!(t in D))return void console.error(`error: unsupported component: ${t}`);const n=await x(t),o=e.map(e=>(0,D[e])(t,n));await Promise.race(o),await n.close()}(i,o.component),s&&await t.rm(s,{recursive:!0,force:!0})}),s.parse()}import.meta.main&&$();export{$ as cli};
2
+ import*as t from"node:fs/promises";import*as e from"node:os";import*as n from"node:path";import{Command as o}from"commander";import{Sequelize as s,DataTypes as i,Model as a,Op as r}from"sequelize";import{Search as c,Term as d,List as l,Rule as f}from"atsds";import{format as h}from"node:util";import{stdout as u,stdin as m}from"node:process";import{unparse as g,parse as p}from"atsds-bnf";import{EGraph as w}from"atsds-egg";import*as y from"node:readline/promises";class S extends a{}class A extends a{}async function x(t){const e=new s(t,{logging:!1});return S.init({id:{type:i.INTEGER,primaryKey:!0,autoIncrement:!0},data:{type:i.TEXT,unique:!0,allowNull:!1}},{sequelize:e,tableName:"facts",timestamps:!1}),A.init({id:{type:i.INTEGER,primaryKey:!0,autoIncrement:!0},data:{type:i.TEXT,unique:!0,allowNull:!1}},{sequelize:e,tableName:"ideas",timestamps:!1}),await e.sync(),e}async function M(t,e){await t.bulkCreate([{data:e}],{ignoreDuplicates:!0})}function q(t){if(!t.startsWith("--")){return`----\n${t.split("\n")[0]}\n`}return null}function v(t){return new f(`----\n${t.toString()}\n`)}function C(t){if(0!==t.length())return null;const e=t.conclusion().term();return e instanceof l?4!==e.length()||"binary"!==e.getitem(0).toString()||"=="!==e.getitem(1).toString()?null:[e.getitem(2),e.getitem(3)]:null}function E(t,e){return new d(`(binary == ${t.toString()} ${e.toString()})`)}class b{core;mapping;constructor(){this.core=new w,this.mapping=new Map}getOrAdd(t){const e=t.toString();return this.mapping.has(e)||this.mapping.set(e,this.core.add(t)),this.mapping.get(e)}find(t){return this.core.find(this.getOrAdd(t))}setEquality(t,e){const n=this.getOrAdd(t),o=this.getOrAdd(e);this.core.merge(n,o)}getEquality(t,e){const n=this.getOrAdd(t),o=this.getOrAdd(e);return this.core.find(n)===this.core.find(o)}rebuild(){this.core.rebuild()}}class T{egraph;terms;facts;newlyAddedTerms;newlyAddedFacts;factMatchingCache;constructor(){this.egraph=new b,this.terms=new Set,this.facts=new Set,this.newlyAddedTerms=new Set,this.newlyAddedFacts=new Set,this.factMatchingCache=new Map}rebuild(){this.egraph.rebuild();const t=Array.from(this.newlyAddedTerms).map(t=>new d(t));for(const e of this.facts){const n=new d(e);this.factMatchingCache.has(e)||this.factMatchingCache.set(e,new Set);const o=this.collectMatchingCandidates(n,t);for(const t of o)this.factMatchingCache.get(e).add(t.toString())}const e=Array.from(this.terms).map(t=>new d(t));for(const t of this.newlyAddedFacts){const n=new d(t);this.factMatchingCache.has(t)||this.factMatchingCache.set(t,new Set);const o=this.collectMatchingCandidates(n,e);for(const e of o)this.factMatchingCache.get(t).add(e.toString())}this.newlyAddedTerms.clear(),this.newlyAddedFacts.clear()}add(t){this.addExpr(t),this.addFact(t)}addExpr(t){const e=C(t);if(!e)return;const[n,o]=e;this.terms.add(n.toString()),this.newlyAddedTerms.add(n.toString()),this.terms.add(o.toString()),this.newlyAddedTerms.add(o.toString()),this.egraph.setEquality(n,o)}addFact(t){if(0!==t.length())return;const e=t.conclusion();this.terms.add(e.toString()),this.newlyAddedTerms.add(e.toString()),this.facts.add(e.toString()),this.newlyAddedFacts.add(e.toString())}*execute(t){yield*this.executeExpr(t),yield*this.executeFact(t)}*executeExpr(t){const e=C(t);if(!e)return;const[n,o]=e;this.egraph.getEquality(n,o)&&(yield t);const s=Array.from(this.terms).map(t=>new d(t)),i=this.collectMatchingCandidates(n,s),a=this.collectMatchingCandidates(o,s);if(0===i.length||0===a.length)return;const r=this.groupByEquivalenceClass(i),c=this.groupByEquivalenceClass(a);for(const e of r)for(const n of c)if(e.size>0&&n.size>0){const o=new d(e.values().next().value),s=new d(n.values().next().value);if(this.egraph.getEquality(o,s))for(const o of e)for(const e of n){const n=E(new d(o),new d(e)),s=t.conclusion(),i=n.match(s);if(i){const t=n.ground(i,"1");t&&(yield v(t))}}}}*executeFact(t){if(0!==t.length())return;const e=t.conclusion();for(const n of this.facts)this.egraph.getEquality(e,new d(n))&&(yield t);const n=Array.from(this.terms).map(t=>new d(t)),o=this.collectMatchingCandidates(e,n);if(0===o.length)return;const s=this.groupByEquivalenceClass(o);for(const t of this.facts){const n=this.factMatchingCache.get(t);if(!n||0===n.size)continue;const o=Array.from(n).map(t=>new d(t)),i=this.groupByEquivalenceClass(o);for(const n of s)for(const o of i)if(n.size>0&&o.size>0){const s=new d(n.values().next().value),i=new d(o.values().next().value);if(this.egraph.getEquality(s,i))for(const s of n)for(const n of o){const o=E(new d(s),new d(n)),i=E(e,new d(t)),a=o.match(i);if(a){const t=o.ground(a,"1");if(t){const e=t.term();e instanceof l&&(yield v(e.getitem(2)))}}}}}}collectMatchingCandidates(t,e){return e.filter(e=>null!==t.match(e))}groupByEquivalenceClass(t){if(0===t.length)return[];const e=new Map;for(const n of t){const t=this.egraph.find(n).toString();e.has(t)||e.set(t,new Set),e.get(t).add(n.toString())}return Array.from(e.values())}}const D={ds:async function(t,e){e||(e=await x(t));const n=new c;let o=-1;for(;;){const t=Date.now();let e=0;const s=await S.findAll({where:{id:{[r.gt]:o}}});for(const t of s)o=Math.max(o,t.id),n.add(t.data);const i=[],a=t=>{const e=t.toString();i.push(M(S,e));const n=q(e);return n&&i.push(M(A,n)),!1};e=n.execute(a),await Promise.all(i);const c=(Date.now()-t)/1e3;if(0===e){const t=Math.max(0,.1-c);await new Promise(e=>setTimeout(e,1e3*t))}}},egg:async function(t,e){e||(e=await x(t));const n=new T;let o=[],s=-1,i=-1;for(;;){const t=Date.now();let e=0;const a=await A.findAll({where:{id:{[r.gt]:i}}});for(const t of a)i=Math.max(i,t.id),o.push(new f(t.data));const c=await S.findAll({where:{id:{[r.gt]:s}}});for(const t of c)s=Math.max(s,t.id),n.add(new f(t.data));n.rebuild();const d=[],l=[];for(const t of o){let o=!1;for(const s of n.execute(t))if(d.push(M(S,s.toString())),e++,t.toString()===s.toString()){o=!0;break}o||l.push(t)}o=l,await Promise.all(d);const h=(Date.now()-t)/1e3;if(0===e){const t=Math.max(0,.1-h);await new Promise(e=>setTimeout(e,1e3*t))}}},input:async function(t,e){e||(e=await x(t));const n=y.createInterface({input:m,output:u});n.setPrompt("input: ");const o=function(t){const e=u.write,n=console.log,o=console.error;let s=!1;return u.write=(n,o,i)=>{if(s)return e.call(u,n,o,i);if("\n"===n||"\r\n"===n)return e.call(u,n,o,i);if(n.includes("\n")){e.call(u,"\r");const a=e.call(u,n,o,i);return s=!0,t.prompt(),s=!1,a}return e.call(u,n,o,i)},console.log=(...t)=>{u.write(h(...t)+"\n")},console.error=(...t)=>{u.write(h(...t)+"\n")},()=>{u.write=e,console.log=n,console.error=o}}(n);n.prompt();for await(const t of n){const e=t.trim();if(""===e||e.startsWith("//"))n.prompt();else{try{const t=p(e).toString();await M(S,t);const n=q(t);n&&await M(A,n)}catch(t){console.error(`error: ${t.message}`)}n.prompt()}}o(),n.close()},output:async function(t,e){e||(e=await x(t));let n=-1,o=-1;for(;;){const t=Date.now();let e=0;const s=await A.findAll({where:{id:{[r.gt]:o}}});for(const t of s)o=Math.max(o,t.id),console.log("idea:",g(t.data)),e++;const i=await S.findAll({where:{id:{[r.gt]:n}}});for(const t of i)n=Math.max(n,t.id),console.log("fact:",g(t.data)),e++;const a=(Date.now()-t)/1e3;if(0===e){const t=Math.max(0,.1-a);await new Promise(e=>setTimeout(e,1e3*t))}}},load:async function(t,e){e||(e=await x(t));const n=y.createInterface({input:m,terminal:!1});for await(const t of n){const e=t.trim();if(""!==e&&!e.startsWith("//"))try{const t=p(e).toString();await M(S,t);const n=q(t);n&&await M(A,n)}catch(t){console.error(`error: ${t.message}`)}}n.close()},dump:async function(t,e){e||(e=await x(t));const n=await A.findAll();for(const t of n)console.log("idea:",g(t.data));const o=await S.findAll();for(const t of o)console.log("fact:",g(t.data))}};function $(){const s=new o;s.name("ddss").description("DDSS - Distributed Deductive System Sorts: Run DDSS with an interactive deductive environment.").option("-a, --addr <url>","Database address URL. If not provided, uses a temporary SQLite database.").option("-c, --component <names...>","Components to run.",["input","output","ds","egg"]).action(async o=>{let s,i=o.addr;if(!i){s=await t.mkdtemp(n.join(e.tmpdir(),"ddss-"));i=`sqlite:///${n.join(s,"ddss.db")}`}if(console.log(`addr: ${i}`),i.startsWith("sqlite://"))i=i.replace("sqlite:///","sqlite:");else if(i.startsWith("mysql://"));else if(i.startsWith("mariadb://"));else{if(!i.startsWith("postgresql://"))return void console.error(`error: unsupported database: ${i}`);i=i.replace("postgresql://","postgres://")}await async function(t,e){for(const t of e)if(!(t in D))return void console.error(`error: unsupported component: ${t}`);const n=await x(t),o=e.map(e=>(0,D[e])(t,n));await Promise.race(o),await n.close()}(i,o.component),s&&await t.rm(s,{recursive:!0,force:!0})}),s.parse()}import.meta.main&&$();export{$ as cli};
package/package.json CHANGED
@@ -45,5 +45,5 @@
45
45
  "tslib": "^2.8.1",
46
46
  "typescript": "^5.9.3"
47
47
  },
48
- "version": "0.0.33"
48
+ "version": "0.0.34"
49
49
  }