Skip to content

Instantly share code, notes, and snippets.

@joe776
Created November 13, 2015 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joe776/8662f9fbc916a5b4f811 to your computer and use it in GitHub Desktop.
Save joe776/8662f9fbc916a5b4f811 to your computer and use it in GitHub Desktop.
PSL basic-example using an existing database
import java.text.DecimalFormat
import edu.umd.cs.psl.application.inference.MPEInference
import edu.umd.cs.psl.config.*
import edu.umd.cs.psl.database.DataStore
import edu.umd.cs.psl.database.Database
import edu.umd.cs.psl.database.Partition
import edu.umd.cs.psl.database.rdbms.RDBMSDataStore
import edu.umd.cs.psl.database.rdbms.driver.H2DatabaseDriver
import edu.umd.cs.psl.database.rdbms.driver.H2DatabaseDriver.Type
import edu.umd.cs.psl.groovy.PSLModel
import edu.umd.cs.psl.model.atom.GroundAtom
import edu.umd.cs.psl.model.predicate.Predicate
import edu.umd.cs.psl.ui.functions.textsimilarity.*
import edu.umd.cs.psl.util.database.Queries
ConfigManager cm = ConfigManager.getManager()
ConfigBundle config = cm.getBundle("basic-example")
def defaultPath = System.getProperty("java.io.tmpdir")
String dbpath = config.getString("dbpath", defaultPath + File.separator + "basic-example")
DataStore data = new RDBMSDataStore(new H2DatabaseDriver(Type.Disk, dbpath, false), config)
PSLModel m = new PSLModel(this, data)
println m
Predicate samePerson = m.getPredicate("SamePerson")
Predicate network = m.getPredicate("Network")
Predicate name = m.getPredicate("Name")
Predicate knows = m.getPredicate("Knows")
def evidencePartition = new Partition(0);
def targetPartition = new Partition(1);
Database db = data.getDatabase(targetPartition, [network, name, knows] as Set, evidencePartition);
println m
MPEInference inferenceApp = new MPEInference(m, db, config);
inferenceApp.mpeInference();
inferenceApp.close();
println m
println "Inference results with hand-defined weights:"
DecimalFormat formatter = new DecimalFormat("#.##");
for (GroundAtom atom : Queries.getAllAtoms(db, samePerson))
println atom.toString() + "\t" + formatter.format(atom.getValue());
db.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment