Skip to content

Commit

Permalink
Do not create absolute URI's from user entered strings, but use defau…
Browse files Browse the repository at this point in the history
…ltInstanceURIHostPrefix from config.

DONE tested with useLocalHostPrefixForURICreation=true & the LDP GET URL of this server:
	the JSON returned is empty
TODO:
- the LDP GET service should return the same data as the web HTML /display service
- content negotiation for LDP server
  • Loading branch information
jmvanel committed Feb 25, 2016
1 parent 5d6078d commit acf7372
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ trait FormSyntaxFactory[Rdf <: RDF, DATASET]
fromUri(_), fromBNode(_), fromLiteral(_)._1))

val rdfs = RDFSPrefix[Rdf]
override val rdf = RDFPrefix[Rdf]
override lazy val rdf = RDFPrefix[Rdf]


/** create Form from an instance (subject) URI;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package deductions.runtime.abstract_syntax

import scala.collection.Seq

import org.w3.banana.FOAFPrefix
import org.w3.banana.PointedGraph
import org.w3.banana.RDF
import org.w3.banana.RDFPrefix
import org.w3.banana.RDFSPrefix
import deductions.runtime.utils.RDFHelpers

/**
* populate Fields in form by inferring possible label from:
Expand All @@ -18,13 +18,14 @@ import org.w3.banana.RDFSPrefix
* systematically trying to get the matching language form,
* and as last fallback, the last segment of the URI.
*/
private[abstract_syntax] trait InstanceLabelsInference2[Rdf <: RDF] {
private[abstract_syntax] trait InstanceLabelsInference2[Rdf <: RDF]
extends RDFHelpers[Rdf] {
self: PreferredLanguageLiteral[Rdf] =>

import ops._
private lazy val foaf = FOAFPrefix[Rdf]
private lazy val rdfs = RDFSPrefix[Rdf]
private lazy val rdf = RDFPrefix[Rdf]
// private lazy val rdf = RDFPrefix[Rdf]

def instanceLabels(list: Seq[Rdf#Node], lang: String = "")(implicit graph: Rdf#Graph): Seq[String] =
list.map { node => instanceLabel(node, graph, lang) }
Expand Down Expand Up @@ -83,20 +84,21 @@ private[abstract_syntax] trait InstanceLabelsInference2[Rdf <: RDF] {
case None => lsegment
}
}

def last_segment(node: Rdf#Node) =
try {
foldNode(node)(
uri => {
val ls = lastSegment(uri)
ls match {
case "" => fromUri(uri)
case _ => ls
}
},
bn => bn.toString(),
x => x.toString())
} catch {
case t: Throwable => node.toString()
}

// /** NOTE: currently lastSegment() in Banana can return null :( */
// def last_segment(node: Rdf#Node) =
// try {
// foldNode(node)(
// uri => {
// val ls = lastSegment(uri)
// ls match {
// case "" => fromUri(uri)
// case _ => ls
// }
// },
// bn => bn.toString(),
// x => x.toString())
// } catch {
// case t: Throwable => node.toString()
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,58 @@

package deductions.runtime.abstract_syntax

import java.net.InetAddress
import java.net.URLEncoder
import scala.language.postfixOps
import org.w3.banana.RDF
import deductions.runtime.services.Configuration
import java.net.InetAddress
import deductions.runtime.services.DefaultConfiguration
import deductions.runtime.utils.URIHelpers
import java.net.URLDecoder

/**
/** TODO move to package services, and rename to URIManagement
* @author j.m. Vanel
*
*/
object UnfilledFormFactory extends DefaultConfiguration {
object UnfilledFormFactory extends DefaultConfiguration
with URIHelpers {

def makeId: String = {
makeId(instanceURIPrefix)
}

/** make URI From String, if not already an absolute URI */
def makeURIFromString(objectStringFromUser: String): String = {
if (isAbsoluteURI(objectStringFromUser))
objectStringFromUser
else {
instanceURIPrefix +
// urlencode takes care of other forbidden character in "candidate" URI
URLEncoder.encode(objectStringFromUser.replaceAll(" ", "_"), "UTF-8")
}
}

/** make String From URI */
def makeStringFromURI( uri: String): String = {
lastSegment( URLDecoder.decode( uri, "UTF-8" ) ).replaceAll("_", " ")
}

/** make a unique Id with given prefix, currentTimeMillis() and nanoTime() */
def makeId(instanceURIPrefix: String): String = {
private def makeId(instanceURIPrefix: String): String = {
instanceURIPrefix + System.currentTimeMillis() + "-" + System.nanoTime() // currentId = currentId + 1
}

/** TODO : get the actual port */
val port = "9000"

val instanceURIPrefix: String = {
val hostNameUsed =
if (useLocalHostPrefixForURICreation) {
"http://" + InetAddress.getLocalHost().getHostName()
// TODO : get the actual port
} else defaultInstanceURIHostPrefix
hostNameUsed + ":" + port + "/" + relativeURIforCreatedResourcesByForm
}

}

/** Factory for an Unfilled Form */
Expand All @@ -27,12 +64,7 @@ trait UnfilledFormFactory[Rdf <: RDF, DATASET]
with FormConfigurationFactory[Rdf, DATASET]
with Configuration {

val instanceURIPrefix: String = // defaultInstanceURIPrefix
if( useLocalHostPrefixForURICreation ) {
val localHost = InetAddress.getLocalHost().getHostName()
localHost + "/" + relativeURIforCreatedResourcesByForm
} else defaultInstanceURIHostPrefix


import ops._

/**
Expand All @@ -51,7 +83,7 @@ trait UnfilledFormFactory[Rdf <: RDF, DATASET]
} else {
lookPropertieslistFormInConfiguration(classs)
}
val newId = makeId
val newId = UnfilledFormFactory . makeId
if (propsListInFormConfig.isEmpty) {
val props = fieldsFromClass(classs, graph)
createFormDetailed(makeUri(newId), addRDFSLabelComment(props), classs, CreationMode)
Expand All @@ -60,7 +92,4 @@ trait UnfilledFormFactory[Rdf <: RDF, DATASET]
CreationMode, formConfig = formConfig)
}

def makeId: String = {
UnfilledFormFactory.makeId(instanceURIPrefix)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package deductions.runtime.services

import deductions.runtime.html.CSS

//trait Configuration {
trait DefaultConfiguration extends Configuration {
/**
* use Local Host from getLocalHost() as Prefix For URI Creation, otherwise use defaultInstanceURIHostPrefix below
* TODO true should be the default, to adapt to normal machine settings
*/
override val useLocalHostPrefixForURICreation = false

/** URI Prefix prepended to newly created resource instances */
override val defaultInstanceURIHostPrefix =
"http://ldp.assemblee-virtuelle.org/" // ??????
/** otherwise use defaultInstanceURIHostPrefix */
override val useLocalHostPrefixForURICreation = false
// "http://ldp.assemblee-virtuelle.org/"
"http://ldp.virtual-assembly.org"

override val relativeURIforCreatedResourcesByForm = "ldp/"
override val relativeURIforCreatedResourcesByLDP = relativeURIforCreatedResourcesByForm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package deductions.runtime.services

import java.net.URLDecoder

import scala.collection.mutable.ArrayBuffer
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.Failure
import scala.util.Try

import org.apache.log4j.Logger
import org.w3.banana.FOAFPrefix
import org.w3.banana.RDF
import org.w3.banana.TryW

import deductions.runtime.dataset.RDFStoreLocalProvider
import deductions.runtime.semlogs.LogAPI
import deductions.runtime.utils.Timer
import deductions.runtime.abstract_syntax.UnfilledFormFactory

trait FormSaver[Rdf <: RDF, DATASET]
extends RDFStoreLocalProvider[Rdf, DATASET]
Expand Down Expand Up @@ -88,10 +86,12 @@ trait FormSaver[Rdf <: RDF, DATASET]
val objectFromUser = foldNode(originalTriple.objectt)(
_ => { if( objectStringFromUser.startsWith("_:") )
BNode(objectStringFromUser.substring(2))
else
// TODO other forbidden character in URI
URI(objectStringFromUser.replaceAll(" ", "_"))
},
else {
if (objectStringFromUser != "")
println(s"objectStringFromUser $objectStringFromUser changed: spaces removed")
URI( UnfilledFormFactory.makeURIFromString(objectStringFromUser) )
}
},
_ => BNode(objectStringFromUser.replaceAll(" ", "_")), // ?? really do this ?
_ => Literal(objectStringFromUser))
if (originalTriple.objectt != objectStringFromUser) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package deductions.runtime.services

import scala.language.postfixOps
import org.w3.banana.FOAFPrefix
import org.w3.banana.PointedGraph
import org.w3.banana.RDF
import org.w3.banana.RDFSPrefix
import org.w3.banana.SparqlGraphModule
import deductions.runtime.dataset.RDFStoreLocalProvider
import org.w3.banana.FOAFPrefix
import scala.language.postfixOps
import deductions.runtime.utils.URIHelpers
import deductions.runtime.abstract_syntax.UnfilledFormFactory

/**
* ensure that types inferred from ontologies are added to objects of given triples
Expand All @@ -15,7 +16,8 @@ import scala.language.postfixOps
* so that the form for V will be correctly populated.
*/
trait TypeAddition[Rdf <: RDF, DATASET]
extends RDFStoreLocalProvider[Rdf, DATASET] {
extends RDFStoreLocalProvider[Rdf, DATASET]
with URIHelpers {

import ops._
import sparqlOps._
Expand All @@ -32,12 +34,16 @@ trait TypeAddition[Rdf <: RDF, DATASET]
v.flatten
}

/** NON transactional */
/** input a triple ?S ?P ?O , where the type(s) ?C of ?O is inferred from rdfs:range of ?P,
* and then these triples are added in given graph URI:
* ?O a ?C
* NON transactional */
def addType(triple: Rdf#Triple, graphURI: Option[Rdf#URI],
graph: Rdf#Graph = allNamedGraph): Iterable[Rdf#Triple] = {
val objectt = triple.objectt
val pgObjectt = PointedGraph[Rdf](objectt, graph)

/** these triples are added in given graph URI: ?O a ?C */
def addTypeValue() = {
val existingTypes = (pgObjectt / rdf.typ).nodes
if (existingTypes isEmpty) {
Expand All @@ -56,15 +62,30 @@ trait TypeAddition[Rdf <: RDF, DATASET]
} else Seq()
}

/** if there is not already some rdfs.label, foaf.lastName, foaf.familyName properties set,
* add a triple
* ?O rdfs.label ?LAB ,
* where ?LAB is computed from URI string of ?O
* NOTE: related to InstanceLabelsInference2#instanceLabel(), but here we actually add a triple,
* because we are in a callback for user edits */
def addRDFSLabelValue() = {
val existingValues = (pgObjectt / rdfs.label).nodes
val existingValues2 = (pgObjectt / foaf.lastName).nodes
val existingValues3 = (pgObjectt / foaf.familyName).nodes
if (existingValues.isEmpty &&
existingValues2.isEmpty &&
existingValues3.isEmpty &&
!objectt.toString().contains(":")) {
val labelTriple = makeTriple(objectt, rdfs.label, Literal(objectt.toString().replace("_", " ")))
( ! isAbsoluteURI(objectt.toString()) ||
objectt.toString().startsWith( UnfilledFormFactory.instanceURIPrefix )
) ) {
// ! objectt.toString().contains(":")) {
if (isAbsoluteURI(objectt.toString()))
println("isAbsoluteURI " + objectt)
val labelTriple = makeTriple(
objectt, rdfs.label,
Literal( UnfilledFormFactory.makeStringFromURI(
objectt.toString() ) )
)
rdfStore.appendToGraph( dataset, makeGraphForSaving(), ops.makeGraph(Seq(labelTriple)))
}
}
Expand All @@ -77,6 +98,8 @@ trait TypeAddition[Rdf <: RDF, DATASET]
lit => URI("")))
}

////

val result = if (objectt.isURI) {
val pgObjectt = PointedGraph[Rdf](objectt, graph)
val existingTypes = (pgObjectt / rdf.typ).nodes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package deductions.runtime.sparql_cache

import java.util.Date

import scala.concurrent.ExecutionContext.Implicits
import scala.util.Failure
import scala.util.Success
import scala.util.Try

import org.apache.log4j.Logger
import org.w3.banana.OWLPrefix
import org.w3.banana.RDF
Expand All @@ -15,9 +13,10 @@ import org.w3.banana.io.RDFLoader
import org.w3.banana.io.RDFReader
import org.w3.banana.io.RDFXML
import org.w3.banana.io.Turtle

import deductions.runtime.dataset.RDFStoreLocalProvider
import deductions.runtime.services.SPARQLHelpers
import deductions.runtime.utils.URIHelpers
import deductions.runtime.utils.RDFHelpers

//import deductions.runtime.abstract_syntax.InstanceLabelsInferenceMemory0

Expand All @@ -34,7 +33,8 @@ trait RDFCacheAlgo[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DATAS
with RDFLoader[Rdf, Try]
with SPARQLHelpers[Rdf, DATASET]
with TimestampManagement[Rdf, DATASET]
with MirrorManagement[Rdf, DATASET] {
with MirrorManagement[Rdf, DATASET]
with RDFHelpers[Rdf] {

import ops._
import rdfStore.transactorSyntax._
Expand Down Expand Up @@ -218,21 +218,14 @@ trait RDFCacheAlgo[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DATAS
throw e
}
}

def isDownloadableURI(uri: Rdf#URI) = {
val u = fromUri(uri)
u.startsWith("http") ||
u.startsWith("ftp:") ||
u.startsWith("file:")
// u.startsWith("_:")
}

/**
* read from uri and store in TDB, no matter what the syntax is;
* can also load an URI with the # part
*/
def storeURINoTransaction(uri: Rdf#URI, graphUri: Rdf#URI, dataset: DATASET): Rdf#Graph = {
Logger.getRootLogger().info(s"Before load uri $uri into graphUri $graphUri")

if (isDownloadableURI(uri)) {
System.setProperty("sun.net.client.defaultReadTimeout", "10000")
System.setProperty("sun.net.client.defaultConnectTimeout", "10000")
Expand All @@ -242,10 +235,10 @@ trait RDFCacheAlgo[Rdf <: RDF, DATASET] extends RDFStoreLocalProvider[Rdf, DATAS
rdfStore.appendToGraph( dataset, graphUri, graph)
Logger.getRootLogger().info(s"storeURI uri $uri : stored into graphUri $graphUri")
graph

} else {
val message = s"Load uri $uri is not possible, not a downloadable URI."
Logger.getRootLogger().warn(message)
// throw new Exception(message)
emptyGraph
}
}
Expand Down
Loading

0 comments on commit acf7372

Please sign in to comment.